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]+$/;

// CheckForHTML.test(value)

function ChkBoxListIsSelected(chkBoxList)
{    
    if (chkBoxList && chkBoxList.nodeName == "TABLE")
    {
        for (i = 0; i < chkBoxList.rows.length; i++)
        {
            for (j = 0; j < chkBoxList.rows[i].cells.length; j++)
            {
                tCol = chkBoxList.rows[i].cells[j];
                chkBox = tCol.childNodes[0];      
                if (chkBox != null && chkBox.nodeName == "INPUT" && chkBox.checked)
                {
                    return true;
                }       
            }
        }
    }
    return false;
}



function ValidateTextField(txtFld, displayName, isRequired, regEx)
{
    if (txtFld.value == "" && isRequired)
    {
        displayName = displayName.replace("$","");
        alert("Please enter " + displayName);
        txtFld.focus();
        return false;
    }
    if(regEx!="")
    {
        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)
    {
        alert("Choose a " + displayName + ".");
        comboFld.focus();
        return false;
    }
    
    return true;  
}


function ValidateForm(formId)
{
    var form = document.getElementById(formId);
    var regEx;
    
    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;
                            }
                        }
                    }
                    else{
                    	isRequired = false;
                    	regEx = "";
                        if (control.getAttribute("isRequired") && control.getAttribute("isRequired") != "undefined" && control.getAttribute("isRequired") == "true")
                            isRequired = true;
                            
                        if(!ValidateTextField(control, control.getAttribute("displayName"), isRequired, regEx))
                            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(control.getAttribute("displayname") + " field can not be blank.");
                            return false;
                        }
                        if(CheckForHTML.test(control.value))
                        {
                            alert("Please enter a valid " + control.getAttribute("displayname") + ".");
                            return false;
                        }
                    }
                    break;
                case "TABLE":
                    if (control.getAttribute("isRequired") && control.getAttribute("isRequired") == "true")
                    {
                        if (!ChkBoxListIsSelected(control))
                        {
                            alert("Please select " + control.getAttribute("displayname") + ".");
                            return false;
                        }
                    }
                    break;
            }
        }
    }
       
    return true;
}

function IsInteger(s) 
{
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}


