Type.registerNamespace('WebApi3.Web.UI.WebControls');

WebApi3.Web.UI.WebControls.ValidatorExtenderBehavior = function(element) {
    WebApi3.Web.UI.WebControls.ValidatorExtenderBehavior.initializeBase(this, [element]);

    // Define property variables
    this._SubmitButtonIDValue = null;
    this._DisabledTextValue = null;
    this._ErrorCssClassValue = null;
    this._originalValidationMethod = null;
    this._validationMethodOverride = null;
    this._elementToValidate = null;
    this._targetControl = null;
    this._invalid = null;
}

WebApi3.Web.UI.WebControls.ValidatorExtenderBehavior.prototype = {

    initialize : function() {
        WebApi3.Web.UI.WebControls.ValidatorExtenderBehavior.callBaseMethod(this, 'initialize');

        // Set references to the target control and the element that is to be validated
        var targetControl = this._targetControl = this.get_element();
        var elementToValidate = this._elementToValidate = $get(targetControl.controltovalidate);
        
        
        // Get the original evaluation function and override it to perform our custom behaviour
        if(targetControl.evaluationfunction) {
            this._originalValidationMethod = Function.createDelegate(targetControl, targetControl.evaluationfunction);
            this._validationMethodOverride = Function.createDelegate(this, this._onvalidate);
            // Unlink the default behaviour as this isn't called when user selects and unselects element without entering anything
            targetControl.evaluationfunction = null; // this._validationMethodOverride; -- uncomment to add default behaviour back
            // Add the handler to the onblur event and the submit buttons click event
            $addHandler(this._elementToValidate, 'blur', Function.createDelegate(this, this._onvalidate));    
            // Check if there is a submit button to attache the OnValidate method to as well
            if(this._SubmitButtonIDValue!=null) {
                $addHandler($get(this._SubmitButtonIDValue), 'click', Function.createDelegate(this, this._onvalidate));    
            }
        } 
    },
    
    dispose : function() {
        // Clear up        
        this._originalValidationMethod = null;
        this._validationMethodOverride = null;
        this._targetControl = null;
        this._elementToValidate = null;
        
        WebApi3.Web.UI.WebControls.ValidatorExtenderBehavior.callBaseMethod(this, 'dispose');
    },
    
    BubbleError : function(val) {    
                
        var objSummary;
        var objStatus;
        // Get the validation group
        var strValidationGroup = $get(this.get_element().id).validationGroup;
        // Find the ValidationSummary control
        for(i=0;i<Page_ValidationSummaries.length;i++)
        {
            if(Page_ValidationSummaries[i].validationGroup == strValidationGroup) { 
                objSummary = Page_ValidationSummaries[i];
                objStatus = objSummary.parentNode.parentNode.parentNode;
                break; 
            }
        }
        
        var errorCount = 0;
        var errorMessages = "";
        // Loop through each invalid validator in the given ValidationGroup and set the error message                
        for(i=0;i<Page_Validators.length;i++)
        {
                       
            if(Page_Validators[i].validationGroup == strValidationGroup && !Page_Validators[i].isvalid) { 
                errorMessages = errorMessages + "<li>" + Page_Validators[i].errormessage + "</li>";
                errorCount++;
            }
        }
        
        // SHow/hide the summary depending on whether there are error messages to show
        if(errorCount>0) 
        {
            if(objStatus.className.indexOf(" status",0) > -1)
            { 
                objStatus.style.display = ''; 
            }
            objSummary.style.display = ''; 
            objSummary.innerHTML = "<ul>" + errorMessages + "</ul>";                        
        }
        else
        {
            if(objStatus.className.indexOf(" status",0) > -1) 
            { 
                objStatus.style.display = 'none'; 
            }
            objSummary.style.display = 'none'; 
            objSummary.innerHTML = "";                        
        }
    },

    // Define the onvalidate function that is called when we want to validate the control
    _onvalidate : function(val) {
    
        // Format for use when called from onBlue event
        // alert(this._originalValidationMethod(this.get_element()));
        // Format for use when call from validators evaluationfunction event
        // alert(this._originalValidationMethod(var));
        
        if(this.get_element()!=null)
        {
            var elementToValidate = $get(this.get_element().controltovalidate);                    
            
            // Check if the content is valid
            if(!this._originalValidationMethod(this.get_element())) {
                // Not valid, change the css of the element and return false
                if(this._ErrorCssClassValue) 
                { 
                    Sys.UI.DomElement.addCssClass(elementToValidate, this._ErrorCssClassValue); 
                    this.get_element().isvalid = false; 
                    this._invalid = true;  
                }            
            } 
            else 
            {
                // valid, remove any error css's applied
                if(this._ErrorCssClassValue && this._invalid) 
                { 
                    Sys.UI.DomElement.removeCssClass(elementToValidate, this._ErrorCssClassValue) 
                    this.get_element().isvalid = true;
                    this._invalid = false; 
                }  
            }        
            // Bubble up any error messages
            this.BubbleError(this); 
                           
            return this.get_element().isvalid
        }
    },
    
    // Methods to get/set the ErrorCssClass property   
    get_ErrorCssClass : function() {
        return this._ErrorCssClassValue;
    },
    set_ErrorCssClass : function(value) {
        this._ErrorCssClassValue = value;
    },
       
    // Methods to get/set the SubmitButtonID property   
    get_SubmitButtonID : function() {
        return this._SubmitButtonIDValue;
    },
    set_SubmitButtonID : function(value) {
        this._SubmitButtonIDValue = value;
    }        
}

WebApi3.Web.UI.WebControls.ValidatorExtenderBehavior.registerClass('WebApi3.Web.UI.WebControls.ValidatorExtenderBehavior', AjaxControlToolkit.BehaviorBase);


