//(Dakar senegal) The functions to manage and to manipulate the cookies
var gJSCMToday = new Date();
var gJSCMExpiryDate = new Date(gJSCMToday.getTime() + (1 * 24 * 60 * 60 * 1000)); // 1 day from now
var gsJSCMKey = "";
var gsJSCMValue = "";

// Create a cookie
function fSetCookie (asName, asValue, abDoEscape, aiExpiryDate)
{
	fSetCookie2 (asName, asValue, abDoEscape, aiExpiryDate, null, null, null)
}

// Create a cookie
function fSetCookie2 (asName, asValue, abDoEscape, aiExpiryDate, asPath, asDomain, abSecure)
{
/*
	var lsTemp = asName + "=" + ((abDoEscape) ? escape (asValue) : asValue)
								+ ((aiExpiryDate) ? "; expires=" + aiExpiryDate.toGMTString() : "")
								+ ((asPath) ? "; path=" + asPath : "")
								+ ((asDomain) ? "; domain=" + asDomain : "")
								+ ((abSecure) ? "; secure" : "");
*/								
	var lsTemp = asName + "=" + ((abDoEscape) ? escape (asValue) : asValue)
								+ ((aiExpiryDate) ? "; expires=" + aiExpiryDate.toGMTString() : "")
								+ "; path=" + "/" 
								+ ((asDomain) ? "; domain=" + asDomain : "")
								+ ((abSecure) ? "; secure" : "");
/*
	var lsTemp = asName + "=" + ((abDoEscape) ? escape (asValue) : asValue)
								+ ((aiExpiryDate) ? "; expires=" + aiExpiryDate.toGMTString() : "")
								+ "; path=" + "	/PublicationServer" 
								+ ((asDomain) ? "; domain=" + asDomain : "")
								+ ((abSecure) ? "; secure" : "");
*/								
	//alert(lsTemp);
	document.cookie = lsTemp;
}

// Remove a cookie
function fDeleteCookie(asName)
{
// <input type="button" value="Delete cookie" language="javascript" onclick="fDeleteCookie(gsCookieName)">
	fDeleteCookie2(asName, null, null)
}

// Remove a cookie
function fDeleteCookie2(asName, asPath, asDomain)
{
	if (fGetCookie(asName, false))
	{
		document.cookie = asName + "=" + ((asPath) ? ";path="   + asPath   : "") + ((asDomain) ? ";domain=" + asDomain : "")
											+ ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

// Get a cookie value
function fGetCookie(asName, abEscape)
{
	var lsSearch = asName + "="
	if (document.cookie.length > 0)
	{ // if there are any cookies
		var liOffset = document.cookie.indexOf(lsSearch)
		if (liOffset != -1)
		{ // if cookie exists
			liOffset += lsSearch.length
			// set index of beginning of value
			var liEnd = document.cookie.indexOf(";", liOffset)
			// set index of end of cookie value
			if (liEnd == -1)
			{
				liEnd = document.cookie.length
			}
			if (abEscape)
				return document.cookie.substring(liOffset, liEnd);
			else
				return unescape(document.cookie.substring(liOffset, liEnd));
		}
	}
}

function fDisplayCookies()
{
// <input type="button" value="Display cookies" language="javascript" onclick="fDisplayCookies()">
	alert(document.cookie);
}

// Remove space char at the beginning and at the end of the string
function fTrim(asString)
{
	var lsRetString = asString;

	while (lsRetString.length > 0 && lsRetString.charAt(0) == " ")
		lsRetString = lsRetString.substring(1, lsRetString.length);

	while (lsRetString.length > 0 && lsRetString.charAt(lsRetString.length - 1) == " ")
		lsRetString = lsRetString.substring(0, lsRetString.length - 1);

	return lsRetString;
}

// Management of "string to string" maps for ASP (Dictionary)
function fMapStrToStrRemoveAll (aiCookieName)
{
	fDeleteCookie(aiCookieName);
}

function fMapStrToStrFindKey (aiCookieName, aiKey)
{
	var lsCookieValue = fGetCookie(aiCookieName, true);
	if (!lsCookieValue)
		lsCookieValue = "";

	var liPos = -1;
	var lContinue = true;
	var lFound = false;
	while (lContinue)
	{
		liPos = lsCookieValue.indexOf("" + aiKey + "=", liPos + 1)
		if (liPos == -1)
			lContinue = false;
		else
		if (liPos == 0 || lsCookieValue.charAt(liPos - 1) == "&")
		{
			lContinue = false;
			lFound = true;
		}
	}

	return liPos
}

function fMapStrToStrSetAt(aiCookieName, aiKey, aiValue)
{
	var lsCookieValue = fGetCookie(aiCookieName, true);
	if (!lsCookieValue)
		lsCookieValue = ""

	fMapStrToStrRemoveKey(aiCookieName, aiKey);

	if (lsCookieValue.length != 0)
		lsCookieValue += "&";

	lsCookieValue += "" + aiKey + "=" + escape(aiValue);
	fSetCookie(aiCookieName, lsCookieValue, false, gJSCMExpiryDate);
}

// Return the number of selected terms
function fMapStrToStrGetCount(aiCookieName)
{
	if (fGetCookie(aiCookieName, false))
	{
		var lsCookieValue = fGetCookie(aiCookieName, false);
		var liPos = -1;
		var liCount = 0;
		do
		{
			liPos = lsCookieValue.indexOf("=", liPos + 1);
			if (liPos != -1)
				liCount++;
		}
		while(liPos != -1);

		return liCount;
	}
	else
		return 0
}

function fMapStrToStrGetAt (aiCookieName, aiPos)
{
	if (aiPos < fMapStrToStrGetCount(aiCookieName))
	{
		var lsCookieValue = fGetCookie(aiCookieName, false);
		var liPos = -1;
		var liCount = 0;
		var lbContinue = true;
		while (lbContinue)
		{
			liPos = lsCookieValue.indexOf("=", liPos + 1);
			if (liCount >= aiPos)
				lbContinue = false;
			else
				liCount++;
		}

		gsJSCMKey = lsCookieValue.substr(0, liPos);
		gsJSCMValue = lsCookieValue.substr(liPos + 1);

		liPos = gsJSCMKey.lastIndexOf("&");
		if (liPos != -1)
			gsJSCMKey = gsJSCMKey.substr(liPos + 1);
		liPos = gsJSCMValue.indexOf("&")
		if (liPos != -1)
			gsJSCMValue = gsJSCMValue.substr(0, liPos);
	}
}

function fMapStrToStrRemoveKey(aiCookieName, aiKey)
{
	var lsCookieValue = fGetCookie(aiCookieName, true);
	if (lsCookieValue)
	{
		var liStartPos = fMapStrToStrFindKey (aiCookieName, aiKey);
		var liEndPos = lsCookieValue.indexOf("&", liStartPos);

		if (liEndPos == -1)
			liEndPos = lsCookieValue.length -1;
		else
			liEndPos--;

		if (liStartPos != -1)
		{
			var liLeftOffset = 0
			var liRightOffset = 0

			if (liStartPos > 0)
			{
				// Delete the "&" on the left, ex: K0=Value0&K1=Value1&K2=Value2
				//                                          ^^^^^^^^^^
				liLeftOffset = -1;
			}
			else
			if (lsCookieValue.length > liEndPos + 1)
			{
				// Delete the "&" on the right, ex: K0=Value0&K1=Value1&K2=Value2
				//                                  ^^^^^^^^^^
				liRightOffset = 1;
			}

			lsCookieValue = lsCookieValue.substring(0, liStartPos + liLeftOffset) + lsCookieValue.substring(liEndPos + liRightOffset + 1, lsCookieValue.length);

			if (lsCookieValue.length == 0) // Don't store a cookie with an empty value !!!
				fDeleteCookie(aiCookieName);
			else
				fSetCookie(aiCookieName, lsCookieValue, false, gJSCMExpiryDate);
		}
	}
}

