// See the "Multi-Page Forms with JavaScript and Cookies" 
//    article for more information. The article was first 
//    published in Possibilities ezine described at 
//    /library/

// The form's <form...> tag needs to contain this attribute:
//        onsubmit="GoToURL('http://example.com/page2.html');"
//    Replace the URL between the single quotes with the URL 
//    of the next form page.

// Put this JavaScript somewhere above the form.

// Two variables in this JavaScript can be customized.

// Customizable variable 1:
// Between the quotation marks, specify the form name:
var FormName = "MyFormAGT";

// Customizable variable 2:
// Between the quotation marks, specify the cookie name:
var CookieName = "MultiPageFormCookieAGT";


////////////////////////////////////////////////
//                                            //
//  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 InsertPreviousInformation() {
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 < 2) { continue; }
   document.write('<input type="text" name="'+pieces[0]+'" value="'+pieces[1]+'">');
   }
} // function InsertPreviousInformation()


function GoToURL(nextpage) {
var l = new Number();
eval('l = document.' + FormName + '.elements.length')
var NameList = new Array();
for (var i = 0; i < l; i++) { 
   var FIELDname = new String();
   var FORMfields = new Number();
   eval('FIELDname = document.' + FormName + '.elements[i].name;');
   if(FIELDname.length < 1) { continue; }
   eval('FORMfields = document.' + FormName + '.' + FIELDname + '.length');
   if(parseInt(FORMfields) > 1) { FORMfields = parseInt(FORMfields); }
   else { FORMfields = 1; }
   NameList.push(FIELDname + '\t' + FORMfields);
   }
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 = '';
      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) { eval('FORMvalue = document.' + FormName + '.' + FORM_NL[0] + '.value;'); }
         }
      else {
         if(FORMtype.indexOf('select') > -1) {
            var checked = false;
            eval('checked = document.' + FormName + '.' + FORM_NL[0] + '.selected;');
            if(checked == true) { eval('FORMvalue = document.' + FormName + '.' + FORM_NL[0] + '.value;'); }
            }
         else { eval('FORMvalue = document.' + FormName + '.' + FORM_NL[0] + '.value;'); }
         }
      if(FORMvalue.length < 1) { continue; }
      CookiePieces.push(FORM_NL[0] + '\t' + FORMvalue);
      continue;
      } 
   for(var ii = 0; ii < FORM_NL[1]; ii++) {
      FORMvalue = '';
      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) { eval('FORMvalue = document.' + FormName + '.' + FORM_NL[0] + '[' + ii + '].value;'); }
         }
      else {
         if(FORMtype.indexOf('select') > -1) {
            var checked = false;
            eval('checked = document.' + FormName + '.' + FORM_NL[0] + '[' + ii + '].selected;');
            if(checked == true) { eval('FORMvalue = document.' + FormName + '.' + FORM_NL[0] + '[' + ii + '].value;'); }
            }
         else { eval('FORMvalue = document.' + FormName + '.' + FORM_NL[0] + '[' + ii + '].value;'); }
         }
      if(FORMvalue.length < 1) { continue; }
      CookiePieces.push(FORM_NL[0] + '\t' + FORMvalue);
      } 
   }
var CookieString = new String();
CookieString = CookiePieces.join('\t\t');
CookieString = escape(CookieString);
document.cookie = CookieName + "=" + CookieString;
alert("Data Saved - Directing You To The Next Page");
location.href = nextpage;
return false;
}
