﻿
///////////////////////////////////////////////////////////////////////////////
// Function:		writeCookie
//
// Description:		Writes the cookie to the users machine.
//
// Parameters:		name
//					STRING [IN] - The name of the cookie.
//					
//					value
//					STRING [IN] - The value to store in the cookie.
//
//					expirationDate
//					STRING [IN] - The expiration date to store for the cookie.
//
//					path
//					STRING [IN] - The path that determine the availability of the cookie.
//
// Remarks:			None.
//
// Return:			None.
//
// Errors:			None		    
//
// History:			11-16-00	JHG		Created
//					10-28-02	JHG		Moved from UserCommon/Globals.asp for
//										implementation of presentation frameworks
///////////////////////////////////////////////////////////////////////////////
function writeCookie (name, value, expirationDate, path) {

	document.cookie = name + "=" + escape(value) + "; expires=" + expirationDate + "; path=" + path;
}

///////////////////////////////////////////////////////////////////////////////
// Function:		getCookieVal
//
// Description:		Returns the value of the selected cookie.
//
// Parameters:		offset
//					LONG [IN] - The starting point for the cookie value.
//
// Remarks:			None.
//
// Return:			None.
//
// Errors:			None		    
//
// History:			11-14-00	JHG		Created
//					10-28-02	JHG		Moved from UserCommon/Globals.asp for
//										implementation of presentation frameworks
///////////////////////////////////////////////////////////////////////////////
function getCookieVal (offset) {
//	Initialize ending of value
	var lEndOfString = document.cookie.indexOf(";", offset)
	
//	Determine if the end of the cookie string is empty and reset the end marker
	if (("" + lEndOfString) == "" || lEndOfString == -1)
		lEndOfString = document.cookie.length

//	Return the appropriate value	
	return unescape(document.cookie.substring(offset, lEndOfString))
	}

///////////////////////////////////////////////////////////////////////////////
// Function:		getCookie
//
// Description:		Returns the specified cookie and its value.
//
// Parameters:		name
//					STRING [IN] - The cookie to retrieve.
//
// Remarks:			This function calls getCookieVal to retrieve the value of the cookie.
//
// Return:			None.
//
// Errors:			None		    
//
// History:			11-14-00	JHG		Created
//					10-28-02	JHG		Moved from UserCommon/Globals.asp for
//										implementation of presentation frameworks
///////////////////////////////////////////////////////////////////////////////
function getCookie (name) {
//  Initialize all variables
	var strArgument = name + "=";
	var lArgumentLength = strArgument.length;
	var lCookieLen = document.cookie.length;
	var i = 0;

//	All cookies are stored as a string, therefore we must retrieve the correct values
//	Loop the the current cookie to find the value associated with the desired cookie
	while (i < lCookieLen) {
		var j = i + lArgumentLength;
		if (document.cookie.substring(i, j) == strArgument) {
			return getCookieVal (j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
//	Return a null string if the cookie doesn't exist
	return null;
}

///////////////////////////////////////////////////////////////////////////////
// Function:		setCookie
//
// Description:		Stores the specified cookie and its value on the users workstation
//
// Parameters:		name
//					STRING [IN] - The cookie to set.
//
//					value
//					STRING [IN] - The value of the cookie.
//
// Remarks:			The expiration date will be set to the date that is one year from the current date.
//					This is necessary to make the cookie persistent.
//					Sub-values are delimited with a tilde (~).
//
// Return:			None.
//
// Errors:			None		    
//
// History:			11-14-00	JHG		Created
//					10-28-02	JHG		Moved from UserCommon/Globals.asp for
//										implementation of presentation frameworks
///////////////////////////////////////////////////////////////////////////////
function setCookie (name, value) {
	
	
//	Set expiration date for one year from current date and time
	var dtExpirationDate = new Date();
	var dtExpirationTime = dtExpirationDate.getTime() + (730 * 24 * 60 * 60 * 1000);
	dtExpirationDate.setTime (dtExpirationTime);
	
	var strCurrentCookie = getCookie (name);
	var strMessage = "bill has";
	var strMessageExists = "The selected bill is currently in your profile.";
	var lCookieLen = 0
	if (strCurrentCookie != null)
		lCookieLen = strCurrentCookie.length;
	var i = 0;
	
//	Set the return message
	if (name == "GMACity") {
		strMessage = "local bills have"
	}
	
//	Determine if the cookie has been set, if not, set it
	if (lCookieLen == 0) {
		writeCookie ( name, value, dtExpirationDate.toGMTString(), "/");
		alert ("The selected " + strMessage + " been added to your profile.");
		return;
	}
	
	var strNewCookie = strCurrentCookie
//	if the cookie has been previously set, determine if the current bill has already been selected
	while (i < lCookieLen) {
		var j = strNewCookie.indexOf("~");
		
		//  If the bill is the first and only bill, then don't add it again
		if ((j == -1) && (strNewCookie == value)) {
			alert(strMessageExists);
			return;
		}
		if (strNewCookie.substring(i, j) == value) {
			alert(strMessageExists);
			return;
		}
		strNewCookie = strNewCookie.substring((j + 1), strNewCookie.length)
		if (j == -1) break;
	}
//	Write out the new cookie	
	writeCookie ( name, strCurrentCookie + "~" + escape(value), dtExpirationDate.toGMTString(), "/");
	
	alert ("The selected " + strMessage + " been added to your profile.");
	
	return;
}

///////////////////////////////////////////////////////////////////////////////
// Function:		resetCookie
//
// Description:		Clears/Deletes the specified cookie.
//
// Parameters:		name
//					STRING [IN] - The cookie to remove.
//
// Remarks:			None.
//
// Return:			None.
//
// Errors:			None		    
//
// History:			11-14-00	JHG		Created
//					10-28-02	JHG		Moved from UserCommon/Globals.asp for
//										implementation of presentation frameworks
///////////////////////////////////////////////////////////////////////////////
function resetCookie (name) {
//  Set the expiration date to Jan 1, 2000 - it must be a date in the past 
	writeCookie ( name, "", "Sat, 01-Jan-00 00:00:01 GMT", "/");
}

///////////////////////////////////////////////////////////////////////////////
// Function:		deleteCookie
//
// Description:		Deletes the specified value from the stored value of the specified Cookie.
//
// Parameters:		name
//					STRING [IN] - The cookie to used for value deletion.
//					
//					value
//					STRING [IN] - The sub-value to delete from the specified value.
//
// Remarks:			This function calls getCookie to retrieve the value of the cookie.
//					Only the specified sub-value will deleted from the cookie value.  The 
//					original cookie will be rewritten to the users workstation.
//					Sub-values are delimited with a tilde (~).
//
// Return:			None.
//
// Errors:			None		    
//
// History:			11-14-00	JHG		Created
//					10-28-02	JHG		Moved from UserCommon/Globals.asp for
//										implementation of presentation frameworks
///////////////////////////////////////////////////////////////////////////////
function deleteCookie (name, value) {
	
//	Set expiration date for one year from current date and time
	var dtExpirationDate = new Date();
	var dtExpirationTime = dtExpirationDate.getTime() + (365 * 24 * 60 * 60 * 1000);
	dtExpirationDate.setTime (dtExpirationTime);
	
//	Retrieve the cookie and initialize the control variables
	var strCurrentCookie = getCookie (name);
	var i = 0;
	var j = 0;

//	Make sure that the length of the cookie is set even when the cookie is non-existent or set to null
	var lCookieLen = 0;
	if (strCurrentCookie != null)
		lCookieLen = strCurrentCookie.length;
	
//	Determine if the cookie exists, if not, return - no need to go any further
	if (lCookieLen == 0) {
		return;
	}

//	Initialize the temporary string variables
	var strNewCookie = "";
	var strTempCookie = strCurrentCookie;

//	if the cookie has been previously set, determine if the current bill is the one selected
//	Loop through the cookie value and pull out each distinct bill ID ("~" separates each distinct bill ID)
	while (j < lCookieLen) {
		var k = strTempCookie.indexOf("~");

	//  If the bill is the first and only bill, then delete and break out of the loop
		if ((k == -1) && (strTempCookie == value)) {
			strTempCookie = "";
			break;
		}
	
	//	Check to see if the specified search value is equal to the current bill ID
		var strSearchString = strTempCookie.substring(i, k);

	//	Reset the temporary variable to the remainder of the cookie value.  This will be used to continue the
	//	the search - the current value will be removed from the cookie string
		strTempCookie = strTempCookie.substring((k + 1), strCurrentCookie.length);
		
	//	If the Bill ID is the value being deleted, break out of the loop
		if (strSearchString == value)
			break;
	
	//	If the new string to search is not the empty string, rebuild the cookie value.
	//	The new Cookie value will be built using the current value
		if (strSearchString != "")
			strNewCookie = strNewCookie + "~" + strSearchString;
	
	//	Increment loop control
		j = k + 1;
		if (k == -1) break;
	}

//	Build the new cookie with the strNewCookie variable and the strTempCookie variable.
//	Only build this if strTempCookie has a value, otherwise strNewCookie will have an additional "~"
//	on the end
	if (strTempCookie != "")
		strNewCookie = strNewCookie + "~" + strTempCookie;
		
//	Strip off the preceding "~"
	if (strNewCookie.indexOf("~") == 0)
		strNewCookie = strNewCookie.substring(1, (strNewCookie.length + 1));

//	Write out the new cookie.  The expiration date must be reset, otherwise the cookie will expire immediately
	writeCookie (name, strNewCookie, dtExpirationDate.toGMTString(), "/");
	
//	Submit the form
	document.forms[0].submit();
}

///////////////////////////////////////////////////////////////////////////////
// Function:		processCityList
//
// Description:		Loops through the list of selected cities and adds the city ids to 
//					the cookie.
//
// Parameters:		strCityList
//					STRING [IN] - The list of cities to be added to the cookie.
//
// Remarks:			None.
//
// Return:			None.
//
// Errors:			None		    
//
// History:			11-17-00	JHG		Created
//					10-29-02	JHG		Moved from UserCommon/Globals.asp for
//										implementation of presentation frameworks
//					11-25-02	JHG		Changed to submit the form when done.
//					12-10-02	JHG		Removed form submission - if submitted
///////////////////////////////////////////////////////////////////////////////
function processCityList (elCityList) {

	// Loop through city list
	for ( var i = 0; i < elCityList.length; i++) 
		
		if (elCityList.options[i].selected)
			setCookie("GMACity", elCityList.options[i].value);	
	
	//document.forms[0].submit();
}