/*
 * Starfleet Legacy Alliance - slasims.com
 * 2007 Website Redesign
 * Copyright © 2007 Captain Warp
 * 
 * Good day to you, wanderer of code
 * Welcome to this design's humble abode.
 * Explore a bit and stay a while
 * And compare your browser to this file.
 * Courtesy always comes off best
 * In a place of learning when a guest.
 * Look around and touch and feel
 * But please do not dare to steal.
 *
 */

/* tells us whether the user has tried to submit before */
var userHasSubmitted = false;

/* configures the authentication form and hooks in appropriate methods */
function setUpAuthenticationForm() {
  /* fetch elements */
  var loginForm = document.getElementById("login_form");
  var submitButton = document.getElementById("login_submit");
  
  /* if those elements don't exist, fail and use plain authentication */
  if(!(loginForm && submitButton)) {
    return false;
  }
  
  /* create the password hash field */
  var loginPasswordHash = document.createElement("input");
  loginPasswordHash.id = "login_password_hash";
  loginPasswordHash.name = "login_password_hash";
  loginPasswordHash.type = "hidden";
  /* loginForm.insertBefore(loginPasswordHash, submitButton); */
  loginForm.appendChild(loginPasswordHash);
  
  /* set up the login event handler */
  loginForm.onsubmit = secureFormData;
}

/* secures the form data */
function secureFormData() {
  /* fetch elements */
  var loginForm = document.getElementById("login_form");
  var usernameField = document.getElementById("login_username");
  var passwordField = document.getElementById("login_password");
  var rememberField = document.getElementById("login_remember_me");
  var submitButton = document.getElementById("login_submit");
  var challengeKeyField = document.getElementById("login_challenge_key");
  var challengeField = document.getElementById("login_password_hash");
  
  /* if those elements don't exist, fail and use plain authentication */
  if(!(loginForm && usernameField && passwordField && rememberField && submitButton && challengeKeyField && challengeField)) {
    return false;
  }
  
  /* compute password hash */
  var computedPasswordHash = hex_sha1(passwordField.value);
  var computedPasswordHash = hex_sha1(computedPasswordHash + challengeKeyField.value);
  
  /* populate the challenge field and blank the password field to protect it against plaintext transmission */
  challengeField.value = computedPasswordHash;
  var passwordString = "";
  for(var i = 0; i < passwordField.value.length; i++) {
    passwordString += " ";
  }
  passwordField.value = passwordString;
  
  /* indicate that the user has submitted the form */
  userHasSubmitted = true;
  
  /* submit the form */
  return true;
}

/* DOM support check -- do nothing if these DOM methods are not supported */
if(document.createElement && document.getElementById) {
  /* use a different event listener for each modern browser */
  if(typeof window.addEventListener != "undefined") {
    // for Gecko, KHTML, and WebKit
    window.addEventListener("load", setUpAuthenticationForm, false);
  } else if(typeof document.addEventListener != "undefined") {
    // for Opera 7
    document.addEventListener("load", setUpAuthenticationForm, false);
  } else if(typeof window.attachEvent != "undefined") {
    // for IE on Windows
    window.attachEvent("onload", setUpAuthenticationForm);
  }
}
