//
// cookie_setup.js
//


var timeout=new Date()

// Pagename is the name of the cookie.  Make this name the name of thw web
// page or the function of the web page.
//
var pagename='phonejam'

// Timeout: 1 sec  = 1000
//          1 min  = 1000 * 60
//          1 hour = 1000 * 60 * 60
//          1 day  = 1000 * 60 * 60 * 24
//
timeout.setTime(timeout.getTime() + 5*60*1000);



// name is a string of the name of your cookie
// value is the value corresponding to name
function SetCookie(name, value) {
  var expString='; expires=' + timeout.toGMTString();
  document.cookie=name + '=' + escape(value) + expString;
}

// returns value of cookie or null if cookie does not exist
function GetCookie(name) {
  var result=null;
  var myCookie=' ' + document.cookie + ';';
  var searchName=' ' + name + '=';
  var startOfCookie=myCookie.indexOf(searchName);
  var endOfCookie;
  if (startOfCookie != -1) {
    startOfCookie += searchName.length; // skip past name of cookie
    endOfCookie=myCookie.indexOf(';', startOfCookie);
    result=unescape(myCookie.substring(startOfCookie, endOfCookie));
  }
  return result;
}

use_cookies = 'yes';

function saveValue(element) {
  if (document.images && use_cookies == 'unsure')
    use_cookies=confirm(
      'Will you allow the values you enter into this form to be '+
      'stored as cookies so that those values will be pre-filled '+
      'the next time you return to this page?') ? 'yes':'no';
  if (document.images && use_cookies == 'yes') {
    if ((element.type == 'text')
     || (element.type == 'hidden')
     || (element.type == 'password')
     || (element.type == 'textarea')
     || (element.type == 'radio')) {
      val=element.value;
    } else if (element.type.indexOf('select') != -1) {
      val='';
      for(k=0;k<element.length;k++) {
        if (element.options[k].selected) {
          val += k+' ';
        }
      }
    } else if (element.type == 'checkbox') {
      val=element.checked;
    }
    SetCookie(pagename+'_'+element.form.name+'_'+element.name,val);
  }
}

function storedValues() {
 if (document.images) { // only do it in JavaScript 1.1 browsers
   for (i=0;i<document.forms.length;i++) {
     for (j=0;j<document.forms[i].elements.length; j++) {
      cookie_name=pagename+'_'+document.forms[i].name+'_'+
        document.forms[i].elements[j].name;
      val=GetCookie(cookie_name);
      if (val) {
        if ((document.forms[i].elements[j].type == 'text')
         || (document.forms[i].elements[j].type == 'hidden')
         || (document.forms[i].elements[j].type == 'password')
         || (document.forms[i].elements[j].type == 'textarea')) {
          document.forms[i].elements[j].value=val;
        } else if
        (document.forms[i].elements[j].type.indexOf('select') != -1) {
          document.forms[i].elements[j].selectedIndex=-1;
          while (((pos = val.indexOf(' ')) != -1) &&
            (val.length > 1)) {
            sel=parseInt(val.substring(0,pos));
            val=val.substring(pos+1,val.length);
            if (sel < document.forms[i].elements[j].length) {
              document.forms[i].elements[j].options[sel].selected=
                true;
            }
          }
        } else if (document.forms[i].elements[j].type == 'checkbox') {
          document.forms[i].elements[j].checked=val;
        } else if (document.forms[i].elements[j].type == 'radio') {
          if (document.forms[i].elements[j].value == val)
            document.forms[i].elements[j].checked=true;
          }
        }
      }
    }
  }
}

// Put the following line in your html in the body statement's onload>
//
//window.onload=storedValues;

