// JavaScript Document for Form Data Storage via Cookies
//Written by Ken Fackler
//Copyright 2006, ALL RIGHTS RESERVED

/*

ASSUMPTIONS:

There is only one form in the document.
Stores only these element types: text, textarea, select lists, grouped radio buttons, and checkboxes.
All elements to be stored have a unique NAME attribute.
All radio buttons are grouped and have a unique VALUE attribute.

REQUIREMENTS:

Run the restoreIt function using onload in the body tag.

Run the storeIt function when the form is submitted.
	Option 1: Use an onsubmit event in the form tag.
	Option 2: Add an onclick statement to the submit button.


*/

//initialize global variables
var sep = "/*_^_*/";	//an unlikely string to be typed
var content;			//receives data from cookie

//determines if the first string contains the second
//returns -1 if not, a positive integer otherwise
function contains(inputString, checkString, startingIndex) {

	if (!startingIndex) {
		startingIndex = 0;
	}

	return inputString.indexOf(checkString);

}

//debugging tool
function s(obj) {
	alert(obj);
}

//collect form data and create cookie
//args: form reference, cookie name, integer days until cookie expires,
//path, domain, secure

function storeIt(theForm,theCookie,expires,path,domain,secure) {
	//For each element in the form of the following types:
	//text,textarea,select-one,select-multiple,radio,checkbox
	//parse its content or state into a string, and
	//store the string in a cookie.

	//temp variable to hold data before writing cookie
	var msg = "";

	//loop thru each element in the form
	for (i=0; i<theForm.length; i++) {

		//branch based on type of element
		switch (theForm[i].type) {

			//a single line text element; append the VALUE attribute to variable MSG
			case "text" : msg += (theForm[i].name + "=" + theForm[i].value + sep);
			break;

			//a multi-line text element; append the VALUE attribute to variable MSG
			case "textarea" : msg += (theForm[i].name + "=" + theForm[i].value + sep);
			break;

			//a drop-down list, single select; append the selected element's index number
			//to variable MSG
			case "select-one" :

				if (theForm[i].name == "airports" || theForm[i].name == "plan" || theForm[i].name == "killer") {

					//do nothing

				}

				else {

					msg += (theForm[i].name + "=");

					for (j=0; j<theForm[i].options.length-1; j++) {

						msg += (theForm[i].options[j].text + "@@");

					}

					//msg += (theForm[i].options[theForm[i].options.length-1].text + sep);
					msg += theForm[i].options[theForm[i].options.length-1].text;
					msg += sep;

				}

			break;

			//a drop-down list, multi select; store an array of true/false entries
			//indicating which options are selected
			case "select-multiple" :

				//skip if this element already stored
				if ( contains(msg,theForm[i].name) >= 0) {
					break;
				}

				//create a reference to the current element
				var currSelect = eval ("theForm." + theForm[i].name);

				//temp array variable to hold the true/false values
				var selectValues = new Array(currSelect.length);

				//loop thru the options in the element
				for (j=0; j<currSelect.length; j++) {

					//set true/false entries
					selectValues[j] = currSelect[j].selected;

				}

				//append temp array to variable MSG
				msg += (theForm[i].name + "=" + selectValues + sep);

			break;

			//radio buttons; store the VALUE attribute of the one that's checked
			case "radio" :

				//skip if this element already stored
				if ( contains(msg,theForm[i].name) >= 0) {
					break;
				}

				//creat a reference to the current element
				var currRadio = eval ("theForm." + theForm[i].name);

				//loop thru the radio buttons
				for (j=0; j<currRadio.length; j++) {

					//if this radio button is checked
					if (currRadio[j].checked) {

						//append its VALUE attribute to variable MSG
						msg += (theForm[i].name + "=" + currRadio[j].value + sep);

					}

				}

			break;

			//checkboxes; store the checked/not checked state of each
			case "checkbox" : msg += (theForm[i].name + "=" + theForm[i].checked + sep);
			break;

			default : //statements

		}

	}

	//setCookie args: name, value, expiredays, path, domain, secure
	setCookie(theCookie, msg, expires, path, domain, secure);

}

//retrieve cookie content and parse into form
//args: form reference, cookie name
function restoreIt(theForm,theCookie) {

	//if the cookie doesn't exist, quit
	//getCookie args: name of cookie
	if (getCookie(theCookie) == null) {
		return;
	}

	//retrieve cookie content into variable CONTENT
	content = getCookie(theCookie);

	//For each element in the form of the following types:
	//text,textarea,select-one,select-multiple,radio,checkbox
	//retrieve the value into the form.

	//loop thru the form elements
	for (i=0; i<theForm.length; i++) {

		//branch based on type of element
		switch (theForm[i].type) {

			//text element; retrieve the VALUE attribute
			case "text" : theForm[i].value = revealValue(theForm[i].name);
			break;

			//textarea element; retrieve the VALUE attribute
			case "textarea" : theForm[i].value = revealValue(theForm[i].name);
			break;

			//drop-down list, single select; set the selected item
			case "select-one" :

				if (theForm[i].name == "airports") {

					//do nothing

				}

				else {

					theForm[i].options.length = 0;
					var restoreRoute = revealValue(theForm[i].name).split("@@");

					for (j=0; j<restoreRoute.length; j++) {

						theForm[i].options.length++;
						theForm[i].options[j].text = restoreRoute[j];

					}

					restoreRoute = null;

				}

			break;

			//drop-down list, multi select; retrieve array of true/false values
			//and set each option state accordingly
			case "select-multiple" :

				//retrieve the string of values, store in variable CURRSELECT,
				//and then convert it to an array
				var currSelect = revealValue(theForm[i].name);
				currSelect = currSelect.split(",");

				//loop thru the options
				for (j=0; j<currSelect.length; j++) {

					//if the option should be selected
					if (currSelect[j] == "true") {

						//turn on the highlight
						theForm[i].options[j].selected = true;

					}

				}

			break;

			//radio buttons; retrieve the VALUE attribute
			case "radio" :

				//create a reference to the current radio button
				var currRadio = revealValue(theForm[i].name);

				//if the current radio button has the same value as
				//the one stored, it must have been checked before,
				//so check it now, otherwise turn it off
				if (currRadio == theForm[i].value) {

					theForm[i].checked = true;
					var storeDistance = theForm[i].value;

				}

				else {

					theForm[i].checked = false;

				}

			break;

			//checkboxes; retrieve the state
			case "checkbox" :

				//if this box was marked as true, check it
				if ( revealValue(theForm[i].name) == "true" ) {

					theForm[i].checked = true;

					switch (theForm[i].name) {

						case "friendly" : ultraFlag = 1;
						break;

						case "eats" : foodFlag = 1;
						break;

					}

				}

				else if ( revealValue(theForm[i].name) == "false" ) {

					theForm[i].checked = false;

					switch (theForm[i].name) {

						case "friendly" : ultraFlag = 0;
						break;

						case "eats" : foodFlag = 0;
						break;

					}

				}

			break;

			default : //

		}

	}

	//Restructure airport list
	showNear(parseFloat(storeDistance),theForm);

}

//parses and returns the values of elements from the string variable CONTENT,
//which was initialized with the data from the cookie
//CONTENT is a global variable defined above

function revealValue(name) {

	//if the element name doesn't exist inside variable CONTTENT,
	//i.e., it wasn't stored in the cookie, return the variable VALUE
	//containing nothing
	if ( contains(content,name,0) < 0 ) {

		return value = "";

	}
	
	var elementStartPos = content.indexOf(name);
	var elementEndPos = content.indexOf("=",elementStartPos);

	var valueStartPos = content.indexOf("=",elementStartPos) + 1;
	var valueEndPos = content.indexOf(sep,valueStartPos);

	var value = content.substring(valueStartPos,valueEndPos);

	return value;

}

