/* COOKIES.JS - JavaScript Cookies Library (version 1.1)
** =====================================================
**
** This library contains OOPed versions of the standard
** set of JS cookie functions: store, get, and del(ete).
**
** You may use and/or modify this JavaScript library
** provided that you maintain this copyright header.
**
** Copyright 1999, Richard Scott (version 1.1). All rights reserved.
**   Streamlined Cookie(), Cookie_store(), Cookie_get(), Cookie_del().
**   Removed document parameter from Cookie() constructor function.
**   Removed CookieTable() diagnostic tool.
**   Added explanatory comments.
**
** Copyright 1997, Christopher Doemel (version 1.0). All rights reserved.
**   There are several good JavaScript cookie libraries around.
**   One is Bill Dortch's public domain cookie functions
**   (at http://www.hidaho.com/cookies/cookie.txt), and another is David 
**   Flanagan's cookie example in JavaScript: The Definitive Guide, 
**   published by O'Reilly Press. This library combines the best features 
**   of both: The thoroughness of Dortch's cookie functions, and the 
**   object-oriented design of Flanagan's cookie example.
*/

/* JavaScript Cookies Library Usage
** ================================
** To create a Cookie object:
**   myCookie = new Cookie(name [, expires] [, domain] [, path] [, isSecure]);
** To retrieve data from a cookie:
**   myCookie = new Cookie("myCookie");
**   cookieInfo = myCookie.get();
** To store data in a cookie:
**   myCookie = new Cookie("myCookie" [, expires]);
**   myInfo = "Hi there!";
**   myCookie.store(myInfo);
** To delete a cookie:
**   myCookie = new Cookie("myCookie");
**   myCookie.del();
*/

/* Cookie()
** ======== 
** The Cookie constructor function takes the following arguments:
** name (required)      The name of the name/value pair stored in the cookie.
** expires (optional)   A date object specifying the lifetime of the cookie.
**                      After the date specified, the browser stops storing the
**                      cookie. If no date is specified, the cookie will be
**                      lost when the browser quits.
** domain (optional)    If a page's URL matches this domain, the browser will
**                      make the cookie available. For instance, if the cookie
**                      domain is set to "palimpsest.com," "tabula.palimpsest.com"
**                      is eligible to receive the cookie. The page's URL is
**                      also matched against the cookie's path (described below).
**                      The default domain is the domain of the document that
**                      creates the cookie.
** path (optional)      Specifies the subset of URLs that will receive the
**                      cookie. The default value is the path of the document
**                      that creates the cookie.
** isSecure (optional)  If the cookie is secure, it will only be transmitted
**                      over secure channels (i.e., to pages sent via HTTPS
**                      servers). The default value is false.
*/

function Cookie(name, expires, domain, path, isSecure)
  {
  this.name     = name;
  this.expires  = expires;
  this.domain   = domain;
  this.path     = path;
  this.isSecure = isSecure;

  if (this.expires)  // adjust cookie date to fix Mac Netscape 2.x bug
    fixCookieDate(this.expires);
  }


/* fixCookieDate()
** ===============
** Internal function that corrects for the Macintosh Netscape 2.x date bug.
** This function is only called by the Cookie() constructor. The fix works
** by creating a Date object for time 0. The getTime() method should also
** result in 0, but in Mac Netscape 2.x, the date is actually skewed by an
** hour. This function corrects for the skew by subtracting the skew
** amount from the Date object.
*/

function fixCookieDate(theDate)
  {
  var testDate = new Date(0); 
  var skew = testDate.getTime();
  if (skew > 0)
    theDate.setTime(theDate.getTime() - skew);
  }


// showCookie 
function showCookie() {
  var wholecookie = document.cookie;
  alert(unescape(wholecookie)); 
  }

//insert upper right picture - either specific picture or a random one
function upperPic(url) {
  max = 4
  n = Math.floor(max*Math.random())  //0 based  
  td1 = '<td width="81"><a href="'
  td2 = '.html"><img border="0" src="'
  td3 = '_small.gif"></a></td>'
  td4='<td width="81"><img border="0" src="'
  td5='"</td>'
  if (url == "0") {
    td = td1+"images/random/"+n+td2+"images/random/"+n+td3
    }
  if (url == "1") {
    td = td1+"../images/random/"+n+td2+"../images/random/"+n+td3
    }
  if (url == "2") {
    td = td1+"../../images/random/"+n+td2+"../../images/random/"+n+td3
    }
  if (url == "3") {
    td = td1+"../../../images/random/"+n+td2+"../../../images/random/"+n+td3
    }
  if (url == "4") {
    td = td1+"../../../../images/random/"+n+td2+"../../../../images/random/"+n+td3
    }
 if ((url != "0") & (url != "1") & (url != "2") & (url != "3") & (url != "4")) {
    td =td4+url+td5
    }
  document.write(td)
  }

//show cookie off or shopping basket line
// prefix is "" for top level, "../" for second level, etc.
function showOffBasket(prefix) { 
  var noc = 'Cookies are turned off. Shopping is disabled. [<a href="'+prefix+'nocookie.html">Explain!</a>]'
  var testC = new Cookie("test");  // create test Cookie object with expire at browser exit
  var testData = testC.get(); 
  if (testData == "") { 
    testC.store("1");  //try to store data
    testData = testC.get(); // fetch it to see if it worked
    if (testData == "") { // Complain, cookies are off
      document.write('<table border="0" width="100%" bgcolor="#E7B454"><tr><td width="100%">');
      document.write(noc);
      document.write("</td></tr></table>");
     }
    }
  else {
    var storeC = new Cookie("store");
    var store = storeC.get();
    if (store == "") {store = "member";}
    var path = prefix+"store/"+store+"/";
    var basmsg = 'Shopping  basket has items in it. [<a href="'+path+'basket.htm">Show Basket</a>][a href="'+path+'index.html">Return to Hikers Market Place</a>'
    var basketC = new Cookie("sku"); // create basket Cookie object
    var basketData = basketC.get(); 
    if (basketData != "") { 
      document.write('<table border="0" width="100%" bgcolor="#E7B454"><tr><td width="100%">');
      document.write(basmsg);
      document.write("</td></tr></table>");
      }
    }
  }

/* Cookie_get()
** ============
** Returns the stored value of the cookie or
** an empty string if the cookie does not exist.
*/

function Cookie_get()
  { 
  // Get the document cookie.
  var theWholeCookie = document.cookie; 

  // Look for the name of the cookie in the document's cookie.
  var cookieStart = theWholeCookie.indexOf(this.name);

  if ((cookieStart == -1) || (theWholeCookie.charAt(cookieStart+this.name.length) == ";"))
    return "";  // The cookie wasn't found, so we return an empty string.
  else
    cookieStart += this.name.length + 1;  /* Add the length of the
                                           ** cookie name to the starting
                                           ** value. The extra 1 is to
                                           ** accomodate the "=" character
                                           ** in the cookie syntax
                                           ** name=value. */

  // Find the ";" that marks the end of our cookie.
  var cookieEnd = theWholeCookie.indexOf(";", cookieStart);
   
  if (cookieEnd == -1)
    cookieEnd = theWholeCookie.length;     /* In the event that a semicolon
                                           ** isn't found, we'll return the
                                           ** remaining length of our cookie. */
   
  // Grab the actual cookie from the entire cookie string.
  var theCookie = theWholeCookie.substring(cookieStart, cookieEnd);

  // Return the decoded cookie.
  return unescape(theCookie);
  }


/* Cookie_store()
** ==============
** Constructs the cookie by assigning all of the various 
** cookie components to document.cookie.
*/

function Cookie_store(string)
  {
  document.cookie = 
    this.name + "=" + escape(string) + 
    ((this.expires) ? "; expires=" + this.expires.toGMTString() : "") + 
    ((this.path) ? "; path=" + this.path : "") + 
    ((this.domain) ? "; domain="  + this.domain : "") + 
    ((this.isSecure) ? "; secure" : "");
  }


/* Cookie_del()
** ============
** Deletes the current cookie by setting the expiration date to the beginning
** of time (as far as the computer is concerned) and providing an empty
** string for the cookie's value.
*/

function Cookie_del()
  {
  document.cookie = 
    this.name + "=" +
    ((this.expires) ? "; expires=" + (new Date(0)).toGMTString() : "") + 
    ((this.path) ? "; path=" + this.path : "") + 
    ((this.domain) ? "; domain=" + this.domain : "");
  }


/* Creating cookie Methods
** =======================
** And, for our grand finale: Use the prototype property to make
** the above cookie functions (Cookie_store, Cookie_get, Cookie_del)
** into methods of a Cookie object, which are called as follows:
**   myCookie.store(myInfo), myCookie.get(), myCookie.del()
*/

new Cookie();  // need to create a "dummy" Cookie object first
Cookie.prototype.store = Cookie_store;
Cookie.prototype.get   = Cookie_get;
Cookie.prototype.del   = Cookie_del;

