// ---------------------------------------------------------------------------
// (c) 2008 Kartagena Norway, Hans Martin Mohn
// ---------------------------------------------------------------------------
// Utility class to aid accessing Cookies
//
// Dependencies:
//    None
//

function gtCookie(document)
{
  this.m_document = document;
  this.m_map      = new Object();
  this.m_crumbs   = null;
  this.m_expires  = null;
  this.m_path     = null;
  this.m_domain   = null;
  this.m_secure   = null;

  this.m_crumbs = unescape(document.cookie).split("; ");
  for (var i = 0; i < this.m_crumbs.length; i++)
  {
    var arg = this.m_crumbs[i];
    var pos = arg.indexOf('=');
    if (pos > 0)
      this.m_map[arg.substring(0, pos)] = arg.substring(pos + 1);
  }
//  alert("gtCookie: m_map is:\n" + gtListProperties(this.m_map));
} // end gtCookie

var GTCOOKIE = gtCookie.prototype;



GTCOOKIE.setNumDaysValid = function gtCookie_setNumDaysValid(days)
{
  this.m_expires = new Date();
  this.m_expires.setDate(this.m_expires.getDate() + days);
}; // end setNumDaysValid()



GTCOOKIE.getCrumb = function gtCookie_getCrumb(name)
{
  return this.m_map[name];
}; // end getParam()



GTCOOKIE.getCrumbDef = function gtCookie_getCrumbDef(name, def)
{
  if (this.m_map[name] !== null)
    return this.m_map[name];
  else
    return def;
}; // end getCrumbDef()



GTCOOKIE.setCrumb = function gtCookie_setCrumb(name, value)
{
  this.m_map[name] = value; // To facilitate reading it back easily (in the same request, with one of the the getCrumb..() functions)
  try // May fail with access error if window has been refreshed!
  {
    // Remember that this writes the single crumb without overwriting any other existing crumbs.
    // document.cooike is not a string even though we treat it as such!!!
    var c = name + "=" + escape(value) +
      (this.m_expires ? "; expires=" + this.m_expires.toGMTString() : "") +
      (this.m_path    ? "; path="    + this.m_path : "") +
      (this.m_domain  ? "; domain="  + this.m_domain : "") +
      (this.m_secure  ? "; secure" : "");
    this.m_document.cookie = c;
//    alert("setCrumb: cookie=" + c + "\nm_map is:\n" + gtListProperties(this.m_map));
  }
  catch(e)
  {
    gtTrace("gtCookie::setCrumb: Error " + e + "\n");
  }
}; // end setCrumb()

