First, the Javascript code:

function SetCookie (name,value,expires,path,domain,secure) { // Example Use: SetCookie (name,value);
   // Version 2.0 By Michael Milette, www.tngconsulting.ca
   // Original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au
   if (value != null && value != "")
      document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
   return (GetCookie(name) != null); // return false if the cookie was refused
}

// This is a new function called FixCookieDate() which corrects a 2.x Mac date bug.

function FixCookieDate (date) {   // ref: http://www.hidaho.com/cookies/cookie.txt   // Version 2.0 By Michael Milette, www.tngconsulting.ca   // Original JavaScript code by Bill Dortch, hIdaho Design bdortch@hidaho.com   var base = new Date(0);   var skew = base.getTime(); // dawn of (Unix) time - should be 0   if (skew > 0)  // Except on the Mac - ahead of its time     date.setTime (date.getTime() - skew); }
function DeleteCookie (name,path,domain) { // Example use: DeleteCookie("name");   // ref: http://www.hidaho.com/cookies/cookie.txt   // Version 2.0 By Michael Milette, www.tngconsulting.ca   // Original JavaScript code by Bill Dortch, hIdaho Design bdortch@hidaho.com   if (GetCookie(name)) {     document.cookie = name + "=" +       ((path) ? "; path=" + path : "") +       ((domain) ? "; domain=" + domain : "") +       "; expires=Thu, 01-Jan-70 00:00:01 GMT";   } }
function GetCookie(name) { // Example use: GetCookie("name");    // Version 2.0 By Michael Milette, www.tngconsulting.ca    // Original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au    var DocCookie = document.cookie;    var index = DocCookie.indexOf(name + "=");    if (index == -1) return null;    index = DocCookie.indexOf("=", index) + 1;    var endstr = DocCookie.indexOf(";", index);    if (endstr == -1) endstr = DocCookie.length;    return unescape(DocCookie.substring(index, endstr)); }

function SetCookie (name,value,expires,path,domain,secure) { // Example Use: SetCookie (name,value);
   // Version 2.0 By Michael Milette, www.tngconsulting.ca
  // Original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au
  if (value != null && value != "")
     document.cookie = name + "=" + escape (value) + 
       ((expires) ? "; expires=" + expires.toGMTString() : "") +
       ((path) ? "; path=" + path : "") +
       ((domain) ? "; domain=" + domain : "") +
       ((secure) ? "; secure" : "");
  return (GetCookie(name) != null); // return false if the cookie was refused
}

function FixCookieDate (date) {
   // Version 2.0 By Michael Milette, www.tngconsulting.ca
   // ref: http://www.hidaho.com/cookies/cookie.txt
   // JavaScript code by Bill Dortch, hIdaho Design <bdortch@hidaho.com>
   var base = new Date(0);
   var skew = base.getTime(); // dawn of (Unix) time -- should be 0
   if (skew > 0)  // Except on the Mac -- ahead of its time
      date.setTime (date.getTime() -- skew);
}

function DeleteCookie (name,path,domain) { // Example use: DeleteCookie("name");
   // Version 2.0 By Michael Milette, www.tngconsulting.ca
// ref: http://www.hidaho.com/cookies/cookie.txt
// JavaScript code by Bill Dortch, hIdaho Design bdortch@hidaho.com 

   if (GetCookie(name)) {
     document.cookie = name + "=" +
       ((path) ? "; path=" + path : "") +
       ((domain) ? "; domain=" + domain : "") +
       "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}

function GetCookie(name) { // Example use: GetCookie("name");
   // Version 2.0 By Michael Milette, TNG Consulting Inc.: tng@cyberus.ca
   // Original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au
   var DocCookie = document.cookie
   var index = DocCookie.indexOf(name + "=");
   if (index == -1) return null;
   index = DocCookie.indexOf("=", index) + 1;
   var endstr = DocCookie.indexOf(";", index);
   if (endstr == -1) endstr = DocCookie.length;
   return unescape(DocCookie.substring(index, endstr));
}

The Documentation

Function name:        SetCookie(name,value,expires,path,domain,secure)

Description:                 Function to create or update a cookie.

Dependencies:                GetCookie function. If you don't want to include the GetCookie function and don't care about validation, you can replace the last "return" line with "return true;"

Return Value:                True if the cookie was saved, false if it was rejected (for example, if the user has disabled support for cookies in their browser).

Parameters:                The first two parameters are required.  The others, if supplied, must be passed in the order listed above.  To omit an unused optional field, use "null" (without the quotes) as a place holder. Trailing omitted parameters do not require a placeholder.

name -- String object containing the cookie name.
value -- String object containing the cookie value.  May contain any valid string characters.
[expires] -- Date object containing the expiration data of the cookie.  If omitted or null, expires the cookie at the end of the current session (when you close the browser).

Example: You can use the following sample code to create a cookie that expires in 28 days:

  var days = 28; // days from now
  var expdate = new Date ();
FixCookieDate (expdate); // Correct for Mac date bug -- call only once for given Date object!
expdate.setTime (expdate.getTime() + (days * 24 * 60 * 60 * 1000));
SetCookie ("LastPage", "East Asia", expdate);

[path] -- String object indicating the path for which the cookie is visible.

If omitted or null, the cookie will only be visible to the page that called the function.
If you specify "/". the cookie will be available to any page on the web site.
If you specify a path, "/world/region" for example, the cookie will only be visible to web pages in or below that directory.

[domain] -- String object indicating the domain for which the cookie is valid.  If omitted or null, uses the domain of the calling document.

[secure] -- Boolean (true/false) value indicating whether cookie transmission requires a secure channel (HTTPS).  

Examples:

To call SetCookie using name, value and path, you would code:

        SetCookie ("myCookieName", "myCookieValue", null, "/");

This would create a cookie called "myCookieName" that would be availble to any web page on the site. Note that trailing omitted parameters do not require a placeholder.

To set a secure cookie for path "/myPath" (and all pages under that path) that expires after the current session, you might code:

        SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);

Here are a few more examples:

       SetCookie ("tempvar", "This is a temporary cookie.");
       SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");
       SetCookie ("paranoid", "This cookie requires secure communications",expdate,"/",null,true);

Function name:        FixCookieDate(date)

Description:                 Function which corrects a 2.x Mac date bug.  Call this function to fix a date object prior to passing it to SetCookie. IMPORTANT: This function should only be called *once* for any given date object!  See example at the end of this document.

Return Value:                Nothing

Parameter:                The parameter is mandatory and will be updated by the function.

Date -- Date Object

Example:

   var days = 28; // days from now
  var expdate = new Date ();
FixCookieDate (expdate); // Correct for Mac date bug -- call only once for given Date object!
expdate.setTime (expdate.getTime() + (days * 24 * 60 * 60 * 1000));

  SetCookie ("LastPage", "East Asia", expdate);

Function name:        GetCookie(name)

Description:                 Function to return the value of the cookie specified by "name".

Return Value:                String object containing the cookie value (any valid string characters), or null if the cookie does not exist.

Parameter:                The parameter is mandatory.

name -- String object containing the cookie name.

Example:                MyCountryVar = GetCookie("Country");

Function name:        DeleteCookie(name,path,domain)

Description:                 Function to delete a cookie specified by "name". (Sets expiration date to start of epoch).

Requirements:                GetCookie() function. If you don't want to include the GetCookie function and don't care about validation, you can replace the last "return" line with "return true;"

Return Value:                Nothing

Parameters:                The first parameter is required.  The others, if supplied, must be passed in the order listed above.  To omit an unused optional field, use "null" (without the quotes) as a place holder. Trailing omitted parameters do not require a placeholder.

name -- String object containing the cookie name.

[path] -- String object indicating the path of the cookie to delete. This MUST be the same as the path used to create the cookie, or null/omitted if no path was specified when creating the cookie.

[domain] -- String object containing the domain of the cookie to delete. This MUST be the same as the domain used to create the cookie, or null/omitted if no domain was specified when creating the cookie.

Example:                DeleteCookie("Country");