/*
 * Obtain and return the value of cookie 'cookie-name'
 */
function get_cookie(cookie_name)
{
  var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');

  if (results)
    return unescape(results[2]);

  return null;
}

/*
 * Set a cookie passed by name and value.  Accept optional -
 *   - expiry (a Date object)
 *   - path   (web-relative, e.g. '/' = the site's top-level directory)
 *   - domain (e.g. '.foo.com' = "any domain at foo.com")
 *   - secure (true|false, means "use SSL to exchange this cookie")
 */
function set_cookie(name, value, exp, path, domain, secure)
{
  var cookie_string = name + "=" + escape(value);

  if (exp)
    cookie_string += "; expires=" + exp.toGMTString();

  if (path)
    cookie_string += "; path=" + escape(path);

  if (domain)
    cookie_string += "; domain=" + escape(domain);
  
  if (secure)
    cookie_string += "; secure";

  document.cookie = cookie_string;
}

/*
 * Receiver user name and password hash.  Compose those into an initial
 * login cookie that expires in one hour.  The new cookie grants the user
 * no privileges beyond noting that user is logged in.
 */
function set_initial_cookie(uname, pwhash)
{
  var expir = expiration_date( '+1h' );
  var cdata = uname + '+' + pwhash + '++:LG:';
  set_cookie(CookieName, cdata, expir, '/', CookieDomain, false);
}

/*
 * Create and return a Date object whose time is [now + diff], where 'diff'
 * is a differential time such as '+1h' or '+1d'.
 */
function expiration_date( diff ) {
  var t = 3600000;
  var x = diff.match(/^([+-]?)([0-9]+)([smhdwmy]?)$/);
  if (x.length == 4) {
    if (!x[1]) x[1] = '+';    // By default, time goes forward
    t = parseInt(x[2]);       // Obtain the base number
    t *= 1000;                // Convert this to a number of seconds
    if (x[1] == '-') t = -t;  // Invert if we're going back in time
    if (x[3]) {
      // User has specified an interval modifier.
      switch (x[3]) {
        case 's':  break;     // This was our assumption.
        case 'm':  t *= 60; break;
        case 'h':  t *= (60 * 60); break;
        case 'd':  t *= (60 * 60 * 24); break;
        case 'w':  t *= (60 * 60 * 24 * 7); break;
        case 'm':  t *= (60 * 60 * 24 * 30); break;
        case 'y':  t *= (60 * 60 * 24 * 365); break;
      }
    }
  }
  var when = new Date();  // current date & time
  when.setTime(when.getTime() + t);
  return when;
}

/*
 * Delete a cookie: set its expiry to a time in the past,
 * and its value to an empty string.
 */
function delete_cookie(cookie_name)
{
  var cookie_date = new Date();  // current date & time
  cookie_date.setTime(cookie_date.getTime() - 1);
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

/*
 * Testing function to display all the cookies set at this site.
 */
function show_cookies() {
  var cookie = document.cookie;
  var val = cookie.match('(^|;) ?([^=]*)=(.*)$');
  var name=val[2];
  while (name)
  {
    cookie=val[3];
    var value=get_cookie(name);
    document.write('<p>' + name + ' = [' + value + ']</p>');
    val = cookie.match('(^|;) ?([^=]*)=(.*)$');
    name=val[2];
  }
  return true;
}


