<!-- begin script
/* 
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. 
*/

// Cookie Name goes here

TheCookieName = 'comcityworkorder.BigCookieinthesky';
numDays       = 15;  //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.form.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.form.FirstName.value;
   WholeCookie = WholeCookie + '`' + document.form.ProductServiceOther.value;
   WholeCookie = WholeCookie + '`' + document.form.LastName.value;
   WholeCookie = WholeCookie + '`' + document.form.CompanyName.value;
   WholeCookie = WholeCookie + '`' + document.form.EmailAddress.value;
   WholeCookie = WholeCookie + '`' + document.form.Phone.value;   
   WholeCookie = WholeCookie + '`' + document.form.CustomerID.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.form.action = '../../Track_WOConvertor.asp' + ckArray[5];
    document.form.action = 
	'../../Track_WOConvertor.asp' 
//TEXT input Form
//alert('ckArray: ' + ckArray[0]);
 if (ckArray[0] != "*") {
  document.form.FirstName.value    = ckArray[1];
  document.form.ProductServiceOther.value     = ckArray[2];
  document.form.LastName.value     = ckArray[3];
  document.form.CompanyName.value  = ckArray[4];
  document.form.EmailAddress.value = ckArray[5];
  document.form.Phone.value        = ckArray[6];
  document.form.CustomerID.value   = ckArray[7];
//Select Form (One entry)
  if (ckArray[11] != "*" )  
        document.form.Type1.value = ckArray[8];
 }
}
//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 -->
