﻿//var OriginalValidatorValidate = ValidatorValidate;

//ValidatorValidate = function(val, validationGroup, event) {
//    OriginalValidatorValidate(val, validationGroup, event);

//    //    debugger;

//    var fieldErrorElement;

//    if (val.controltovalidate) {
//        var controlToValidate = $("#" + val.controltovalidate);
//        fieldErrorElement = controlToValidate.parent();
//    } else {
//        var validator = $("#" + val.id);
//        fieldErrorElement = validator.parent().children().first().parent();
//    }
//    
//    
//    debugger;
//    if (val.isvalid) {
//    //only remove class if it's the same validator that set the class
//        fieldErrorElement.removeClass("fieldError");
//    } else {
//        //fieldErrorElement.focus();
//        fieldErrorElement.addClass("fieldError");
//    }
//};

//ValidatorCommonOnSubmit = function(a, b, c, d) {
//debugger;
//    ClearValidatorCallouts();
//    var result = SetValidatorCallouts();
//    return result;
//}
function ValidatorCommonOnSubmit() {
    var result = !Page_BlockSubmit;
    Page_BlockSubmit = false;
    ClearValidatorCallouts();
    return result;
}
ValidatorValidate = function(val, validationGroup, event) {
    val.isvalid = true;

    if ((typeof (val.enabled) == 'undefined' || val.enabled != false) && IsValidationGroupMatch(val, validationGroup)) {
        if (typeof (val.evaluationfunction) == 'function') {
            val.isvalid = val.evaluationfunction(val);
            if (!val.isvalid && Page_InvalidControlToBeFocused == null &&
             typeof (val.focusOnError) == 'string' && val.focusOnError == 't') {
                ValidatorSetFocus(val, event);
            }
        }
    }
    ClearValidatorCallouts();
    SetValidatorCallouts();
    ValidatorUpdateDisplay(val);
}

SetValidatorCallouts = function() {
    var i;
    var pageValid = true;
    for (i = 0; i < Page_Validators.length; i++) {
        var ctrl = $("#" + Page_Validators[i].controltovalidate);
        if (!Page_Validators[i].isvalid) {
            if (ctrl.length > 0) {
                if (pageValid)
                    ctrl.focus();
            }
            else {
                ctrl = $("#" + Page_Validators[i].id);
            }
            var firstElement = $(ctrl.prevAll()[ctrl.prevAll().length - 1]);
            // get the div
            firstElement.parent().addClass("fieldError");
            pageValid = false;
        }
    }
    return pageValid;
}
ClearValidatorCallouts = function() {
    var i;
    var invalidConrols = [];
    $("#debug").innerHTML = "";
    for (i = 0; i < Page_Validators.length; i++) {
        var ctrl = $("#" + Page_Validators[i].controltovalidate);
        if (ctrl.length == 0) {
            ctrl = $("#" + Page_Validators[i].id);
        }
        var firstElement = $(ctrl.prevAll()[ctrl.prevAll().length - 1]);
        // get the div
        firstElement.parent().removeClass("fieldError");
    }
}
