function WriteCookie(name, value) {
  var hours = "";
	var path = "";
	var domain = "";
  document.cookie = name + '=' + escape(value) + ((hours)?(';expires=' + ((new Date((new Date()).getTime() + parseInt(hours)*3600000)).toGMTString())):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'');
}
function ReadCookie(name) {
  if (document.cookie == '') { // there's no cookie, so go no further
		return ""; 
  } else { // there is a cookie
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
		var NN2Hack = firstChar + name.length;
		if ((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
			firstChar += name.length + 1; // skip 'name' and '='
			lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
			if(lastChar == -1) lastChar = theBigCookie.length;
			return unescape(theBigCookie.substring(firstChar, lastChar));
		} else { // If there was no cookie of that name, return empty string.
			 return "";
		}
  }	
}
function GetFormData(sFormName) {
  // Returns a list of key pair values from the given form in sFormName.
  var sValue, sName, sType;
  var sData = "";
  var iElements = document.forms[sFormName].elements.length;
  for (var i = 0; i < iElements; i++) {
    sName = document.forms[sFormName].elements[i].name;
		sType = document.forms[sFormName].elements[i].type;
		if (sType == "checkbox") {
			if (document.forms[sFormName].elements[i].checked) {
				sValue = "true";
			} else {
				sValue = "false";
			}
		} else if (sType == "radio") {
	  	if (document.forms[sFormName].elements[i].checked) {
				sValue = document.forms[sFormName].elements[i].value;
	  	} else {
				sValue = "";
	  	}
		} else {
	  	sValue = document.forms[sFormName].elements[i].value;
		}
		if (((sType == "radio") && (sValue == "")) == false) {
	  	sData = sData + sName + "=" + escape(sValue) + "; ";
		}
  }
  return sData;
}
function SetFormData(sFormName, sData) {
	// Fills out the form in sFormName with the key pair values in 
	// sData.
  var sValue, sName, sType;
  var iElements = document.forms[sFormName].elements.length;
  for (var i = 0; i < iElements; i++) {
    sName = document.forms[sFormName].elements[i].name;
		sType = document.forms[sFormName].elements[i].type;
		sValue = DelLeftMost(sData, sName + "=");
		sValue = unescape(GetLeftMost(sValue, "; "));
		if (sType == "checkbox") {
	  	if (sValue == "true") {
				document.forms[sFormName].elements[i].checked = 1;
	  	} else {
				document.forms[sFormName].elements[i].checked = 0;
	  	}
		} else if (sType == "radio") {
		  if (sValue == document.forms[sFormName].elements[i].value) {
				document.forms[sFormName].elements[i].checked = 1;
			}
		} else {
	  	document.forms[sFormName].elements[i].value = sValue
		}
  }
}
function SaveFormToCookie(sFormName) {
	// Save the form in sFormName to a cookie with the same name.
	var sData = GetFormData(sFormName);
	WriteCookie(sFormName, sData);
}
function LoadFormFromCookie(sFormName) {
	// Fills out the form in sFormName with the key pair values of 
	// data found in a cookie with the same name.
	var sData = ReadCookie(sFormName);
	SetFormData(sFormName, sData);
}
