﻿function HackValidators(validatorErrorCssClass) {
    //Creating the global validation error css class variable
    document.validatorErrorCssClass = validatorErrorCssClass || "error";

    //Creating a function to validate the control
    ValidateControl = function (control, validationGroup) {
        control.isvalid = true;
        control.errormessages = [];
        for (var num1 = 0; num1 < control.Validators.length; num1++) {
            var val1 = control.Validators[num1];
            ValidatorValidate(val1, validationGroup, null);
            if (!val1.isvalid) {
                control.isvalid = false;
                control.errormessages.push(val1.errormessage);
            }
        }
        return control.isvalid;
    }

    //If page validators is defined twicking the validators and controls titles
    if (typeof (Page_Validators) != "undefined") {
        //Twicking the validators and controls title
        for (var num1 = 0; num1 < Page_Validators.length; num1++) {
            var val = Page_Validators[num1];
            if (val != null) {
                var valClient = val.clientvalidationfunction;
                if (valClient == undefined) {

                    //Setting span tooltip to the error message
                    val.setAttribute("title", val.errormessage);

                    //Getting the control to validate
                    var control = document.getElementById(val.controltovalidate);
                    //Copying the original title into a back up property
                    if (control != null) {
                        control.originalTitle = control.getAttribute("title") || "";
                    }
                }
            }
        }
    }

    //Checking if ValidatorUpdateDisplay exists
    if (typeof (ValidatorUpdateDisplay) != "undefined") {
        //Copy ValidatorUpdateDisplay to BaseValidatorUpdateDisplay
        BaseValidatorUpdateDisplay = ValidatorUpdateDisplay;

        //Replacing ValidatorUpdateDisplay
        ValidatorUpdateDisplay = function (val) {
            //Invoking original ValidatorUpdateDisplay function saved in BaseValidatorUpdateDisplay
            BaseValidatorUpdateDisplay(val);

            //Getting the cotrol to validate
            var el = document.getElementById(val.controltovalidate);

            if (el == null || el.Validators == undefined) {
                return;
            }

            //Getting the css class for the error
            var cssClass = val.ValidationErrorCssClass || val.getAttribute("validationErrorCssClass") || validatorErrorCssClass;

            //Checking if it has already the css class
            var flag1 = el.className.indexOf(cssClass) != -1;
            //Checking if all validators are valid
            var array1 = [];
            var flag2 = true;
            for (var num1 = 0; num1 < el.Validators.length; num1++) {
                var val1 = el.Validators[num1];
                if (!val1.isvalid) {
                    flag2 = false;
                    array1.push(val1.errormessage);
                }
            }
            if (control != null) {
                control.isvalid = flag2;
                control.errormessages = array1;
            }

            //If it is a checkbox or radio button using the parent element
            if (el.type == "checkbox" || el.type == "radio") el = el.parentNode;

            //Setting the title
            el.setAttribute("title", flag2 ? el.originalTitle : array1.join("\n"));

            //if it is valid and it has the css class remove it
            if (flag1 && flag2) {
                //Removing the css class
                el.className = el.className.replace(new RegExp("\\b" + cssClass + "\\b", "i"), "").replace(/^\s+|\s(?=\s+)|\s+$/m, "");
            }
            //if it is not valid and it has not the css class add it
            else if (!flag1 && !flag2) {
                //Adding the css class
                el.className += (el.className.length != 0 ? " " : "") + cssClass;
            }
        }
    }
}
