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 CheckForEmail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
var CheckForPnumber = /^\d{5,6}$/;
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,})(?=.*[a-zA-Z])(?=.*[!@#$%^&+*_=-]).*$/;
var CheckForText = /^[a-zA-Z0-9!@#$%^&+'*-_=\s]+$/;


function ValidateTextField(txtFld, displayName, isRequired, regEx)
{
    if (txtFld.value == "" && isRequired)
    {
        switch(displayName)
        {
            case "Facility":
                message = "Please enter the square footage of your facility.";
                break;
            case "Select State":
                message = "Please select a state.";
                break;
            case "Your Name":
                message = "Please enter your name.";
                break;
            case "Title":
                message = "Please enter your title.";
                break;
            case "Company":
                message = "Please enter your company.";
                break;
            case "Email Address":
                message = "Please enter a valid email address.";
                break;
            default:
                message = "Please enter a valid "+ displayName + ".";
                break;
        }
        displayName = displayName.replace("$","");
        alert(message);
        txtFld.focus();
        return false;

    }
    if(txtFld.value == "" && !isRequired && txtFld.getAttribute("customType")=="number")
    {
        txtFld.value = "0";
    }
    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 = "The phone number is invalid.\nPlease ensure it is formatted as ZXX XXX XXXX or ZXXXXXXXXX or ZXX-XXX-XXXX.\nAll X's above represents 0-9 and all Z's represents 2-9.";
                break; 
            case "$Zip":
                message = "The postal code is invalid.\nPlease ensure it is formatted as XXXXX or XXXXXX.\n All X's above represents 0-9.";
                break;
            default:
                message = "Please enter a valid " + displayName + ".";
                break;
        }
        
        alert(message);
        txtFld.focus();
        return false;
    }      
    
    return true;  
}
function ValidateComboField(comboFld, displayName)
{
    if (comboFld.selectedIndex == 0)
    {
        if(displayName=="Select State"){
        //alert("combo");
        alert("Please select a state.");
        }
        else{
        alert("Please choose the " + displayName + ".");
        }
        comboFld.focus();
        return false;
    }
    
    return true;  
}


function ValidateForm(formId)
{
    var form = document.getElementById(formId);
    if (form)
    {
        var controlArray = form.getElementsByTagName("*");
        var ele, nat, fuel, lpg, step2;
        ele = "";
        nat = "";
        fuel = "";
        lpg = "";
        step2=0;
        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("displayName")=="Electric"){
                            step2 = 1;
                            ele = control.value;
                        }
                        if(control.getAttribute("displayName")=="Natural Gas"){
                            step2 = 1;
                            nat = control.value;
                        }
                        if(control.getAttribute("displayName")=="Fuel Oil"){
                            step2 = 1;
                            fuel = control.value;
                        }
                        if(control.getAttribute("displayName")=="LPG"){
                            step2 = 1;
                            lpg = control.value;
                        }
                        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];
                    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 == '')
                        {
                            alert("Please enter a valid " + control.getAttribute("displayname")+ ".");
                            return false;
                        }
                        if(CheckForHTML.test(control.value))
                        {
                            alert("Please enter a valid " + control.getAttribute("displayname") + ".");
                            return false;
                        }
                    }
                    break;
            }
        }
        
    }
    if(step2 == 1){
        if(ele=="0" && nat=="0" && fuel=="0" && lpg=="0"){
            alert("Please enter atleast one of the value in Electric, Natural Gas, Fuel Oil or LPG.");
            return false;
        }
    }
    //form.submit();   
    return true;
}

function IsInteger(s) 
{
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

