/*******************************************************************************
 *
 *  File Name.... loginPage.js
 *
 *  Written By... Alex Porven
 *  Created...... September 20, 2008
 *
 ******************************************************************************/

 var userid = "";
 var passwd = "";

////////////////////////////////////////////////////////////////////////////////
//                         FUNCTION:  CreateLoginPage                         //
////////////////////////////////////////////////////////////////////////////////

function CreateLoginPage() {

  // New Member Message
  var Txt = '<div class="NewMbrMsg">New Users: Start here by ' + 
            '<a href="http://www.auxedirectory.org/activate/index.php" '+
            'target="_blank">activating your account</a> to access the ' +
            'Directory.</div>';

  // Login Error Message Division
  Txt = Txt + '<div id="LoginError"></div>';

  // Start The Login Table
  Txt = Txt + '<table class="Login" align="center">';
  // Title
  Txt = Txt + '<tr><td class="Title" colspan="3">eDirectory and ' +
              'Non-Member Login</td></tr>';
  // ID Input
  Txt = Txt + '<tr><td class="RowLabel">Emp/User ID:</td>' +
              '<td><input id="userid" type="text" size="20" maxlength="20" ' +
              'onkeypress="return UserIDinput(event);" /></td></tr>';
  // Password Input
  Txt = Txt + '<tr><td class="RowLabel">Password:</td><td>' +
              '<input id="passwd" type="password" size="20" maxlength="10" ' +
              'onkeypress="return PasswordInput(event);" alt="passwd" /></td> ' +
              '<td id="PasswdState">(Hidden)</td></tr>';
  // Login Button
  Txt = Txt + '<tr><td class="LoginButton" colspan="3" align="center">' +
              '<input type="button" value="Login" onclick="ProcessLogin();">' +
              '</td></tr>';
  // Password Visibility Message
  Txt = Txt + '<tr><td id="PasswdMsg" colspan="3"' +
        'onmouseover="document.getElementById(\'PasswdMsg\').style.color=\'#0000FF\';"' +
        'onmouseout="document.getElementById(\'PasswdMsg\').style.color=\'#000000\';"' +
        'onclick="TogglePasswdVis();" >Make password input visible</td></tr>';

  // End of Login Table
  Txt = Txt + '</table>';

  //------------------------//
  // Display The Login Page //--------------------------------------------------
  //------------------------//

  document.getElementById("LoginPageContents").innerHTML = Txt;

}

////////////////////////////////////////////////////////////////////////////////
//                       FUNCTION: CreatePasswordUpdate                       //
////////////////////////////////////////////////////////////////////////////////

function CreatePasswordUpdate() {

  // Page Heading
  Txt = '<div class="PageTitle">Your Password Has Expired</div>';

  // Begin Password Change Table
  Txt = Txt + '<table class="Login" align="center">';
  // Table Heading Row
  Txt = Txt + '<tr><td class="Title" colspan="2">Enter A NEW Password</td></tr>';
  // Password Row
  Txt = Txt + '<tr><td class="RowLabel">Type Password:</td><td>' +
        '<input id="passwd1" type="password" size="20" maxlength="10" ' +
        'onkeypress="return Password1Input(event)" /></td></tr>';
  // Re-type Password Row
  Txt = Txt + '<tr><td class="RowLabel">Re-type Password:</td><td>' +
        '<input id="passwd2" type="password" size="20" maxlength="10" ' + 
        'onkeypress="return Password2Input(event)" />' +
        '</td></tr>';
  // Submit Button Row
  Txt = Txt + '<tr><td class="LoginButton" colspan="2">' +
        '<input type="button" value="Submit" ' +
        'onclick="UpdatePassword()"></td></tr>';
  // End of Password Change Table
  Txt = Txt + '</table>';

  // Error Message Division
  Txt = Txt + '<div id="LoginError"></div>';

  // Display The Password Change Table;
  document.getElementById("LoginPageContents").innerHTML = Txt;

  // Set the focus to the First Password Field
  document.getElementById("passwd1").focus();

}

////////////////////////////////////////////////////////////////////////////////
//                            FUNCTION:  PageSetup                            //
////////////////////////////////////////////////////////////////////////////////

function PageSetup() {

  CreateLoginPage();

  if ( navigator.appName == "Microsoft Internet Explorer" ) {
    document.getElementById("PasswdState").innerHTML = "";
    document.getElementById("PasswdMsg").innerHTML = "";
  }

  //------------------------//
  // Check For Login Errors //--------------------------------------------------
  //------------------------//

  loginError = GetCookie( "LoginError" );
  if ( loginError != null ) {
    DeleteCookie( "LoginError" );
    text = "Invalid ID/Password: ACCESS DENIED!";
    document.getElementById("LoginError").innerHTML = text;
  }

  //-------------------------------------//
  // Position the cursor in the ID Field //-------------------------------------
  //-------------------------------------//

  document.getElementById("userid").focus();

}

////////////////////////////////////////////////////////////////////////////////
//                          FUNCTION: Password1Input                          //
////////////////////////////////////////////////////////////////////////////////

function Password1Input( e ) {
  var keynum;
  if ( window.event ) keynum = e.keyCode;
  else if ( e.which ) keynum = e.which;
  if ( keynum == 13 ) document.getElementById("passwd2").focus();
  keychar = String.fromCharCode( keynum );
  return keychar;
}

////////////////////////////////////////////////////////////////////////////////
//                         FUNCTION: Password2Input                           //
////////////////////////////////////////////////////////////////////////////////

function Password2Input( e ) {
  var keynum;
  if ( window.event ) keynum = e.keyCode;
  else if ( e.which ) keynum = e.which;
  if ( keynum == 13 ) UpdatePassword();
  keychar = String.fromCharCode( keynum );
  return keychar;
}

////////////////////////////////////////////////////////////////////////////////
//                          FUNCTION:  PasswordInput                          //
////////////////////////////////////////////////////////////////////////////////

function PasswordInput( e ) {
  var keynum;
  if ( window.event ) keynum = e.keyCode;
  else if ( e.which ) keynum = e.which;
  if ( keynum == 13 ) ProcessLogin();
  keychar = String.fromCharCode( keynum );
  return keychar;
}

////////////////////////////////////////////////////////////////////////////////
//                           FUNCTION: ProcessLogin                           //
////////////////////////////////////////////////////////////////////////////////

function ProcessLogin() {

  //-----------------//
  // Local Variables //---------------------------------------------------------
  //-----------------//

  var pageRequest = false;
  var postData    = "";

  var objErrorMsg = document.getElementById("LoginError");
  objErrorMsg.innerHTML = "";

  //---------------------//
  // Get ID and Password //-----------------------------------------------------
  //---------------------//

  userid = document.getElementById("userid").value;
  passwd = document.getElementById("passwd").value;

  //------------------------//
  // Check for empty fields //--------------------------------------------------
  //------------------------//

  if ( userid == "" && passwd == "" ) {
    text = "ERROR: Both ID and Password Fileds MUST be filled in!";
    objErrorMsg.innerHTML = text;
    return false;
  } else if ( userid == "" ) {
    text = "ERROR: The ID Filed MUST be filled in!";
    objErrorMsg.innerHTML = text;
    return false;
  } else if ( passwd == "" ) {
    text = "ERROR: The Password Filed MUST be filled in!";
    objErrorMsg.innerHTML = text;
    return false;
  }

  //-------------------------------------------------//
  // Write the User ID and Password to the Post Data //-------------------------
  //-------------------------------------------------//

  postData = "User=" + userid + "||" + passwd;

  //-----------------------------//
  // Get The Page Request Object //---------------------------------------------
  //-----------------------------//

  if ( window.XMLHttpRequest ) pageRequest = new XMLHttpRequest();
  else if ( window.ActiveXObject ) pageRequest = new ActiveXObject("Microsoft.XMLHTTP");
  else {
    text = "ERROR: Your Browser  Cannot Generate A Login Request!<br />ACCESS DENIED";
    objErrorMsg.innerHTML = text;
    return false;
  }

  //--------------------------------//
  // Get The User Access Permission //------------------------------------------
  //--------------------------------//

  pageRequest.open('POST', 'utilities/Login.php', false);
  pageRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  pageRequest.send( postData );
  var reply = pageRequest.responseText;

  //------------------//
  // Test The Results //--------------------------------------------------------
  //------------------//

  if ( reply == null ) {
    text = "ERROR: Your Browser  Cannot Generate A Login Request!<br />ACCESS DENIED";
    objErrorMsg.innerHTML = text;
    return false;
  } else {
    var txtStat   = reply.split("||");
    var statusMsg = txtStat[1];
    var status    = txtStat[0];
        txtStat   = status.split("=");
    var statusNum = parseInt( txtStat[1] );
    var text = "";
    switch ( statusNum ) {
    case 0:
        var caller = GetCookie( 'Caller' );
        DeleteCookie( 'Caller' );
        window.open( caller, '_top' );
        return true;
        break;
    case -9:
        text = "Error Processing Login Request!";
        break;
    case -1:
        text = "ERROR: Could not connect to the database server!";
        //text = "ERROR: " + statusMsg;
        break;
    case -2:
        text = "ERROR: Could not connect to the database!";
        //text = "ERROR: " + statusMsg;
        break;
    case -3:
        text = "ERROR: Could not query the Non-Member Table.";
        //text = "ERROR: " + statusMsg;
        break;
    case 1:
        text = "ERROR: User ID/Password is NOT valid!";
        //text = "ERROR: User ID is NOT valid!";
        break;
    case 2:
        text = "ERROR: User ID/Password is NOT valid!";
        //text = "ERROR: User Password is NOT valid!";
        break;
    case 3:
        CreatePasswordUpdate();
        return false;
        break;
    }
    objErrorMsg.innerHTML = text;
    return false;
  }

}

////////////////////////////////////////////////////////////////////////////////
//                         FUNCTION:  TogglePasswdVis                         //
////////////////////////////////////////////////////////////////////////////////

function TogglePasswdVis() {

  if ( navigator.appName == "Microsoft Internet Explorer" ) return false;

  if ( document.getElementById("passwd").type == "password" ) {
    document.getElementById("passwd").type = "text";
    document.getElementById("PasswdState").innerHTML = "(Visible!)";
  } else {
    document.getElementById("passwd").type = "password";
    document.getElementById("PasswdState").innerHTML = "(Hidden)";
  }

}

////////////////////////////////////////////////////////////////////////////////
//                          FUNCTION: UpdatePassword                          //
////////////////////////////////////////////////////////////////////////////////

function UpdatePassword() {

  var objPasswd1 = document.getElementById("passwd1");
  var objPasswd2 = document.getElementById("passwd2");

  var objErrorMsg = document.getElementById("LoginError");
  objErrorMsg.innerHTML = "";

  var passwd1 = objPasswd1.value;
  var passwd2 = objPasswd2.value;

  //---------------------------------//
  // Check if password 1 and 2 match //-----------------------------------------
  //---------------------------------//
  
  if ( passwd1 != passwd2 ) {
    objErrorMsg.innerHTML = "ERROR: The Passwords DO NOT Match!";
    objPasswd1.value = "";
    objPasswd2.value = "";
    objPasswd1.focus();
    return;
  }

  //----------------------------------------------------//
  // Check if the new password matches the old password //----------------------
  //----------------------------------------------------//

  if ( passwd1 == passwd ) {
    objErrorMsg.innerHTML = "ERROR: You CANNOT Re-Use Your Old Password";
    objPasswd1.value = "";
    objPasswd2.value = "";
    objPasswd1.focus();
    return;
  }

  //-------------------------------------------------//
  // The Password MUST be at least 6 characters long //-------------------------
  //-------------------------------------------------//

  if ( passwd1.length < 6 ) {
    objErrorMsg.innerHTML = "ERROR: The New Password MUST be At Least 6 Characters Long!";
    objPasswd1.value = "";
    objPasswd2.value = "";
    objPasswd1.focus();
    return;
  }

  //-------------------------//
  // Create The Post Message //-------------------------------------------------
  //-------------------------//
  
  postData = "Data=" + userid + "||" + passwd1;

  //-----------------------------//
  // Get The Page Request Object //---------------------------------------------
  //-----------------------------//

  if ( window.XMLHttpRequest ) pageRequest = new XMLHttpRequest();
  else if ( window.ActiveXObject ) pageRequest = new ActiveXObject("Microsoft.XMLHTTP");
  else {
    text = "ERROR: Your Browser  Cannot Generate A Password Update Request!<br />ACCESS DENIED";
    document.getElementById('LoginError').innerHTML = text;
    return false;
  }

  //-------------------------//
  // Send The Update Request //-------------------------------------------------
  //-------------------------//

  pageRequest.open('POST', 'utilities/UpdatePassword.php', false);
  pageRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  pageRequest.send( postData );
  var reply = pageRequest.responseText;

  //------------------//
  // Test The Results //--------------------------------------------------------
  //------------------//

  if ( reply == null ) {
    text = "ERROR: Your Browser  Cannot Generate A Password Update Request!<br />ACCESS DENIED";
    document.getElementById('LoginError').innerHTML = text;
    return false;
  } else {
    var txtStat   = reply.split("||");
    var statusMsg = txtStat[1];
    var status    = txtStat[0];
        txtStat   = status.split("=");
    var statusNum = parseInt( txtStat[1] );
    var text = "";
    switch ( statusNum ) {
    case 0:
        var caller = GetCookie( 'Caller' );
        DeleteCookie( 'Caller' );
        window.open( caller, '_top' );
        return true;
        break;
    case -1:
        text = "ERROR: Could not connect to the database server!";
        //text = "ERROR: " + statusMsg;
        break;
    case -2:
        text = "ERROR: Could not connect to the database!";
        //text = "ERROR: " + statusMsg;
        break;
    case -3:
        text = "ERROR: Could not query the Non-Member Table.";
        //text = "ERROR: " + statusMsg;
        break;
    case 1:
        text = "ERROR: Your password could not be updated.";
        //text = "ERROR: " + statusMsg;
    default:
        var caller = GetCookie( 'Caller' );
        DeleteCookie( 'Caller' );
        window.open( caller, '_top' );
        return true;
    }
    objErrorMsg.innerHTML = text;
  }

}

////////////////////////////////////////////////////////////////////////////////
//                           FUNCTION:  UserIDinput                           //
////////////////////////////////////////////////////////////////////////////////

function UserIDinput( e ) {
  var keynum;
  if ( window.event ) keynum = e.keyCode;
  else if ( e.which ) keynum = e.which;
  if ( keynum == 13 ) document.getElementById("passwd").focus();
  keychar = String.fromCharCode( keynum );
  return keychar;
}

