// handleForm Client Functions
//
// Version 1.0, 1999-10-19, Neil Erdwien
//
//
// The handleFormRetainField* functions allow form field values to be
// stored in a cookie and retained from session to session.
//
// Currently, only TEXT fields are retained


function handleFormRetainFieldsSave() {

  var argv = handleFormRetainFieldsSave.arguments;
  var argc = handleFormRetainFieldsSave.arguments.length;
  var expires = (argc > 0) ? argv[0] : null;
  var path    = (argc > 1) ? argv[1] : null;
  var domain  = (argc > 2) ? argv[2] : null;
  var secure  = (argc > 3) ? argv[3] : false;

   // Go through the array of fields to be saved, and set a cookie for each
   for (var i =0; i < handleFormRetainFieldsArray.length; i++) {
      var f = handleFormRetainFieldsArray[i];
      var v = handleFormSetCookie(f.name, f.value, expires, path, domain, secure);
   }
}


function handleFormRetainFieldsDefine() {

   // Create a global array that will hold the field object for
   // each field to be retained.
   handleFormRetainFieldsArray = handleFormRetainFieldsDefine.arguments;

}


function handleFormRetainFieldsRestore() {

   // For each field defined to be retained, pull the cookie value
   // and set the corresponding form field.
   for (var i =0; i < handleFormRetainFieldsArray.length; i++) {
      var f = handleFormRetainFieldsArray[i];
      var v = handleFormGetCookie(f.name);
      if (v) {
         f.value = v;
      }
   }
}


// The following cookie manipulation routines are modified from:
//
// Cookie Functions - Second Helping  (21-Jan-96)
// Written by:  Bill Dortch, hIdaho Design <bdortch@netw.com>
// The following functions are released to the public domain.
//
// For this application, the functions have "handleForm" prepended
// to their names, unused functions have been deleted, and
// documentation has been trimmed.

function handleFormGetCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function handleFormGetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
     return handleFormGetCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

function handleFormSetCookie (name, value) {
  var argv = handleFormSetCookie.arguments;
  var argc = handleFormSetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}
