<!-- 
// =============================================================================================
// STORE And RECALL Javascript Functions 
// by Dale Anderson (http://www.daleanderson.ca/) July 2002
// =============================================================================================
//
// PURPOSE
// =======
//      This script is to enable html inputs (checkboxes, textboxes, radio 
//      buttons, and single-line dropdown lists) to remember their previous 
//      value or state, either from page to page within a website, or between 
//      visits to a page on a website.
//
// FEATURES
// ========    
//      Handles most common input types, saves your users entire MINUTES! Of frustrating
//      repetitive form input.
// 
//      Input Types Handled: 
//          - Single Line Text Inputs
//          - Single-Line SELECT lists
//          - Radio Buttons
//          - Checkboxes
//  
//      Input types not handled: 
//          - Textareas...................  Values can be remembered and restored 
//                                          as far as the input value is concerned, 
//                                          but the value text does not show up in 
//                                          the document upon restoration.
//          - Multi-Line SELECT lists....   I have not tested these, nor do I ever use
//                                          them. Who knows what will happen!
//          - Passwords..................   For obvious reasons. 
//          - Submit / reset Buttons.....   These don't change. There's no sense 
//                                          in remembering values for them.
//  
//  
//  HOWTO (2 steps, plus one that takes care of itself)
//  ===================================================
//      Step 1: Include the Javascript Code
//      -----------------------------------
//      Paste the following line into the HEAD section of your HTML document, and replace the 
//      *** PATH TO FILE *** with the virtual path to where this file lives on your server:
//
//          <script language="JavaScript" src="/*** PATH TO FILE ***/SmartFormInputs.js"></script>
//
//
//
//      Step 2: Remembering Values
//      --------------------------
//      For any input you want to be remembered from page visit to page visit, simply 
//      include the javascript handler: 
//          
//          onChange="store(this)"

//      inside the input tag.
//      Works for single-line SELECT inputs, single-line text inputs, checkboxes, and
//      radio buttons. (see above notes).
//
//          
//
//      Step 3: Restoring Values Upon Page Load
//      ---------------------------------------
//      Nothing to do! 
//      Happens AUTOMATICALLY! as long as you've included this script in the page.
//          
//          
//
//
//
//  EXAMPLES
//  ========
//      You can copy & paste any of the following into a page that has this script 
//      included, and they would all work.
//
//      Examples:
//              
//          <input type="text" name="txt1" value="jijpioje" onChange="store(this)">
//          <input type="radio" name="radio1" value="a" checked onChange="store(this)"> 
//          <input type="radio" name="radio1" value="b" onChange="store(this)"> 
//          <input type="radio" name="radio1" value="c" onChange="store(this)"> 
//          <input type="checkbox" name="chk1" value="true" onChange="store(this)"> 
//          <input type="checkbox" name="chk2" value="true" checked onChange="store(this)"> 
//          <input type="hidden" name="hidden1" value="boo!" onChange="store(this)">
//           <select name="select1" onChange="store(this)"> 
//             <option value="a">a</option>
//             <option value="b">b</option>
//             <option value="c">c</option>
//             <option value="x">x</option>
//             <option value="y">y</option>
//             <option value="z">z</option>
//           </select>
//
//
//          
//          
//  WATCH OUT!  
//  ==========
//      Your HTML document should have UNIQUE NAMES FOR EACH INPUT even if they 
//      are within different forms on a given page. Otherwise, this script will 
//      get confused, and there is no telling what nasty errors will pop up.
//          
//  
//
//
// 




// --------------------------------------------------------------------------------------


var sar_expDays = 30;  // set this value to however many days you want your cookies to last


// --------------------------------------------------------------------------------------
// Internal Functions for handling cookies
function sar_setCookie(name, val)
{
  var exp = new Date()
  var cookieTimeToLive = exp.getTime() + (sar_expDays * 24 * 60 * 60 * 1000)
  exp.setTime(cookieTimeToLive)
  document.cookie = name + "=" + escape(val) + "; expires=" + exp.toGMTString()
}
function sar_getCookie(name)
{
  var cookieNameLen = name.length
  var cLen = document.cookie.length
  var i = 0
  var cEnd
  var myStringToReturn
  var myStringToReturnLen
  while (i < cLen) 
  {
    var j = i + cookieNameLen
    if (document.cookie.substring(i,j) == name)
    {
      cEnd = document.cookie.indexOf(";",j)
      if (cEnd == -1)
      {
        cEnd = document.cookie.length
      }
      myStringToReturn = unescape(document.cookie.substring(j,cEnd))
      myStringToReturnLen = myStringToReturn.length
      myStringToReturn = myStringToReturn.substring(1,myStringToReturnLen+1)
      return myStringToReturn
    }
    i++
  }
  return ""
}


// --------------------------------------------------------------------------------------
// Save the form input values to perstent memory (i.e. the cookie)
//
function store(objFormInput)
{
    var inputName = "", inputVal = "";
    var f = 0, e = 0;
    var formInputName = "";
    if (objFormInput.length)
    {
        sar_setCookie(objFormInput.name, objFormInput.options[objFormInput.selectedIndex].value);
    }
    else 
    {
        if (objFormInput.type=="checkbox")
        {
            if (objFormInput.checked == true)
            {
                sar_setCookie(objFormInput.name, "checked");
            } 
            else
            {
                sar_setCookie(objFormInput.name, "");
            }
        } 
        else if (objFormInput.type=="password")
        {
            // do not store values for passwords
        }
        else if (objFormInput.type=="textarea")
        {
            // do not store values for textarea inputs. it doesn't work that well on the other end.
        }
        else 
        {
            // Implied: type = "text", "textarea", "hidden", "file", or "radio"
            // in any case, we can handle as a text input.
            sar_setCookie(objFormInput.name, objFormInput.value);
        }
    }
}



// --------------------------------------------------------------------------------------
// Load the input values back into the page upon load.
// 
function sar_recall() {
  var strCookieName, strCookieVal;
  var iFormsCount = 0;
  var iElementsCount = 0;
  var formInputName, formInputVal;
  var objFormInput;
  var i, j;
  var key, value, type, len, formName, chkd;
  // loop thru each form in the page
  for(iFormsCount=0;iFormsCount < document.forms.length;iFormsCount++) {
    // loop thru each input in the form
    for(iElementsCount=0;iElementsCount < document.forms[iFormsCount].elements.length;iElementsCount++) {
      // retrieve the cookie value for a given form input.
      // Set the cookie name to look for... for each form input in the document
      formInputName = document.forms[iFormsCount].elements[iElementsCount].name;
      strCookieName = formInputName
      objFormInput = document.forms[iFormsCount].elements[iElementsCount]
      if (objFormInput.type!="hidden") {
        // Look in the document's cookie object and see if there is any value there for that corresponds to the form input name.
        strCookieVal = sar_getCookie(strCookieName);
        if (strCookieVal != null) {
          // AHA! There is a cookie value for this form input.
          // Now we want to find out what kind of form input we are dealing with
          if (objFormInput.length) {
            // This is a dropdown list
            for(i=0; i < objFormInput.length; i++) {
              selectVal = objFormInput.options[i].value
              if(strCookieVal == selectVal) {
                objFormInput.options[i].selected = true;
              }
            }
          } else {
            if (objFormInput.type=="radio") {
              if(objFormInput.value==strCookieVal) {
                objFormInput.checked = true;
              }
            } 
            if (objFormInput.type=="checkbox") {
              if(strCookieVal=="checked") {
                objFormInput.checked = true;
              }
            } 
            else if (objFormInput.type=="text" || objFormInput.type=="file" || objFormInput.type=="textarea") {
              // will be some sort of text input.
              objFormInput.value = strCookieVal
            }
          }
        }
      }
    }
  }
}






















// ====================================================================
// *SAFE* window.onload SCRIPT!
// Found At: http://javascript.about.com/library/scripts/blsafeonload.htm
// ====================================================================
//
// Call the following with your function name as the argument
// For example...
// what would normally be:
//
//       <body onLoad="foo();"> 
// 
// would become: 
//
// <scr1pt>
//      SafeAddOnload(foo);
// </scr1pt>
// 
// And you can put it pretty much Anywhere in the document!


// Browser Detection
var isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
var NS4 = (document.layers) ? true : false;
var IEmac = ((document.all)&&(isMac)) ? true : false;
var IE4plus = (document.all) ? true : false;
var IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
var IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
var ver4 = (NS4 || IE4plus) ? true : false;
var NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;

// Body onload utility (supports multiple onload functions)
var gSafeOnload = new Array();

function SafeAddOnload(f)
{
  if (IEmac && IE4)  // IE 4.5 blows out on testing window.onload
  {
    window.onload = SafeOnload;
    gSafeOnload[gSafeOnload.length] = f;
  }
  else if  (window.onload)
  {
    if (window.onload != SafeOnload)
    {
      gSafeOnload[0] = window.onload;
      window.onload = SafeOnload;
    }    
    gSafeOnload[gSafeOnload.length] = f;
  }
  else
    window.onload = f;
}
function SafeOnload()
{
  for (var i=0;i<gSafeOnload.length;i++)
    gSafeOnload[i]();
}













// --------------------------------------------------------------------------------------
// Set up the trigger for the Recall function to happen upon page load....
// 
// 


SafeAddOnload(sar_recall);

// -->
