/* some general cookie functions */

function GetCookie(sName)
{
	// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0].replace(/^\s+|\s+$/g, ""))   // trims leading and trailing white space
			return unescape(aCrumb[1]);
	}
	return null;   // a cookie with the requested name does not exist
}

function SetTempCookie(sName, sValue)
{
	document.cookie = sName + "=" + escape(sValue) + "; path=/";
}

function SetPermanentCookie(sName, sValue, expireDays)
{
	var expireDate = new Date ();
	expireDate.setTime(expireDate.getTime() + (expireDays * 24 * 3600 * 1000));
	
	document.cookie = sName + "=" + escape(sValue) + "; path=/" + ((expireDays == null) ? "" : "; expires=" + expireDate.toGMTString());
}

function DeleteCookie(sName) 
{
	if (GetCookie(sName))
	{
		document.cookie = sName + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function ToggleCookie(sName)
{ 
	var currentValue = GetCookie(sName);
	if (currentValue == null)   // cookie not found, assume false
		currentValue = "false";

	// toggle and set the cookie
	var newValue = (currentValue == "true" ? "false" : "true");
	SetCookie(sName, newValue);
}

