function AllControllersValidatorsIsValid(controllerObj) { 
    if (controllerObj!=null && controllerObj.Validators!=null) {
        var i;
        for (i = 0; i < controllerObj.Validators.length; i++) {
            var val = controllerObj.Validators[i];
            if (val && !val.isvalid ) { 
                return false;
            }    
        }
    }   
    return true;
}         


function GetValidationControllers(val) {
    //NOTE:
    if (typeof(Page_ValidationControllers) != "undefined")
        if ((typeof(val.Controllers) == "undefined") || val.Controllers==null)
        {
            var controllers = new Array();
            var i,j;
            for (i = 0; i < Page_ValidationControllers.length; i++) {
                for (j = 0; j < Page_ValidationControllers[i].Validators.length; j++) {
                    if(val == Page_ValidationControllers[i].Validators[j]) {
                        controllers.push(Page_ValidationControllers[i]);
                    }
                }    
            }
            val.Controllers = controllers;
       }
    return val.Controllers;
}
    
function SelectFirstInvalidTab(validationGroup) {    
    //we assume 1 tabstip on page
    if (typeof(Page_TabStrip) != "undefined" && Page_TabStrip != null) {
        selectedTabIsValid = true;
        selectedIndex = Page_TabStrip.SelectedIndex;
        if (Page_TabStrip.SelectedIndex != -1) {
            for (i = 0; i < Page_Validators.length; i++) {
                var val = Page_Validators[i];
                if (!val.isvalid && IsValidationGroupMatch(val, validationGroup) && !isNaN(val.TabIndex) && Number(val.TabIndex) == selectedIndex) {
                    selectedTabIsValid = false;
                    break;
                }  
            }  
        }  
        if (selectedTabIsValid) {
            for (i = 0; i < Page_Validators.length; i++) {
                var val = Page_Validators[i];
                if (!val.isvalid && IsValidationGroupMatch(val, validationGroup) && !isNaN(val.TabIndex)) {
                    try{Page_TabStrip.Tabs[Number(val.TabIndex)].Select();}catch(e){}
                    break;
                }  
            }  
        }
    }
}

    
//overrided functions from system.web.dll webuivalidation.js
function Page_ClientValidate(validationGroup) {
    Page_InvalidControlToBeFocused = null;
    if (typeof(Page_Validators) == "undefined") {
        return true;
    }
    var i;
    for (i = 0; i < Page_Validators.length; i++) {
        ValidatorValidate(Page_Validators[i], validationGroup, null);
    } 
    SelectFirstInvalidTab(validationGroup);
    ValidatorUpdateIsValid();
          
    ValidationSummaryOnSubmit(validationGroup);
    Page_BlockSubmit = !Page_IsValid;
    return Page_IsValid;
}

function UpdateFunctionDefault(isValid, errorStyle, errorCssClass, controlToUpdate) {    
    if (!isValid) {
        if (typeof(controlToUpdate.oldCssText) == "undefined") {
            controlToUpdate.oldCssText = controlToUpdate.style.cssText;
        }
        if (typeof(controlToUpdate.oldClassName) == "undefined") {
            controlToUpdate.oldClassName = controlToUpdate.className;
        }
        //
        newstyle = controlToUpdate.style.cssText + "; " + errorStyle;
        //eval("controlToUpdate.style.cssText='"+newstyle+"'");
        controlToUpdate.style.cssText = newstyle;
        
        newClassName = controlToUpdate.className + " " + errorCssClass;
        controlToUpdate.className = newClassName;
        //controlToUpdate.runtimeStyle.cssText = newstyle;
    }
    else {
        if (typeof(controlToUpdate.oldCssText) != "undefined") {
            controlToUpdate.style.cssText = controlToUpdate.oldCssText;

        }
        if (typeof(controlToUpdate.oldClassName) != "undefined") {
            controlToUpdate.className = controlToUpdate.oldClassName;

        }
    }      
}  

 function ValidatorUpdateDisplay(val) {
    
    if (typeof(Page_ValidationControllers) != "undefined") {
        var controllerArray = GetValidationControllers(val);
        
        var ind;
        for (ind = 0; ind < controllerArray.length; ind++) {         
            var controller = controllerArray[ind];
            var controlToUpdate = controller.ControlToUpdate;
            if ((typeof(controlToUpdate) != "undefined") && controlToUpdate!=null) {
                
                controllerIsValid = AllControllersValidatorsIsValid(controller);
                
                controllerElement = document.getElementById(controller.Id);
                if (controllerElement)
                {
					errorStyle =  controllerElement.style.cssText;
	                
					errorCssClass =  controllerElement.className;
					controller.UpdateFunction(controllerIsValid, errorStyle, errorCssClass, controlToUpdate);
                }
            }
        }
    }
    
    if (typeof(val.display) == "string") {
        if (val.display == "None") {
            return;
        }
        if (val.display == "Dynamic") {
            val.style.display = val.isvalid ? "none" : "inline";
            return;
        }
    }
    if ((navigator.userAgent.indexOf("Mac") > -1) &&
        (navigator.userAgent.indexOf("MSIE") > -1)) {
        val.style.display = "inline";
    }
    val.style.visibility = val.isvalid ? "hidden" : "visible";
}

function ValidationSummaryOnSubmit(validationGroup) {
    if (typeof(Page_ValidationSummaries) == "undefined")
        return;
    var summary, sums, s;
    for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
        summary = Page_ValidationSummaries[sums];
        summary.style.display = "none";
        if (!Page_IsValid && IsValidationGroupMatch(summary, validationGroup)) {
            var i;
            if (summary.showsummary != "False") {
                summary.style.display = "";
                if (typeof(summary.displaymode) != "string") {
                    summary.displaymode = "BulletList";
                }
                switch (summary.displaymode) {
                    case "List":
                        headerSep = "<br>";
                        first = "";
                        pre = "";
                        post = "<br>";
                        end = "";
                        break;
                    case "BulletList":
                    default:
                        headerSep = "";
                        first = "<ul>";
                        pre = "<li>";
                        post = "</li>";
                        end = "</ul>";
                        break;
                    case "SingleParagraph":
                        headerSep = " ";
                        first = "";
                        pre = "";
                        post = " ";
                        end = "<br>";
                        break;
                }
                s = "";
                if (typeof(summary.headertext) == "string") {
                    s += summary.headertext + headerSep;
                }
                s += first;
                for (i=0; i<Page_Validators.length; i++) {
                    if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {
                        s += pre + Page_Validators[i].errormessage + post;
                    }
                }
                s += end;
                summary.innerHTML = s;
                window.scrollTo(0,0);
            }
            if (summary.showmessagebox == "True") {
                s = "";
                if (typeof(summary.headertext) == "string") {
                    s += summary.headertext + "\r\n";
                }
                var lastValIndex = Page_Validators.length - 1;
                for (i=0; i<=lastValIndex; i++) {
                    if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {
                        switch (summary.displaymode) {
                            case "List":
                                s += Page_Validators[i].errormessage;
                                if (i < lastValIndex) {
                                    s += "\r\n";
                                }
                                break;
                            case "BulletList":
                            default:
                                s += "- " + Page_Validators[i].errormessage;
                                if (i < lastValIndex) {
                                    s += "\r\n";
                                }
                                break;
                            case "SingleParagraph":
                                s += Page_Validators[i].errormessage + " ";
                                break;
                        }
                    }
                }
                alert(s);
            }
            if (typeof(summary.ShowInStatusBar) != "undefined" && summary.ShowInStatusBar && typeof(SetErrorMessage) == "function") {
                SetErrorMessage(summary.ErrorMessage, false);
            }
        }
    }
 } 

