// ----------------AGWeb Cookie Code ---------------
// These functions create a temporary cookie (not written to disk)
// and store or retrieve form values in it.
//
// A cookie is simple a string which has a name attached to it.
// We divide a cookie into named crumbs.  Each crumb holds a collection
// of values for a particular map or chart.  A crumb name must have
// exactly the number of characters set by "crumbNlen" below.
// A crumb is further divided into named chips.  Each chip holds a
// single value.  All chip values are strings.
//
// 06-jul-00  T.Dessent
// - Function addChip - include crumbSep at the beginning of first
//     chip in a crumb, as well as in subsequent chips.
// - Function getChip - return empty string if chip name not found.
// - Both addChip and getChip now search for chip name enclosed in
//     separators (crumbSep, chipSep) to avoid finding a substring
//     of a longer chip name (e.g., "epol" and "epolMin").

token = "";
var appName = "AGWB";
var BigCrumb   = "";
var BigCrumb2  = "";
var GlbCrumb = "";
var BigCookie  = " ";
var crmblensz  = 3;
var chipSep    = ":";
var crumbHead  = "CRMB";
var cookieHead = "AIRS";
var crumbSep   = "@";
var crumbNlen  = 6;
// This function creates an array with the number of elements
// equal to the number of arguments passed to the function.
function initArray() {
	this.length = initArray.arguments.length;
	for (var i = 0; i < this.length; i++)
		this[i] = initArray.arguments[i];
	}
// create an array of elements containing AGWEB graphic names
var allCrumbs = new initArray("RDYTVW", "PltLoc", "MonLoc", "PltMon", "PSIMAP", "GLOBAL", "EMISUM", "EMSDST", "NONATN", "PLSRCH", "ZIPLOC", "MSALOC");
// This function is passed a "true" or "false" and returns the appropriate
// boolean value. Seems like a limitation in JavaScript.
function boolvalue(inbool)
	{
	if (inbool == "true") return true;
	else return false
	}
// a function to strip leading zeros off of the numbers
function StripLeadZero(astring)
	{
	var i = 0;
	while(astring.length > 0 )
		{
		if (astring.charAt(i) != "0") break;
		else i++;
		}
	return(astring.substring(i,astring.length));
	}

// SetupCookie creates a new cookie so that crumbs can be added to it.
function SetupCookie(x,CookieName)
	{
	x = cookieHead + CookieName;
	return x;
	}
// storeCookie - save the cookie that was baked to the disk for later retrieval
function storeCookie (name,value,expires,path,domain,secure)
	{
	newdate = new Date();
	newdate = newdate + (9*24*60*60*1000);
	document.cookie = name + "=" + escape (value) +
		((expires) ? "; expires=" + newdate : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	}
// this function gets the cookie from the disk so that we can pull the crumbs out.
function retrieveCookie()
	{
	tcookie = unescape(document.cookie);
	tindex = tcookie.indexOf("EPA=");
	tmpCookie=tcookie.substring(tindex+4,tcookie.length);
	if (tmpCookie == "")
		tmpCookie = SetupCookie(tmpCookie,appName);
	return(tmpCookie);
	}
// this function separates the cookie into crumbs, replaces the named crumb,
// and reassembles the cookie.
function saveCrumbs(acookie,myCrumb,crumbName)
	{
	var newCookie = "";
	newCookie = SetupCookie(newCookie,appName);
	for (var i = 0; i < allCrumbs.length; i++)
		{
		thisCrumb=getCrumb(acookie,allCrumbs[i]);
		if (allCrumbs[i] == crumbName)
			thisCrumb = myCrumb;
		if (thisCrumb != "")
			newCookie=addCrumb(newCookie,thisCrumb);
		}
	return(newCookie);
	}
// This function deletes the cookie from disk... restarts new.
function deleteCookie (name,path,domain)
	{
	if (document.cookie)
		{
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	}
// This function creates a named crumb that you can add chips to.
function setupCrumb(x,CrumbName)
	{
	if (CrumbName.length != crumbNlen)
		alert("AGWEB Software error: CrumbName length is: " + CrumbName.length +
			" and it should be " + crumbNlen);
	x = crumbHead + CrumbName;
	return x;
	}

	// This function adds a crumb to the cookie.
function addCrumb(acookie,myCrumb)
	{
	var clen = 0;
	var slen = 0;
	clen = myCrumb.length + crmblensz;
	slen = ("" + clen).toString();
	while (slen.length < crmblensz)
	   slen = "0"+slen;
	acookie = acookie + slen + myCrumb;
	return(acookie);
	}
// pull out a named crumb from a cookie.
function getCrumb(acookie,crumbName)
	{
	var crumbjar  = "";
	var tcrumbjar = "";
	var crumblen  = 0;
	var curtoken  = "";
	var thisCrumbName = "";
	var toekn = "";
	var hlen = cookieHead.length + 4;
	crumbjar = acookie.substring(hlen,acookie.length);
	while(crumbjar.length > 0 )
		{
		crumblen = parseInt(StripLeadZero(crumbjar.substring(0,3)));
		if (isNaN(crumblen) || crumblen < 3) break;
		toekn    = crumbjar.substring(3,crumblen);
		thisCrumbName = toekn.substring(crumbHead.length,crumbHead.length+crumbNlen);
		if (thisCrumbName == crumbName)
			return toekn;
		crumbjar = crumbjar.substring(crumblen,crumbjar.length);
		}
	return "";
	}
// This function puts a chip into a crumb.
function addChip(acrumb,mychipname,mychipval)
	{
	i = acrumb.lastIndexOf(crumbSep + mychipname + chipSep);
	if (i > 0)
		alert("AGWeb Error: chip name " + mychipname + " already exists!");
	acrumb = acrumb + crumbSep + mychipname + chipSep + mychipval;
	return acrumb;
	}
// This function pulls out a chip from a crumb.
function getChip(acrumb,mychipname)
	{
	var i;
	var j;
	var s = crumbSep + mychipname + chipSep;
	i = acrumb.lastIndexOf(s);
	if (i > -1)
		{
		i += s.length;  // skip over chip name and separator
		j = acrumb.indexOf(crumbSep,i);  // locate end of chip value
		if (j < 0)  j = acrumb.length;
		return (acrumb.substring(i,j));
		}
	return "";  // return empty string if chip name not found
	}

// Get preferences from a transient cookie
function getPrefs() {
	var tCookie = "";
	var tCrumb = "";
	var tValue = "";
	tCookie = retrieveCookie();
	if (tCookie.length > 0) {
		tCrumb = getCrumb(tCookie,"GLOBAL");
		if (tCrumb.length > 0)
			tValue = getChip(tCrumb,"mapsize");
	}
	if (tValue == "") tValue = "zsc";
	return tValue;
}

// Save preferences in a transient cookie
function savePrefs() {
	var tCookie = "";
	var tCrumb = "";
	var tChip  = "";
	tCrumb  = setupCrumb(tCrumb,"GLOBAL");
	tCrumb = addChip(tCrumb,"mapsize",document.forms["submit"].mapsize.value);
	// write or update the transient cookie
	tCookie = retrieveCookie();
	tCookie = saveCrumbs(tCookie,tCrumb,"GLOBAL");
	storeCookie("EPA",tCookie,null,"/",null,false);
}

//--------------END OF COOKIE CODE --------------------------
