﻿var oldValidatorUpdateDisplay;
var Page_HighlightValidators = new Array();
var Page_HighlightValidatorsSummaryByControl = new Array();

function newValidatorUpdateDisplay(val)
{
    if (typeof (tinyMCE) != 'undefined')
    {
        tinyMCE.triggerSave(true, true);
    }
    // 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);
            
            // add class and change tooltip
            
            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])
                {
                    if (!Page_HighlightValidatorsSummaryByControl[controlID][i].isvalid) hasErrors = true;
                }
                
                if (!hasErrors)
                {
                    RemoveClassName(control, errorClassName);
                    if (control.title != null)
                    {
                        control.title = "";
                    }
                }
                
                if (!val.isvalid)
                {
                    //alert(controlID);
                    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)");
                }
            }
        }
    }
}

//for tests
function ForceHighlightValidateList(valList) {
	var arr = valList.split(',');
	
	for (var i = 0; i < arr.length; ++i) {
		var val = document.getElementById(arr[i]);
		ForceHighlightValidate(val);
	}
}
//for tests
function ForceHighlightValidate(val) {

	alert(val.id);

	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) {
			alert('finded');
			alert(controlID);
			var control = document.getElementById(controlID);
			alert('getted');

			// add class and change tooltip

			alert(errorClassName);
			if (errorClassName != "") {
				alert('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]) {
					if (!Page_HighlightValidatorsSummaryByControl[controlID][i].isvalid) hasErrors = true;
				}

				if (!hasErrors) {
					alert('!hasErrors');
					RemoveClassName(control, errorClassName);
					if (control.title != null) {
						control.title = "";
					}
				}

				if (!val.isvalid) {
					alert('!val.isvalid');
					alert(control.className);
					AddClassName(control, errorClassName);
					alert(control.className);
					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;
				alert('sum');
			}

			// 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);
    elem.isError = true;
}

function RemoveClassName (elem, className) {
    elem.className = trim(elem.className.replace(className, ""));
    elem.isError = false;
}

window.onload = function() {
	// hijack ValidatorUpdateDisplay() method
	if (typeof (ValidatorUpdateDisplay) != 'undefined') {
		oldValidatorUpdateDisplay = ValidatorUpdateDisplay;
		ValidatorUpdateDisplay = newValidatorUpdateDisplay;
	}
}
