function loginTimeoutManager()
  {
  this.userid = null;

  this.mb = new generalMessageBox(100, "session_timeout_box", "session_timeout_box_fv");
  this.mb.setForceView();

  this.disabled = false;

  this.failureNoted = false;
  this.serverErrorNoted = false;

  this.timerInterval = 60*1000;
  this.timer = null;

  this.sudoInfo = null;

  var parent = this;
  this.timerFn = function()
    {
    parent.timer = null;
    parent.timer = window.setTimeout(parent.timerFn, parent.timerInterval);
    parent.refresh();
    }

  this.setTimerInterval = function(value)
    {
    //this.timerInterval = value;
    this.timerInterval = 10 * 1000;
    if (this.timer != null)
      {
      window.clearTimeout(this.timer);
      this.timer = window.setTimeout(parent.timerFn, this.timerInterval);
      }
    }

  this.refresh = function()
    {
    if (document.getElementById('footer_login_timeout_status') == null)
      {
      return;
      }

    var rsp = new phpRequest(new objfn('readResult', this), 'all', 'loginstatus', '/common/footer_timeout_rsp.php');
    rsp.setMethod('get');
    rsp.setErrorHandler(new objfn('reportError', this));
    rsp.run();
    }

  this.reportError = function(type, message) {
    parent.serverErrorOccurred = true;
    if (parent.serverErrorNoted) {
      return;
    }

    parent.setTimerInterval(10 * 1000);
    parent.serverErrorNoted = true;
    parent.mb.clear();

    var rootNode = document.getElementById('footer_login_timeout_status');
    while(rootNode.childNodes.length != 0) {
      rootNode.removeChild(rootNode.childNodes[0]);
    }
    rootNode.appendChild(document.createTextNode("Your current login status is unknown."));
    var loginButton = document.getElementById('lcsr_login_button');
    if (loginButton != null) {
      loginButton.style['display'] = 'none';
      loginButton.style['visibility'] = 'hidden';
    }

    var mbNode = document.createElement("div");
    var mbTitle = document.createElement("div");
    var mbBody = document.createElement("div");
    var mbOKBtn = document.createElement("div");

    mbTitle.className = "session_timeout_box_title";
    mbTitle.appendChild(document.createTextNode("Server Communication Error"));

    mbBody.className = "session_timeout_box_body";
    mbBody.appendChild(document.createTextNode("Your current login status in unknown due to a failure to communicate with the web server. Please check your network connection."));

    mbOKBtn.className = "session_timeout_box_button";
    mbOKBtn.onclick = function() {
      parent.mb.clear();
    };
    mbOKBtn.appendChild(document.createTextNode("OK"));

    mbNode.appendChild(mbTitle);
    mbNode.appendChild(mbBody);
    mbNode.appendChild(mbOKBtn);
    parent.mb.set(mbNode);
  };

  this.readResult = function(result)
    {
    if (this.serverErrorOccurred) {
      this.failureNoted = false;
      this.disabled = false;
      this.serverErrorOccurred = false;
      this.serverErrorNoted = false;
      this.mb.clear();
    }

    var rootNode = document.getElementById('footer_login_timeout_status');
    if (rootNode == null)
      {
      return;
      }
    while(rootNode.childNodes.length != 0)
      {
      rootNode.removeChild(rootNode.childNodes[0]);
      }

    if (result['as'] != null && result['as'] != "")
      {
      this.userid = result['as'];
      this.sudoInfo = result['sudoStatus'];
      }


    var text = "";

    if (result['time'] > 0)
      {
      var loginButton = document.getElementById('lcsr_login_button');
      if (loginButton != null)
        {
        loginButton.style['display'] = 'inline';
        loginButton.style['visibility'] = 'visible';
        while (loginButton.childNodes.length != 0)
          {
          loginButton.removeChild(loginButton.childNodes[0]);
          }
        loginButton.appendChild(document.createTextNode('Logout'));
        loginButton.href = "/common/logout.php";
        loginButton.onclick = null;
        }

      var minutes = result['time']/60;
      minutes = Math.ceil(minutes);
      text = "Hello, "+this.userid+". You have been logged in since "+result['onSince']+".";
/*
      if (minutes == 1)
        {
        text = text + " You will be logged out in less then a minute.";
        }else
        {
        text = text + " You will be logged out in "+minutes+" minutes.";
        }
*/
      this.mb.clear();
      this.failureNoted = false;
      this.disabled = false;
      var newInterval = result['time'];
      if (this.timerInterval != newInterval*1000)
        {
        this.setTimerInterval(newInterval*1000);
        }
      }else
      {
      var parent = this;
      var loginButton = document.getElementById('lcsr_login_button');
      if (loginButton != null)
        {
        loginButton.style['display'] = 'inline';
        loginButton.style['visibility'] = 'visible';
        while (loginButton.childNodes.length != 0)
          {
          loginButton.removeChild(loginButton.childNodes[0]);
          }
        loginButton.appendChild(document.createTextNode('Login Again'));
        loginButton.href = "javascript:void(null);";
        loginButton.onclick = function(evnt) {parent.popupQuickLogin(evnt);}
        }

      text = "You are no longer logged in.";
      if (!this.failureNoted && !this.disabled)
        {
        this.failureNoted = true;
        var mbNode = document.createElement('div');
        var mbTitle = document.createElement('div');
        var mbBody = document.createElement('div');
        var mbIgnoreBtn = document.createElement('div');
        var mbLoginBtn = document.createElement('div');
        mbTitle.className = "session_timeout_box_title";
        mbTitle.appendChild(document.createTextNode('Login Expired'));

        mbBody.className = "session_timeout_box_body";
        mbBody.appendChild(document.createTextNode('Your login has expired. If you do not "Login Again", then any further submissions to the server will be lost.'));
 
        mbIgnoreBtn.className = "session_timeout_box_button";
        mbIgnoreBtn.appendChild(document.createTextNode('Confirm Logout'));
        mbIgnoreBtn.onclick = function()
          {
          parent.mb.clear();
          parent.disabled = true;
          parent.setTimerInterval(60*1000);
          }

        mbLoginBtn.className = "session_timeout_box_button";
        mbLoginBtn.appendChild(document.createTextNode('Login Again'));

        mbLoginBtn.onclick = function(evnt)
          {
          parent.popupQuickLogin(evnt);
          }
        mbNode.appendChild(mbTitle);
        mbNode.appendChild(mbBody);
        mbNode.appendChild(mbIgnoreBtn);
        mbNode.appendChild(mbLoginBtn);
        this.setTimerInterval(10*1000);
        this.mb.set(mbNode);
        }
      }

    rootNode.appendChild(document.createTextNode(text));

    }

  this.popupQuickLogin = function(evnt)
    {
    if (evnt == null)
      {
      evnt = event;
      }
    var boxWidth = 300;
    var boxHeight = 170;
    var screenLeft = Math.floor(evnt.screenX-boxWidth/2);
    var screenTop = Math.floor(evnt.screenY-boxHeight/2);
    var windowOpts = "dependent=1,alwaysRaised=1,toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,height="+boxHeight+",width="+boxWidth+",left="+screenLeft+",top="+screenTop;
    try
      {
      var loginAsId = this.userid;
      var sudoTo = null
      if (this.sudoInfo != null && this.sudoInfo['on'] == 1)
        {
        sudoTo = loginAsId;
        loginAsId = this.sudoInfo['originalAs'];
        }
      window.open("https://"+document.location.host+"/common/quicklogin.php?id="+loginAsId+(sudoTo!=null?'&sudo='+sudoTo:''), "lcsr_quick_login", windowOpts);
      }catch(e)
      {
      }
    this.setTimerInterval(10*1000);
    this.mb.clear();
    }

  this.timer = setTimeout(this.timerFn, 1000*3);
  }

new loginTimeoutManager();
