/* Client login popup */

function clientLogin() 
{
  attachEventHandler(window, "load", this.handleLoad);
}

clientLogin.prototype = {


  showing : false,

  handleLoad: function() 
  {
    DOMEvents.attach(document.getElementById('showclientlogin'), "click", clientLogin.show);
    if (document.getElementById('showclientlogin2'))
      DOMEvents.attach(document.getElementById('showclientlogin2'), "click", clientLogin.show);
  },

  show : function() {

    if (clientLogin.showing == true) return;

    clientLogin.showing = true;

    document.getElementsByTagName('body')[0].className += ' show-clientlogin';
    document.getElementById('clientlogin-name').focus();

    return false;

  },
  
  hide : function() {
    if (clientLogin.showing == false) return;

    clientLogin.showing = false;
    document.body.className = document.body.className.replace(/\s*show-clientlogin/, '');
  },

  login : function() {
    var name = document.getElementById('clientlogin-name').value;
    var pass = document.getElementById('clientlogin-password').value;
    
    var url = "/client/login.html?name=" + name + "&password=" + pass + "&dt=" + new Date().getTime();
    var req = eval(xmlhttp.request(url).responseXML);
    
    if (req && req.documentElement)
      var docId = req.documentElement.getAttribute("docid");
    else
      alert("Failed to initialize XmlHttp request.\n\nLogin was unsuccessfull.\n\n error: " + xmlhttp.responseText);
    
    // employees go to admin site
    if (req && docId == "0")
      window.location.href = '/admin/default.aspx';
    else if (req && docId) 
      window.location.href = '/client/index.html';
    else 
      alert("The information you submitted was incorrect.\nPlease try again."); 
  }

}

var clientLogin = new clientLogin();
