function LegalDisclaimerValidatorTip_ClientValidate(sender, args){        
    args.IsValid = (document.getElementById(chkLegalDisclaimerTipID).checked);
}

function LegalDisclaimerValidatorVideo_ClientValidate(sender, args){        
    args.IsValid = (document.getElementById(chkLegalDisclaimerVideoID).checked);
}

function submitForm(){
    var isValidForm = true;
    var Invalids = 0;
    
    var arrAllFields = $(".toBeValidated").get();
    for (var x = 0; x < arrAllFields.length; x++){
        if(arrAllFields[x].className.indexOf("tickWrap") != -1){
            isValidForm = false;
            Invalids++;
        }
    }
        
    var arrChkBxs = $(".validateThisChkBx > INPUT").get();
    for(var i = 0; i < arrChkBxs.length; i++){
        if(arrChkBxs[i].checked != true){
            isValidForm = false;
            Invalids++;
            $('SPAN.errMsg').show();
        } else {
            $('SPAN.errMsg').hide();
        }   
    }    
        
    if(isValidForm){
        var arrButtons = document.getElementById('hiddenButton').getElementsByTagName('INPUT');
        arrButtons[0].click();
    } else {
        forceValidation(true);
        $('div.errorMsg').show();
        var a = document.getElementById("submittiptop");
        if (a.offsetParent.offsetParent) {
            var offset = (a.offsetTop + a.offsetParent.offsetTop + a.offsetParent.offsetParent.offsetTop + a.offsetParent.offsetParent.offsetParent.offsetTop + a.offsetParent.offsetParent.offsetParent.offsetParent.offsetTop);
        } else {
            var offset = (a.offsetTop + a.offsetParent.offsetTop);
        }
        window.scrollTo(0, offset);
    }
}    
       
function ValidateThis(vInput, vType, compareID, validateEmpty) {
    if (validateEmpty == undefined) validateEmpty = true;
    if(vType == 'text'){        
        if(TrimString(vInput.value) == ""){
            vInput.parentNode.className = 'toBeValidated tickWrap';
        } else {
            vInput.parentNode.className = 'toBeValidated tickValid';
        }
    }
    if(vType == 'select'){        
        if(vInput.selectedIndex == 0){
            vInput.parentNode.className = 'toBeValidated tickWrap';
        } else {
            vInput.parentNode.className = 'toBeValidated tickValid';
        }
    }
    if(vType == 'email'){                 
        if (!emailValidationRequired && vInput.value == "" && validateEmpty) {
            vInput.parentNode.className = 'toBeValidated tickValid';
        } else if(isValidEmailAdd(vInput.value)){
            vInput.parentNode.className = 'toBeValidated tickValid';
        } else {
            vInput.parentNode.className = 'toBeValidated tickWrap';
        }
        // enable the compare email validation if necessary
        if (emailValidationRequired || vInput.value != "") {
            enableRequiredValidators(true);
        } else {
            enableRequiredValidators(false);
        }
        // revalidate the compare email address
        if (document.getElementById(confirmEmailAddress1)) {
            ValidateThis(document.getElementById(confirmEmailAddress1), "compare", 'EmailAdd', validateEmpty);
        } else if (document.getElementById(confirmEmailAddress2)) {
            ValidateThis(document.getElementById(confirmEmailAddress2), "compare", 'EmailAdd', validateEmpty);
        }
    }
    if(vType == 'compare'){            
        var arrInputs = ById(compareID).getElementsByTagName('INPUT');
        var CompareValue = arrInputs[0];
        if (!emailValidationRequired && vInput.value == "" && validateEmpty && CompareValue.value == "") {
            vInput.parentNode.className = 'toBeValidated tickValid';
        } else if(isValidEmailAdd(vInput.value)&& vInput.value == CompareValue.value && vInput.value != ""){
            vInput.parentNode.className = 'toBeValidated tickValid';
        } else {
            vInput.parentNode.className = 'toBeValidated tickWrap';
        }            
    } 
    if(vType == 'ytURL'){
        var txt = new RegExp(/http:\/\/www.youtube.com\/watch[\S]v=\S+/);
        if(txt.test(vInput.value)){
            vInput.parentNode.className = 'toBeValidated tickValid';
        } else {
            vInput.parentNode.className = 'toBeValidated tickWrap';
        }
    }
    if(vType == 'school' && validateEmpty) {
        vInput.parentNode.className = 'toBeValidated tickValid';
    }
}

function enableRequiredValidators(bEnable) {
    if (document.getElementById(emailRequiredValidator1) != null) {
        ValidatorEnable(document.getElementById(emailRequiredValidator1), bEnable);
    }
    if (document.getElementById(emailRequiredValidator2) != null) {
        ValidatorEnable(document.getElementById(emailRequiredValidator2), bEnable);
    }
}

function forceValidation(validateEmpty) {
    // check each field to make sure it is valid
    for (i = 0; i < valElems.length; i++) {
        if (document.getElementById(valElems[i].id) != null) {
            if (valElems[i].type == "text" || valElems[i].type == "textarea") {
                ValidateThis($("#" + valElems[i].id).get(0), "text", '', validateEmpty);
            } else if (valElems[i].type == "school") {
                ValidateThis($("#" + valElems[i].id).get(0), "school", '', validateEmpty);
            } else if (valElems[i].type == "select") {
                ValidateThis($("#" + valElems[i].id).get(0), "select", '', validateEmpty);
            } else if (valElems[i].type == "email") {
                ValidateThis($("#" + valElems[i].id).get(0), "email", '', validateEmpty);
            } else if (valElems[i].type == "compare") {
                ValidateThis($("#" + valElems[i].id).get(0), 'compare', 'EmailAdd', validateEmpty);
            } else if (valElems[i].type == "ytURL") {
                ValidateThis($("#" + valElems[i].id).get(0), 'ytURL', '', validateEmpty);
            }
        }
    }
}

$(document).ready(function() {
    $('div.toBeValidated').addClass("tickWrap");
    // if values are already valid in form then display green tick
    forceValidation(false);
    // enable client-side validation
    for (i = 0; i < valElems.length; i++) {
        var validators = valElems[i].validators.split(",");
        for (j = 0; j < validators.length; j++) {
            if (document.getElementById(validators[j]) != null) {
                if (emailValidationRequired || (!emailValidationRequired && validators[j].indexOf("EmailRequired") == -1)) {
                    ValidatorEnable(document.getElementById(validators[j]), true);
                }
            }
        }
    }
});
