
//Cookie facility
var docCookie = document.cookie // Make easier for user by saving info for auto-entry later
docCookie = equalIn(nameOut(docCookie))
var numFields = 25	// Init array
var numYrs = 2	// Persistence
var  = new createArray(numFields)
if (docCookie != null && docCookie != '') { // get cookie, if exists
	arraySubParms(docCookie)
}

function initField(theForm,fieldName) {
//alert("Form is " + theForm.name);
	theForm.elements[fieldName].value=((""+parms[fieldName]) >= "" && parms[fieldName] != "undefined" ? (""+parms[fieldName]) : "")
}

function createArray(n) {	// Create array for form fields
	this.length = n
	for (i=1 ; i <= numFields; i++) {
		this[i] = "@";
		this["@"] = "";
		this[i] = "";
	}
	return this
}

function recordTypeSelection(theForm,mySelection) {
	theForm.typeOrg.value=mySelection.options[mySelection.selectedIndex].value;
	theForm.typeNum.value=""+mySelection.selectedIndex;
}

function recordPositionSelection(theForm,mySelection) {
	theForm.positionID.value=mySelection.options[mySelection.selectedIndex].value;
	theForm.positionNum.value=""+mySelection.selectedIndex;
}

// Record values
function rememberValues(theForm) {
		document.data=theForm;	// Data to save
		toCookie();	// Save data
}

// Persistant data routines
// Escapes any semicolons in data
function fixSep(what) {
	n=0
	while ( n >= 0 ) {
		n = (""+what).indexOf(';',n);
		if (n < 0) {
			return (what);
		}
		else {
			what = what.substring(0,n) + escape(';') + what.substring(n+1,what.length);
			n++;
		}
	}
	return what
}

// Substitutes + for space in data
function fixSpa(what) {
	n=0
	while ( n >= 0 ) {
		n = what.indexOf(' ',n)
		if (n < 0) return what	
		else {
			what = what.substring(0,n) + '+' + what.substring(n+1,what.length)
			n++
		}
	}
	return what
}

// Remove NAME= from cookie
function nameOut(what) {
	if (what.length>0) {
		n = what.indexOf('=',0)
		if (n < 0) return what
		else {
			what = what.substring(n+1,what.length)
			n++
		}
	}
	return what
}

// Substitutes ~ for = in data
function equalOut(what) {
	n=0
	while ( n >= 0 ) {
		n = parseInt(what.indexOf('=',n))
		if (n < 0) return what	
		else {
			what = what.substring(0,n) + '~' + what.substring(n+1,what.length)
			n++
		}		
	}
	return what
}

// Substitutes = for ~ in data
function equalIn(what) {
	n=0
	while ( n >= 0 ) {
		if (what.length>0) {
			n = what.indexOf('~',n)
		} else {
		return what
		}
		if (n < 0) return what	
		else {
			what = what.substring(0,n) + '=' + what.substring(n+1,what.length)
			n++
		}		
	}
	return what
}

function toCookie() {
	document.cookie = '';
	var newCookie = '';
	var nform = document.data;
	for (i=0 ; i<nform.length; i++) {
		astr = ""+fixSep(nform.elements[i].value);
		//astr = nform.elements[i].value 
		astr = ((""+nform.elements[i].name) > "" ? (""+nform.elements[i].name) + '=' + astr + '&' : "");
		newCookie=newCookie + astr;
	}
	expr = makeYearExpDate(numYrs)
	newCookie="NAME="+equalOut(fixSpa(newCookie.substring(0,(newCookie.length-1)))) + ';expires=' + expr + ';path=/';
	document.cookie=newCookie;
}

function makeYearExpDate(yr) {
	var expire = new Date ();
	expire.setTime (expire.getTime() + ((yr *365) *24 * 60 * 60 * 1000));
	expire = expire.toGMTString()
	return expire
}

function getCookieAt(n) {
	var e = document.cookie.indexOf (";", n);
	if (e == -1) e = document.cookie.length;
	rstr = unescape(document.cookie.substring(n,e));
	return rstr;
}

// fills global array from cookie info
function arraySubParms(astr) {
	var k = astr.length;
	var astr = astr.substring(1,k);
	var lstr = '';
	var rstr = '';
	var bstr = '';
	var crstr = '';
	var counter = 1;
	for (i = 0 ; i <= k ; i++) {
		ccStr ='';
		ccStr = astr.substring(i,i+1);
		if (ccStr == '+') ccStr = ' ';
		if (ccStr == '%') {
			var xx = astr.substring(i,i+3);
			ccStr = unescape(xx);	
			i += 2;
		}
		if (ccStr == '=') {
				parms[counter] = bstr;
				bstr = '';
				continue;
		}
		//right hand member of pair
		if (ccStr == '&') {
			parms[parms[counter]] = bstr;
			if (parms[parms[counter]] == ";") parms[parms[counter]]="";
			counter++
			bstr = '';
			ccStr='';
			continue;
		}
		bstr += ccStr;
	}
}
//End of cookie facility
