// javascript library

////////////////////////////////////////////////////////////////////////////////

function pageLoad()
{
  visitorCookie.load();
}

////////////////////////////////////////////////////////////////////////////////

function mailPassword(pLoginForm)
{
  pLoginForm.action ="mail_password.php";
  pLoginForm.submit();
}

////////////////////////////////////////////////////////////////////////////////

function openWindow(pUrl,pWidth,pHeight)
{
  win = window.open(pUrl,"","width=" + pWidth + ",height=" + pHeight);
}

////////////////////////////////////////////////////////////////////////////////

function toggleAutoRefresh(pCheckbox)
{
  if ( pCheckbox.checked )
  {
    refreshCookie.value="30";
    timeoutId = setTimeout("reloadPage()",10000);
  }
  else
  {
    refreshCookie.value="-1";
    clearTimeout(timeoutId);
  }
  refreshCookie.store();
}

function reloadPage()
{
  url = window.location;
  window.location = url;
}

function setReloadPage()
{
  url = window.location;
  if ( refreshCookie.load() )
  {
    if ( refreshCookie.value > -1 )
    {
      timeoutId = setTimeout("reloadPage()", refreshCookie.value * 1000 );
      document.forms[0].PP_AUTO_REFRESH.checked = true;
    }
  }
}

////////////////////////////////////////////////////////////////////////////////

function Cookie(document, name, hours, path, domain, secure)
{
    this.$document = document;
    this.$name = name;
    if (hours)
        this.$expiration = new Date((new Date()).getTime() + hours*3600000);
    else this.$expiration = null;
    if (path) this.$path = path; else this.$path = null;
    if (domain) this.$domain = domain; else this.$domain = null;
    if (secure) this.$secure = true; else this.$secure = false;
}

// This function is the store() method of the Cookie object.
function _Cookie_store()
{
    var cookie = this.$name + '=' + this.value;
    if (this.$expiration)
        cookie += '; expires=' + this.$expiration.toGMTString();
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    if (this.$secure) cookie += '; secure';

    this.$document.cookie = cookie;
}

// load php cookie
function _Cookie_load()
{
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;

    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false; 
    start += this.$name.length + 1;
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);

    this.value = unescape(cookieval);

    return true;
}

// This function is the remove() method of the Cookie object.
function _Cookie_remove()
{
    var cookie;
    cookie = this.$name + '=';
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

    this.$document.cookie = cookie;
}

// Create a dummy Cookie object, so we can use the prototype object to make
// the functions above into methods.
new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;

////////////////////////////////////////////////////////////////////////////////
// Timezone functions
// The php code expects all times to use GMT. It is the responsibility of the
// html/javscript code to convert times between GMT and local timezone.

function convertToGmt(e)
{
  d = new Date(e);
  s = d.getUTCFullYear() + "-" 
    + d.getUTCMonth() + "-"
    + d.getUTCDate() + "-"
    + d.getUTCHours() + ":"
    + d.getUTCMinutes() + ":"
    + d.getUTCSeconds() 
    ;
  alert("convertToGmt: " + e.toGMTString());
  return(e);
}

function convertToLocalTZ(e)
{
  alert("convertToLocalTZ: " + e);
  // Dates are expected to be formatted as: 2000-01-01 12:00:00

  d = new Date();
  d.setUTCFullYear(e.substr(0,4));
  d.setUTCMonth(e.substr(5,2));
  d.setUTCDate(e.substr(8,2));
  d.setUTCHours(e.substr(11,2));
  d.setUTCMinutes(e.substr(14,2));
  d.setUTCSeconds(e.substr(17,2));

  alert("convertToLocalTZ: " + d.toLocaleString());
  return(d);
}

////////////////////////////////////////////////////////////////////////////////

var visitorCookie = new Cookie(document,"visitor",2400,"/racchvs");
var timeoutId;
var refreshCookie = new Cookie(document,"refresh",2400,"/racchvs");
// Store timezone offset in a cookie so it can be read by the server code
var tzOffsetCookie = new Cookie(document,"tzOffset",0);
tzOffsetCookie.value = (new Date()).getTimezoneOffset();
tzOffsetCookie.store();
