var CheckForSemiColon = /^[^:]+$/;
var CheckForZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
var CheckForEmail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
var CheckForPnumber = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
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= /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
var CheckForUsername = /^\w{5,12}$/;
var CheckForPassword = /^[a-zA-Z0-9!@#$%^&+*_]{6,12}$/;
var CheckForDate = /^.*(?=.{7,})(?=.*[a-zA-Z])(?=.*[!@#$%^&+*_=-]).*$/;
var CheckForText = /^[a-zA-Z0-9!@#$%^&+'*-_=\s]+$/;
var CheckForPhone = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;


function ChkBoxListIsSelected(chkBoxListId)
{
    chkBoxList = document.getElementById(chkBoxListId);
    
    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, defaultText)
{
    if ((txtFld.value == "" || txtFld.value == defaultText) && isRequired)
    {
        displayName = displayName.replace("$","");
        alert(displayName + " is required.\nPlease enter a valid value.");
        txtFld.focus();
        return false;
    }
    
    if (txtFld.value != "" && regEx != "" && !regEx.test(txtFld.value))
    {
        alert(displayName + " is not valid.\nPlease enter a valid value.");
        txtFld.focus();
        return false;
    }      
    
    return true;  
}
function ValidateComboField(comboFld, displayName)
{
    if (comboFld.selectedIndex == 0)
    {
        alert(displayName + " is required.\nPlease select a valid value.");
        comboFld.focus();
        return false;
    }
    
    return true;  
}

function ValidateListField(listFld, displayName)
{
    var checked = false;
    var listItems = control.getElementsByTagName("INPUT");
    for (var j = 0; j < listItems.length; j++)
    {
        if (listItems[j].checked)
        {
            checked = true;
            break;
        }
    }
    
    if (!checked)
    {
        alert(displayName + " is required.  Please choose a valid value.");
        listFld.focus();
        return false;
    }
    
    
    return true;  
}

function Validate_Form(controlIds)
{
    var controls = controlIds.split(',');
    
    for (i = 0; i < controls.length; i++)
    {
        control = document.getElementById(controls[i]); 
        if (control)
        {           
            switch (control.getAttribute("objectType").toLowerCase())
            {
                case "textbox":
                case "password":
                    if (control.getAttribute("customType"))
                    {
                        switch(control.getAttribute("customType").toLowerCase())
                        {
                            case "numeric":
                                regEx = CheckForNumeric;
                                break;
                            case "alphabetic":
                                regEx = CheckForAlphabets;
                                break;
                            case "alphanumeric":
                                regEx = CheckForAlphaNumeric;
                                break;
                            case "email":
                                regEx = CheckForEmail;
                                break;
                            case "zip":
                                regEx = CheckForZip;
                                break;
                            case "phone":
                                regEx = CheckForPhone;
                                break;                            
                            default:
                                regEx = "";
                                break;
                        }
                    }
                    
                    if (regEx == "" && control.getAttribute("regEx"))
                    {
                        regEx = control.getAttribute("regEx");
                        if (regEx.charAt(0) != "/")
                            regEx = "/" + regEx;
                        if (regEx.charAt(regEx.length - 1) != "/")
                            regEx = regEx + "/";
                        debugger
                        regEx = eval(regEx);
                    }
                    
                    isRequired = false;
                    if (control.getAttribute("required") && control.getAttribute("required").toLowerCase() == "true")
                        isRequired = true;
                        
                    if(!ValidateTextField(control, control.getAttribute("displayName"), isRequired, regEx, control.getAttribute("defaultText")))
                        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 "fileupload":
                case "datepicker":
                    dateControl = document.getElementById(controls[i] + "_txt");
                    isRequired = false;
                    if (control.getAttribute("required") && control.getAttribute("required").toLowerCase() == "true")
                        isRequired = true;
                    
                    regEx = "";   
                    if(!ValidateTextField(dateControl, control.getAttribute("displayName"), isRequired, regEx, control.getAttribute("defaultText")))
                        return false;
                                    
                    break;
                case "dropdown":
                    if (control.getAttribute("required") && control.getAttribute("required").toLowerCase() == "true")
                    {
                        if(!ValidateComboField(control, control.getAttribute("displayName")))
                            return false;
                    }
                    break;
                case "textarea":
                    if (control.getAttribute("required") && control.getAttribute("required").toLowerCase() == "true")
                    {                 
                        displayName = control.getAttribute("displayName");   
                        if (control.value == null || control.value == '' || control.value == control.getAttribute("defaultText"))
                        {
                            alert(displayName + " is required.\nPlease enter a valid value.");
                            return false;
                        }
                        if(CheckForHTML.test(control.value))
                        {
                            alert(displayName + " is not valid.\nPlease enter a valid value.");
                            return false;
                        }
                    }
                    break;
                case "radiobutton":
                case "checkbox":
                    if (control.getAttribute("required") && control.getAttribute("required").toLowerCase() == "true")
                    {
                        if(!ValidateListField(control, control.getAttribute("displayName")))
                            return false;
                    }
                    break;
                case "captcha":
                    var captchaTxt = document.getElementById(controls[i] + "_txt");
                    var captchaHdn = document.getElementById(controls[i] + "_hdn");
                    if (captchaTxt && captchaHdn && captchaTxt.value != captchaHdn.value)
                    {
                        alert("Please enter the exact text from the field.");
                        captchaTxt.focus();
                        return false;
                    }
                    break;
            }
        }
    }
       
    return true;
}

function IsInteger(s) 
{
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

