// COPYRIGHT (c) 2006 Mixon/Hill, Inc., ALL RIGHTS RESERVED.
//-----------------------------------------------------------
// SubLogin.js
//      Submits the user's username and password to the database
//      to see if it matches.  If it does, then establish the
//      Credentials for the user so it can be checked by all other pages.
//-----------------------------------------------------------


var nLastLogin = 0;

//-----------------------------------------------------------
// InitPageCode()
//      This is called when the HTML page is loaded.
//      Put all page initializing code here.
//-----------------------------------------------------------
function InitPageCode()
{
    setTimeout(OnLoad, 100);
}


function OnLoad()
{
    var oUserName = document.getElementById("userName");
    oUserName.focus();
}


//-----------------------------------------------------------
// Login()
//      Extract the username and password from the key/value collection
//      on the HTML page.
//      Submit the username and password to the database to see if the
//      username and password are a valid combination.
//-----------------------------------------------------------
function Login()
{
    var oXmlRequest = new XmlRequest();
    oXmlRequest.getXml("GetSession.jsp", cbGetSessionResponse);
}

function cbGetSessionResponse(oXml)
{
    var sPassword = new String(document.getElementById("password").value);
    sPassword = sPassword.trim();
    // Create the XmlHttpRequest object and send the request to the database
    var oXmlRequest = new XmlRequest();
    oXmlRequest.addParameter("userName", document.getElementById("userName").value);
    oXmlRequest.addParameter("password", sha256_digest(sPassword));
    oXmlRequest.addParameter("loginType", 4);
    oXmlRequest.getXml("Login.jsp", GetResponse);
}
//Trims extra white space from the password (AM 06/10/2010)
String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

//-----------------------------------------------------------
// GetResponse()
//      Process the response from the SQL call.
//-----------------------------------------------------------
function GetResponse(oXml)
{
    var oResult = oXml.documentElement;
    var sSessionId = oResult.getAttribute("sessionId");
    var oRows = oXml.documentElement.getElementsByTagName("row");

    // DEBUGGING ALERT!
    // Get the Session ID for debugging against IIS.
    // prompt("DEBUG: SessionID", "(S(" + oResult.getAttribute("sessionId") + "))");

    //---------------------------------------------------
    // There should only be 1 row returned from the SQL query.
    // Any other number of rows indicates a problem.
    //---------------------------------------------------
    if (oRows.length == 0)
    {
        alert("Invalid username/password");
    }
    else if (oRows.length == 1)
    {
        var oRow = oRows[0];

        // If the user is suspended, put up a message and go back
        // to the Login page.
        if (oRow.getAttribute("suspended") == "true")
        {
            alert("Your account access has been suspended.\nPlease contact Kansas City Scout at (816) 622-6500.");
            setTimeout(ExitSystem, 100);
            return;
        }
        
        // Store the last login time.
        nLastLogin = oRow.getAttribute("lastLogin");
            
        // If the user needs to change the password, then
        // pop up the ChangePassword page.
        if (oRow.getAttribute("changePassword") == 1)
        {
            alert("You must change your password before continuing.");
            window.popupClosing = popupClosing;
            window.forceChange = 1;
            window.open("MyProfilePassword.html?OpenForm", "new_win", "width=400, height=350");
        }
        else
        {
            setTimeout(DoSetupWizard, 100);
        }
    }
}


function ExitSystem()
{
    document.location = "SubLogin.html";
}


function DoSetupWizard()
{
    if (nLastLogin == 0)
    {
        document.location = "SubSetupServices.html";
    }
    else
    {
        UpdateLastLogin();
    }
}


function UpdateLastLogin()
{
    var oXmlRequest = new XmlRequest();
    oXmlRequest.getXml("SubUpdateLastLogin.jsp", cbUpdateResponse);
}


function cbUpdateResponse(oXml)
{
    var oResult = oXml.documentElement;
    var nRowsAffected = oResult.getAttribute("rowsAffected");

    // NOTE: What should we do if we couldn't update the last login time?
    setTimeout(FinishLogin, 100);
}


//-----------------------------------------------------
// returnOnEnter()
//      Makes the login work when the user presses enter 
//      on the password field 
//-----------------------------------------------------
function returnOnEnter(myfield, e)
{
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;

    // Calls the appropriate function if the user hits enter   
    if (key == 13) 
    {
        setTimeout(Login, 100);
        return(false);
    }
}


//---------------------------------------------------------
// popupClosing()
//      Called when a popup window is closing.
//      Because the child window is disappearing, we have to
//      set the timeout and do the desired actions after waiting
//      some length of time (e.g., 10 milliseconds).
//      This gives the child window time to disappear and then lets
//      us regain control of the processing.
//---------------------------------------------------------
function popupClosing()
{
    setTimeout(FinishLogin, 100);
}


//---------------------------------------------------------
// FinishLogin()
//      Continue to the next page.
//---------------------------------------------------------
function FinishLogin()
{
    document.location = "SubHome.html";
}
