//__Cookie functions written by Randall Ingermanson.

/**
 * Concatenates an array of data values as one long delimiter-separated string 
 * and saves it as a single cookie.
 * @return Returns true if the cookie appears to have been written, false otherwise.
 * @param thisDocument The document. Must pass this in or the cookie functions fail!
 * @param name The name of the cookie.
 * @param valueArray The array of values of the cookie.
 * @param delimiter The separator between values.
 * @param daysTillExpiration The number of days in the future when the 
 * cookie should expire.
 */
function RSI_writeArrayDataCookie(thisDocument, name, valueArray, delimiter, daysTillExpiration)
{
	//__Concatenate the array.
	var value = "";
	
	if (valueArray != null)
	{
		for (var i = 0; i < valueArray.length; i++)
		{
			//__Add the next value.
			value += valueArray[i];
			
			//__If this is not the last element, add the delimiter.
			if (i < valueArray.length - 1)
				value += delimiter;
		}	//__End for-loop over the array.
	}
	
	//__Save the value as a cookie.
	var result = RSI_writeCookie(thisDocument, name, value, daysTillExpiration);
	return result;	
}	//__End RSI_writeArrayDataCookie().

/**
 * Looks up a cookie that was previously saved using the 
 * function RSI_writeArrayDataCookie() and parses this as 
 * a delimiter-separated series of floats.
 * @return Returns the array that was created from the cookie.
 * @param thisDocument The document. Must pass this in or the cookie functions fail!
 * @param name The name of the cookie.
 * @param delimiter The delimiter used to separate values in the cookie.
 */
function RSI_lookupFloatArrayCookie(thisDocument, name, delimiter)
{
	//__Read the cookie as a string.
	var value = RSI_lookupOneCookie(thisDocument, name);
	if (value == null)
		return null;
		
	//__Split the string using the delimiter.
	var resultArray = value.split(delimiter);
	if (resultArray == null || resultArray.length == 0)
		return null;
		
	//__Parse each entry in the tempArray.
	for (var i = 0; i < resultArray.length; i++)
	{
		resultArray[i] = parseFloat(resultArray[i]);
	}	//__End for-loop over the tempArray.
		
	//__Return the result.
	return resultArray;
}	//__End RSI_lookupFloatArrayCookie().

/**
 * Strips out the leading blanks from a string.
 * @return Returns the resulting string with no blanks.
 * If the input string was all blanks, then an empty string is returned.
 */
function RSI_stripLeadingBlanks(thisString)
{
	//__Check for the first nonblank character.
	for (var i = 0; i < thisString.length; i++)
	{
		//__When I find a nonblank character, return the string from that point onward.
		if (thisString.charAt(i) != " ")
			return thisString.substr(i);
	}	//__End for-loop over characters.
	
	//__If we made it this far, the string was all blanks, so return an empty string.
	return "";
}	//__End RSI_stripLeadingBlanks().

/**
 * Writes a cookie.
 * @return Returns true if the cookie appears to have been written, false otherwise.
 * @param thisDocument The document. Must pass this in or the cookie functions fail!
 * @param name The name of the cookie.
 * @param value The value of the cookie.
 * @param daysTillExpiration The number of days in the future when the 
 * cookie should expire.
 */
function RSI_writeCookie(thisDocument, name, value, daysTillExpiration)
{
	//__Get today's date.
	var today = new Date();
	var numMSec = daysTillExpiration*86400*1000;
	var expireDate = new Date();
	expireDate.setTime(today.getTime() + numMSec);
	
	//__Write the cookie string.
	var cookieString = name + "=" + escape(value) + ";expires=" + expireDate.toGMTString() + ";path=/";
	thisDocument.cookie = cookieString;
	var cookieWritten = thisDocument.cookie;
	
	//__Return a flag.
	return (cookieWritten.length > 0);			
}	//__End RSI_writeCookie().

/**
 * Reads all the cookies for this site and parses them into two parallel arrays.
 * @param thisDocument The document. Must pass this in or the cookie functions fail!
 * @param Returns an array of cookie names.
 * @param Returns an array of cookie values.
 */
function RSI_readCookiesToArrays(thisDocument, nameList, valueList)
{
	//__Parse the cookie string into name-value pairs. All pairs are separated by ";".
	var cookieString = thisDocument.cookie;
	var cookieList = cookieString.split(";");
	
	//__Parse each cookie and save it in the arrays.
	for (var i = 0; i < cookieList.length; i++)
	{
		//__Parse the next cookie into two pieces.  They are separated by "=".
		var thisCookie = cookieList[i].split("=");
		
		//__Put the parts into an array. Note that the cookie string contains both blanks 
		//__and "=" characters, so we need to strip out any leading blanks from the names.
		nameList[i] = RSI_stripLeadingBlanks(thisCookie[0]);
		valueList[i] = unescape(thisCookie[1]);
	}	//__End for-loop over the list of cookies.
}	//__End RSI_readCookiestoArrays().

/**
 * Reads all the cookies for this site and parses them into an associative array.
 * @return Returns an associative array of values indexed by name.
 * @param thisDocument The document. Must pass this in or the cookie functions fail!
 */
function RSI_readCookiesToArray(thisDocument)
{
	//__Parse the cookie string into name-value pairs. All pairs are separated by ";".
	var cookieString = thisDocument.cookie;
	var cookieList = cookieString.split(";");
	var resultArray = new Array();
	
	//__Parse each cookie and save it in the array.
	for (var i = 0; i < cookieList.length; i++)
	{
		//__Parse the next cookie into two pieces.  They are separated by "=".
		var thisCookie = cookieList[i].split("=");
		
		//__Put the parts into an array. Note that the cookie string contains both blanks 
		//__and "=" characters, so we need to strip out any leading blanks from the names.
		var name = RSI_stripLeadingBlanks(thisCookie[0]);
		var value = unescape(thisCookie[1]);
		resultArray[name] = value;
	}	//__End for-loop over the list of cookies.
	
	//__Return the result.
	return resultArray;
}	//__End RSI_readCookiestoArray().

/**
 * Looks up the value of a single named cookie.
 * @return Returns the value of the named cookie, or returns null if it was not found.
 * @param thisDocument The document. Must pass this in or the cookie functions fail!
 * @param name The name of the cookie to look up.
 */
function RSI_lookupOneCookie(thisDocument, name)
{
	//__Parse the cookie string into name-value pairs. All pairs are separated by ";".
	var cookieString = thisDocument.cookie;
	var cookieList = cookieString.split(";");

	//__Parse each cookie.
	for (var i = 0; i < cookieList.length; i++)
	{
		//__Parse the next cookie into two pieces.  They are separated by "=".
		var thisCookie = cookieList[i].split("=");
		
		//__Strip off a possible leading or trailing blank.
		var thisName = RSI_stripLeadingBlanks(thisCookie[0]);
		
		//__If thisName matches the name we're looking for, then return the result.
		if (thisName == name)
		{
			var result = thisCookie[1];
			if (result == null)
				return null;
			return unescape(result);
		}
	}	//__End for-loop over the list of cookies.
	
	//__If we made it this far, the cookie isn't there.
	return null;
}	//__End RSI_lookupOneCookie().

