var CheckForSemiColon = /^[^:]+$/;
var CheckForZip = /^(\d{5}-\d{4}|\d{5}|\d{9})$|^([a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d)$/;
//var CheckForPhone = /^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/;
var CheckForPhone = /^[0-9]+[\s.-]*$/;
var CheckForEmail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
var CheckForPnumber = /^[0-9]+[\s.-]*$/;
var CheckForCsvExt = /[.][c|C][s|S][v|V]$/;
var CheckForGifExt = /[.][g|G][i|I][f|F]$/;
var CheckForHTML = /(<[\S]*)$/;
var CheckForSpecialChars = /^[^)\]\[%#&|=('":]+$/;
var CheckForAlphabets = /^[a-zA-Z\s]+$/;
var CheckForAlphaNumeric= /^[a-zA-Z0-9]+$/;
var CheckForNumeric= /^[0-9]+$/;
var CheckForUsername = /^\w{5,12}$/;
//var CheckForPassword = /^([a-zA-Z0-9]*[!@#$%^&+*_]+){6,12}/;
var CheckForPassword = /^.*(?=.{7,12})(?=.*[a-zA-Z0-9])(?=.*[!@#$%^&+*_=-]).*$/;
var CheckForText = /^[a-zA-Z0-9!@#$%^&+'*-_=\s]+$/;
//var CheckForPassword = /^.*(?=.{7,})(?=.*[a-zA-Z])(?=.*[!@#$%^&+*_=-]).*$/;

function ValidateTextField(txtFld, displayName, isRequired, regEx)
{
    if (txtFld.value == "" && isRequired)
    {
        displayName = displayName.replace("$","");
        alert("Please enter "+ displayName + ".");
        txtFld.focus();
        return false;
    }
    
    if (txtFld.value != "" && regEx != "" && !regEx.test(txtFld.value))
    {
        switch (displayName)
        {
            case "$Username":
                message = "Please enter a valid user name.\nIt should be between five (5) and twelve (12) alphanumeric characters in length.";
                break;
            case "$Password":
                message = "Please enter a valid password.\nIt should be between six (6) and twelve (12) valid characters in length.";
                break; 
            case "$Email":
                message = "The email address is invalid.\nPlease ensure it is formatted as user@domain.com.";
                break; 
            case "$Phone":
                message = "Please enter a valid phone number";
                break; 
            case "$Zip":
                message = "The postal code is invalid.\nPlease ensure it is formatted as XXXXX or XXXXXX.";
                break;
            default:
                message = "Please enter a valid " + displayName + ".";
                break;
        }
        
        alert(message);
        txtFld.focus();
        return false;
    }      
    
    return true;  
}
function ValidateComboField(comboFld, displayName,isRequired)
{
    if (comboFld.selectedIndex == 0 && isRequired)
    {
        alert("Choose a " + displayName + ".");
        comboFld.focus();
        return false;
    }
    
    return true;  
}

function ValidateForm(formId)
{

    var form = document.getElementById(formId);
    if (form)
    {
        var controlArray = form.getElementsByTagName("*");
     
        for (i = 0; i < controlArray.length; i++)
        {
            control = controlArray[i];
            
            switch (control.nodeName)
            {
                case "INPUT":
                    if (control.getAttribute("customType") && control.getAttribute("customType") != "undefined")
                    {
                        switch(control.getAttribute("customType"))
                        {
                            case "text":
                                regEx = CheckForAlphabets;
                                break;
                            case "richtext":
                                regEx = CheckForText;
                                break;
                            case "number":
                                regEx = CheckForNumeric;
                                break;
                            case "alphanumeric":
                                regEx = CheckForAlphaNumeric;
                                break;
                            case "email":
                                regEx = CheckForEmail;
                                break;
                            case "username":
                                regEx = CheckForUsername;
                                break;
                            case "password":
                                regEx = CheckForPassword;
                                break;
                            case "zip":
                                regEx = CheckForPnumber;
                                break;
                            case "phone":
                                regEx = CheckForPhone;
                                break;
                            default:
                                regEx = "";
                                break;
                        }
                        
                        isRequired = false;
                        if (control.getAttribute("isRequired") && control.getAttribute("isRequired") != "undefined" && control.getAttribute("isRequired") == "true")
                            isRequired = true;
                            
                        if(!ValidateTextField(control, control.getAttribute("displayName"), isRequired, regEx))
                            return false;
                        
                        if (control.getAttribute("length") && IsInteger(control.getAttribute("length")))
                        {
                            var length = parseInt(control.getAttribute("length"));
                            if (control.value.length > length)
                            {
                                alert(control.getAttribute("displayName") + " should be less than " + control.getAttribute("length") + " characters in length.");
                                return false;
                            }
                        }
                    }
                    break;
                case "SELECT":
                    control = controlArray[i];
                    
                     isRequired = false;
                     if (control.getAttribute("isRequired") && control.getAttribute("isRequired") != "undefined" && control.getAttribute("isRequired") == "true")
                            isRequired = true;
                            
                    if (control.getAttribute("customType") && control.getAttribute("customType") != "undefined")
                    {
                        if(!ValidateComboField(control, control.getAttribute("displayName")))
                            return false;
                    }
                    break;
                case "TEXTAREA":
                    if (control.getAttribute("isRequired") && control.getAttribute("isRequired") == "true")
                    {                    
                        if (control.value == null || control.value =='')
                        {
                            alert("Please enter " + control.getAttribute("displayname")+ ".");
                            return false;
                        }
                        if(CheckForHTML.test(control.value))
                        {
                            alert("Please enter a valid " + control.getAttribute("displayname") + ".");
                            return false;
                        }
                    }
                    break;
            }
        }
    }   
    return true;
}

function IsInteger(s) 
{
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function ResetStateCombo(stateId, countryId)
{
    country = document.getElementById(countryId);
    state = document.getElementById(stateId);
    
    if(country.options[country.selectedIndex].value != "US")
        state.selectedIndex = 0;
    
    return false;
}