﻿
// Get Form Element Value by ID [Currently Not Used]
GetLoginElement = function(id){
    if(document.all && !document.getElementById) {
        document.getElementById = function(id){
            return document.all[id];
        }
    }
    
    return document.getElementById(id);
}

// Makes Login Button Default Button if form is used
var __LoginFired = false;
function FireLoginButton(event, target) {
    if (!__LoginFired && event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) {
        var defaultButton = document.getElementById(target);
        if (defaultButton && typeof(defaultButton.click) != "undefined") {
            __LoginFired = true;
            defaultButton.click();
            event.cancelBubble = true;
            if (event.stopPropagation) event.stopPropagation();
            return false;
        }
    }
    return true;
}

// validates form is filled out
function ValidateGlobalLogin() 
{
    // Set global references to form elements for use in all JavaScript
    UsernameTextBox = document.forms[0].globalLoginUsername;
    PasswordTextBox = document.forms[0].globalLoginPassword;
    LoginButton = document.forms[0].globalLoginButton;
    
    if(UsernameTextBox.value == "" || PasswordTextBox.value == "")
    {
        alert("Please enter a valid username and password");
        __LoginFired = false
        return false;
    }    
    return true;
}

// response from GetSaltValue; hashes password text box; calls ValidateUser
function GlobalLogin_ClickResponse(result,context)
{
    //alert("GlobalLogin_ClickResponse:"+ result);
    var Salt = result;
    PassHash = b64_sha1(result + PasswordTextBox.value);
    
    PasswordTextBox.value = PassHash;
    
    // Due to a bug in MS __PendingCallBack method, we must set a short timeout
    setTimeout("ValidateUser()",100);
}

// Response (custid) from ValidateUser; At this point validation failed
function ValidateUserResponse(result,context)
{
    // Validation Failed
    if(result == "-1"){
        __LoginFired = false
        alert("You have entered an incorrect username and password \nor your access is denied. \n\nPlease try again."); 
        HideProgress();
    }
        
    // Validation Success: Redirect User
    var redirectFlag = "redirect=";
    if(result.indexOf(redirectFlag) > -1)
    {
        ShowProgress(redirectingText);
        window.location.href = result.substring(redirectFlag.length,result.length);
    }
        
    // Clear Password TextBox
    PasswordTextBox.value = "";       
}

function ShowProgress(Message)
{
    var loginform = GetLoginElement("GlobalLoginFormDiv");
    var progress = GetLoginElement("ProgressDiv");
    loginform.style.visibility = "hidden";
    loginform.style.display = "none";
    progress.style.visibility = "visible";
    progress.style.display = "block";
    progress.innerHTML = Message;
}

function HideProgress()
{
    var loginform = GetLoginElement("GlobalLoginFormDiv");
    var progress = GetLoginElement("ProgressDiv");
    progress.style.visibility = "hidden";
    progress.style.display = "none";
    loginform.style.visibility = "visible";
    loginform.style.display = "block";
}
            
         