// JavaScript Document
<!-- Copyright 2005 Bontrager Connection, LLC
// For more information, see the "Restoring Form Field Values" 
//    article, first published in Possibilities ezine described 
//    at http://willmaster.com/possibilities/

// The form's <form...> tag needs to contain this attribute:
//        onsubmit="javascript:return RecordFormInformation();"

// Put this JavaScript somewhere below the form.

// Two variables in this JavaScript can be customized.

// Customizable variable 1:
// Between the quotation marks, specify the form name:
// var FormName = "form1";

// Customizable variable 2:
// Between the quotation marks, specify the cookie name:
var CookieName = "FormRestoreCookie";


////////////////////////////////////////////////
//                                            //
//  No other customizations need to be done.  //
//                                            //
////////////////////////////////////////////////

function GetCookie() {
if(document.cookie.length < 1) { return ''; }
var cookiename = CookieName + '=';
var cookiebegin = document.cookie.indexOf(cookiename);
cookiebegin += cookiename.length;
var cookieend = document.cookie.indexOf(";",cookiebegin);
if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
return document.cookie.substring(cookiebegin,cookieend);
} // function GetCookie()


function RestoreFormInformation() {
var CookieString = GetCookie();
if(CookieString.length < 3) { return; }
CookieString = unescape(CookieString);
var CookiePieces = CookieString.split('\t\t');
for(var i = 0; i < CookiePieces.length; i++) {
   var pieces = CookiePieces[i].split('\t');
   if(pieces.length < 3) { continue; }
   if(pieces[1] == -1) {
      if( (pieces[2].indexOf('radio') > -1) || (pieces[2].indexOf('checkbox') > -1) ) {
         eval('document.' + FormName + '.' + pieces[0] + '.checked = true;');
         }
      else {
         if(pieces[2].indexOf('select') > -1) { eval('document.' + FormName + '.' + pieces[0] + '.selected = true;'); }
         else {
            var ta = pieces[3].split('"');
            pieces[3] = ta.join('\\"');
            var ta = pieces[3].split('\n');
            pieces[3] = ta.join('\\n');
            eval('document.' + FormName + '.' + pieces[0] + '.value = "' + pieces[3] + '";');
            }
         }
      continue;
      } 
   if( (pieces[2].indexOf('radio') > -1) || (pieces[2].indexOf('checkbox') > -1) ) { eval('document.' + FormName + '.' + pieces[0] + '[' + pieces[1] + '].checked = true;'); }
   else {
      if(pieces[2].indexOf('select') > -1) { eval('document.' + FormName + '.' + pieces[0] + '[' + pieces[1] + '].selected = true;'); }
      else {
         var ta = pieces[3].split('"');
         pieces[3] = ta.join('\\"');
         var ta = pieces[3].split('\n');
         pieces[3] = ta.join('\\n');
         eval('document.' + FormName + '.' + pieces[0] + '[' + pieces[1] + '].value = "' + pieces[3] + '";');
         }
      }
   }
} // function RestoreFormInformation()


function RecordFormInformation() {
var l = new Number();
eval('l = document.' + FormName + '.elements.length')
var NameList = new Array();
for (var i = 0; i < l; i++) { 
   var FORMname = new String();
   var FORMfields = new Number();
   eval('FORMname = document.' + FormName + '.elements[i].name;');
   if(FORMname.length < 1) { continue; }
   eval('FORMfields = document.' + FormName + '.' + FORMname + '.length');
   if(parseInt(FORMfields) > 1) { FORMfields = parseInt(FORMfields); }
   else { FORMfields = 1; }
   NameList.push(FORMname + '\t' + FORMfields);
   }
var FORMfieldNameNumber = new Number();
var FORMvalue = new String();
var FORMtype = new String();
var Done = new String();
var CookiePieces = new Array();
for (var i = 0; i < NameList.length; i++) { 
   var FORM_NL = NameList[i].split('\t');
   if(Done.indexOf(' ' + FORM_NL[0] + ' ') > -1) { continue; }
   Done += ' ' + FORM_NL[0] + ' ';
   if(FORM_NL[1] == 1) {
      FORMvalue = '';
      FORMfieldNameNumber = -1;
      eval('FORMtype = document.' + FormName + '.' + FORM_NL[0] + '.type;');
      if( (FORMtype.indexOf('radio') > -1) || (FORMtype.indexOf('checkbox') > -1) ) {
         var checked = false;
         eval('checked = document.' + FormName + '.' + FORM_NL[0] + '.checked;');
         if(checked == true) { FORMvalue = 'C'; }
         }
      else {
         if(FORMtype.indexOf('select') > -1) {
            var checked = false;
            eval('checked = document.' + FormName + '.' + FORM_NL[0] + '.selected;');
            if(checked == true) { FORMvalue = 'S'; }
            }
         else { eval('FORMvalue = document.' + FormName + '.' + FORM_NL[0] + '.value;'); }
         }
      if(FORMvalue.length < 1) { continue; }
      CookiePieces.push(FORM_NL[0] + '\t' + FORMfieldNameNumber + '\t' + FORMtype + '\t' + FORMvalue);
      continue;
      } 
   for(var ii = 0; ii < FORM_NL[1]; ii++) {
      FORMvalue = '';
      FORMfieldNameNumber = ii;
      eval('FORMtype = document.' + FormName + '.' + FORM_NL[0] + '[' + ii + '].type;');
      if(FORMtype == undefined) { eval('FORMtype = document.' + FormName + '.' + FORM_NL[0] + '.type;'); }
      if( (FORMtype.indexOf('radio') > -1) || (FORMtype.indexOf('checkbox') > -1) ) {
         var checked = false;
         eval('checked = document.' + FormName + '.' + FORM_NL[0] + '[' + ii + '].checked;');
         if(checked == true) { FORMvalue = 'C'; }
         }
      else {
         if(FORMtype.indexOf('select') > -1) {
            var checked = false;
            eval('checked = document.' + FormName + '.' + FORM_NL[0] + '[' + ii + '].selected;');
            if(checked == true) { FORMvalue = 'S'; }
            }
         else { eval('FORMvalue = document.' + FormName + '.' + FORM_NL[0] + '[' + ii + '].value;'); }
         }
      if(FORMvalue.length < 1) { continue; }
      CookiePieces.push(FORM_NL[0] + '\t' + FORMfieldNameNumber + '\t' + FORMtype + '\t' + FORMvalue);
      } 
   }
var CookieString = new String();
CookieString = CookiePieces.join('\t\t');
CookieString = escape(CookieString);
document.cookie = CookieName + "=" + CookieString;
return true;
} // function RecordFormInformation()

RestoreFormInformation(); // Calls the main function
