//-------------------------------------------------------------------//
// Function to trim whitespaces from the start and end of strings
//
// Parameters: str - the strig to be trimed
//
// Author: Steven Levithan
// Site: http://blog.stevenlevithan.com/archives/faster-trim-javascript
//-------------------------------------------------------------------//
function trim (str)
{
    var str = str.replace(/^\s\s*/, ''), ws = /\s/, i = str.length;
    while (ws.test(str.charAt(--i)));
    
    return str.slice(0, i + 1);
}

//-------------------------------------------------------------------//
// Function to validate the forms on the mortgage rate pages
//
// Variables: strErrorMessages - A string that will contain the errors
//                               to display.
//
//            arrErrorMessages - An array containing all the possible
//                               error messages for each input field.
//                               Key = input field id
//                               value = error message
//
// Parameters: strFormName - A string containing the name of the form
//                           whose inputs we want to validate
// 
// Output: If there are errors, it will display an alert box containing
//         all of the user's errors and it will return false to
//         ensure that the form doesn't submit.
//
//         If there are no errors, it will return true to ensure that
//         the form submits.
//-------------------------------------------------------------------//
function validateMortgageRates(strFormName)
{
    // variable to hold the error(s)
    var strErrorMessage = '';
    
    // array containing all input field ids and their error messages
    var arrErrorMessages = new Object();
    arrErrorMessages['c_score'] = "Please select your credit score.";
    arrErrorMessages['hloan'] = "Please select what is most important to you in a home loan.";
    arrErrorMessages['zip'] = "Please enter a valid zip code.";
    
    // loop thru each form element
    for(i=0; i < document.forms[strFormName].elements.length; i++)
    {
        // bol to contain whether the form element's value is valid
        var bolIsValid = true;
        
        // get the elements name + value
        var strElementName = trim(document.forms[strFormName].elements[i].name);
        var strElementValue = trim(document.forms[strFormName].elements[i].value);
        
        // loop thru the array containing the error messages for each
        // possible input field
        for(strInputName in arrErrorMessages)
        {
            // if the input's name equals the form element's name
            if(strInputName == strElementName)
            {
                // if the form element's value is empty
                if(strElementValue == "") { bolIsValid = false;  }
                
                // if the form element's name is 'zip' and its value is valid
                if(strElementName == 'zip' && bolIsValid === true)
                {
                    // check to see if its a valid 5 digit zip
                    var regex = /^\d{5}$/;
                    if(!regex.test(strElementValue)) { bolIsValid = false; }
                }
                
                // if the form element's value is invalid
                if(bolIsValid === false)
                {
                    // get the input's error message + append it to strErrorMessage
                    strErrorMessage += "\n - " + arrErrorMessages[strInputName];
                }
            }
        }
    }
    
    // if there are error messages, then display the messages
    // and dont submit the form
    if(trim(strErrorMessage) != "")
    {
        strErrorMessage = 'Invalid information entered.' + strErrorMessage;
        
        alert(strErrorMessage);
        return false;
    }
    
    // if there are no errors then return true and submit the form
    return true;
}
