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;
    }
  }
  if (isValid)
  {
      var username = document.forms[0].txtUserName.value;
      username = username.toUpperCase();
    if (username.substring(0,7) == "COLLEGE" && username.charAt(7) != "\\")
    {
      allObjects.err.title = "Incorrect format";
      allObjects.err.msg = "You must login using COLLEGE\\StudentNumber";
      document.forms[0].txtUserName.focus();
      isValid = false;
    }
  }
  return isValid;
}

function LoginClick()
{
  clearError();
  if (Validate())
  {
    var pwdandslt = document.forms[0].txtPassword.value + allObjects.login.salt;
    var username = document.forms[0].txtUserName.value;
    username = username.toUpperCase();
    allObjects.login.password = hex_sha1(pwdandslt);
    if (username.substring(0,7) != "COLLEGE")
    {
            username = "COLLEGE\\" + document.forms[0].txtUserName.value;
    }
    allObjects.login.user = username;
    getServerMessageField().value = JSON.stringify(allObjects);
    document.forms[0].txtPassword.value = "";
    return true;
  }
  else
  {
    displayError();
    return false;
  }
}
function addLogin()
{
  var lBody = document.getElementById("loginBody");
  var newR, newC;
  for (var i = 0; i < 5; i++)
  {
    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 = "<input name='txtUserName' type='text' size='15'/>";
               break;
      case 2: newC.innerHTML = "Password:";
               newC.className = "Caption";
               break;
      case 3: newC.innerHTML = "<input name='txtPassword' type='password' size='15'/>";
               break;
      case 4: newC.innerHTML = "<input type='submit' class='Button' value='Login'/>";
               break;
    }
  }
}
