// include  <SCRIPT src='/javascript/cookies.js'></SCRIPT> in <head> tag

// Encode the cookie values.  The caller is required to ensure that the values
// for separator and delimiter are not used in the string.
// name - the name of the cookie
// data - hash of key/value pairs of the cookie data
// separator - single character; goes inbetween each key/value pair
//               (ie '=' in: key=value)
// delimiter - single character; goes after each key/value pair
//               (ie ';' in: key=value;key=value)
// expire (optional) - experation date
function encodeCookie(name, data, separator, delimiter, expire)
{
 var info = new Array()
 var i = 0;
 for (var key in data) {
   info[i] = key + separator + data[key]
   ++i;
 }
 var value = info.join(delimiter)

 setCookie(name, value, expire)
}

// Decode the cookie values.
// name - the name of the cookie
// separator - see encodeCookie()
// delimiter - see encodeCookie()
function decodeCookie(name, separator, delimiter)
{
 var stuff = getCookie(name);
 // now seperate it into key value pairs
 var data = new Object ()

 if (stuff) {
  var info = new Array()
  info = stuff.split(delimiter)
  for (var i = 0; i < info.length; ++i)
  {
   var each = new Array()
   each = info[i].split(separator)
   data[each[0]] = each[1]
  }
 }
 return data
}


// Sets cookie values and escapes invalide charaters in the value.
// Expiration date is optional
// Takes in name of the cookie and its value.  name must be a valid
// variable name.
function setCookie(name, value, expire) {
 document.cookie = name + "=" + escape(value)
   + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
   + "; path=/"
}

// returns the cookie value unescaped
function getCookie(name) {
 var search = name + "="
 // if there are any cookies
 if (document.cookie.length > 0) {
  offset = document.cookie.indexOf(search)
  // if cookie exists
  if (offset != -1) {
   offset += search.length
   // set index of beginning of value
   end = document.cookie.indexOf(";", offset)
   // set index of end of cookie value
   if (end == -1)
    end = document.cookie.length
   return unescape(document.cookie.substring(offset, end))
  }
 }
}

