﻿// (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('AjaxControlToolkit');

AjaxControlToolkit.ValidatorCalloutBehavior = function AjaxControlToolkit$ValidatorCalloutBehavior(element) {
    AjaxControlToolkit.ValidatorCalloutBehavior.initializeBase(this, [element]);
    
    //this._warningIconImageUrl = null;
    this._arrowImageUrl = null;
    this._backgroundImageUrl = null;
    this._closeImageUrl = null;
    this._errorTitle = null;
    this._highlightCssClass = null;
    this._width = "200px";
    this._invalid = false;
    this._originalValidationMethod = 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._focusHandler = Function.createDelegate(this, this._onfocus);
    this._closeClickHandler = Function.createDelegate(this, this._oncloseClick);
    this._midTopCell = null;
    this._rightTopCell = null;
}
AjaxControlToolkit.ValidatorCalloutBehavior.prototype = {
    initialize : function() {
        AjaxControlToolkit.ValidatorCalloutBehavior.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;            
        }
    },

    _ensureCallout : function() {
        if (!this._isBuilt) {
              
            var elt = this.get_element();
            //
            // create the DOM elements
            //
            var elementToValidate = this._elementToValidate = $get(elt.controltovalidate);
	        var popupTableBody = document.createElement("tbody");
            var popupTableRow = document.createElement("tr");
            var popupTable = this._popupTable = document.createElement("table");
            var topRow = document.createElement("tr");
            var leftTopCell = document.createElement("td");
            var arrowImage = this._arrowImage = document.createElement("img");
            var closeImage = this._closeImage = document.createElement("img");
            var midTopCell = this._midTopCell = document.createElement("td");
            var rightTopCell = this._rightTopCell = document.createElement("td");
            var botRow = document.createElement("tr");
            var leftBotCell = document.createElement("td");
            var midBotCell = document.createElement("td");      
	        // 
	        // popupTable
	        //
            popupTable.cellPadding = 0;
            popupTable.cellSpacing = 0;
            popupTable.border = 0;
            popupTable.width = this.get_width();
            popupTable.style.display = 'none';
            //
            // popupTableRow
            //     
            popupTableRow.vAlign = 'top';
            popupTableRow.style.height = "100%";
            //
            // topRow
            //        
            topRow.vAlign = 'top';
            topRow.style.height = "100%";
            // 
	        // leftTopCell
	        //
            leftTopCell.width = "12px";
            leftTopCell.style.height = "23px";
            leftTopCell.style.verticalAlign = "top";
            //
            // arrowImage
            //
            arrowImage.src = this.get_arrowImageUrl();   
            //
            // closeImage
            //
            closeImage.src = this.get_closeImageUrl();
            closeImage.style.cursor = 'pointer';            
            // 
	        // midTopCell
	        //
            midTopCell.width = "100%";
            midTopCell.id = "tcCallout";
            midTopCell.style.height = "21px";        
            midTopCell.style.padding = "2px 0px 0px 2px";
            midTopCell.style.paddingLeft = "2px";
            midTopCell.style.fontWeight = "bold";
            midTopCell.style.color = "#ffffff";
            midTopCell.background = this.get_backgroundImageUrl();
            midTopCell.innerHTML = this.get_errorTitle();
            midTopCell.style.backgroundColor = '#c21717';
            midTopCell.style.fontFamily = 'Trebuchet MS", sans-serif';
            midTopCell.style.fontSize = '12px';                 
            // 
	        // rightTopCell
	        //
            rightTopCell.width = "23px";
            rightTopCell.style.height = "23px";
            rightTopCell.style.verticalAlign = "top";            
            rightTopCell.style.textAlign = 'right';              
            //
            // botRow
            //        
            botRow.vAlign = 'top';
            botRow.style.height = "100%";    
            // 
	        // leftTopCell
	        //
            leftBotCell.width = "12px";
            leftBotCell.style.verticalAlign = "top";        
            // 
	        // midBotCell
	        //
            midBotCell.width = "100%";
            midBotCell.style.verticalAlign = "top";
            midBotCell.style.padding = "5px";
            midBotCell.innerHTML = this._getErrorMessage();   
            midBotCell.style.borderTop = "1px solid #bcbcbc";
            midBotCell.style.borderLeft = "1px solid #bcbcbc";
            midBotCell.style.borderBottom = "1px solid #bcbcbc";
            midBotCell.style.borderRight = "1px solid #bcbcbc"; 
            midBotCell.colSpan = '2';  
            midBotCell.style.fontFamily = 'Trebuchet MS", sans-serif';
            midBotCell.style.fontSize = '12px'; 
            midBotCell.style.backgroundColor = '#ffffff';                                                 
            //
            // arrowImage
            //
            arrowImage.src = this.get_arrowImageUrl();   
            //
            // closeImage
            //
            closeImage.src = this.get_closeImageUrl();
            closeImage.style.cursor = 'pointer';            
            //
            // Create the DOM tree
            //
            elt.parentNode.appendChild(popupTable)
            popupTable.appendChild(popupTableBody);
            popupTableBody.appendChild(topRow);
            topRow.appendChild(leftTopCell);
            leftTopCell.appendChild(arrowImage);
            topRow.appendChild(midTopCell);
            topRow.appendChild(rightTopCell);
            rightTopCell.appendChild(closeImage);
            
            popupTableBody.appendChild(botRow);
            botRow.appendChild(leftBotCell);
            botRow.appendChild(midBotCell); 
            //
            // initialize behaviors
            //
            this._popupBehavior = $create(
                AjaxControlToolkit.PopupBehavior, 
                { 
                    positioningMode : AjaxControlToolkit.PositioningMode.Absolute,
                    parentElement : elementToValidate
                }, 
                { }, 
                null,
                this._popupTable);
            
            // Create the animations (if they were set before initialize was called)
            if (this._onShowJson) {
                this._popupBehavior.set_onShow(this._onShowJson);
            }
            if (this._onHideJson) {
                this._popupBehavior.set_onHide(this._onHideJson);
            }
            
            $addHandler(this._closeImage, "click", this._closeClickHandler);
            this._isBuilt = true;
        }
    },
    
    dispose : function() {
        
        if (this._isBuilt) {
            this.hide();
            
            if (this._focusAttached) {
                $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;
        }
        AjaxControlToolkit.ValidatorCalloutBehavior.callBaseMethod(this, 'dispose');
    },    
    
    _getErrorMessage : function() {
        return this.get_element().errormessage || AjaxControlToolkit.Resources.ValidatorCallout_DefaultErrorMessage;
    },
        
    show : function(force) {        
        if (force || !this._isOpen) {
            this._isOpen = true;
            if(force && AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout) {
                AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout.hide();
            }
            if(AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout != null) {
                return;
            }
            AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout = this;        
            this._popupBehavior.set_x($common.getSize(this._elementToValidate).width);
            this._popupBehavior.show();
        }
    },
    
    hide : function() {
        if(AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout == this) {
            AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout = null;
        }
        if (this._isOpen || $common.getVisible(this._popupTable)) {
            this._isOpen = false;
            this._popupBehavior.hide();
        }
    },

    _onfocus : function(e) {
        if(!this._originalValidationMethod(this.get_element())) {
            this._ensureCallout();
             if(this._highlightCssClass) {
                Sys.UI.DomElement.addCssClass(this._elementToValidate, this._highlightCssClass);
            }
            this.show(true);
            return false;
        } else {
            this.hide();
            return true;
        }
    },
    
    _oncloseClick : function(e) {
        this.hide();
    },
    
    _onvalidate : function(val) {
        if(!this._originalValidationMethod(val)) {
            this._ensureCallout();
            if(this._highlightCssClass) {
                Sys.UI.DomElement.addCssClass(this._elementToValidate, this._highlightCssClass);
            }
            if (!this._focusAttached) {
                $addHandler(this._elementToValidate, "focus", this._focusHandler);
                this._focusAttached = true;
            }
            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 + set: background image url
    get_backgroundImageUrl : function() {
        return this._backgroundImageUrl;
    },
    set_backgroundImageUrl : function(value) {
        
        if (this._backgroundImageUrl != value) {
            this._backgroundImageUrl = value;
            if (this.get_isInitialized()) {
                this._midTopCell.background = value;
                //this._rightTopCell.background = value;
            }
            this.raisePropertyChanged("backgroundImageUrl");
        }
    },

    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 + set: arrow image url
    get_arrowImageUrl : function() {
        return this._arrowImageUrl;
    },
    set_arrowImageUrl : function(value) {

        if (this._arrowImageUrl != value) {
            this._arrowImageUrl = value;
            if (this.get_isInitialized()) {
                this._arrowImage.src = value;
            }
            this.raisePropertyChanged("arrowImageUrl");
        }
    },
        
    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 + set: error title
    get_errorTitle : function() {
        return this._errorTitle;
    },
    set_errorTitle : function(value) {

        if (this._errorTitle != value) {
            this._errorTitle = value;
            if (this.get_isInitialized()) {
                this._midTopCell.innerHTML = _errorTitle;
            }
        }
    },

    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;
    }
}
AjaxControlToolkit.ValidatorCalloutBehavior.registerClass('AjaxControlToolkit.ValidatorCalloutBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();