/*=============================================================================
' Library Description:
'
' Contains JavaScript code used for handling and manipulating cookies in an
' ASP environment running IIS.
' 
'============================================================================*/

/*=============================================================================
' Subroutine:	SetCookie
' Description:	Write a customized cookie to the user's machine.
' Notes:		Use a date in the past to have it expire immediately, or
'				a date in the future to have it persist.
' Assumptions:  The browser has cookies enabled.
' Dependencies:	None.
' Usage:		SetCookie ("MyCookie", strMyContents, objDate, "/");
' Input:		name - name of the cookie
'				value - value of the cookie
'				expires - expiration date of the cookie (defaults to end of current session)
'				path - path for which the cookie is valid (defaults to path of calling document)
'				domain - domain for which the cookie is valid (defaults to domain of calling document)
'				secure - Boolean value indicating if the cookie transmission requires a secure transmission
' Return value: Boolean.
'------------------------------------------------------------------------------
' 2001-12-01 CDR - Created.
' 2002-01-17 JRM - Added some error checking and return value.
' 2002-04-19 JRM - Added alert message when setting the cookie fails.
'============================================================================*/

function SetCookie (name, value, expires, path, domain, secure) {
	try {
		var curCookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
		
  		document.cookie = curCookie;
 	} catch (e) {
 		alert ("Error! Could not set the requested cookie.\n\n" + e.number + "\n" + e.description);
  		return (false);
  	}
  	
	return (true);
}

/*=============================================================================
' Subroutine:	GetCookie
' Description:	Returns a string containing the value of the specified cookie 
'				or null if cookie does not exist.
' Notes:		None.
' Assumptions:  The browser has cookies enabled.
' Dependencies:	None.
' Usage:		strCookieContents = GetCookie("MyCookie");
' Input:		name - name of the desired cookie
' Return value: String.
'------------------------------------------------------------------------------
' 2001-12-01 CDR - Created.
'============================================================================*/

function GetCookie (name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);

	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) { return null; }
	} else {
		begin += 2;
	}
	
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	
	var strReturnValue = unescape(dc.substring(begin + prefix.length, end));
	
	return (strReturnValue);
}

/*=============================================================================
' Subroutine:	CookieStringIsMultiValue
' Description:	None.
' Notes:		None.
' Assumptions:  None.
' Dependencies:	None.
' Usage:		None.
' Input:		None.
' Return value: None.
'------------------------------------------------------------------------------
' 2002-01-31 JRM - Created.
'============================================================================*/

function CookieStringIsMultiValue (strCookieValue) {
	return (strCookieValue.match(/=/));
}

/*=============================================================================
' Subroutine:	EraseCookieContents
' Description:	None.
' Notes:		None.
' Assumptions:  None.
' Dependencies:	None.
' Usage:		None.
' Input:		None.
' Return value: None.
'------------------------------------------------------------------------------
' 2002-01-31 JRM - Created.
'============================================================================*/

function EraseCookieContents (strSingleCookie, strDomain, blnSecure) {
	var objDate = new Date();
	var strPathName = "/";
	
	objDate.setTime (objDate.getTime() - 365 * 24 * 60 * 60 * 1000);

	var strNewCookie = strSingleCookie + "=" + "" +
		((objDate) ? "; expires=" + objDate.toGMTString() : "") +
		((strPathName) ? "; path=" + strPathName : "") +
		((strDomain) ? "; domain=" + strDomain : "") +
		((blnSecure) ? "; secure" : "");
		
	document.cookie = strNewCookie;
}

/*=============================================================================
' Subroutine:	WriteMultiValueCookie
' Description:	None.
' Notes:		None.
' Assumptions:  None.
' Dependencies:	None.
' Usage:		None.
' Input:		None.
' Return value: None.
'------------------------------------------------------------------------------
' 2002-02-01 JRM - Created.
' 2002-04-18 JRM - Modified code to build and write the cookie (we basically 
'				integrated the BuildMultiValueCookie() call into the function.)
'============================================================================*/

function WriteMultiValueCookie (strMultiCookie, strNewVariable, strNewValue, strPathName, strDomain, blnSecure) {
	var strCookieContents = BuildMultiValueCookie (strMultiCookie, strNewVariable, strNewValue);
	
	// Set the default path to "/" if nothing was provided.
	if (! strPathName) { strPathName = "/"; }
	
	var objDate = new Date();
	objDate.setTime (objDate.getTime() + 365 * 24 * 60 * 60 * 1000);

	var strNewCookie = strCookieContents +
		((objDate) ? "; expires=" + objDate.toGMTString() : "") +
		((strPathName) ? "; path=" + strPathName : "") +
		((strDomain) ? "; domain=" + strDomain : "") +
		((blnSecure) ? "; secure" : "");
	
	document.cookie = strNewCookie;
}

/*=============================================================================
' Subroutine:	GetMultiValueCookie
' Description:	None.
' Notes:		None.
' Assumptions:  None.
' Dependencies:	None.
' Usage:		None.
' Input:		None.
' Return value: None.
'------------------------------------------------------------------------------
' 2002-04-18 JRM - Created.
' 2002-04-19 JRM - Made sure to 'var' the i and j in for() loops, to prevent
'				conflicts with other i's and j's in other loops and functions.
'============================================================================*/

function GetMultiValueCookie (strWantedCookie, strVariableName) {
	var strCookieFile = document.cookie;
	var strNewCookie = "";
	
	//-----------------------------------------------------------------------------
	// Go through each set of cookies in the cookie file and build the appropriate
	// cookie string.
	//-----------------------------------------------------------------------------
	var blnFoundCookie = false;
	var aryChunks = strCookieFile.split(/\s*;\s*/);
	
	for (var i = 0; i < aryChunks.length; i++) {
		var strChunks = aryChunks[i];
		var aryValues = strChunks.match(/^([^=]*)=(.*)$/);
		var strThisCookieName = aryValues[1];
		var strThisCookieContents = aryValues[2];
		var blnFoundVariable = false;
		
		//-----------------------------------------------------------------------------
		// Make sure we have a multi-value cookie.
		//-----------------------------------------------------------------------------
		if (! CookieStringIsMultiValue(strThisCookieContents)) {
			continue;
		}
		//-----------------------------------------------------------------------------
		// Make sure this is the multi-value cookie requested.
		//-----------------------------------------------------------------------------
		if (strThisCookieName != strWantedCookie) {
			continue;
		} 
			
		//-----------------------------------------------------------------------------
		// Go through each set of name/value pairs in the cookie contents and look for
		// the requested cookie variable.
		//-----------------------------------------------------------------------------
		var arySubCookies = strThisCookieContents.split(/&/);
		blnFoundCookie = true;
		strNewCookie += strThisCookieName + "=";
				
		for (var j = 0; j < arySubCookies.length; j++) {
			var arySubValues = arySubCookies[j].match(/^([^=]*)=(.*)$/);
			var strVarName = arySubValues[1];
			var strVarData = arySubValues[2];
					
			if (strVarName == strVariableName) {
				// The variable exists. Return its value.
				return (strVarData);
			}
		}
	}
			
	return (null);
}

/*=============================================================================
' Subroutine:	BuildMultiValueCookie
' Description:	None.
' Notes:		None.
' Assumptions:  None.
' Dependencies:	None.
' Usage:		None.
' Input:		None.
' Return value: String.
'------------------------------------------------------------------------------
' 2002-01-31 JRM - Created.
' 2002-04-18 JRM - Fixed bug where we were creating an invalid cookie when
'				a single multi-value cookie was built.
' 2003-05-30 JRM - Added error handling to fix single-variable bug.
'============================================================================*/

function BuildMultiValueCookie (strWantedCookie, strNewVariable, strNewValue) {
	var strCookieFile = document.cookie;
	var strNewCookie = "";
	
	//-----------------------------------------------------------------------------
	// Go through each set of cookies in the cookie file and build the appropriate
	// cookie string.
	//-----------------------------------------------------------------------------
	var blnFoundCookie = false;
	var aryChunks = strCookieFile.split(/\s*;\s*/);
	
	for (var i = 0; i < aryChunks.length; i++) {
		var strThisCookieName;
		var strThisCookieContents;

		var blnFoundVariable = false;
		var strChunks = aryChunks[i];
		var aryValues = strChunks.match(/^([^=]*)=(.*)$/);
		
		if (aryValues) {
			strThisCookieName = aryValues[1];
			strThisCookieContents = aryValues[2];
		} else {
			strThisCookieName = strChunks;
			strThisCookieContents = "";
		}
		
		//-----------------------------------------------------------------------------
		// Make sure we have a multi-value cookie.
		//-----------------------------------------------------------------------------
		if (! CookieStringIsMultiValue(strThisCookieContents)) {
			continue;
		}
		//-----------------------------------------------------------------------------
		// Make sure this is the multi-value cookie requested.
		//-----------------------------------------------------------------------------
		if (strThisCookieName != strWantedCookie) {
			continue;
		} 
			
		//-----------------------------------------------------------------------------
		// Go through each set of name/value pairs in the cookie contents and look for
		// the requested cookie variable.
		//-----------------------------------------------------------------------------
		var arySubCookies = strThisCookieContents.split(/&/);
		blnFoundCookie = true;
		strNewCookie += strThisCookieName + "=";
				
		for (var j = 0; j < arySubCookies.length; j++) {
			var arySubValues = arySubCookies[j].match(/^([^=]*)=(.*)$/);
			var strVarName = arySubValues[1];
			var strVarData = arySubValues[2];
					
			if (strVarName == strNewVariable) {
				// The variable exists. Modify its value.
				strNewCookie += strVarName + "=" + escape(strNewValue);
				blnFoundVariable = true;
			} else {
				// Leave this variable intact. Check the next one.
				strNewCookie += strVarName + "=" + strVarData;
			}

			if (j < arySubCookies.length - 1) {
				strNewCookie += "&";
			}
		}
				
		//-----------------------------------------------------------------------------
		// If the variable being set was not found in the cookie that already existed,
		// add the requested variable to the end of that cookie.
		//-----------------------------------------------------------------------------
		if (! blnFoundVariable) {
			strNewCookie += "&" + strNewVariable + "=" + escape(strNewValue);
		}
	}
			
	if (! blnFoundCookie) {
		strNewCookie += strWantedCookie + "=" + strNewVariable + "=" + escape(strNewValue);
	}		
	
	return (strNewCookie);
}