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 = /^\d{5,6}$/;
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,})(?=.*[a-zA-Z])(?=.*[!@#$%^&+*_=-]).*$/;
var CheckForText = /^[a-zA-Z0-9!@#$%^&+'*-_=\s]+$/;

// CheckForHTML.test(value)

function ValidateGlobalSearch(radio1, radio2)
{
    if (txtSearch)
    {
        clicked = true;
        if (txtSearch.value == '' || txtSearch.value == txtSearchText)
        {
            alert("Please enter a search term.");
            txtSearch.focus();
            return false;
        }
        if (CheckForHTML.test(txtSearch.value) || !CheckForSpecialChars.test(txtSearch.value))
        {
            alert("A potentially dangerous value was detected from the keyword field.\nPlease enter valid keyword.");		
            txtSearch.focus();
            return false;
        }
    }
    
    r1 = document.getElementById(radio1);
    r2 = document.getElementById(radio2);
    
    if (r1 && r2)
    {
        if (!r1.checked && !r2.checked)
        {
            alert("Please choose either research or entire site.");
            return false;
        }
    }
    
    
    return true;
}

function ValidateBrowseResearch(txtKeywordId, ddlDateId)
{
    txtKeyword = document.getElementById(txtKeywordId);
    ddlDate = document.getElementById(ddlDateId);
    if (txtKeyword)
    {
        if(txtKeyword.value != '' && (CheckForHTML.test(txtKeyword.value) || !CheckForSpecialChars.test(txtKeyword.value)))
        {
            alert("A potentially dangerous value was detected from the keyword field.\nPlease enter valid keyword.");		
            txtKeyword.focus();
            return false;
        }
        
        if(ddlDate)
        {
            if (txtKeyword.value == '' && ddlDate.options[ddlDate.selectedIndex].value == "")
            {
                alert("Enter atleast one search criteria.");
                txtKeyword.focus();
                return false;
            }
        }        
    }
    
    return true;
}

function ValidateSearchCriteria(txtKeywordId)
{
    txtKeyword = document.getElementById(txtKeywordId);
    if (txtKeyword)
    {
        if(txtKeyword.value != '' && (CheckForHTML.test(txtKeyword.value) || !CheckForSpecialChars.test(txtKeyword.value)))
        {
            alert("A potentially dangerous value was detected from the keyword field.\nPlease enter valid keyword.");		
            txtKeyword.focus();
            return false;
        }
    }
    
    return true;
}

function ValidateAdvancedResearch(txtKeywordId, txtStartDateId, txtEndDateId, chkIndustryId, chkRegionId, chkTopicId, chkAuthorId, chlTypeId)
{
    txtKeyword = document.getElementById(txtKeywordId);
    isSelected = false;
    if (txtKeyword)
    {
        if(txtKeyword.value != '' && (CheckForHTML.test(txtKeyword.value) || !CheckForSpecialChars.test(txtKeyword.value)))
        {
            alert("A potentially dangerous value was detected from the keyword field.\nPlease enter valid keyword.");		
            txtKeyword.focus();
            return false;
        }
        
        if(txtKeyword.value != '')
            isSelected = true;          
    }
    
    if (!isSelected)
    {
        txtStartDate = document.getElementById(txtStartDateId);
        txtEndDate = document.getElementById(txtEndDateId);
        
        if (txtStartDate && txtStartDate.value != '' && txtEndDate && txtEndDate.value != '')
            isSelected = true;
    }
    
    if (!isSelected)
        isSelected = ChkBoxListIsSelected(chkIndustryId) || ChkBoxListIsSelected(chkRegionId) || ChkBoxListIsSelected(chkTopicId) || ChkBoxListIsSelected(chkAuthorId) || ChkBoxListIsSelected(chlTypeId) 
    
    if (!isSelected)
    {
        alert("Enter atleast one search criteria.");
        txtKeyword.focus();
        return false;
    }
    
    return true;
}

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 ValidateLogin(txtUserNameId, txtPasswordId)
{
    txtUserName = document.getElementById(txtUserNameId);
    txtPassword = document.getElementById(txtPasswordId);
    
    if (txtUserName && txtPassword)
    {
        if(txtUserName.value == '')
        {
            alert("Enter username.");
            txtUserName.focus();
            return false;
        }
        else if (txtUserName.value.indexOf(' ') > -1)
        {
            alert("Space is not allowed in username.\nPlease enter valid username.");		
            txtUserName.focus();
            return false;
        }
        else if(!CheckForText.test(txtUserName.value))
        {
            alert("A potentially dangerous value was detected from the username field.\nPlease enter valid username.");		
            txtUserName.focus();
            return false;
        }
        if(txtPassword.value == '')
        {
            alert("Enter password.");
            txtPassword.focus();
            return false;
        }
        else if(CheckForHTML.test(txtPassword.value))
        {
            alert("A potentially dangerous value was detected from the password field.\nPlease enter valid password.");		
            txtPassword.focus();
            return false;
        }
        
        return true;    
    }   
}

function ValidateLostPassword(txtUserNameId)
{
    txtUserName = document.getElementById(txtUserNameId);
    
    if (txtUserName)
    {
        if(txtUserName.value == '')
        {
            alert("Enter username.");
            txtUserName.focus();
            return false;
        }
        else if (txtUserName.value.indexOf(' ') > -1)
        {
            alert("Space is not allowed in username.\nPlease enter valid username.");		
            txtUserName.focus();
            return false;
        }
        else if(!CheckForText.test(txtUserName.value))
        {
            alert("A potentially dangerous value was detected from the username field.\nPlease enter valid username.");		
            txtUserName.focus();
            return false;
        }
        
        return true;
    }
}

function ValidateRegStep1(containerId, emailFldId, confirmEmailFldID)
{
    if(!ValidateForm(containerId))
        return false;
    
    var emailFld = document.getElementById(emailFldId);
    var confirmEmailFld = document.getElementById(confirmEmailFldID);
    
    if (emailFld.value != confirmEmailFld.value)
    {
        alert("The email ids you entered do not match. Please confirm the email");
        confirmEmailFld.focus();
        return false;
    }       
    
    return true;
}

function ValidateRegStep2(containerId, pwdFldId, confirmPwdFldId)
{
    if(!ValidateForm(containerId))
        return false;
    
    var pwdFld = document.getElementById(pwdFldId);
    var confirmPwdFld = document.getElementById(confirmPwdFldId);
    
    if (pwdFld.value != confirmPwdFld.value)
    {
        alert("The passwords you entered do not match. Please confirm the password");
        confirmPwdFld.focus();
        return false;
    }
    
    return true;
}

function ValidateUpdateProfile(containerId, emailFldId, confirmEmailFldID, pwdFldId, confirmPwdFldId)
{
    if(!ValidateForm(containerId))
        return false;
    
    var emailFld = document.getElementById(emailFldId);
    var confirmEmailFld = document.getElementById(confirmEmailFldID);
    var pwdFld = document.getElementById(pwdFldId);
    var confirmPwdFld = document.getElementById(confirmPwdFldId);
    
    if (emailFld.value != confirmEmailFld.value)
    {
        alert("The email ids you entered do not match. Please confirm the email");
        confirmEmailFld.focus();
        return false;
    }
    
    if (pwdFld.value != confirmPwdFld.value)
    {
        alert("The passwords you entered do not match. Please confirm the password");
        confirmPwdFld.focus();
        return false;
    }
    
    return true;
}

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)
{
    if (comboFld.selectedIndex == 0)
    {
        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("*");
     
        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];                    
                    if (control.style.display != "none" && 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;
}