
/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

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

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function queryField(opt)
{
  var keyloc		// The location of the start of "key=value"
  var nextkey 		// The start of the next key
  var start 		// The start of the value
  var opts			// The options specified by the search string
  var optval		// The value of the selected option
  // Determine the options/search string
  opts=location.search
  // Most keys start after an & and are followed by an = sign
  keyloc = opts.indexOf("&" + opt + "=")
  // If a string isn't found, indexOf returns -1.  So, we try the "first"
  // key, which appears right after the initial question mark
  if(keyloc == -1) {
    keyloc = opts.indexOf("?" + opt + "=")
  }
  // If, at this point, we still haven't found the key, stop.
  if (keyloc == -1) {
    return ""
  } 
  // The value normally ends with an ampersand (which marks the start of the next key/value pair)
  nextkey = opts.indexOf("&",keyloc+1)  
  // But sometimes there is no next pair
  if (nextkey == -1) {
    nextkey = opts.length
  }
  // Okay, what next?  Verify that it's reasonable
  if (nextkey < keyloc) {
    return ""
  }  
  // Get and return the value
  sval = keyloc+2+opt.length
  optval = plustospace(unescape(opts.substring(sval,nextkey)))
  return optval
} // getOption()

// Function
//   plustospace
// Description
//   Converts all the plus signs in a string to spaces.
//   (Most browsers convert spaces to pluses for form submission)
// Note
//   Not all of the code is what I'd expect, but this has been
//   thoroughly tested on Netscape Navigator 3.0 for the Mac.
function plustospace(txt)
{
  // Sanity check on empty string
  if (txt == "") { return txt }
  
  // Variables
  var newtxt=""  // The txt without the spaces
  var pos=0      // The position of the plus sign
  var prev=0     // The position of the previous plus sign
  var done=false // sentinel for loop
  var tmp        // Used for debugging
  
  // Repeatedly find the next + sign, stopping when no more
  // are found
  // alert("Text is '" + txt + "'") // DEBUG
  while (!done) {
    pos = txt.indexOf("+",prev)
    // tmp = prompt("Plus found at '" + pos + "'", "OK")  // DEBUG
    // if (tmp != "OK") { done = 1 }// DEBUG
    if (prev >= txt.length) {
      done = true
    }
    else if (pos == 0) {
      prev=1
      newtxt += " "
    }
    else if ((pos < 0) || (pos == "")) {
      // Not found ... exit
      done = true
    }
    else {
      // Copy text
      if (pos>prev) { newtxt += txt.substring(prev,pos) }
      newtxt += " "
      // And move on
      prev=pos+1
    }
  }
  // Get the last little bit
  newtxt += txt.substring(prev,txt.length)
  return newtxt  
} // plustospace()

function displayCookieValue(cookieName)
{
	alert(getCookie(cookieName));
}
