/* 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 [, new Date(2009, 11, 1)]);
** 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);
  }


/* 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)
    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;

