// updated for Feb 25th, 2007
// instantiate the variable if a browserback cookie was found (indicating that the consumer had
// been to the app in the current session
var browsercookie = getCookie( "browserback" );
if(browsercookie) {
   var browserDo = browsercookie;
}

var browserDirection = goBack();

// this function determines whether the consumer wants to go forward into the app, or back to the
// static page they came from.
function goBack(){
	var cookiename = "browserback";
	var cookievalue = "";
	//make this a session cookie, so expiration is blank
	var expiration = "";

    if(browsercookie && browserDo == "wentforward") {
		// if this condition is met, the cookie already existed and the consumer went INTO the app
		// so we reset the cookievalue because we are sending the consumer back..and send them back an extra page

		browserDirection = "gobacktwice";
		cookievalue = "wentback";
	}
	else {
		// trying to go back to app after hitting back button when in app and being back at brand page
		// Update the cookie - consumer wants to go foward after going back
		cookievalue = "wentforward";
		browserDirection = "goforward";
	}

	//save our cookie info
	plant_cookie(cookiename, cookievalue, expiration);

	return(browserDirection);
}

// This function goes and gets the cookie for the browser back redirect code.
function getCookie(cookiename) {
	var cookie = " " + document.cookie;
	var search = " " + cookiename + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
   return(setStr);
}

function plant_cookie(name, value, expires){
	// this cookie will be a session cookie - therefore we omit the expire value
	//expires.setDate(expires.getDate() + 1);
	var path = "/";

	// get the full url string
	var url = new String( window.location );

	// strip off the leading protocol ("http://") and strip any trailing file-path and query string
	var domain = url.substring(7, url.indexOf( ".com/"));

	// strip off any preceding subdomains (www.sub.subsub...)
	domain = domain.substring(domain.lastIndexOf(".") + 1, domain.length) + ".com";

	//set the domain for the cookie - we are planting a hostname cookie here
	var cookieDomain = url.substring(7, url.indexOf( ".com/"))+".com";
	document.cookie = name+"="+value+";path="+path+";host="+cookieDomain+";false";
}
