//Begin Add Methods to the string object.
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); } //Add trim() function to chomp whitespace from the beginning and ending of the string
String.prototype.trimDashes = function() { return this.replace(/-/g, ''); } //Add trimDashses() function to chomp -'s from the string.

//Add isValidId() function to test the for a valid id pattern.
//NB: isValidId() tests for -'s but dashses are NOT a valid
//id pattern.  Use String.trimDashes() to remove dashes.
String.prototype.isValidId = function() {
    if(this.search(/^\d{3}-\d{2}-\d{4}$/) > -1)
    {
        return true;
    }

    if(this.search(/^\d{9}$/) > -1)
    {
        return true;
    }

    //A valid id pattern was not found.
    return false;        
};

//Add isValidEmail() function to check for an email-like pattern.
//NB: This is not a strong catch, but is suitable for our purposes.
String.prototype.isValidEmail = function() {
    //Fairly simple regular expression that checks for an email-like pattern.
    if(this.search(/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/) > -1)
    {
        return true;
    }
    
    //A valid email-like pattern was NOT found.
    return false;        
};

//var maintenancemode = true;
var maintenancemode = false;

//Check the form for input errors, warn the user and stop the processing of the form.             
function checkForm(form)
{
    var login = form.elements["LOGIN"]; //Gets the login control
    var password = form.elements["PASSWORD"]; //Gets the password control

/*
    if(true == true)
    {
        alert("There is a planned maintenance period during the weekend of March 21st through March 23rd.\nDuring this time period you may experience intermittent access to your account.");
    }
*/

    if(maintenancemode == true)
    {
        alert("We are currently in a maintenance cycle.  Your account should be available shortly."); 
        return false;
    }
    
    //Check for non-viable id sources.
    if(!login.value.isValidId() && !login.value.isValidEmail())
    {
        alert("Login Id must be a valid Social Security Number or Email Address.");
        return false;
    }
    
    //If the id source is an ssn, chomp the dashes.
    if(login.value.isValidId())
    {
        login.value = login.value.trimDashes();
    }
    
    //check to see if the password block is whitespace.
    //NB: Other password conditions exist, but are NOT
    //tested via client side javascript.
    //NB: If login.value.isValidId() == false then this
    //will not be reachable code until isValidId() == true.
    if(password.value.trim() == '')
    {
        alert("Password cannot be blank.");
        return false;
    }
    
    return true;
}

function swapLanguage(control)
{
    var enLogin = "Login";
    var enPassword = "Password";
    
    var spLogin = "Iniciar Sesión";
    var spPassword = "Contraseña";
    
    if(control.value == "en")
    {
        document.getElementById("spanLogin").innerText = enLogin;
        document.getElementById("spanPassword").innerText = enPassword;
        document.getElementById("submit").value = enLogin;		    
    }
    
    if(control.value == "sp")
    {
        document.getElementById("spanLogin").innerText = spLogin;
        document.getElementById("spanPassword").innerText = spPassword;
        document.getElementById("submit").value = spLogin;
    }
}
