
<!-- begin script

//////// JavaScript to save Personal Form data for recall on next use. //////

//////	3-Mar-1996 Copyright (C) 1996 by Geoff Inglis. You may use these
//////	routines in your pages (commercial or noncommercial) as long as
//////	you include these four lines. You may not Re-sell this code. 
//////  Questions (or if you use this code) E-mail: inglis_geoff@Yahoo.com 

/* 
 This deceptively simple application demonstrates saving re-useable form
information to a Magic Cookie. All elements are stored in one Cookie, and
restored on loading of the form. The data can be stored for any length of
time, editable in the JavaScript source. One of each of the form elements
that Netscape allows to be read are demonstrated here. All data
are stored on the client machine, there is no Server CGI involved. 
All the work is done by JavaScript, except serving up the original form
and script below. 

 Some minor bugs and gotchas:
1.)Macintoshes "onSubmit" submits the form before executing all the scripts
   so you need to push the save button ONCE first before pushing the "Order
   Book NOW". Or execute the "Order Book NOW" twice.
2.)If using a radio button you must put a "dummy" onClick=0 in each
   button. (see example). If you don't it will still work fine, but the 
   answers will be out of order, when the Client changes from Netscape 2.0
   to 3.0.
3.)If you have any IMG tags, *any* *at* *all* including little dividers
   or icons used as buttons, YOU MUST HAVE a HEIGHT and WIDTH argrument
   for EACH ONE!!! JavaScript simple does NOT work with out them!!!
4.)ENCTYPE=text/plain fails on all Mac platforms in the Form ACTION.
   So for now it is commented out.
5.)UNIX platforms do not correctly restore the SELECT and MULTIPLE SELECT
   fields. These problems seem to be fixed in Atlas and 3.xx versions.
6.)Netscape does not allow saving or restoring a "password" form element 
   type with javascript.

 Future enhancments:
In the newest version of Netscape's 3.0, JavaScript will allow one
to detect what type of form element is being used. So I will eventually
allow you to just LIST the elements that you would like to save and
restore. That will make editing these routines for any form  MUCH
easier. 'Till then, here at least is the functionality. 
*/

//\/\/\/\/\/\/\/\/\  ** Here is the CookieName */\/\/\/\/\/\/\

TheCookieName = 'NETSCAPE_LIVEWIRE.BigCookieinthesky';
numDays       = 183;  //Days 'till Cookie expires.(eg. 183 days = 6 months)

//** Edit this function to add form entries:
// Add one line like this for each form entry you wish to save in a Cookie
// WholeCookie = WholeCookie + '`' + document.TestForm.Entry2.value;
//                                                     ||||||
// Entry2 (above) should be replaced by the "name value" for each form item.   
// Note that each form TYPE has a slightly different method. In my
// Next version (depending on Netscape 3.0's javascript), I will automatically
// test the TYPE and do the correct thing. At that time editing this routine
// for your OWN form should be made MUCH easier!!! 3.0 has FORM TYPE. Yea!

// Write One Big Cookie with all the values in it.
function WriteOneBigCookie () {
   var expire = new Date ();
   expire.setTime (expire.getTime() + (numDays * 24 * 3600000)); //6 months from now!
//                                     (dd) (hr) (ms in hr)
   var WholeCookie = expire ;

//Text entry
   WholeCookie = WholeCookie + '`' + document.TestForm.First_Name.value;
   WholeCookie = WholeCookie + '`' + document.TestForm.Middle_Initial.value;
   WholeCookie = WholeCookie + '`' + document.TestForm.Last_Name.value;
   WholeCookie = WholeCookie + '`' + document.TestForm.business.value;
   WholeCookie = WholeCookie + '`' + document.TestForm.Address.value;
   WholeCookie = WholeCookie + '`' + document.TestForm.Address2.value;
   WholeCookie = WholeCookie + '`' + document.TestForm.City.value;
   WholeCookie = WholeCookie + '`' + document.TestForm.zip.value;
   WholeCookie = WholeCookie + '`' + document.TestForm.email_student.value;

//Put cookie in the Oven Bake 'till done.
  document.cookie = TheCookieName +"=" + escape (WholeCookie) +
                    "; expires=" + expire.toGMTString() ;

// alert('Your Personal information has been stored, ready for the next time you use this page.');
}

//**Edit this function for each corresponding function above.
//  They are numbered in the order they are placed in the Cookie
//  starting with zero which is ALWAYS the expriation date.

function UpdateForm () {

//Get the Cookievalue then use a Cookie Cutter to slice it up in an array.
MakeCookieArray(GetCookie(TheCookieName));

//FORM action 
//  document.TestForm.action = 'mailto:inglis_geoff@yahoo.com,' + ckArray[5];
    document.TestForm.action = 
	'http://www.idealcoffee.com/cgi/reorder.cgi';

//TEXT input Form

//alert('ckArray: ' + ckArray[0]);
 if (ckArray[0] != "*") {
  document.TestForm.ExpDate.value  = ckArray[0];

  document.TestForm.First_Name.value  = ckArray[1];
  document.TestForm.Middle_Initial.value   = ckArray[2];
  document.TestForm.Last_Name.value   = ckArray[3];
  document.TestForm.business.value   = ckArray[4];
  document.TestForm.Address.value   = ckArray[5];
  document.TestForm.Address2.value   = ckArray[6];
  document.TestForm.City.value  = ckArray[7];
  document.TestForm.zip.value  = ckArray[8];
  document.TestForm.email_student.value  = ckArray[9];

 }
}

//Get and return the index of THE checked radio button
 function setChkIndx(theName) { 
   for (var i=0; i < theName.length; i++ ) {
   if (theName[i].checked == true) return i; 
   }
 return "*"; // One item is always supposed to be checked but... 
 }

//Find and return list of indexes of *all* the selected MULTIPLE selects. YUCK.
//Return list of indexes, seperated by ",".
 function findChkIndx_MS(theName) { 
 var indxlist = "";
   for (var i=0; i < theName.options.length; i++ ) {
   if (theName.options[i].selected == true) indxlist = indxlist + "," + i; 
   }
 return (indxlist == "")?indxlist = "*":indxlist;
 }

//Use the list to SET all stored MULTIPLE selects.
// Most efficient storage for a very long list, but messy.
// Requires clearing all before loading to get only those checked.
 function setChkIndx_MS(formObj,theList) {
   for (var i=0; i < formObj.options.length; i++) { 
   formObj.options[i].selected = false;} //clear all.
   var ilen = 0;
   while ( ilen < theList.length-1 ) { 
    var indxstart = theList.indexOf(',',ilen);
    if (indxstart == -1) return;

    ilen = theList.indexOf(',',indxstart+1);
    if (ilen == -1) ilen = theList.length;

    //make sure its an integer before using as subscript
    var indx = parseInt(theList.substring(indxstart+1,ilen) ,10);
    formObj.options[indx].selected = true;
   }
 }

//Get the cookie from a list of possible cookies. Honest!
function GetCookie (CookieName) {
  var cname = CookieName + "=";
  var i = 0;
  while (i < document.cookie.length) {
    var j = i + cname.length;
    if (document.cookie.substring(i, j) == cname){
	var leng = document.cookie.indexOf (";", j);
	if (leng == -1) leng = document.cookie.length;
	return unescape(document.cookie.substring(j, leng));
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; //thats -1 plus 1, duh.
  }
  return "*";
}

//Set the Cookie with expire date in the past - 2 days to get it for sure.
function DelEatCookie (name) {
  var expire = new Date();
  expire.setTime (expire.getTime() - 2 * 86400001);  //-2 days ago. Stale Cookie
  document.cookie = name + "=*; expires=" + expire.toGMTString();
}

//Parse Big  Cookie. A CookieCutter if you will.
function MakeCookieArray(cookieValue) {
  var i = 0,indx = 0, citemlen =0;
  ckArray = new Array();
   if ( cookieValue == null ) {ckArray[0]= "*";return}//Data has expired or never entered.
   if ( cookieValue == "*"  ) {ckArray[0]= "*";return}//Data has expired or never entered.
  while (citemlen < cookieValue.length) {
    citemlen=(cookieValue.indexOf("`", indx)>0)
	     ?cookieValue.indexOf("`", indx):cookieValue.length;
    ckArray[i]= cookieValue.substring(indx, citemlen); i++;
    indx = citemlen + 1;
  }
}

// end script -->


