/*
Persistent form data library

function saveSettings(form,tag,exclude)
function useSettings(form,tag)
function clearSettings(form,tag)

form is the html form whose data is to be saved.
tag is an arbitrary text string, used to identify this setting bundle.
exclude is an array containing the names of widgets that are not to be saved.

Prerequisites:
  The page is responsible for thelogic that controls whether or not the
  current settings should be saved prior to form submission.
  See Example below.

Limits:
  The total size of the settings cookie (encoded) must not exceed 2K.
  The total number of cookies (including all others) must not exceed 20 per
    domain. IE may have a smaller limit.

Usage and operation:

1.

saveSettings(form,tag,excludelist) should be called prior to submitting the form data.
This can be accomplished in an onSubmit handler, or by using a button's onClick handler to accomplish the form submission. 

example A.

<form>
<input type="text" name="foo" value="bar">
<textarea rows="2" cols="80" name="quickdoc">
Type your message here
</textarea>
<input type="button" 
   name="theSubmitter" value="Send It" onclick="doit(this.form)>
<input type="checkbox" name="savesettings" checked="true">
</form>
var submitted = false;
function doit(form) {
if (submitted) {
  alert("you did this already!");
  return;
}
if (validate(form)){
  var ex = new Array(1);
  ex[0] = "quickdoc";
  if (form.savesettings.checked) // to save or not to save, that is the question
     saveSettings(form,"myformdata",ex);
  submitted = true;
  form.submit();
} 
else alert("Your data is not up to snuff, try again");

}
function validate(form) {

if (form.foo.value.length == 0 || form.quickdoc.value.length == 0) return false;
return true;
}

In the above example, the value of the widget "foo" would be saved, the rest
of the fields would not. Each of the remaining fields are either buttons,
 or specifically excluded.

The names and values of each text, textarea, and select widget whose names
are not in the "exclude" list  are encoded and
saved into a browser cookie. 
button, hidden, radio,   and password widgets are not saved. 
file and password values can not normally be set from script. hidden values are not normally user setable, so need not be saved away. select widgets are preferred to readio buttons for template driven layouts, and so are not supported.

The cookie used to store the data is named using the 
supplied tag. The path is always set to "/" and the domain is defaulted to the subdomain containing the serving host. The expiration time is set one year hence. For example
if parameter "tag" is "1015-myformdata" and the host serving the page is www.xpedite.com, the cookie is:

1015-myformdata="encoded data goes here"; path="/"; domain=".xpedite.com"; expires="Thu, 10-Jan-2001 00:00:01 GMT"

if the page had been served from igateqa1.etn6c.xsiqa.xdds.xpedite.com, the domain subparameter would be set to ".etn6c.xsiqa.xdds.xpedite.com".

2.

useSettings(form,tag)

form and tag parameters are the same as above.

useSettings should be called to initialize the form from the saved settings.
This can be accomplished by using an "onLoad" handler in the body tag of the template. 

<body onLoad='useSettings(document.forms[0],"myformdata")' >

alternatively, the form itself can define a button to accomplish the  task...

<input type=botton onClick="useSettings(this.form,'myformdata')" >

If a settings cookie if found then
  the routine cycles through each form element. 
    If the element is a text widget or a textarea widget
       if the settings cookie has a value with the same name as the widget
         the widgets value is set from stored value.
    else If the element is a select widget
       if the settings cookie has a value with the same name as the widget
           the options associated with the widget are cycled through
              if the value of one of the options matches the value stored in the
               settings cookie, the selectedIndex of the select widget is set to
                 the index of the matching option.
else
   nothing is done.

3.
clearSettings(form,tag);

form and tag parameters are the same as above.
The associated cookie (if any) is immediately expired.
The form is reset, restoring any html coded initial values.

*/

function getSetting(name,str) {
var arg = name + "=";
var alen = arg.length;
var clen = str.length;
var i = 0;
while (i < clen) {
  var j = i + alen;
  var tst = str.substring(i,j);
  if (tst == arg) return getCookieVal(j,str,",");
  i = str.indexOf(",",i) + 1;
  if (i == 0) break;
}
return null;
}
function useSettings(form,tag) {
var savedSettings = getCookie(tag);
if (savedSettings == null || savedSettings == "")
   return;
var iter;
for (iter = 0; iter < form.elements.length; iter++) {
   var ele = form.elements[iter];
   astring = getSetting(ele.name,savedSettings);
   if (astring == null) continue;
   if (ele.type == "text" || ele.type == "textarea" || ele.type == "file") {
     ele.value = astring;
   } else if (ele.type == "select-one") {
     var o = ele.options;
     for (var i = 0; i < o.length; i++) {
        if (o[i].value == astring) {
          ele.selectedIndex = i;
          break;
        }
     }
   } else if (ele.type == "checkbox") {
    ele.checked = eval(astring);
   }
  }
}
function isExcluded(name, namelist) {
for (var i = 0; i < namelist.length; i++)
   if (namelist[i] == name) return true;
return false;
}
function saveSettings(form,tag,exclude) {
var settingsString = "";
var hostname=document.location.hostname;
var domainName=hostname.substring(hostname.indexOf("."),hostname.length);
var iter;
for (iter = 0; iter < form.elements.length; iter++) {
   var ele = form.elements[iter];
   if (isExcluded(ele.name,exclude)) continue;
   var astring ="";
   if (ele.type == "text" || ele.type == "textarea" || ele.type == "file") {
     astring = ele.name + "=" + escape(ele.value);
   } else if (ele.type == "select-one") {
    astring = ele.name + "=" + escape(ele.options[ele.selectedIndex].value);
   } else if (ele.type == "checkbox") {
    astring = ele.name + "=" + escape(ele.checked);
   }
    // else astring = ele.name + ":" + ele.type;
  if (astring != "") {
     if (settingsString == "") settingsString = astring;
     else settingsString += ("," + astring);
    }
}
if (settingsString.length > 1900) {
    alert("Settings data to large to save");
    return;
}
var expires = new Date();
var newTime = expires.getTime() + (365 * 24 * 60 * 60 * 1000);
expires.setTime(newTime);
var cookiestr = tag + "="  + escape(settingsString) + "; path=/; domain=" + domainName + "; expires=" + expires.toGMTString();
document.cookie = cookiestr;
}
function getCookieVal(offset,str,delimiter) {
var endstr = str.indexOf(delimiter,offset);
if (endstr == -1) endstr = str.length;
return unescape(str.substring(offset,endstr));
}

function clearSettings(form,tag) {
var hostname=document.location.hostname;
var domainName=hostname.substring(hostname.indexOf("."),hostname.length);
var clrString = tag + "=" + "; path=/; domain=" + domainName 
   + "; expires=Thu, 01-Jan-1970  00:00:01 GMT";
document.cookie = clrString;
form.reset();

}

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