﻿Type.registerNamespace('DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel');

/// <summary>Extender Enum DynamicDisplayMatchMode</summary>
DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode = function() {
    throw Error.invalidOperation();
}
DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode.prototype = {
    Show: 0,
    Hide: 1
}
DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode.registerEnum('DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode', false);

/// <summary>
/// Dynamic Display Panel Extender Control
///     this extender allows hide/show of ASP.Net Panel based on selected value in 
///     the ASP.Net DropDownList or HTMLSelect.  Selected values in drop down must equal one of the 
///     condition values for panel to hode or show.  the panel's style display property is used to
///     hide and show the panel.  The property Dispay determines the style.display.  The default is
///     inline.
/// </summary>
/// <remarks>
///    properties: 
///      {name: 'TargetControlID', type: String} : Default value for extenders identifing the Panel control
///      {name: 'ControlId', type: String} : id of ASP.Net DropDownList or HTML Select control
///      {name: 'ConditionValues', type: String} : CSV of values that match drop down list values
///      {name: 'Display', type: String} : text value to use in panel style.display.  default value is inline.
///      {name: 'MatchMode', type: String} : hide or show based on when a match is found uses DynamicDisplayMatchMode enum
///      {name: 'InitializeOnInit', type: bool : Perform initialize of control on the Init of control.
/// </remarks>
/// <history>
///     <change date="02/26/2007 12:18:00" ticket="">
///         <author>Steven Berenbrock</author>
///         <description>Initial version.</description>
///     </change>
/// </history>
DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayPanelBehavior = function(element) {
    DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayPanelBehavior.initializeBase(this, [element]);
    ///<summary>DynamicDisplayPanelBehavior Extender</summary>
    ///<param name="element">Associated element</param>

    //Properties
    this._controlId = null;
    this._conditionValues = null;
    this._display = 'inline';
    this._matchMode = DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode.Show;
    this._initializeOnInit = false;

    // Member variables
    this._controlElement = null;
    this._conditionArray = null;
    
    // Event delegates
    this._eventHandler = null;
}

DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayPanelBehavior.prototype = {
    //*****************************************************************
    //Override methods
    //
    initialize : function() {
        DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayPanelBehavior.callBaseMethod(this, 'initialize');

        var targetElement = this.get_element();
        
        this._controlElement = this._getElement(this._controlId);
        Sys.Debug.assert(this._controlElement != null, "Couldn't find element '" + this._controlId + "'");

        if (this._controlElement) {
            //establish event handlers
            this._setHandler();
        }

        //build conditions array
        if (this._conditionValues && (-1 != this._conditionValues.indexOf(','))) {        
            // Parse the _conditionValues
            this._conditionArray = this._conditionValues.split(',');
        }        
        else {
            //build array with single value
            this._conditionArray = [this._conditionValues];
        }
        
        // Check our client state.  If it's present,
        // that means this is a postback, so we restore the state.
        var lastState = DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayPanelBehavior.callBaseMethod(this, 'get_ClientState');                
        if (lastState && lastState != "") {
            var mode = Number(lastState);
            if (mode == 0){
                this._displayContainer(DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode.Show);
            }
            else if (mode == 1) {
                this._displayContainer(DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode.Hide);            
            }
        }                
        
        if (this._initializeOnInit) {          
            // fire control DDL change event to populate self
            this._onControlEvent(null);
        }
    },

    dispose : function() {
        var targetElement = this.get_element();

        if (this._controlElement != null) {
            if (this._eventHandler) {
                $removeHandler(this._controlElement, "change", this._eventHandler);
                this._eventHandler = null;
            }
        }
        
        DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayPanelBehavior.callBaseMethod(this, 'dispose');
    },

    //*****************************************************************
    // Property get/set methods
    //
    get_ControlId : function() {
        return this._controlId;
    },

    set_ControlId : function(value) {
        this._controlId = value;
    },

    get_ConditionValues : function() {
        return this._conditionValues;
    },

    set_ConditionValues : function(value) {
        this._conditionValues = value;
    },
    
    get_Display : function() {
        return this._display;
    },

    set_Display : function(value) {
        this._display = value;
    },

    get_MatchMode : function() {
        return this._matchMode;
    },

    set_MatchMode : function(value) {
        this._matchMode = value;
    },
    
    get_InitializeOnInit : function() {
        return this._initializeOnInit;
    },

    set_InitializeOnInit : function(value) {
        this._initializeOnInit = value;
    },

    //*****************************************************************
    // Custom methods
    //
    _setupState : function(mode) {
        /// <summary>Get all the state set consistently when we toggle</summary>
        /// <param name="toggleIndex" type="int">index value used in toggle</param>
        DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayPanelBehavior.callBaseMethod(this, 'set_ClientState', [mode.toString()]);                        
    },
    
     _getElement : function(elementId) {
        ///<summary>Private method getElement</summary>
        ///<param name="elementId">Associated DOM element id to get</param>
        ///<remarks>use the $get to retrieve the DOM element identified by the id. if not found, return null.</remarks>
        if (elementId) {
            var element = $get(elementId);
            if (element) {
                return element;
            }
        }
        return null;    
    },

    _setHandler : function() {
        ///<summary>Private method _setHandler - sets up the event handler on the control</summary>
        this._eventHandler = Function.createDelegate(this, this._onControlEvent);
        $addHandler(this._controlElement, "change", this._eventHandler);
        Sys.Debug.assert(this._eventHandler != null, "Couldn't create event ( change event handler for '" + this._controlElement.id + "'");
    },

    _matchedValues : function() {
        ///<summary>Private method _matchedValues - gets the selected ddl value and matches it to a value
        ///     in the conditionValues array.</summary>
        ///<returns type=bool>true if value found, else false</returns>
        var matched = false;        
        
        //get control property value and match to condition array
        var value = null;
        try {
            value = this._controlElement.options[this._controlElement.selectedIndex].value;
        }
        catch(e) {
            value = null;
        }
        
        if (value) {
            for (var index = 0; index < this._conditionArray.length; index++) {
                if (this._conditionArray[index] == value) {
                    matched = true;
                    break;
                }
            }
        }
        return matched;
    },

    _displayContainer : function(mode) {
        ///<summary>Private method _displayContainer - based on match parm hide or show the panel</summary>
        ///<param name="match">AjaxTestControlExtender.DynamicDisplayOnMatch enum</param>
        switch (mode) {
            case DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode.Hide:
                this.hide();
                break;
            case DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode.Show:
                this.show();
                break;
        }
    },

    hide : function() {
        ///<summary>Public method hide</summary>
        ///<remarks>hides the container using display style none</remarks>
        var targetElement = this.get_element();
        targetElement.style.display = 'none';
    },
    
    show : function() {
        ///<summary>Public method show</summary>
        ///<remarks>displays the container using display style defined in _display</remarks>
        var targetElement = this.get_element();
        if (this._display) {
            targetElement.style.display = this._display;
        }
        else {
            targetElement.style.display = 'inline';
        }
    },
    
    //*****************************************************************
    // Event handler methods
    //
     _onControlEvent : function() {
        ///<summary>Event handler for onchange ddl event</summary>
        if (this._matchedValues()) {
            this._displayContainer(this._matchMode);            
            this._setupState(this._matchMode);
        }
        else {
            var mode = (this._matchMode == DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode.Hide) 
                ? DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode.Show : DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayMatchMode.Hide; 
            this._displayContainer(mode);
            this._setupState(mode);
        }
    }
}

DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayPanelBehavior.registerClass('DTG.Dollar.Web.Consumer.Common.Ajax.DynamicDisplayPanel.DynamicDisplayPanelBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();