/*
	Name:	saveform.js
	Created 21.02.2000 by Kåre Morstøl
	Changed 06.03.2000 -----"---------
		only saves values in text-fields.
		cookiename based on path, not document name, so several forms can use the same cookie.
	
	Functions for saving the values in a form into a cookie, 
	and loading them from the cookie into the form.
	Uses functions from the "cookie.js"-file. Remember to include it
	before this file.
	The name of the cookie is the name of the form + the string "values".
	The cookie will selfdestruct after a year of not being changed.
	Only works with form elements of type text, textarea, checkbox and radio.
	The form can contain elements of other types, but their values wont be saved.
	If the number of elements in the form is changed, it will be blank the next time
	it is loaded. But if only the order of recognized types in the form is changed, 
	some weird results can occur. If such changes are performed, a change in the name of
	the form as well could be a good idea. A fresh cookie is then created, since 
	its name is made up of the forms name.
*/


/*	The character used to separate the values in the form when saved to the cookie.
	If a user for some sadistic, evil reason desides to enter 
	this character into the form, the form will be blank the next time it is 
	loaded, because the number of form elements and the number of values in 
	the cookie won't match. */
var separator = "#";


//converts a string to boolean
function toBoolean(s) {
	if(s=="true") {
		return true;
		}
	else return false;
}

//gets cookies name, which is the domain+path+"_"+formname of the calling document
function getCookieName(form) {
	var name;
	if(document.location.search!="")
		name = document.location.href.substring(0,document.location.href.indexOf(document.location.search));
	else
		name = document.location.href;
	name = name.substring(0,name.lastIndexOf("/")+1);

	name += "_" + form.name;
//	alert(name);
	return name;
}



//saves the values of a form into a cookie
function saveForm(form) {
	//store the values of the form in a cookie
	var values="";
	var counter=0;
	for(var i=0;i<form.elements.length;i++) {
		switch(form.elements[i].type) {
			case "text":		values += form.elements[i].value + " " + separator;
								counter++;
								break;
/*			case "textarea":
			case "radio":
			case "checkbox":	values += form.elements[i].checked + " " + separator;
								break;
*/
			default:		break;
		}
	}
//	alert("saveform "+counter+" "+values);
	var expdate = new Date ();
	FixCookieDate (expdate); // Correct for Mac date bug
	expdate.setYear(expdate.getYear()+1);	//expires in one year
//	if(!(cookiename)) throw new exception("saveForm(form,cookiename): Cookiename not set");
//	SetCookie (getCookieName(form), values, expdate, (null), window.location.hostname);
	SetCookie (getCookieName(form), values, expdate);
//	SetCookie (form.name+"_values", values, expdate, window.location.pathname, window.location.hostname);
}
	
//loads values stored in a cookie into a form.
function loadForm(form) {
	var values, value;
	values = GetCookie(getCookieName(form));

	if(values != null) {
		var valuesArray = values.split(new RegExp(separator));
		var valuesIndex = 0;
//		if(form.elements.length!=valuesArray.length) return;	//number of elements in form has been changed since cookie was made
		for(var i=0;i<form.elements.length;i++) {
			switch(form.elements[i].type) {
/*				case "radio":	
				case "checkbox":	form.elements[i].checked = toBoolean(value);
									break;
				case "textarea":
*/
				case "text":		value = valuesArray[valuesIndex++];
									value = value.substring(0,value.length-1);
									form.elements[i].value = value;
									//alert("load text =>"+value);
									break;
			}
		}
	}
}