// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.


/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference path="../ExtenderBase/BaseScripts.js" />
/// <reference path="../Common/Common.js" />
/// <reference path="../Compat/Timer/Timer.js" />
/// <reference path="../Animation/Animations.js" />
/// <reference path="../Animation/AnimationBehavior.js" />
/// <reference path="../PopupExtender/PopupBehavior.js" />


Type.registerNamespace('Evolutia');

Evolutia.validatorBlur = function AjaxControlToolkit$validatorBlur(element) {
    Evolutia.validatorBlur.initializeBase(this, [element]);
    
    this._warningIconImageUrl = null;
    this._closeImageUrl = null;
    this._highlightCssClass = null;
    this._width = "200px";
    this._invalid = false;
    this._originalValidationMethod = null;
    this._otherOriginalValidationMethod = null;
    this._validationMethodOverride = null;
    this._elementToValidate = null;
    this._popupTable = null;
    this._errorMessageCell = null;
    this._calloutArrowCell = null;
    this._warningIconImage = null;
    this._closeImage = null;
    this._popupBehavior = null;
    this._onShowJson = null;
    this._onHideJson = null;
    this._focusAttached = false;
    this._isOpen = false;
    this._isBuilt = false;
    this._divImage = null;
    this._focusHandler = Function.createDelegate(this, this._onfocus);
    this._blurHandler = Function.createDelegate(this, this._onblur);
    //this._closeClickHandler = Function.createDelegate(this, this._oncloseClick);
    this.otherValidator = null;
    this._otherValidator
}
Evolutia.validatorBlur.prototype = {
    initialize : function() {
        Evolutia.validatorBlur.callBaseMethod(this, 'initialize');
        var elt = this.get_element();
        //               
        // Override the evaluation method of the current validator
        //
        
        if(elt.evaluationfunction) {
            this._originalValidationMethod = Function.createDelegate(elt, elt.evaluationfunction);
            this._validationMethodOverride = Function.createDelegate(this, this._onvalidate);
            elt.evaluationfunction = this._validationMethodOverride;            
        }
       if (this.otherValidator != null) {
            this._otherValidator = $get(this.otherValidator);
            //alert(_other);
            this._otherOriginalValidationMethod = Function.createDelegate(this._otherValidator, this._otherValidator.evaluationfunction);
            
            
       }else {
            this._otherOriginalValidationMethod = function() {
                return true;
            };
       }
        
    },

    _ensureCallout : function() {
        if (!this._isBuilt) {
              
            var elt = this.get_element();
            //
            // create the DOM elements
            //
           
            var elementToValidate = this._elementToValidate = $get(elt.controltovalidate);
            var divImage = this._divImage = document.createElement("div");
            Sys.UI.DomElement.addCssClass(this._divImage, "validatorCross");
            $(divImage).injectAfter($(elementToValidate));
            $(elementToValidate).setStyle('float', 'left');
            $(divImage).style.left = $(elementToValidate).getLeft() + $(elementToValidate).getSize().x + 15 +  "px";
            //$(divImage).style.top = $(elementToValidate).getTop() + "px";
            //elt.parentNode.appendChild(divImage);
            divImage.style.display = 'none';
            //$(divImage).style.top = $(elementToValidate).getTop() + "px";          
            
           /*this._popupBehavior = $create(
                AjaxControlToolkit.PopupBehavior, 
                { 
                    positioningMode : AjaxControlToolkit.PositioningMode.Absolute,
                    parentElement : elt.parentNode
                }, 
                { }, 
                null,
                this._divImage);*/
            
            //Create the animations (if they were set before initialize was called)
           
            this._isBuilt = true;
            
            
        }
        if (!this._focusAttached) {
                
                $addHandler(this._elementToValidate, "focus", this._focusHandler);
                $addHandler(this._elementToValidate, "blur", this._blurHandler);
                this._focusAttached = true;
        }
    },
    
    dispose : function() {
        
        if (this._isBuilt) {
            this.hide();
            
            if (this._focusAttached) {
                $removeHandler(this._elementToValidate, "blur", this._blurHandler);
                $removeHandler(this._elementToValidate, "focus", this._focusHandler);
                this._focusAttached = false;
            }
            //$removeHandler(this._closeImage, "click", this._closeClickHandler);
            
            this._onShowJson = null;
            this._onHideJson = null;
            if (this._popupBehavior) {
                this._popupBehavior.dispose();
                this._popupBehavior = null;
            }
            if (this._closeBehavior) {
                this._closeBehavior.dispose();
                this._closeBehavior = null;
            }
            if (this._popupTable) {
                this._popupTable.parentNode.removeChild(this._popupTable);
                this._popupTable = null;
                this._errorMessageCell = null;
                this._elementToValidate = null;
                this._calloutArrowCell = null;
                this._warningIconImage = null;
                this._closeImage = null;
            }
            this._isBuilt = false;
        }
        Evolutia.validatorBlur.callBaseMethod(this, 'dispose');
    },    
    
    _getErrorMessage : function() {
        return this.get_element().errormessage || AjaxControlToolkit.Resources.ValidatorCallout_DefaultErrorMessage;
    },
        
    show : function(force) {        
       
            //this._isOpen = true;
            //if(force && Evolutia.validatorBlur._currentCallout) {
            //    Evolutia.validatorBlur._currentCallout.hide();
            //}
            //if(Evolutia.validatorBlur._currentCallout != null) {
            //    return;
            //}
            //Evolutia.validatorBlur._currentCallout = this;        
            //this._divImage.set_x($common.getSize(this._elementToValidate).width);
            Sys.UI.DomElement.removeCssClass(this._divImage, "validatorOK");
            Sys.UI.DomElement.addCssClass(this._divImage, "validatorCross");
            //this._popupBehavior.show();
            this._divImage.style.display='block';
        
    },
    
    hide : function() {
        //if(Evolutia.validatorBlur._currentCallout == this) {
        //    Evolutia.validatorBlur._currentCallout = null;
        //}
        this._ensureCallout();
        //this._isOpen = false;
        Sys.UI.DomElement.removeCssClass(this._divImage, "validatorCross");
        Sys.UI.DomElement.addCssClass(this._divImage, "validatorOK");
        //this._popupBehavior.hide();
        //this._popupBehavior.show();
        this._divImage.style.display='block';
            
        
    },
    
    _onblur : function(e) {
        
        if(!this._originalValidationMethod(this.get_element()) || !this._otherOriginalValidationMethod(this._otherValidator)) {
            this._ensureCallout();
             if(this._highlightCssClass) {
                Sys.UI.DomElement.addCssClass(this._elementToValidate, this._highlightCssClass);
            }
            this.show(true);
            return false;
        } else {
            this.hide();
            return true;
        }
    },

    _onfocus : function(e) {
           
            //this._ensureCallout();
            //this._popupBehavior.hide();
            this._divImage.style.display='none';
       
    },
    
    _oncloseClick : function(e) {
        this.hide();
    },
    
    _onvalidate : function(val) {
        this._ensureCallout();
        
        if((!this._originalValidationMethod(val) || !this._otherOriginalValidationMethod(this._otherValidator)) && !this._elementToValidate.disabled) {
            
            if(this._highlightCssClass) {
                Sys.UI.DomElement.addCssClass(this._elementToValidate, this._highlightCssClass);
            }
            
            this.show(false);
            this._invalid = true;
            return false;
        } else {
            if(this._highlightCssClass && this._invalid) {
                Sys.UI.DomElement.removeCssClass(this._elementToValidate, this._highlightCssClass)
            }
            this._invalid = false;
            this.hide();
            return true;
        }
        
    },
    
  
    
    get_onShow : function() {
        /// <value type="String" mayBeNull="true">
        /// Generic OnShow Animation's JSON definition
        /// </value>
        return this._popupBehavior ? this._popupBehavior.get_onShow() : this._onShowJson;
    },
    set_onShow : function(value) {
        if (this._popupBehavior) {
            this._popupBehavior.set_onShow(value)
        } else {
            this._onShowJson = value;
        }
        this.raisePropertyChanged('onShow');
    },
    get_onShowBehavior : function() {
        /// <value type="AjaxControlToolkit.Animation.GenericAnimationBehavior">
        /// Generic OnShow Animation's behavior
        /// </value>
        return this._popupBehavior ? this._popupBehavior.get_onShowBehavior() : null;
    },
    onShow : function() {
        /// <summary>
        /// Play the OnShow animation
        /// </summary>
        /// <returns />
        if (this._popupBehavior) {
            this._popupBehavior.onShow();
        }
    },
        
    get_onHide : function() {
        /// <value type="String" mayBeNull="true">
        /// Generic OnHide Animation's JSON definition
        /// </value>
        return this._popupBehavior ? this._popupBehavior.get_onHide() : this._onHideJson;
    },
    set_onHide : function(value) {
        if (this._popupBehavior) {
            this._popupBehavior.set_onHide(value)
        } else {
            this._onHideJson = value;
        }
        this.raisePropertyChanged('onHide');
    },
    get_onHideBehavior : function() {
        /// <value type="AjaxControlToolkit.Animation.GenericAnimationBehavior">
        /// Generic OnHide Animation's behavior
        /// </value>
        return this._popupBehavior ? this._popupBehavior.get_onHideBehavior() : null;
    },
    onHide : function() {
        /// <summary>
        /// Play the OnHide animation
        /// </summary>
        /// <returns />
        if (this._popupBehavior) {
            this._popupBehavior.onHide();
        }
    },
    
    get_warningIconImageUrl : function() {
        return this._warningIconImageUrl;
    },
    set_warningIconImageUrl : function(value) {
        
        if (this._warningIconImageUrl != value) {
            this._warningIconImageUrl = value;
            if (this.get_isInitialized()) {
                this._warningIconImage.src = value;
            }
            this.raisePropertyChanged("warningIconImageUrl");
        }
    },

    get_closeImageUrl : function() {
        return this._closeImageUrl;
    },
    set_closeImageUrl : function(value) {

        if (this._closeImageUrl != value) {
            this._closeImageUrl = value;
            if (this.get_isInitialized()) {
                this._closeImage.src = value;
            }
            this.raisePropertyChanged("closeImageUrl");
        }
    },
        
    get_width : function() {
        return this._width;
    },
    set_width : function(value) {

        if (this._width != value) { 
            this._width = value;
            if (this.get_isInitialized()) {
                this._popupTable.style.width = _width;
            }
            this.raisePropertyChanged("width");
        }
    },

    get_highlightCssClass : function() {
        return this._highlightCssClass;
    },
    set_highlightCssClass : function(value) {

        if (this._highlightCssClass != value) {
            this._highlightCssClass = value;
            this.raisePropertyChanged("highlightCssClass");
        }
    },
    
    get_isOpen : function() {
        return this._isOpen;
    }
}
Evolutia.validatorBlur.registerClass('Evolutia.validatorBlur', Sys.UI.Behavior);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

