﻿function DisableValidators(flag)
{
    try {
        if (Page_Validators) {
            for (i = 0; i < Page_Validators.length; i++) {
                Page_Validators[i].enabled = flag;
            }
        }
    }
    catch(err) {
    
    }
}

function EnableVal()
{
    DisableValidators(true);
    Page_ClientValidate();  
    if(!Page_IsValid)
    {
        DisableValidators(false);
    }
    return Page_IsValid;
}

// Change the label with class "errorlabel" before a validated object to red when not valid.

$(document).ready(function(e) {
    $("span.validation-error")
        .bind("DOMAttrModified propertychange", function(e) {
            // Exit early if IE because it throws this event lots more  
            if (e.originalEvent.propertyName && e.originalEvent.propertyName != "isvalid") return;

            var controlToValidate = $("#" + this.controltovalidate);
            var validators = controlToValidate.attr("Validators");
            if (validators == null) return;

            var isValid = true;
            $(validators).each(function() {
                if (this.isvalid !== true) {
                    isValid = false;
                }
            });

            var control = controlToValidate.prevAll('.errorlabel');
            if (!control.length)
                control = controlToValidate.parent().prevAll('.errorlabel');
            if (!control.length)
                control = controlToValidate.parent().parent().prevAll('.errorlabel');
            
            if (control.length) {
                if (isValid) {
                    $(control[0]).removeClass("formerrorlabel");
                } else {
                    $(control[0]).addClass("formerrorlabel");
                }
            }
        });
});  
