/******************************************************************************
*                                                                             *
* [formsavr.js]   Boedeker Plastics, Inc.   Scott Baer                        *
*                                                                             *
* Copyright 1999 by Mike Hall.                                                *
* Web address: http://www.brainjar.com                                        *
* Last update: February 5, 2000.                                              *
*                                                                             *
* Allows form data to be saved and restored using a single cookie. May be     *
* used by any form. Fields can optionally be excluded on an individual basis. *
*                                                                             *
* 15Feb01 - changed name from formsaver.js, implemented with "ORDER.HTM"      *
* 01Mar01 - added alerts after cookie info is retrieved or not                *
*                                                                             *
******************************************************************************/

// Determine if browser can run this script.

var capable;
var name = navigator.appName.toLowerCase();
var vers = parseFloat(navigator.appVersion);

if ((name.indexOf("netscape") >= 0 && vers >= 4) || (name.indexOf("microsoft") >= 0 && vers >= 4))
  capable = true;
else
  capable = false;

// Constants.

var FLDSEP;    // Special characters used as separators in cookie data string.
var IDXSEP;

if (capable) {
  FLDSEP = String.fromCharCode(1);
  IDXSEP = String.fromCharCode(2);
}

//----------------------------------------------------------------------------
// Stores data currently entered on a form as cookies. Always returns true.
//
//   name    - The form name.
//   days    - Number of days to keep the cookies.
//   exclude - A comma-delimited list of field names that should not be
//             stored. Use to exclude sensitive data such as password,
//             credit card numbers, etc.
//---------------------------------------------------------------------------- 

function storeData(name, days, exclude) {

  var f;
  var expdate;
  var list, include;
  var i;
  var fld;
  var data;

  if (!capable)
    return true;

  // Get the named form, skip processing if not found.

  if (!(f = document.forms[name]))
    return true;

  // Initialize the data field.

  // Set the expiration date.

  if (days == "")
    days = 0;
  expdate = new Date();
  expdate.setTime (expdate.getTime() + (86400 * 1000 * days));

  // Build a list of field names for exclusion.

  list = new Array();
  if (exclude != "") {
    i = 0;
    while ((i = exclude.indexOf(",")) >= 1) {
      list[list.length] = exclude.substr(0, i);
      exclude = exclude.substr(i + 1);
    }
    list[list.length] = exclude;
  }

  // Run through the fields and add field name/value pairs to the data string.

  data = FLDSEP;
  for (i = 0; i < f.length; i++) {
    fld = f.elements[i];

    // Is field in exclusion list?

    include = true;
    for (j = 0; j < list.length; j++)
      if (fld.name == list[j])
        include = false;

      if (include) {

      // Checkboxes and radio buttons.

      if ((fld.type == "checkbox" || fld.type == "radio") && fld.checked)
        data += setData(fld.name, fld.value);

      // Selection lists (single).

      if (fld.type == "select-one")
        if (fld.selectedIndex >= 0)
          data += setData(fld.name, fld.options[fld.selectedIndex].value);

      // Selection lists (multiple). Add a unique name/value pair for each selected item.

      if (fld.type == "select-multiple")
        for (j = 0; j < fld.options.length; j++)
          if (fld.options[j].selected)
            data += setData(fld.name + IDXSEP + j, fld.options[j].value);

      // Text fields.

      if (fld.type == "hidden" || fld.type == "password" || fld.type == "text" || fld.type == "textarea")
        data += setData(fld.name, fld.value);
    }
  }

  // Set the cookie.

  deleteCookie(name);
  setCookie(name, data, expdate);

  return true;
}

//----------------------------------------------------------------------------
// Retrieves data from the cookie and sets the values in the corresponding
// form fields. Returns true if data was found, false otherwise.
//
//   name - The form name.
//----------------------------------------------------------------------------

function retrieveData(name) {

  var f;
  var i, j;
  var fld;
  var s;
  var data;

  if (!capable) {
    alert('No Order Form Data Can Be Retrieved');
    return false;
    }
    
  // Get the named form, return if not found.

  if (!(f = document.forms[name])) {
    alert('No Order Form Data Can Be Retrieved');
    return false;
    }

  // Get the cookie for this form.

  data = getCookie(name);
  if (data == "") {
    alert('No Previous Order Form Data To Be Retrieved');
    return false;
    }

  // Run through the fields and retrieve the values.

  for (i = 0; i < f.elements.length; i++) {
    fld = f.elements[i];

    // Checkboxes and radio buttons.

    if ((fld.type == "checkbox" || fld.type == "radio") && (s = getData(fld.name, data)) != null && fld.value == s)
      fld.checked = true;

    // Selection lists (single).

    if (fld.type == "select-one" && (s = getData(fld.name, data)) != null)
      for (j = 0; j < fld.options.length; j++)
        if (fld.options[j].value == s)
          fld.options[j].selected = true;

    // Selection lists (multiple).

    if (fld.type == "select-multiple")
      for (j = 0; j < fld.options.length; j++)
        if ((s = getData(fld.name + IDXSEP + j, data)) != null && fld.options[j].value == s)
          fld.options[j].selected = true;

    // Text fields.

    if ((fld.type == "hidden" || fld.type == "password" || fld.type == "text" || fld.type == "textarea") && (s = getData(fld.name, data)) != null)
      fld.value = s;
  }

  alert('Boedeker Plastics On-Line Order Form Contents Retrieved');

  return true;
}

/*****************************************************************************
* These functions set and retrieve the field name/value pairs stored in the  *
* cookie.                                                                    *
*****************************************************************************/

//----------------------------------------------------------------------------
// Given a field name and value, creates a name/value string that can be
// appended to the cookie data. A null string is returned if the given value
// is null.
//----------------------------------------------------------------------------

function setData(name, value) {

  if (value != "")
    return name + "=" + value + FLDSEP;
  else
    return "";
}

//----------------------------------------------------------------------------
// Given a field name, this function will extract the matching value found
// in the data string. Null is returned if no match is found.
//----------------------------------------------------------------------------

function getData(name, data) {

  var i, j;
  var s;

  if (data == "")
    return null;

  s = FLDSEP + name + "=";
  i = data.indexOf(s);
  if (i >= 0) {
    i += s.length;
    j = data.indexOf(FLDSEP, i);
    if (j >= 0)
      return data.substr(i, j - i);
  }

  return null;
}

/*****************************************************************************
* These are the basic functions to set, get and delete a cookie.             *
*****************************************************************************/

//----------------------------------------------------------------------------
// Set a cookie given a name, value and expiration date.
//----------------------------------------------------------------------------


function setCookie (name, value, expires) {

  document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
}

//----------------------------------------------------------------------------
// Returns the value of the named cookie.
//----------------------------------------------------------------------------

function getCookie(name) {

  var search;

  search = name + "=";
  offset = document.cookie.indexOf(search);
  if (offset != -1) {
    offset += search.length;
    end = document.cookie.indexOf(";", offset);
    if (end == -1)
      end = document.cookie.length;
    return unescape(document.cookie.substring(offset, end));
  }
  else
    return "";
}

//----------------------------------------------------------------------------
// Delete the named cookie.
//----------------------------------------------------------------------------

function deleteCookie(name) {

  var expdate = new Date();
  expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
  setCookie(name, "", expdate);
}

