// ----------------------------------------------------------------
// Cookie Item Functions
// ----------------------------------------------------------------

// Sets a cookie item (name value pair) value in a cookie.
//
function SetCookieItem(strCookieName, strItemName, strItemValue)
{
	// If cookie are not enabled then exit function and return false.
	if(CookiesEnabled() == false) return false;

	var blnAlreadySet  = false;
	var strCookieValue = GetCookie(strCookieName);
	
	if(strCookieValue == "")
	{
		strCookieValue = strItemName + "#" + strItemValue;
	}
	else
	{
		var objreg		= new RegExp("#");
		var aItems		= strCookieValue.split(objreg);
			
		for(var intSub = 0; intSub < aItems.length; (intSub = intSub + 2))
		{
			if(aItems[intSub] == strItemName)
			{
				aItems[intSub + 1] = strItemValue;
				blnAlreadySet = true;
			};
		};
				
		if(blnAlreadySet == true)
		{
			strCookieValue = "";
		
			for(var intSub = 0; intSub < aItems.length; (intSub = intSub + 2))
			{
				if(intSub == 0)
				{
					strCookieValue = aItems[intSub] + "#" + aItems[intSub + 1];
				}
				else
				{
					strCookieValue = strCookieValue + "#" + aItems[intSub] + "#" + aItems[intSub + 1];
				};
			};
		}
		else
		{
			// If the item does't already exist we can simply add it to the end.
			strCookieValue = strCookieValue + "#" + strItemName + "#" + strItemValue;
		};		
		
	};
	
	return SetCookie(strCookieName, strCookieValue);
}

// Returns the cookie item value.
// Returns "" if the cookie or cookie item doesn't exist.
// Also always returns "" if cookies are not enabled.
//
function GetCookieItem(strCookieName, strItemName)
{
	// If cookie are not enabled then exit function and return "".
	if(CookiesEnabled() == false) return "";

	var strValue		= "";
	var strCookieValue	= GetCookie(strCookieName);
	
	if(strCookieValue == "")
	{
		strValue = "";
	}
	else
	{
		var objreg		= new RegExp("#");
		var aItems		= strCookieValue.split(objreg);
		
		for(var intSub = 0; intSub < aItems.length; (intSub = intSub + 2))
		{
			if(aItems[intSub] == strItemName)
			{
				strValue = aItems[intSub + 1];
				break;
			};
		};
	};
	
	return strValue;
}

// Deletes a cookie item.
// Returns true if the cookie item is deleted.
// Returns false if the cookie or cookie item is not found.
//
function DeleteCookieItem(strCookieName, strItemName)
{
	// If cookie are not enabled then exit function and return false.
	if(CookiesEnabled() == false) return false;

	var blnItemFound	= false;
	var strCookieValue	= GetCookie(strCookieName);
	
	if(strCookieValue != "")
	{
		var objreg		= new RegExp("#");
		var aItems		= strCookieValue.split(objreg);
		
		for(var intSub = 0; intSub < aItems.length; (intSub = intSub + 2))
		{
			if(aItems[intSub] == strItemName)
			{
				blnItemFound = true;
				aItems[intSub] = "";
				break;
			};
		};
		
		if(blnItemFound == true)
		{
			strCookieValue = "";
		
			for(intSub = 0; intSub < aItems.length; (intSub = intSub + 2))
			{
				if(aItems[intSub] != "")
				{
					if(strCookieValue != "")
					{
						strCookieValue = (strCookieValue + "#");
					};
				
					strCookieValue = (strCookieValue + aItems[intSub] + "#" + aItems[intSub + 1]);
				};
			};
			
			if(strCookieValue != "")
			{
				SetCookie(strCookieName, strCookieValue);
			}
			else
			{
				DeleteCookie(strCookieName);
			};	
		};
		
	};
	
	return blnItemFound;	
}

// ----------------------------------------------------------------
// Cookie Functions
// ----------------------------------------------------------------

var g_intCookieCount = -1;

// Returns true if cookie are enabled.
// Returns false is cookies have been disabled in the Internet Explorer
// settings.
//
function CookiesEnabled()
{
	return window.navigator.cookieEnabled;
} 

// Sets a cookie value.
// strName			: The name of the cookie.
// strValue			: The value of the cookie.
// intExpiresDays	: The number of days before the cookie expires.
//					  Optional: If not specified the cookie doesn't expire.
//
function SetCookie(strName, strValue, intExpiresDays)
{
	// If cookies are not enabled then exit function and return false.
	if(CookiesEnabled() == false) return false;

	if(arguments.length = 2)
	{
		// If argument intExpiresDays is not specified then set the expiry to
		// approximately 20 years. This effectively meaning the cookie doesn’t expire.
		var intExpiresDays = (365 * 20);
	};
		
	var objDte = new Date()
	var NowPlusOneWeek = objDte.getTime() + (intExpiresDays * 24 * 60 * 60 * 1000);
		
	objDte.setTime(NowPlusOneWeek);
	
	strPath = ";path=/";
	
	document.cookie = strName + "=" + strValue + "; expires=" + objDte.toGMTString() + strPath;
	
	return true;
}

// Returns the cookie value.
// Returns "" if the cookie doesn't exist.
//
function GetCookie(strName)
{
	// If cookies are not enabled then exit function and return "".
	if(CookiesEnabled() == false) return "";

	var strCookieValue = "";
					
	var strCookie = document.cookie;
						
	var objreg = new RegExp(";");

	var aCookies = strCookie.split(objreg);
			
	for(var intSub = 0; intSub < aCookies.length; ++intSub)
	{
		var objreg = new RegExp("=");

		var aNameValue = aCookies[intSub].split(objreg);
	
		if( (aNameValue.length == 2) && ((aNameValue[0] == strName) || ( (aNameValue[0]) == " " + strName)) )
		{
			strCookieValue = aNameValue[1];
			break;
		};

	};
			
	return strCookieValue;
}

var g_intCookieCount = -1;

// Returns the number of cookies currently set.
//
function GetCookieCount()
{
	// If cookie are not enabled then exit function and return 0.
	if(CookiesEnabled() == false) return 0;

	var intCnt = 0;
	
	if(g_intCookieCount != -1)
	{
		intCnt = g_intCookieCount;
	}
	else
	{
		var strCookie = document.cookie;
	
		if(strCookie != "")
		{	
			var objreg	 = new RegExp(";");
			var aCookies = strCookie.split(objreg);
			
			intCnt = aCookies.length;
		};
		
		g_intCookieCount = intCnt;
	};
	
	return intCnt;
}
		
// Deletes the specified cookie.
// Has no return value.
//
function DeleteCookie(strName)
{
	// If cookie are not enabled then exit function.
	if(CookiesEnabled() == false) return;

	var objDte = new Date()
	objDte.setTime(objDte.getTime() - (1 * 24 * 60 * 60 * 1000));
			
	document.cookie = strName + "=; expires=" + objDte.toGMTString();
}
		
// Deletes all cookies for this domain.
// Returns the number of cookied deleted.
//
function DeleteAllCookie()
{
	// If cookie are not enabled then exit function and return 0.
	if(CookiesEnabled() == false) return 0;

	var intCookieCnt = 0;
					
	var strCookie = document.cookie;
						
	var objreg = new RegExp(";");

	var aCookies = strCookie.split(objreg);
			
	for(var intSub = 0; intSub < aCookies.length; ++intSub)
	{
		var objreg = new RegExp("=");
		
		var aNameValue = aCookies[intSub].split(objreg);
				
		DeleteCookie(aNameValue[0])
				
		++intCookieCnt;
	};
			
	return intCookieCnt;
}