// JavaScript Document - Cookie Handling
//------------------------  Header  -----------------------------
//	Name:			cookie.js
//	Version:		v01.00
//	V Date:		Aug. 16, 2006
//	Author:		Michael B. Bender
//	Copyright:	2006 Allibus Corp. All rights reserved.
// Compatability:
//		jScript:	1.2 - requires "slice" method for arrays
//		Jscript:	2.0
//		IE: tbd
//		NN: tbd
//	History:		See revision history at bottom of script
//---------------------------------------------------------------

// --------------------  Classes  -------------------------------
function cookieClass(name, value)			// define cookie class
{
	this.name = name;								// Name of the cookie
	this.value = value;							// value (unescaped)
}

// --------------------- Globals  -------------------------------
var	numCookies_G;
var	cookieLst_G;
var 	cookieErr_G		= 0;						// Last error code received
var	usrCookie_G		= "";						// original user's cookie string
var	cookieSupport_G	= true;				// assume cookie support

// -------------------- Functions -------------------------------
// -- internal functions
//
function deleteInternalCookie (name)		// Deletes a cookies from the internal list
{
	var i, j;
	for (i=0; i<numCookies_G; i++)			// scan current cookies
	{
		if (cookieLst_G [i].name == name)	// found it.
		{
			for (j=i+1; j<numCookies_G; j++)	// move the rest
			{
				cookieLst_G [j-1] = cookieLst_G [j];
			}
			numCookies_G --;						// reduce count
			cookieLst_G = cookieLst_G.slice (0, numCookies_G);
			break;
		}
	}
}

function addInternalCookie (name, value)	// adds a cookie to the internal list
{
	cookieLst_G [numCookies_G] = new cookieClass;
	cookieLst_G [numCookies_G].name = name;
	cookieLst_G [numCookies_G].value = value;
	numCookies_G ++;
}

function deleteCookie (name)
{
	putCookie (name, "", -1, "");			// do a put with an expiration in the past
}

function putCookie (name, value, months, path)	// Writes the cookie
// Note: this also updates the current cookie list as necessary
// months: 	0 = expiration not set (for session-only cookies
// 			+ = number of months in the future to expire
//				- = delete this cookie (sets expiration in past)
{
	var cExpires = "";
	var cValue = "";
	var cPath = "";
	var cString="";
	
	cValue = escape(value);				// clear out unpleasant characters
	if (months != 0)						// Then set expiration date
	{											// months < 0 causes deletion
		var lclDate = new Date();		// get the date
		lclDate.setMonth(lclDate.getMonth() + months);
		cExpires = ";expires=" + lclDate.toGMTString() + ";";
	}
	cExpires = cExpires.replace("UTC", "GMT");
	if ((path != null) && (path.length > 0))
		cPath = "path=" + path + ";";
	cString = name + "=" + cValue + cExpires + cPath;
	document.cookie = cString;
	
	//
	// now, find the cookie in the local list and update appropriately
	//
	if (months < 0)
		deleteInternalCookie (name);
	else
	{
		for (i=0; i<numCookies_G; i++)
		{
			if (cookieLst_G [i].name == name)
			{
				cookieLst_G[i].value = value;
				break;
			}
		}
		// then we didn't find it
		addInternalCookie (name, value);
	}
}

function parseCookie ()
// ----------  Function Header  --------------
//	Name:			parseCookie
//	Purpose:		Retrieves raw cookies and parses them into memory
//	Inputs:		none (user's cookies)
//	Process:		get all cookies
//					While there's some cookie left
//						create a new internal cookie (class)
//						save the name
//						save the cookie value
//						incriment number of cookies
//					endw
//	Outputs:		true if cookies supported, otherwise false.
//	-------------------------------------------
{
	var cIndex 	= 0;						// index
	var cStart 	= 0;						// start of current element
	var cEnd 	= 0;						// end of current element

	usrCookie_G = document.cookie;	// get our cookie
	numCookies_G = 0;						// Initialize our list
	cookieSupport_G = true;				// assume they're supported
	cookieLst_G = new Array;			//	Create array of cookies
	//
	// scan through the cookie string looking for names
	//
	while (cEnd < usrCookie_G.length)						// There's more to this cookie
	{
		cookieLst_G [numCookies_G] = new cookieClass;
		cEnd = usrCookie_G.indexOf("=", cStart);			// Find the '='
		cookieLst_G [numCookies_G].name = usrCookie_G.substring(cStart, cEnd);
		cStart = ++cEnd;											// Value starts after the = sign.
		cEnd = usrCookie_G.indexOf(";", cStart);			// find the ';' terminator
		if (cEnd == -1)											// then, end of string
			cEnd = usrCookie_G.length;
		cookieLst_G [numCookies_G].value = unescape(usrCookie_G.substring(cStart, cEnd));
		cStart = cEnd + 2;										// past the ; and the space
		numCookies_G++;											// indicate one more cookie added
	}			
	//
	// if we got a cookie, then they're supported. If not, test for support
	// 
	if (numCookies_G == 0)												// >IF : there are no cookies saved
	{																			// THEN: test for support
		var testExpDate = new Date();									// current date
		testExpDate.setDate(testExpDate.getDate() + 1); 		// add a day
		testExpDate = testExpDate.toGMTString();					// convert to gmt
		testExpDate = testExpDate.replace("UTC", "GMT"); 		// I readly don't know why I have to do this.
		document.cookie = "ct=ct; expires=" + testExpDate;		// add a scookie with a date in the future (non-sesson)
		if (document.cookie.length == 0)								// >IF : Cookie not saved
		{						
			cookieSupport_G = false;									// THEN: no cookie support
		}
	}
	return cookieSupport_G;
}

function getCookie (name)
// ----------  Function Header  --------------
//	Name:			getCookie 
//	Purpose:		retrieves the raw but escaped cookie string
//	Inputs:		cookie name
//	Process:		search through the cookies in memory
//					if found
//						return the string
//	Outputs:		cookie string or NULL if not found
//	-------------------------------------------
{
	var i;
	var returnString = null;
	
	for (i=0; i<numCookies_G; i++)
	{
		if (cookieLst_G[i].name == name)
		{
			returnString = cookieLst_G[i].value;
			break;
		}
	}
	return returnString;
}
// -------------------  History  -----------------------
// v01.00 Created and tested Aug 16, 2006 MBB
// ----------------  end History  -----------------------