﻿Type.registerNamespace('DTG.Dollar.Web.Consumer.Common.Ajax.ToggleLabelContent');

/// <summary>
/// Toggle Label Content Extender Control
///     this extender allows toggle of ASP.Net Label text based on selected value in 
///     the ASP.Net DropDownList or HTMLSelect.  Selected values in drop down must equal one of the 
///     condition values for toggle to trigger.  Condition and toggle must match in order.  If condition
///     value at index 2 is matched to control drop down list value, the label is toggled to value
///     in toggle value at postion 2.
/// </summary>
/// <remarks>
///    properties: 
///      {name: 'TargetControlID', type: String} : Default value for extenders identifing the Label 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: 'ToggleValues', type: String} : CSV of values that are used to toggle
///      {name: 'NoMatchText', type: String} : text value to display in label when no match is found or toggle value
///             does not exist for found condition index.
///      {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.ToggleLabelContent.ToggleLabelContentBehavior = function(element) {
    DTG.Dollar.Web.Consumer.Common.Ajax.ToggleLabelContent.ToggleLabelContentBehavior.initializeBase(this, [element]);
    ///<summary>ToggleLabelContentBehavior Extender</summary>
    ///<param name="element">Associated element</param>

    //Properties
    this._controlId = null;
    this._conditionValues = null;
    this._toggleValues = null;
    this._noMatchText = null;
    this._initializeOnInit = false;
    
    // Member variables
    this._controlElement = null;
    this._conditionArray = null;
    this._toggleArray = null;
    
    // Event delegates
    this._eventHandler = null;
}

DTG.Dollar.Web.Consumer.Common.Ajax.ToggleLabelContent.ToggleLabelContentBehavior.prototype = {
    //*****************************************************************
    //Override methods
    //
    initialize : function() {
        DTG.Dollar.Web.Consumer.Common.Ajax.ToggleLabelContent.ToggleLabelContentBehavior.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];
        }

        //build toggle array
        if (this._toggleValues && (-1 != this._toggleValues.indexOf(','))) {        
            // Parse the toggleValues
            this._toggleArray = this._toggleValues.split(',');
        }        
        else {
            //build array with single value
            this._toggleArray = [this._toggleValues];
        }
        
        // 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.ToggleLabelContent.ToggleLabelContentBehavior.callBaseMethod(this, 'get_ClientState');                
        if (lastState && lastState != "") {
            var labelIndex = Number(lastState);  
            this._toggleLabel(labelIndex);
        }        

        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.ToggleLabelContent.ToggleLabelContentBehavior.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_ToggleValues : function() {
        return this._toggleValues;
    },

    set_ToggleValues : function(value) {
        this._toggleValues = value;
    },
    
    get_NoMatchText : function() {
        return this._noMatchText;
    },

    set_NoMatchText : function(value) {
        this._noMatchText = value;
    },

    get_InitializeOnInit : function() {
        return this._initializeOnInit;
    },

    set_InitializeOnInit : function(value) {
        this._initializeOnInit = value;
    },
    
    //*****************************************************************
    // Custom methods
    //
    _setupState : function(toggleIndex) {
        /// <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.ToggleLabelContent.ToggleLabelContentBehavior.callBaseMethod(this, 'set_ClientState', [toggleIndex.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 + "'");
    },

        
    _matchConditionValues : function() {
        ///<summary>Private method _matchConditionValues - gets the selected ddl value and matches it to a value
        ///     in the conditionValues array.</summary>
        ///<returns type=int>index of value found, else -1</returns>
        var matchedIndex = -1;        
        
        //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) {
                    matchedIndex = index;
                    break;
                }
            }
        }
        return matchedIndex;
    },

    _toggleLabel : function(index) {
        /// <summary>toggle the label text using the toggle array and index parm</summary>
        /// <param name="index" type="int">index to use toggle label text</param>
        var targetElement = this.get_element();
        if ((index >= 0) && (index < this._toggleArray.length)) {
            targetElement.innerHTML = this._toggleArray[index];
        }
        else {
            if (this._noMatchText) {
                targetElement.innerHTML = this._noMatchText;
            }
        }
    },
        
    //*****************************************************************
    // Event handler methods
    //
     _onControlEvent : function() {
        ///<summary>Event handler for onchange ddl event</summary>
        var conditionIndex = this._matchConditionValues();
        //toggle label text 
        this._toggleLabel(conditionIndex);            
        this._setupState(conditionIndex);
    }
}

DTG.Dollar.Web.Consumer.Common.Ajax.ToggleLabelContent.ToggleLabelContentBehavior.registerClass('DTG.Dollar.Web.Consumer.Common.Ajax.ToggleLabelContent.ToggleLabelContentBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();