﻿Type.registerNamespace("Legacy");
//debugger;
Legacy.DropDownList = function(element)
{
    Legacy.DropDownList.initializeBase(this, [element]);

    // Fields
    this._container = null;
    this._textBox = null;
    this._disabled = null;
    this._watermark = null;
    this._textBoxBackground = null;
    this._watermarkCssClass = null;
    this._focusCssClass = null;
    this._hiddenValue = null;
}

Legacy.DropDownList.prototype =
{
    initialize: function()
    {
        Legacy.DropDownList.callBaseMethod(this, "initialize");
        $addHandlers(
            this.get_element(),
            {
                "change": Function.createDelegate(this, this._changedHandler),
                "keyup": Function.createDelegate(this, this._keyUpHandler),
                "focus": Function.createDelegate(this, this._focusHandler),
                "blur": Function.createDelegate(this, this._blurHandler)
            },
            this
        );
        this._initUI();
    },
    dispose: function()
    {
        $clearHandlers(this.get_element());
        Legacy.DropDownList.callBaseMethod(this, "dispose");
    },

    // Object Properties
    get_container: function()
    {
        return this._container;
    },
    set_container: function(value)
    {
        this._container = value;
    },
    get_textBox: function()
    {
        return this._textBox;
    },
    set_textBox: function(value)
    {
        this._textBox = value;
    },
    get_textBoxBackground: function()
    {
        return this._textBoxBackground;
    },
    set_textBoxBackground: function(value)
    {
        this._textBoxBackground = value;
    },
    get_disabled: function()
    {
        return this._disabled;
    },
    set_disabled: function(value)
    {
        this._disabled = value;
    },
    get_hiddenValue: function()
    {
        return this._hiddenValue;
    },
    set_hiddenValue: function(value)
    {
        this._hiddenValue = value;
    },

    // Properties
    /// <summary>Gets the watermark string associated with this textbox.</summary>
    get_watermark: function()
    {
        return (this._watermark != null) ? this._watermark : "";
    },
    /// <summary>Sets the watermark string associated with this textbox.</summary>
    set_watermark: function(value)
    {
        var e = Function._validateParams(arguments, [{ name: "value", type: String, mayBeNull: false, optional: false}]);
        if (e) throw e;

        if (value.valueOf() != this.get_watermark())
        {
            this._watermark = value.valueOf();
            this.raisePropertyChanged("watermark");
        }
    },
    /// <summary>Gets the watermark CSS class associated with this textbox.</summary>
    get_watermarkCssClass: function()
    {
        return (this._watermarkCssClass != null) ? this._watermarkCssClass : "";
    },
    /// <summary>Sets the watermark CSS class associated with this textbox.</summary>
    set_watermarkCssClass: function(value)
    {
        var e = Function._validateParams(arguments, [{ name: "value", type: String, mayBeNull: false, optional: false}]);
        if (e) throw e;

        if (value.valueOf() != this.get_watermarkCssClass())
        {
            this._watermarkCssClass = value.valueOf();
            this.raisePropertyChanged("watermarkCssClass");
        }
    },
    /// <summary>Gets the focus CSS class associated with this textbox.</summary>
    get_focusCssClass: function()
    {
        return (this._focusCssClass != null) ? this._focusCssClass : "";
    },
    /// <summary>Sets the focus CSS class associated with this textbox.</summary>
    set_focusCssClass: function(value)
    {
        var e = Function._validateParams(arguments, [{ name: "value", type: String, mayBeNull: false, optional: false}]);
        if (e) throw e;

        if (value.valueOf() != this.get_focusCssClass())
        {
            this._focusCssClass = value.valueOf();
            this.raisePropertyChanged("focusCssClass");
        }
    },
    /// <summary>Gets the current selected index.</summary>
    get_selectedIndex: function()
    {
        return this.get_element().selectedIndex;
    },
    /// <summary>Gets the current selected index.</summary>
    set_selectedIndex: function(value)
    {
        var e = Function._validateParams(arguments, [{ name: "value", type: Number, mayBeNull: false, optional: false}]);
        if (e) throw e;

        this.get_element().selectedIndex = value;
        this._raiseChangeEvent();
    },

    // Methods
    reset: function()
    {
        var element = this.get_element();

        if (!element.disabled)
        {
            if (element.options.length > 0)
            {
                if (this.get_selectedIndex() != 0)
                {
                    this.set_selectedIndex(0);
                }
            }
            this._update();
        }
    },
    disable: function()
    {
        var element = this.get_element();

        if (!element.disabled)
        {
            this._disable();
        }
    },
    bind: function(dataSource)
    {
        var e = Function._validateParams(arguments, [{ name: "dataSource", type: Array, mayBeNull: true, optional: false}]);
        if (e) throw e;

        var element = this.get_element();
        var selectedValue = this.get_hiddenValue().value;

        if (!element.disabled)
        {
            element.innerHTML = "";

            if ((dataSource != null) && (dataSource.length > 0))
            {
                for (index = 0; index < dataSource.length; index++)
                {
                    var option = document.createElement("OPTION");

                    if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
                    {
                        option.innerText = dataSource[index].Name;
                    }
                    else
                    {
                        option.text = dataSource[index].Name;
                    }

                    option.value = dataSource[index].ID;

                    if ((selectedValue !== undefined) && (selectedValue !== null) && (selectedValue == dataSource[index].ID))
                    {
                        option.selected = true;
                    }

                    element.appendChild(option);
                }
            }

            this._raiseChangeEvent();
        }
    },

    // Event Handlers
    _changedHandler: function(event)
    {
        this._setSelectedValue();
        this._update();
    },
    _keyUpHandler: function(event)
    {
        if (Sys.Browser.agent != Sys.Browser.InternetExplorer)
        {
            var element = this.get_element();
            var e = document.createEvent("HTMLEvents");
            e.initEvent("change", false, false);
            element.dispatchEvent(e);
        }
    },
    _focusHandler: function(event)
    {
        this._focus();
    },
    _blurHandler: function(event)
    {
        this._blur();
    },

    // Helper Methods
    _initUI: function()
    {
        this._update();
    },
    _update: function()
    {
        var element = this.get_element();

        if (!element.disabled)
        {
            var selectedIndex = this.get_selectedIndex();
            if ((selectedIndex >= 0) && (selectedIndex < element.options.length))
            {
                this._enable();
            }
            else
            {
                this._disable();
            }
        }
    },
    _enable: function()
    {
        var element = this.get_element();
        var textBox = this.get_textBox();

        textBox.innerHTML = element.options[this.get_selectedIndex()].text;
        Sys.UI.DomElement.removeCssClass(textBox, this.get_watermarkCssClass());
        element.style.visibility = "visible";
        this.get_disabled().style.visibility = "hidden";
    },
    _disable: function()
    {
        var element = this.get_element();
        var textBox = this.get_textBox();

        element.innerHTML = "";
        textBox.innerHTML = this.get_watermark();
        Sys.UI.DomElement.addCssClass(textBox, this.get_watermarkCssClass());
        element.style.visibility = "hidden";
        this.get_disabled().style.visibility = "visible";
    },
    _focus: function()
    {
        Sys.UI.DomElement.addCssClass(this.get_textBoxBackground(), this.get_focusCssClass());
    },
    _blur: function()
    {
        Sys.UI.DomElement.removeCssClass(this.get_textBoxBackground(), this.get_focusCssClass());
    },
    _setSelectedValue: function()
    {
        var element = this.get_element();
        this.get_hiddenValue().value = element.value;
    },
    _raiseChangeEvent: function()
    {
        if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
        {
            this.get_element().fireEvent("onchange");
        }
        else
        {
            var e = document.createEvent("HTMLEvents");
            e.initEvent("change", false, false);
            this.get_element().dispatchEvent(e);
        }
    }
}

Legacy.DropDownList.descriptor =
{
    properties:
    [
        { name: "container", type: Object },
        { name: "textBox", type: Object },
        { name: "textBoxBackground", type: Object },
        { name: "disabled", type: Object },
        { name: "watermark", type: String },
        { name: "watermarkCssClass", type: String },
        { name: "focusCssClass", type: String },
        { name: "hiddenValue", type: Object }
    ]
}

Legacy.DropDownList.registerClass("Legacy.DropDownList", Sys.UI.Control);

if (typeof (Sys) !== "undefined")
{
    Sys.Application.notifyScriptLoaded();
}