var the_QS = "";			// the querystring if one was passed
var the_REFERER = "";		// the currently REFERER
var data_cookie = "";		// DATA stored in the cookie
var refer_cookie = "";		// REFER stored in the cookie

// there is a cookie so lets keep the existing options but overwrite the REFER
// read the cookie and split/substring the long string
var entire_cookie = document.cookie;
arrayOfCookies = entire_cookie.split(";");
for (i=0;i<arrayOfCookies.length;i++) 
{
	if (arrayOfCookies[i].indexOf("DATA") != -1)
		data_cookie += arrayOfCookies[i];
	if (arrayOfCookies[i].indexOf("REFER") != -1)
		refer_cookie += arrayOfCookies[i].substr(arrayOfCookies[i].indexOf("=")+1);
}

// if no DATA cookie was read in set a default value for it
if (data_cookie == "")
	data_cookie = "DATA=F5001";

// set the cookie the first time this way if moneyWorkshop isn't executed 
// at least the REFER is recorded in the cookie
function init() 
{
	the_QS = getQS();
	if (the_QS.length < 1) {
		// no querystring passed so get referer from cookie
		the_REFERER = refer_cookie;
	}
	else {
		// querystring passed so set referer
		the_REFERER = the_QS;
		// save the REFER in a cookie
		write_Cookie("REFER="+the_REFERER);
	}
}


// Called from a "buy it" button
function moneyWorkshop (identifier) 
{
    //alert('DEBUG: In cookie_common ' + identifier);
	setCookie(identifier);
	window.location = "/ds/config.html";
}

function setCookie(identifier) 
{	
	if (identifier == "F1ALL")
		data_cookie = "DATA=F1AL1";
		
	else if (identifier == "F5ALL")
		data_cookie = "DATA=F5AL1";

	else if (identifier == "G5ALL")
		data_cookie = "DATA=G5AL1";
			
	else if (identifier == "F500")
		data_cookie = "DATA=F5001";
		
	else if (identifier == "F100")
		data_cookie = "DATA=F1001";
				
	else if (identifier == "G500")
		data_cookie = "DATA=G5001";
		
	else if (identifier == "F1G5")
		data_cookie = "DATA=F1G51";
		
	else if (identifier == "F1G5ALL")
		data_cookie = "DATA=F1GA1";

	else if (identifier == "F5G5")
		data_cookie = "DATA=F5G51";
		
	else if (identifier == "F5G5ALL")
		data_cookie = "DATA=AF5G1";
	
	else if (identifier == "DTSC")
		data_cookie = "DATA=DTSC1";
	
	else if (identifier == "FGCS")
		data_cookie = "DATA=FGCS1";

	else if (identifier == "FBCS")
		data_cookie = "DATA=FBCS1";

   // alert('DEBUG: In data_cookie ' + data_cookie);

	write_Cookie(data_cookie);
}

// get the query string passed to the page
function getQS() 
{
	var theQS = "";
	var theUrl = document.URL;
	var qsStart = theUrl.indexOf("?") + 1;
	if (qsStart > 0)
		theQS = theUrl.substr(qsStart, theUrl.length);
	return theQS;
}

function write_Cookie(myCookie)
{
	//alert(myCookie);
	// set value of expiration date for cookie
	var _twoWeek = 14 * 24 * 60 * 60 * 1000;
	var _expDate = new Date();
	_expDate.setTime(_expDate.getTime() + _twoWeek);
	// write the cookie
	document.cookie = myCookie + "; EXPIRES=" + _expDate.toGMTString();
}

