/**********************************************************************
 * Filename     : from_reviews_referral.js
 * Author       : Michael Barquero
 * Created      : 2008-12-09
 * Description  : tests referrer; if from 'store' or 'reviews', display disclaimer
 * Comment      : 
 *********************************************************************/
/**********************************************************************
 * Revised by	: Wayne J. Earl 
 * Revised date	: 2008-12-09
 * Description	: Removed store test
 * 				: Added test for fda cookie to prevent nuisance display
 *********************************************************************/
/**********************************************************************
 * Revised by	: Tommy J Smith
 * Revised date	: 2010-01-19
 * Description	: Changed the FDA disclaimer so it is called when you go from
 *				: reviews to store and not show the scrollbar and added the
 *				: functionality to show when going from store to reviews. Both
 *				: add a session cookie to the computer. This used to be called
 *				: from three different js files - now they are all in one.
 *********************************************************************/

 /********************************************
 * Add the following line to calling page ...
 * -------------------------------------------
 * <script type="text/javascript" src="/clientscript/disclaimer/from_reviews_referral.js"></script>
 *
 ********************************************/
//Set initial variables and check the referrer to set the referrer flag
var fdaCookieStore = "bbFDAStore";
var fdaCookieReviews = "bbFDAReviews";
var fromReviews = false;

//Check the referrer and call the showDisclaimer function if from reviews
function checkReferrer() {
    if (document.referrer.indexOf('reviews.bodybuilding.com/') != -1) {
	    fromReviews = true;
	    showDisclaimer('visible',fromReviews);
    }
}

//Check to see if cookie is set for this name. If it is, return it - if not, return empty string
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
	{
	c_start=c_start + c_name.length+1;
	c_end=document.cookie.indexOf(";",c_start);
	if (c_end==-1) c_end=document.cookie.length;
	return unescape(document.cookie.substring(c_start,c_end));
	}
  }
return "";
}
//Show disclaimer if no cookie exists or go to link if cookie doesn't exit. This
//checks a hidden word in the popup that identies if the user coming in to the store
//or going out of the store.
function showDisclaimer(status,fromReviews) {
	if(document.body) {
		if (status == "hidden") {
			if (document.getElementById('disclaimer_origin').innerHTML == "in") {
				window.location = document.referrer;
			} else {
				window.location = document.URL;
			}
		}
		var fdaCookieName = (fromReviews == true) ? fdaCookieReviews : fdaCookieStore;
		var fdaCookie = getCookie(fdaCookieName);
		if (fdaCookie == 'viewed') {
			document.body.style.overflow="visible";
		    document.documentElement.style.overflow="visible";
			return true;
		}
		document.body.style.overflow="hidden";
		document.documentElement.style.overflow="hidden";
		var disclaimerText = getText(fromReviews);
		document.getElementById('disclaimer_content_mask').style.width = "3900px";
		document.getElementById('disclaimer_content_mask').style.height = "2000px";
		origin = (fromReviews == true) ? "in" : "out";
		document.getElementById('disclaimer_text').innerHTML = disclaimerText;
		document.getElementById('disclaimer_origin').innerHTML = origin;
		document.getElementById('disclaimer_content_mask').style.visibility = status;
		document.getElementById('disclaimer_content_c').style.visibility = status;
		document.getElementById('disclaimer_content').style.visibility = status;
		document.getElementById('disclaimer_content').style.position = "absolute";
		document.getElementById('disclaimer_content').style.left = "90px";
		document.getElementById('disclaimer_content').style.top = "50px";
		return false;
	}
	return false;
}
//Set cookie and follow link
function followLink()
{
	if (document.getElementById('disclaimer_origin').innerHTML == "out") {
	    document.cookie = fdaCookieStore + "=viewed; path=/; domain=bodybuilding.com;";
		window.location = document.getElementById('fdaDisclaimerLink').href;
	} else {
	    document.cookie = fdaCookieReviews + "=viewed; path=/; domain=bodybuilding.com;";
		window.location = document.URL;
	}
}

//Get text for disclaimer
function getText(fromReviews)
{
	returnText = "You are now leaving the Bodybuilding.com Store and entering the Bodybuilding.com <br />" +
				 "Product Reviews section. Products in the Store are not intended to diagnose, treat, <br />" +
				 "cure, or prevent any disease. To return to the Store, please click NO below. Click <br />" +
				 "YES below to show your agreement and to enter the Bodybuilding.com Product <br />" +
				 "Reviews section.";
	if (fromReviews == true) {
		returnText = "You are now leaving Bodybuilding.com Product Reviews and entering the <br />" +
					 "Bodybuilding.com Store. Products sold in the Store are not intended to <br />" +
					 "diagnose, treat, cure, or prevent any disease. To return to Product <br />" +
					 "Reviews, please click NO below. Click YES below to show your agreement <br />" +
					 "and to enter the Bodybuilding.com Store.";
	}
	return returnText;
}

