﻿if (!SIGI) throw ("SIGI.DropDownList depends on the following scripts. Please make sure these are included.\nSIGI.js");

// === classes ===
SIGI.DropDownList = function() {
    this._id;
    this._dataSource = "";
    this._dataTextField = "";
    this._dataValueField = "";
    this._dataFilter = "";
    this._selectedIndex = -1;
    this._enabled = true;
    this._emptyOptionText = "";
    this._initialDisplay = "";
    this._items = new SIGI.DropDownListItemCollection();
};

SIGI.DropDownList.inherits(SIGI.UI.Control);

// ## Region - Property gets and sets
SIGI.DropDownList.prototype.get_value = function() {
    return this.get_element().value;
};
SIGI.DropDownList.prototype.set_value = function(pValue) {
    return this.get_element().value = pValue;
};
SIGI.DropDownList.prototype.get_text = function() {
    if (this.get_selectedIndex() > -1)
        return this._items[this.get_selectedIndex()]._text;
    else
        return "";
};
SIGI.DropDownList.prototype.get_dataSource = function() {
    return this._dataSource;
};
SIGI.DropDownList.prototype.set_dataSource = function(pValue) {
    this._dataSource = pValue;
};
SIGI.DropDownList.prototype.get_dataTextField = function() {
    return this._dataTextField;
};
SIGI.DropDownList.prototype.set_dataTextField = function(pValue) {
    this._dataTextField = pValue;
};
SIGI.DropDownList.prototype.get_dataValueField = function() {
    return this._dataValueField;
};
SIGI.DropDownList.prototype.set_dataValueField = function(pValue) {
    this._dataValueField = pValue;
};
SIGI.DropDownList.prototype.get_dataFilter = function() {
    return this._dataFilter;
};
SIGI.DropDownList.prototype.set_dataFilter = function(pValue) {
    this._dataFilter = pValue;
};
SIGI.DropDownList.prototype.get_selectedIndex = function() {
    if (this._selectedIndex != this.get_element().options.selectedIndex)
        this._selectedIndex = this.get_element().options.selectedIndex
    return this._selectedIndex;
};
SIGI.DropDownList.prototype.set_selectedIndex = function(pValue) {
    this._selectedIndex = pValue;
    this._items[pValue].set_selected(true);
};
SIGI.DropDownList.prototype.get_enabled = function() {
    return !this.get_element().disabled;
};
SIGI.DropDownList.prototype.set_enabled = function(pValue) {
    SIGI.changeState(this.get_element(), !pValue);
};
SIGI.DropDownList.prototype.get_toolTip = function() {
    return this.get_element().getAttribute("tooltip");
};
SIGI.DropDownList.prototype.set_toolTip = function(pValue) {
    this.get_element().setAttribute("tooltip", pValue);
};
SIGI.DropDownList.prototype.get_emptyOptionText = function() {
    return this._emptyOptionText;
};
SIGI.DropDownList.prototype.set_emptyOptionText = function(pValue) {
    this._emptyOptionText = pValue;
};
SIGI.DropDownList.prototype.get_items = function(itemId) {
    if (arguments.length)
        return (this._items.get_item(itemId));
    else
        return (this._items);
};
SIGI.DropDownList.prototype.get_visible = function() {
    if (this.get_element().style.display == "none")
        return false;
    else
        return true;
};
SIGI.DropDownList.prototype.set_visible = function(pValue) {
    if (pValue)
        this.get_element().style.display = this._initialDisplay;
    else
        this.get_element().style.display = "none";
};
SIGI.DropDownList.prototype.focus = function() {
    SIGI.setFocus(this.get_element());
};

SIGI.DropDownList.prototype.set_statusText = function(pValue) {
    this.get_element().textElement.statusText = pValue;
};
SIGI.DropDownList.prototype.get_statusText = function() {
    return this.get_element().textElement.statusText;
};
SIGI.DropDownList.prototype.clearListBox = function() {
    // clear select first
    while (0 < this.get_element().length) {
        this.get_element().removeChild(this.get_element().options[0]);
    }
    this._items.clear();
};

SIGI.DropDownList.prototype.dataBind = function() {
    var xmlTable = SIGI.XML.getXmlDataIsland(this._dataSource);

    if (xmlTable != null) {
        this.clearListBox();

        var opt;
        // add a blank Select value
        if (this._emptyOptionText != "") {
            this._items.add(new SIGI.DropDownListItem(this._emptyOptionText, "", true, null));
        }

        var nodes;
        if (this._dataFilter.toString().length > 0) {
            try {
                nodes = xmlTable.documentElement.selectNodes("ROW[" + this._dataFilter + "]");
            }
            catch (exc) {
                nodes = xmlTable.documentElement.childNodes;
            }
        }
        else {
            nodes = xmlTable.documentElement.childNodes;
        }


        for (var i = 0; i < nodes.length; i++) {
            var value = "";
            var text = "";
            var selected = false;
            value = nodes[i].attributes.getNamedItem(this._dataValueField).text;
            text = nodes[i].attributes.getNamedItem(this._dataTextField).text;
            this._items.add(new SIGI.DropDownListItem(text, value, selected, null));
        }
    }
};
SIGI.DropDownList.prototype._onItemSelected = function(item) {

    for (var i = 0; i < this._items.length; i++) {
        this._items[i]._selected = this._items[i]._value == item._value;
    }
    item._listElement.setAttribute("selected", item._selected);
};
SIGI.DropDownList.prototype._onitemadd = function(item, index) {
        if (!item._listElement) {
            item._listElement = document.createElement("option");
            this.get_element().options.add(item._listElement);
            item._listElement.innerHTML = item._text;
            item._listElement.setAttribute("value", item._value);
            if (item._selected)
                item._listElement.setAttribute("selected", item._selected);
        }
        item._index = index;    
    //    item._listElement.tabIndex = 1;
        item._listElement.hideFocus = false;
        item._onSelected = this.context(this._onItemSelected);
};
SIGI.DropDownList.prototype._onitemremove = function(item, index) {
    this.get_element().options.remove(index);
};
SIGI.DropDownList.prototype._onchange = function() {
    //alert(this.get_element().textElement.value + ':' + this.get_text());
    this.get_element().textElement.value = this.get_text();
    this._items.findByText(this.get_text()).set_selected();
};

SIGI.DropDownList.prototype.initialize = function() {

    this._id = this.get_element().id;
    this._initialDisplay = this.get_element().style.display;
    this.get_element().textElement = document.getElementById(this._id + '_Text');

    /************************************************/
    /*    WIRE UP THE EVENTS                        */
    /************************************************/

    this._items.onadd = this.context(this._onitemadd);
    this._items.onremove = this.context(this._onitemremove);

    for (var i = 0; i < this.get_element().length; i++) {
        var item = new SIGI.DropDownListItem(trim(this.get_element()[i].innerHTML), trim(this.get_element()[i].getAttribute("value")), false, this.get_element()[i]);
        this._items.add(item);
    }

    //NEW to support SelectedText property
    this.get_element().textElement.value = this.get_text();
    //SIGI.attachEvent(this.get_element(), "onchange", this.context(this._onchange));
};

/* DropDownListItemCollection */
SIGI.DropDownListItemCollection = function() {
    this.length = 0;
};
SIGI.DropDownListItemCollection.inherits(SIGI.CollectionObject);

SIGI.DropDownListItemCollection.prototype.findByValue = function(val) {
    for (i = 0; i < this.length; i++) {
        if (this[i]._value.toLowerCase() == val.toString().toLowerCase())
            return this[i];
    }
};
SIGI.DropDownListItemCollection.prototype.findByText = function(val) {
    for (i = 0; i < this.length; i++) {
        if (this[i]._text.toLowerCase() == val.toString().toLowerCase())
            return this[i];
    }
};
SIGI.DropDownListItemCollection.prototype.get_count = function() {
    return this.count();
};
SIGI.DropDownListItemCollection.prototype.get_indexOf = function(item) {
    return this.indexOf(item);
};
SIGI.DropDownListItemCollection.prototype.get_isEmpty = function() {
    return this.isEmpty();
};
SIGI.DropDownListItemCollection.prototype.get_item = function(val) {
    return this[parseInt(val)];
};

/* DropDownListItem */
SIGI.DropDownListItem = function(text, value, selected, element) {
    this._listElement = element;
    this._text = text;
    this._value = value;
    this._index = -1;
    this._selected = (selected != null) ? toBoolean(selected) : false;
    this._onSelected = new Function();
};

SIGI.DropDownListItem.prototype.set_text = function(text) {
    this._text = text;
    if (this._listElement)
        this._listElement.innerHTML = text;
};
SIGI.DropDownListItem.prototype.get_text = function() {
    return this._text;
};
SIGI.DropDownListItem.prototype.set_value = function(value) {
    this._value = value;
    if (this._listElement)
        this._listElement.value = value;
};
SIGI.DropDownListItem.prototype.get_value = function() {
    return this._value;
}
SIGI.DropDownListItem.prototype.get_index = function() {
    return this._index;
};
SIGI.DropDownListItem.prototype.set_selected = function() {
    this._selected = true;
    this._onSelected(this);
};
SIGI.DropDownListItem.prototype.get_selected = function() {
    return this._selected;
};