//
// PersistentForm class
// ====================
//
//   (C) 2000 Mark van 't Zet (mark@base.nl)
//   NOTE: You may use this code only if you:
//      - do not change it
//      - include these remarks
//
// WHAT IT IS
// ==========
//
//   A class that provides functionality for 
//   - storing a user's form input in a cookie
//   - restoring form fields from a saved cookie
//
// HOW YOU USE IT
// ==============
//
// - In script in the HEAD-section of HTML write:
//
//   var myPF = new PersistentForm('prefix_');
//
// - In the HTML body-tag write:
//
//   <body (..) onLoad="myPF.restoreForm('myFormName')">
//
// - In the form tag write:
//
//   <form (..) name="myFormName" onSubmit="myPF.storeForm('myFormName')">
//
// REMARKS:
// ========
//
// - Cookie script is not included. Include "cookies.js", like so:
//   <script type="text/javascript" src="http://www.base.nl/javascript/cookies.js">
//   separately in the head of your page.
//
// - The prefix-string in the constructor is optional (default prefix is empty string)
//   It is advisable to provide a prefix particular to that site/page, so the risk
//   of confusing cookies/forms is minimized.
//
// - Select type="multiple" is not (yet) supported
//

function PersistentForm() {
	this.cookiePrefix = (arguments.length > 0 ? arguments[0] : "");	
	this.storeForm = PF_storeForm;
	this.restoreForm = PF_restoreForm;
	this.getElement = PF_getElement;
}

function PF_storeForm (formName) {
   var f = document.forms[formName];
   if (typeof(f) == "undefined")
      return;
   var cookieVal = "", addVal, e;
   for (var i = 1; i <= f.elements.length; i++) {
      addVal = "";
      e = f.elements[i-1];
      if (e.type == "text")
         addVal = "t|" + e.name + "|" + e.value;
      if (e.type == "checkbox")
         addVal = "c|" + e.name + "|" + e.checked;
      if (e.type == "select-one")
         addVal = "s|" + e.name + "|" + e.selectedIndex;
      if (e.type == "radio" && e.checked)
         addVal = "r|" + e.name + "|" + e.value;
      if (addVal != "")
         cookieVal += (cookieVal != "" ? "," : "") + addVal;
   }
   setCookie(this.cookiePrefix + formName, cookieVal);
}

function PF_restoreForm(formName) {
   var f = document.forms[formName];
   if (typeof(f) == "undefined")
      return;
   var cookieVal = getCookie(this.cookiePrefix + formName);
   var eState, e, eType, eValue, nr = 1;
   if (cookieVal == null) {
      return;
   }
   eState = this.getElement(cookieVal, nr++);
   while (eState) {
      sepLoc = eState.indexOf("|", 2);
      if (sepLoc != -1) {
         eType = eState.substr(0,1);
         e = eval("f." + eState.substr(2, sepLoc-2));
         if (typeof(e) != "undefined") {
	        eValue = eState.substr(sepLoc + 1, eState.length);
            if (eType == "t") {
	           e.value = eValue;
            }
            if (eType == "c") {
               e.checked = (eValue == "true");
            }
            if (eType == "s") {
               e.selectedIndex = eValue;
            }
            if (eType == "r") {
               for (i = 0; i < e.length; i++) {
		          e[i].checked = (e[i].value == eValue);
               }
            }
         }
      }
      eState = this.getElement(cookieVal, nr++);
   }
}

function PF_getElement(s, nr) {
   var offset = 0;
   while ((nr > 1) && (offset != -1)) {
      offset = s.indexOf(",");
      if (offset != -1) {
         s = s.substr(offset + 1, s.length);
         nr--;
      }
   }
   if (nr > 1) {
      return "";
   }
   offset = s.indexOf(",");
   if (offset == -1) {
      return s;
   } else {
      return s.substr(0, offset);
   }
}

