
// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value) 
{	
	if (setCookie.arguments.length<2) 
	{ 
		alert("bad cookie");
		return;
	}
	var expireDate = new Date();
	expireDate.setTime(expireDate.getTime() + (24*60*60*1000*365));
	var memento = getRawCookie("GrapeFlix");
	var cache = new Cache();
	if(memento != "" && memento != undefined){
		cache.fromMemento(memento);
	}
	cache.put(name, value);
	memento = cache.toMemento();
	var curCookie = "GrapeFlix" + "=" + memento + "; expires=" + expireDate.toGMTString();
	//var curCookie =  name + "=" + value +"; expires=" + expireDate.toGMTString();
	//var curCookie = "GrapeFlix" + "=" + name + "=" + value +"; expires=" + expireDate.toGMTString();
	//alert(curCookie);
	//document.cookie["Sample"] = curCookie;
	document.cookie = curCookie;
}



// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
	var dc = document.cookie;
	var index = dc.indexOf("GrapeFlix=");
    if (index == -1) return null;
    index = dc.indexOf("=", index) + 1; // first character
    var endstr = dc.indexOf(";", index);
    if (endstr == -1) endstr = dc.length; // last character
    var coo = unescape(dc.substring(index, endstr));
    if(coo.indexOf("&") < 1){
    	return null;
    }
    var cache = new Cache();
    cache.fromMemento(coo);
    return cache.get(name);
}

function getRawCookie(name) {
  var dc = document.cookie;
  
 var index = dc.indexOf(name + "=");
      if (index == -1) return null;
      index = dc.indexOf("=", index) + 1; // first character
      var endstr = dc.indexOf(";", index);
      if (endstr == -1) endstr = dc.length; // last character
      return unescape(dc.substring(index, endstr));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name) 
{
	if (deleteCookie.arguments.length<1) 
	{ 
		alert("bad cookie delete");
		return;
	}
    if (getCookie(name)) 
    {
		var expireDate = new Date();
		expireDate.setTime(expireDate.getTime() + (24*60*60*1000*365));
  		var memento = getRawCookie("GrapeFlix");
  		var cache = new Cache();
  		cache.fromMemento(memento);
  		cache.remove(name);
  		memento = cache.toMemento();
  		var curCookie = "GrapeFlix" + "=" + escape(memento) + "; path=/; expires=" + expireDate.toGMTString();
  		//var curCookie = name + "=" + value +"; path=/; expires=" + expireDate.toGMTString();
		document.cookie = curCookie;
	}
}

function setChip(cookieName, chipName, chipValue, expires, path, domain, secure) 
{
	var strNotSoYummyCookie = "";
	var strYummyCookie = "";
	strNotSoYummyCookie = getCookie(cookieName);
	if (strNotSoYummyCookie == null) 
	{	
		strYummyCookie = chipName + "_CP_" + chipValue;
	} 
	else 
	{		
		var chips = strNotSoYummyCookie.split("_DH_");
		var existingChip;
		var existingChipName;
		var existingChipValue;	
		var bChipFound = false;		
		for (var i = 0; i < chips.length; ++i) 
		{
			existingChip = "";
			existingChipName = "";
			existingChipValue = "";
			existingChip = chips[i].split("_CP_");
			existingChipName = existingChip[0];
			if (existingChip.length > 1) 
			{
				existingChipValue = existingChip[1];
			} 
			else 
			{
				existingChipValue = "";
			}
			if (strYummyCookie != "") 
			{
				strYummyCookie += "_DH_";
			}	
			if (existingChipName == chipName) 
			{
				strYummyCookie += chipName + "_CP_" + escape(chipValue);
				bChipFound = true;		
			} 
			else 
			{
				strYummyCookie += existingChipName + "_CP_" + existingChipValue;
			}	
		}
		if (!bChipFound) 
		{
			if (strYummyCookie != "") 
			{
				strYummyCookie += "_DH_";
			}		
			strYummyCookie += chipName + "_CP_" + escape(chipValue);
		}
	}	
	setCookieNoEscape(cookieName, strYummyCookie, expires, path, domain, secure);	
}

function setChipNoEscape(cookieName, chipName, chipValue, expires, path, domain, secure) {
	var strNotSoYummyCookie = "";
	var strYummyCookie = "";
	strNotSoYummyCookie = getCookie(cookieName);
	if (strNotSoYummyCookie == null) {	
		strYummyCookie = chipName + "_CP_" + chipValue;
	} else {		
		var chips = strNotSoYummyCookie.split("_DH_");
		var existingChip;
		var existingChipName;
		var existingChipValue;	
		var bChipFound = false;		
		for (var i = 0; i < chips.length; ++i) {
			existingChip = "";
			existingChipName = "";
			existingChipValue = "";
			
			existingChip = chips[i].split("_CP_");
			existingChipName = existingChip[0];
			if (existingChip.length > 1) {
				existingChipValue = existingChip[1];
			} else {
				existingChipValue = "";
			}
			if (strYummyCookie != "") {
				strYummyCookie += "_DH_";
			}	
			if (existingChipName == chipName) {
				strYummyCookie += chipName + "_CP_" + chipValue;
				bChipFound = true;		
			} else {
				strYummyCookie += existingChipName + "_CP_" + existingChipValue;
			}	
		}
		if (!bChipFound) {
			if (strYummyCookie != "") {
				strYummyCookie += "_DH_";
			}		
			strYummyCookie += chipName + "_CP_" + chipValue;
		}
	}	
	setCookieNoEscape(cookieName, strYummyCookie, expires, path, domain, secure);		
}

function getChip(cookieName, chipName) {
	var strNotSoYummyCookie = "";
	var strYummyCookie = "";
	strNotSoYummyCookie = getCookie(cookieName);
	if (strNotSoYummyCookie == null) {	
		return null;
	} else {		
		var chips = strNotSoYummyCookie.split("_DH_");
		var existingChip;
		var existingChipName;
		var existingChipValue;	
		var bChipFound = false;		
		for (var i = 0; i < chips.length; ++i) {
			existingChip = "";
			existingChipName = "";
			existingChipValue = "";
			
			existingChip = chips[i].split("_CP_");
			existingChipName = existingChip[0];
			if (existingChip.length > 1) {
				existingChipValue = existingChip[1];
			} else {
				existingChipValue = "";
			}
			if (strYummyCookie != "") {
				strYummyCookie += "_DH_";
			}	
			if (existingChipName == chipName) {
				return unescape(existingChipValue);
				bChipFound = true;
			}	
		}
		if (!bChipFound) {
			return null;
		}
	}	
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

var featureBits = "";

function setFeatureValue(bit, aValue) {
	var newFeatures = "";
	for (var i = 0; i < featureBits.length || i <= bit; i++) {
		if (i == bit) {
			if (aValue) {
				newFeatures += "1";
			} else {
				newFeatures += "0";
			}
		} else {
			if (featureBits.charAt(i) != "") {
				newFeatures += featureBits.charAt(i);
			} else {
				newFeatures += "n";
			}
		}
	}
	featureBits = newFeatures;
	setChip("ifilm_pers","ft",featureBits);
}	
	
function getFeatureValue(bit) {
	featureBits = getChip("ifilm_pers","ft");
	if (featureBits != null) {
		if (bit > featureBits.length) {
			return null;
		} else {
			if (featureBits.charAt(bit) == "n") {
				return null;
			} else {
				if (featureBits.charAt(bit) == "1") {
					return true;
				} else {
					return false;
				}
			}				
		}
	} else {
		return null;
	}		
}	

function getFeature() {
	return featureBits;
}