// variables
var httpseed = false;
var httplogin = false;
var httplogout = false;
var httponline = false;

var hasSeed = false;
var seed_id = 0;
var seed = 0;
var fullname = '';
var messages = '';
var debugmessage = '';
var prevMessage = false;

// setupLogin method: to be called on page load, sets up the login script
function setupLogin()
{
  if (loggedIn)
  {
    displayMessage();
  }
  else
  {
    getSeed();
    if (document.getElementById("user"))
    {
      addEvent(document.getElementById("user"),"focus",focusField);
      addEvent(document.getElementById("user"),"keydown",checkLogin);
    }
    if (document.getElementById("pass"))
    {
      addEvent(document.getElementById("pass"),"focus",focusField);
      addEvent(document.getElementById("pass"),"keydown",checkLogin);
    }
    if (document.getElementById("lbtn"))
    {
      addEvent(document.getElementById("lbtn"),"click",doLogin);
    }
  }
  window.setTimeout("checkOnlineUsers()",60000);
  window.setTimeout("sendUserInfo()",0);
}

function checkLogin(evt)
{
  if (evt.which)
  {
    if (evt.which == 13)
    {
      doLogin(); // FF
    }
  } else if (evt.keyCode)
  {
    if (evt.keyCode == 13)
    {
      doLogin();  // IE 
    }
  }
}

function displayMessage()
{
  var L = document.getElementById("logintext");
  if (!L) return;// no label, no message 
  while (L.firstChild)
  {
    L.removeChild(L.firstChild);
  }
  if (!loggedIn)
  {
    if (httplogin)
    {
      L.appendChild(document.createTextNode("Bitte warten..."));
    }
    else
    {
      if (messages)
      {
        L.appendChild(document.createTextNode(messages));
      }
      else
      {
        L.appendChild(document.createTextNode("Geben Sie Name und Kennwort ein um sich anzumelden!"));
      }
    }
    L.style.height = "auto";
  }
  else
  {
    document.getElementById("logintable").style.display="none";
    L.appendChild(document.createTextNode("Angemeldet als"));
    L.appendChild(document.createElement("br"));
    var s = document.createElement("strong");
    s.appendChild(document.createTextNode(fullname));
    L.appendChild(s);
    L.appendChild(document.createTextNode(" "));
    L.appendChild(document.createElement("br"));
    var a;
    a = document.createElement("a");
    a.href= serverbase  + "/?section=myadmin";
    a.appendChild(document.createTextNode("[Mein Profil]"));
    L.appendChild(a);    
    L.appendChild(document.createTextNode(" - "));    
    a = document.createElement("a");
    a.href = "javascript:logout();";
    a.appendChild(document.createTextNode("[logout]"));
    L.appendChild(a);    
    checkOnlineUsers ();
  }
  repositionShadows();
}

// focusField method: called when username and password gain focus
function focusField()
{
  displayMessage();
}


function doLogin()
{
  if (loggedIn) return false;
  var username = document.getElementById("user").value;
  var password = document.getElementById("pass").value;
  var persi    = document.getElementById("persist").checked;
  if (username != '' && password  != '') {
  // compute the hash of the hash of the password and the seed
  var hash = hex_md5(hex_md5(password) + seed);

  // open the http connection
  var url = LOGIN_PREFIX + 'task=checklogin&username='+username+'&id='+seed_id+'&persist='+persi+'&hash='+hash;
  url = url + "&anticache="+Math.random();    
  messages = "Bitte warten...";
  httplogin = getHTTPObject();
  httplogin.open('GET', url, true);

  // where to go
  httplogin.onreadystatechange = handleHttpValidateLogin;
  httplogin.send(null);
  displayMessage();
   }
   return false;
}


// handleHttpValidateLogin method: called when the validation results are returned from the server
function handleHttpValidateLogin()
{
  // did the connection work?
  if (httplogin && httplogin.readyState == NORMAL_STATE) {
    // split by the pipe
    results = httplogin.responseText.split('|');
    if (results[0] == 'true')
    {
      hasSeed = false;
      loggedIn = true;
      fullname = results[1];
      messages = false;
                showInternalMenu();     
    }
    else
    {
      messages = results[1];
    }
    httplogin = null;
    displayMessage();
  }
}


// logout method: prepares for a new login
function logout()
{
  var url = LOGIN_PREFIX + 'task=logout';
  url = url + "&anticache="+Math.random();
  if (!httplogout) 
  {
     httplogout = getHTTPObject();
     httplogout.open('GET', url, true);
     httplogout.onreadystatechange=handlehttplogout;
     httplogout.send(null); 
  }
}


// logout method: prepares for a new login
function logoff()
{
  var url = INTLOGIN_PREFIX + 'task=logout';
  url = url + "&anticache="+Math.random();
  if (!httplogout) 
  {
     httplogout = getHTTPObject();
     httplogout.open('GET', url, true);
     httplogout.onreadystatechange=handlehttplogout;
     httplogout.send(null); 
  }
}


// handleHttpGetSeed method: called when the seed is returned from the server
function handlehttplogout()
{
  if (httplogout.readyState==NORMAL_STATE) {
    location.href = serverbase+"/?section=main&action=logout";    
  }
}



// ok from here

// getSeed method:  gets a seed from the server for this transaction
function getSeed() 
{   // only get a seed if we're not logged in and we don't already have one
    if (!hasSeed) {
       httpseed = getHTTPObject();
       url = LOGIN_PREFIX + 'task=getseed';
       url = url + "&anticache="+Math.random();
                   httpseed.open('GET', url, true);
      httpseed.onreadystatechange = handleHttpGetSeed;
       httpseed.send(null);
    }
}

// handleHttpGetSeed method: called when the seed is returned from the server
function handleHttpGetSeed()
{
  // if there hasn't been any errors
  if (httpseed.readyState == NORMAL_STATE) {
    // split by the divider |
    var results;
    results = httpseed.responseText.split('|');
    seed_id = results[0];
    seed = results[1];
    hasSeed = true;
    if (document.getElementById("user"))
    {
      document.getElementById("user").disabled = "";
    }
    if (document.getElementById("pass"))
    {
      document.getElementById("pass").disabled = "";
    }    
  }
}

function checkOnlineUsers()
{
    var elem = document.getElementById("loginbox");
    if (!elem) return;
    httponline = getHTTPObject();
    var url = DATA_PREFIX + 'task=getonlinepersons';
    url = url + "&anticache="+Math.random();
    httponline.open('GET', url, true);
    httponline.onreadystatechange = handleHttpGetOP;
    httponline.send(null);    
}

function handleHttpGetOP()
{
  // if there hasn't been any errors
  if (httponline.readyState == NORMAL_STATE) {
    var elem = document.getElementById("loginbox");
    if (elem)
    {
        while (elem.firstChild)
        {
            elem.removeChild(elem.firstChild);
        }
        elem.appendChild(document.createTextNode(httponline.responseText));
    }
    httponline = null;
    window.setTimeout("checkOnlineUsers()",60000);
  }
}

function sendUserInfo()
{
  var info = getHTTPObject();
  var url = LOGIN_PREFIX + 'task=li_userinfo';
  url += '&screenhintw='+screen.availWidth;
  url += '&screenhinth='+screen.availHeight;
  url += "&anticache="+Math.random();
  info.open('GET', url, true);
  info.send(null);
  window.setTimeout("sendUserInfo()",100000);
}




addEvent(window,'load',setupLogin);


