var allObjects = null;

function SetUpPage()
{
  if (LanguageSupport())
  {
    var msgBox = document.getElementById("leftmessage");
    var kids = msgBox.childNodes;
    var numkids = kids.length;
    for (var i = numkids - 1; i >= 0; i--)
    {
      msgBox.removeChild(kids[i]);
    }
    addLogin();
    allObjects = eval('(' + getServerMessageField().value + ')');
    if (allObjects.err.msg.length > 0)
    {
      displayError();
    }
    if (allObjects.login.user.length > 0)
    {
      document.forms[0].txtUserName.value = allObjects.login.user;
      document.forms[0].txtPassword.focus();
    }
    else
    {
      document.forms[0].txtUserName.focus();
    }
    document.forms[0].onsubmit = LoginClick;
  }
  else
  {
    var msgBox = document.getElementById("leftmessage");
    var kids = msgBox.childNodes;
    var numkids = kids.length;
    for (var i = numkids - 1; i >= 0; i--)
    {
      msgBox.removeChild(kids[i]);
    }
    displayJavascriptLevelMessage();
  }
}
function LanguageSupport()
{
  var support = false;
  support = (document.getElementById && document.getElementsByTagName && document.createElement);
  if (support)
  {
    support = ((document.implementation && document.implementation.createDocument) || (window.ActiveXObject));
  }
  return support;
}
function displayJavascriptLevelMessage()
{
  var msgBox = document.getElementById("leftmessage");
  var errText = "<h3>Incorrect Javascript Level</h3>";
  errText += "<p>This application requires a modern implementation of Javascript, with support for DHTML and Asynchronous Requests.";
  errText += "Your browser does NOT appear to support the required functionality.</p>";
  errText += "<p>We use and recommend <a href='http://www.mozilla.org'>Firefox</a> browser, but the application should run equally ";
  errText += "well with a number of recent browsers, including Internet Explorer 6 and Safari 1.2.</p>";
  msgBox.innerHTML = errText;
}
function displayError()
{
  var msgBox = document.getElementById("leftmessage");
  var errText = "<h3>" + allObjects.err.title + "</h3><p>" + allObjects.err.msg + "</p>";
  msgBox.innerHTML = errText;  
}
function clearError()
{
  var msgBox = document.getElementById('leftmessage');
  if (msgBox != null)
  {
    var kids = msgBox.childNodes;
    var numkids = kids.length;
    for (var i = numkids - 1; i >= 0; i--)
    {
      msgBox.removeChild(kids[i]);
    }
  }
}
function Validate()
{
  allObjects.err.title = "";
  allObjects.err.msg = "";
  var isValid = true;
  if (document.forms[0].txtUserName.value.length == 0)
  {
    allObjects.err.title = "Required Field";
    allObjects.err.msg = "Please enter User Name.";
    document.forms[0].txtUserName.focus();
    isValid = false;
  }
  if (isValid)
  {
    if (document.forms[0].txtPassword.value.length == 0)
    {
      allObjects.err.title = "Required Field";
      allObjects.err.msg = "Please supply a password.";
      document.forms[0].txtPassword.focus();
      isValid = false;
    }
  }
  return isValid;
}
function LoginClick()
{
  clearError();
  if (Validate())
  {
    var useSSL = false;
    if (allObjects.app.ssl != "undefined")
      useSSL = allObjects.login.usessl;


    allObjects.login.user = document.forms[0].txtUserName.value;


    //CAUTION: when the SSL is set to true. PEPi will assume that HTTPS: is being used and will send the pwd as plain text.    
    if (useSSL == false)
    {
      var pwdandslt = document.forms[0].txtPassword.value + allObjects.login.salt;
      allObjects.login.password = hex_sha1(pwdandslt);
    }
    else
      allObjects.login.password = document.forms[0].txtPassword.value;

    getServerMessageField().value = JSON.stringify(allObjects);
    document.forms[0].txtPassword.value = "";
    return true;
  }
  else
  {
    displayError();
    return false;
  }
}
function addLogin()
{
  var lBody = document.getElementById("loginBody");
  var user,pass,button;

  //  for (var i = 0; i < 5; i++)
  //  {

 
  user = lBody.insertRow(lBody.rows.length);
  var lblusername = user.insertCell(0);
  var txtusername = user.insertCell(1);
  lblusername.innerHTML = "Username:"
  lblusername.className = "Caption";
  txtusername.innerHTML = "<input name='txtUserName' type='text' Style='width:150px;' size='15'/>";

  password = lBody.insertRow(lBody.rows.length);
  var lblpassword = password.insertCell(0);
  var txtpassword = password.insertCell(1);
  lblpassword.innerHTML = "Password:"
  lblpassword.className = "Caption";
  txtpassword.innerHTML = "<input name='txtPassword' type='password' Style='width:150px;' size='15'/>";

  button = lBody.insertRow(lBody.rows.length);
  button.insertCell(0);
  buttoncell = button.insertCell(1);
  buttoncell.innerHTML = "<input type='submit' class='Button' Style='float:right;' value='Login'/>";


  //    
  //    newR = lBody.insertRow(lBody.rows.length);
  //    newC = newR.insertCell(0);
  //    
  //    switch (i)
  //    {
  ////      case 0:
  ////        newC.innerHTML = "User ID:";
  ////        newC.className = "Caption";
  ////        break;
  //      case 1:
  //        newC.innerHTML = "User ID:\t<input name='txtUserName' type='text' Style='width:150px;' size='15'/>";
  //        newC.className = "Caption";
  //        break;
  ////      case 2:
  ////        newC.innerHTML = "Password:";
  ////        newC.className = "Caption";
  ////        break;
  //      case 3:
  //        newC.innerHTML = "Password:\t <input name='txtPassword' type='password' Style='width:150px;' size='15'/>";
  //        newC.className = "Caption";
  //        break;
  //      case 4:
  //        newC.innerHTML = "<input type='submit' class='Button' value='Login'/>";
  //        break;
  //    }
  //}
}
