﻿var oldValidatorUpdateDisplay;
var Page_HighlightValidators = new Array();
var Page_HighlightValidatorsSummaryByControl = new Array();
var Page_ValidationfocusSet = true;

function newValidatorUpdateDisplay(val)
{
    // call hijacked ValidatorUpdateDisplay() method
    oldValidatorUpdateDisplay(val);

    for (i = 0; i < Page_HighlightValidators.length; i++)
    {
        var validatorID = Page_HighlightValidators[i][0];
        var controlID = Page_HighlightValidators[i][1];
        var errorClassName = Page_HighlightValidators[i][2];
        var errorCallFunction = Page_HighlightValidators[i][3];
        
        if (val.id == validatorID && val.controltovalidate == controlID)
        {
            var control = document.getElementById(controlID);
//            if (!control)
//            {
//                document.title = controlID;
//            }
           
           if (control != null)
           {
                if (errorClassName != "")
                {
                    // remove error class name and title if no one of the previous validators
                    // with the same controltovalidate property wasn't failed
                    var hasErrors = false;                
                    for (i in Page_HighlightValidatorsSummaryByControl[controlID])
                    {    
                        var valid = Page_HighlightValidatorsSummaryByControl[controlID][i].isvalid;
                        if (valid != null) 
                        {
                            if (!valid)
                            {
                                hasErrors = true;
                            }
                        }
                    }
                    
                    if (!hasErrors)
                    {           
                        RemoveClassName(control, errorClassName);
                        if (control.title != null)
                        {
                            control.title = "";
                        }
                    }
                    
                    if (!val.isvalid)
                    {
                        AddClassName(control, errorClassName);
            
                        if (control.title != null && val.attributes.title != null)
                        {
                            control.title = val.attributes.title.value;
                        }
                    }
                    
                    if (typeof(Page_HighlightValidatorsSummaryByControl[controlID]) == "undefined")
                    {
                        Page_HighlightValidatorsSummaryByControl[controlID] = new Array();
                    }
                    
                    Page_HighlightValidatorsSummaryByControl[controlID][validatorID] = val;
                }
                
                // call custom function
                
                if (errorCallFunction != "" && eval("typeof("+errorCallFunction+")") == "function")
                {
                    if (!val.isvalid)
                    {
                        eval(errorCallFunction+"(val)");
                    }
                }
            }
        }
    }
}

function trim(str) {
    return str.replace(/^\s+|\s+$/, "");
}

function AddClassName (elem, className) 
{
    RemoveClassName(elem, className);
    elem.className = trim(elem.className + " " + className);
}

function RemoveClassName (elem, className) 
{
    if (elem)
    {
        elem.className = trim(elem.className.replace(className, ""));
    }
}

window.onload = function()
{
    // hijack ValidatorUpdateDisplay() method 
    if (window.ValidatorUpdateDisplay)
    {
        oldValidatorUpdateDisplay = ValidatorUpdateDisplay;
    }
    ValidatorUpdateDisplay = newValidatorUpdateDisplay;
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();