// Cookie functions written by Randall Ingermanson

/**
 * 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 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 stripLeadingBlanks().

/**
 * Writes a cookie.
 * @return Returns true if the cookie appears to have been written, false otherwise.
 * @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(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=/";
	document.cookie = cookieString;
	var cookieWritten = document.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 Returns an array of cookie names.
 * @param Returns an array of cookie values.
 */
function RSI_readCookiesToArrays(nameList, valueList)
{
	//__Parse the cookie string into name-value pairs. All pairs are separated by ";".
	var cookieString = document.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] = 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.
 */
function RSI_readCookiesToArray()
{
	//__Parse the cookie string into name-value pairs. All pairs are separated by ";".
	var cookieString = document.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 = 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 name The name of the cookie to look up.
 */
function RSI_lookupOneCookie(name)
{
	//__Parse the cookie string into name-value pairs. All pairs are separated by ";".
	var cookieString = document.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 = stripLeadingBlanks(thisCookie[0]);
		
		//__If thisName matches the name we're looking for, then return the result.
		if (thisName == name)
			return unescape(thisCookie[1]);
	}	//__End for-loop over the list of cookies.
	
	//__If we made it this far, the cookie isn't there.
	return null;
}	//__End RSI_lookupOneCookie().

