 // Generic cookie handler. It is used as follows:
 /*
   // First instantiate the Cookie object
   var cookie = new Cookie('cookieName', expiry period in hours, etc etc);

   // set the values we want to store
   cookie.bookTitle = "Oliver Twist";
   cookie.author = "Charles Dickens";  etc etc

   // Store the cookie
   cookie.store();

   // To retrieve the date, first check if it has already been created
   if (cookie.load())
   { // if load returns true, the cookie already exists, so:
     var bookTitle = cookie.bookTitle;  // "Oliver Twist"
   }

   // Delete if required
   cookie.remove();
 */

  function Cookie(name, hours, path, domain, secure)
  {
    // Constructor function
    // All predefined properties begin with '$' to distinguish them from
    // other properties, which are the value to be stored in the cookie
    //if (document) {this.$document = document;} else {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 = null;}
    if (secure) {this.$secure = true;} else {this.$secure = false;}
  }

  Cookie.prototype.store = function ()
  {
    // First, loop through the [properties of the object and put together the value of the cookie
    // Since cookies use = and ; as separators, this uses : and & for the individual variables held
    // Each one is escaped in case it contains punctuation or illegal characters
    var cookieVal = "";
    for (var prop in this)
    {
      // Ignore properties beginning with '$' and methods
      if (prop.charAt(0) == '$' || typeof this[prop] == 'function') {continue;}
      if (cookieVal != "") {cookieVal += '&';}
      cookieVal += prop + ':' + escape(this[prop]);
    }

    // Now assemble 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';}

    //this.$document.cookie = cookie;   //According to book - but this doesn't work & using eval gives a js error(???)
    document.cookie = cookie;
  }

  Cookie.prototype.load = function()
  {
    // First, get a list of all the cookies relating to thios document

    //var allCookies = eval(this.$document +".cookie");
    var allCookies = document.cookie;
    if (allCookies == "" || allCookies == undefined) {return false;}
    //alert ("ac= " + unescape(allCookies));
    // Extract named cookie from list
    var start = allCookies.indexOf(this.$name + '=');
    if (start == -1) {return false;}
    start += this.$name.length + 1;
    var end = allCookies.indexOf(';', start);   // alert ("s=" + start + ",e=" + end);
    if (end == -1) {end = allCookies.length;}
    var cLength = end - start;
    var cookieVal = allCookies.substr(start, cLength);        //alert ("cV=" + cookieVal);

    // Now need to extract the individual stae variables from the cooke value
    var a = cookieVal.split('&');  // split into array of name/value pairs
    for (var i=0; i < a.length; i++) {a[i] = a[i].split(':'); } // split each pair into an array
    // now set the properties of this cookie object to the values extracted
    for (var i=0; i < a.length; i++) {this[a[i][0]] = unescape(a[i][1]);}
    return true;
  }

  Cookie.prototype.remove = function ()
  {
    var cookie;
    cookie = this.$name + '=';
    if (this.$path) {cookie += '; path=' + this.$path;}
    if (this.$domain) {cookie += '; domaun=' + this.$domain;}
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
    //this.$document.cookie = cookie;
    document.cookie = cookie;
  }