/* Cookie Utility Classes using Javascript (Page339) */
/* Source - Javascript: The Definitive Guide (O'Reilly Press) */

/* CONSTRUCTOR: ***************************************
Creates a cookie object for the specified document, name and attributes ...
Information: ---------------------------------------
Document:  object that the cookie is stored for. Required.
name:      name of the cookie. Required
hours:     optional number - hours to expiry ...
path:      optional (full http://)
domain:    optional (full http://)
secure:    optional boolean value
----------------------------------------------------
*/ 

function Cookie(document,name,hours,path,domain,secure)
 /* Predefined value are distinguished with the prefix '$' ... */

 {
 this.$document = document;
 this.$name = name;
 if (hours)
   this.$expiration = new Date((new Date()).getTime() + hours*3600000);
 else
   this.$expiration = null;

 if (path) this.$path=path; else this.$path=null;
 if (domain) this.$domain=domain; else this.$domain=domain;
 if (secure) this.$secure=secure; else this.$secure=secure;
 }
/* ************************************************ */


/* STORING: ***************************************
Stores Method for the cookie object ...
Information: ---------------------------------------
Separators:        '=' and ';' (@ start and end of field)
State variables:   ':' and '&' ...
Note we escape the value of each state variable in case it contains punctuation
or other illegal characters.
----------------------------------------------------
*/ 

function _Cookie_store()
 {
 /* Loop through the cookie object to determine each cookie value ... */
 var cookieval = '';
 for (var prop in this) {
   /* Ignore variables starting with $ and methods */
   if (prop.charAt(0) == '$' || ((typeof this[prop]) == 'function'))
     continue;
   if (cookieval != "") cookieval += "&";
   cookieval += prop + ':' + escape(this[prop]);
   }

  /* Put together complete cookie string */
  var cookie = this.$name + '=' + cookieval;
  if (this.$expiration)
    cookie += '; expires' + '=' + this.$expiration.toGMTString();
  if (this.$path)
    cookie += '; path' + '=' + this.$path;
  if (this.$domain)
    cookie += '; domain' + '=' + this.$domain;
  if (this.$secure)
    cookie += '; secure';

  /* Store cookie by setting document.cookie property ... */
  this.$document.cookie = cookie;
 
//alert('Data stored: \n' + cookie);

 }
/* ************************************************ */

/* LOAD: *******************************************
Load Method for the cookie object ...
----------------------------------------------------
*/ 

function _Cookie_load()
 {
 /* List all cookies and set this cookie value ... */
 var allcookies = document.cookie;
 if (allcookies == "") return false;

//alert('Data read: \n' + allcookies);

 /* Extract cookie of this name */
 var start = allcookies.indexOf(this.$name + '=');
//    document.write('Start: (' + start + ') ...<br>');
 if (start == -1) {
//     document.write('No cookie read for this name (' + this.$name + '=' + ') ...<br>');
     return false;             //** Cookie not defined for this page
     }
 if (start != -1) {
//    document.write('Cookie read for this name (' + this.$name + '=' + ') ...<br>');             //** Cookie defined for this page
    }
 start += this.$name.length + 1;
//    document.write('NewStart: (' + start + ') ...<br>');
 var end = allcookies.indexOf(';', start);
//   document.write('End set ... Position: ' + end + '<br>');
 if (end == -1)  {
//    document.write('No end read for this name (' + this.$name + '=' + ') ...<br>'); 
    end = allcookies.length;
   }
 if (end != -1) {
// document.write('End found for this name (' + this.$name + '=' + ') ... Position: ' + end + '<br>'); 
  }
 var cookievalue = allcookies.substring(start, end);

//alert('This cookie read: \n' + cookievalue);

 /* Determine individual state values from the named cookie. */
 /* Values separated by ampersands and colons ... */
 var a = cookievalue.split('&');           //Break into name/value pairs
 for (var i=0; i<a.length; i++)
   a[i] = a[i].split(':');

 /* Set individual state values. */
 for (var i=0; i<a.length; i++){
   this[a[i][0]] = unescape(a[i][1]);

  }
  return true;
 }
/* ************************************************ */


/* REMOVE: *******************************************
Remove Method for the cookie object ...
----------------------------------------------------
*/ 

function _Cookie_remove()
 {
 var cookie;
 cookie = this.$name + '=';

  if (this.$path) cookie += '; path='+ this.$path;
  if (this.$domain) cookie += '; domain='+ this.$domain;
  if (this.$secure) cookie += '; secure='+ this.$secure;
  cookie += '; expires=Fri, 02-Jan-1970 00:00:00GMT';

  /* Store cookie by setting document.cookie property ... */
  this.$document.cookie = cookie;
 }
/* ************************************************ */


/* ** CREATE COOKIE CLASS OBJECT ***********************
Use object to make the functions above into methods
--------------------------------------------------------
*/ 

new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;

/* ************************************************** */


//====================================================
// Code above is the definition of the Cookie class
// Code below is a sample use of that class ...
//====================================================

/* ** CREATE COOKIE TO SAVE CLASS OF PAGE **************
Name relates to this path and subpaths.
Expiration date set 100 days hence.
--------------------------------------------------------
*/ 
var visitordata = new Cookie(document, "products_count_state", 2400);

/* Set inital fields for Cookie */
if (!visitordata.load()) {
//if (visitordata.order != '') var thisOrder = visitordata.order;
if (visitordata.order == null) visitordata.order = '*';
if (visitordata.visits == null) visitordata.visits = 0;
if (visitordata.Name == null) visitordata.Name = '';  
if (visitordata.Address == null) visitordata.Address = '';  
if (visitordata.Suburb == null)  visitordata.Suburb = '';  
if (visitordata.State == null)  visitordata.State = '';  
if (visitordata.Postcode == null)  visitordata.Postcode = '';  
if (visitordata.Country == null) visitordata.Country = '';  
if (visitordata.Phone == null)  visitordata.Phone = '';  
if (visitordata.Email == null)  visitordata.Email = '';  
if (visitordata.Delivery == null) visitordata.Delivery = '';
if (visitordata.Speed == null)  visitordata.Speed = '';  
if (visitordata.Payment ==null)  visitordata.Payment = '';  
if (visitordata.OrderID ==null)  visitordata.OrderID = '';  
if (visitordata.Previous ==null)  visitordata.Previous = '';  
if (visitordata.prevOrders ==null)  visitordata.prevOrders = '';  
if (visitordata.lastOrderID ==null)  visitordata.lastOrderID = '';  
}
/* Keep track of visits to this page */
visitordata.visits++;

/* Store cookie values to update visit and reset expiry date */
visitordata.store();

/* ************************************************** */
