/*

Paul Stephens' NetScape-based cookie-handling library

DISCLAIMER: THESE JAVASCRIPT FUNCTIONS ARE SUPPLIED 'AS IS', WITH 
NO WARRANTY EXPRESSED OR IMPLIED. YOU USE THEM AT YOUR OWN RISK. 
NEITHER PAUL STEPHENS NOR PC PLUS MAGAZINE ACCEPTS ANY LIABILITY FOR 
ANY LOSS OR DAMAGE RESULTING FROM THEIR USE, HOWEVER CAUSED. 

http://web.ukonline.co.uk/paul.stephens/index.htm

Feel free to use this code, but please leave this comment block in.

This Cookie-handling library is based mainly on NetScape's JavaScript tutorial,
but has been modified slightly and also tested with Microsoft's Internet Explorer 3.01.

Link this library into a page via this HTML:

<script src="pscklib.js"></SCRIPT>

(NOTE - you MUST include the </SCRIPT> tag)

The functions in this library are:

setCookie(name, value, lifespan, path) - creates (or updates) cookie with specified lifespan in days

setDatedCookie(name, value, expire) - creates (or updates cookie) with specified expiry date

getCookie(Name) - retrieve named cookie's value

deleteCookie(Name, Path) - delete named cookie (by setting its expiry date to yesterday)

*/


// setCookie() 
// - function to set a cookie value for the current document, with an optional lifespan 
// (expressed in days) and access Path. If you don't supply a lifespan, the 
// cookie only lasts for the user's current browser session (which is fine
// for things like quiz scores and inter-document variables).
//
// Note that JavaScript's cookie creation system actually lets you nominate a 
// specific expiry date (e.g. 12th May 1998) rather than a lifespan. However
// most people sem to want to specify a lifespan from the current date, so I've built
// the calculation into this function. This library also includes a copy of the original 
// NetScape routine, which takes a specific expiry date parameter, under the 
// function name setDatedCookie().

// Usage example for this function:
//
//  setCookie("UserName", Uname, 60, "/")
//
// (where 'Uname' is a variable which you've filled from a data entry 
//  form or prompt() method)
//
// if the value of Uname was "Bill Smith", this would create the following 
// cookie:
//   "Username=Bill Smith"
// with an expiry date 60 days from today, and accessible to all pages within your 
// web site (thanks to the PATH setting of "/", i.e. root directory). 
// You could retrieve the value ("Bill Smith") later 
// using the function call getCookie("UserName")

// Note that this function uses the JavaScript escape() function to encode the cookie's value
// ("var cookietext = name + "=" + escape(value)"). This allows you to include otherwise 
// 'illegal' characters such as ; and space in your values. 

// Your cookie names should only include letters, numbers and the underscore character, so
// "Cookie_1"  is OK, but "Cookie 1;" isn't.

function setCookie (name, value, lifespan, access_path) {
      
  var cookietext = name + "=" + escape(value)  // Set up basic cookie string
    if (lifespan != null) {                    // Lifespan specified - add 'expires' keyword to cookie string
      var today=new Date()     
      var expiredate = new Date()      
      expiredate.setTime(today.getTime() + 1000*60*60*24*lifespan) // Calculate expiry date by adding lifespan value to today's date
      cookietext += "; expires=" + expiredate.toGMTString() 
    }
    if (access_path != null) {                // Access path specified - add PATH keyword to cookie string
      cookietext += "; PATH="+access_path     // Note '+=' operator - equivalent to 'cookietext = cookietext +'
    }
   document.cookie = cookietext               // Write/update cookie by setting document.cookie = cookie string
   return null  
}

// This is the original NetScape cookie-creation function, which I've renamed 
// setDatedCookie(). It allows you to nominate a specific date (in JavaScript 
// Date() object format, as opposed to a lifespan in days. You can still omit the 
// date parameter to create a temporary cookie.
//
function setDatedCookie(name, value, expire) {
      document.cookie = name + "=" + escape(value)
      + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}


// Function to extract individual named cookie from document.cookie string.
// Based on the sample routine from www.netscape.com, with slight modifications
// Example usage: var UserName = getCookie(UserName)
//
function getCookie(Name) {
  var search = Name + "="                       // Add = sign to cookie name parameter (e.g. "Name=Value")
  var CookieString = document.cookie            // Place cookie string for this document into a variable
  var result = ""                               // Give 'result' variable default value 
  if (CookieString.length > 0) {                // if there are any cookies at all
    offset = CookieString.indexOf(search)       // "Offset" now points to the start of the cookie name (if present)
    if (offset != -1) {                         // if cookie name is present, i.e. it exists in this string
      offset += search.length                   // make 'offset' point to beginning of value (i.e. after the "=" sign
      end = CookieString.indexOf(";", offset)   // set index of end of cookie value
      if (end == -1)                            // if this is the last cookie in the string, so no "; " terminator
        end = CookieString.length               // end is = last character in cookie string
      result = unescape(CookieString.substring(offset, end))         
                                                // update 'result' variable with cookie value  
      } 
    }
   return result                                // Return default or updated 'result' value
}

// This is an easy one - delete a cookie by calling createCookie() with a negative
// lifespan paramater - this will give it an expiry date of yesterday, causing
// the browser to delete it.

// Note that if you've created a cookie from a page in a subdirectory on your Web site,
// and given it a Path parameter (typically "/"), then if you want to delete it from
// a page in the same subdirectory, you have to specify the same Path value
// to deleteCookie().
//
// Usage example 1: deleteCookie("UserName")
// - this deletes a cookie which was created by a page in the same subdirectory, 
//   without a Path value.

// Usage example 2: deleteCookie("UserName", "/")
// - this deletes a cookie which was created by a page in the same or another subdirectory, 
//   with a Path value of "/".

function deleteCookie(Name, Path) {
  setCookie(Name,"Deleted", -1, Path)
}

