﻿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._selectedIndex = -1;
    this._enabled = true;
    this._emptyOptionText = "";
    this._initialDisplay = "";
    this._items = new SIGI.DropDownListItemCollection();
    this._dataTextField = "";
    this._dataValueField = "";
    this._dataFilter = "";
    this._dataFilterXQueryByElementValue = "";
    this._dataFilterXQueryByAttributeValue = "";
    this._xmlDataIslandName = "";
    this._dataSource = "";
    this._xmlDataIsland = null;
    this._displayDelimiter = "";
};

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;
    if (pValue > "") {
        this._xmlDataIsland = document.getElementById(pValue);
    }
};
SIGI.DropDownList.prototype.get_xmlDataIslandName = function() {
    //return this._xmlDataIslandName;
    return this.get_dataSource();
};
SIGI.DropDownList.prototype.set_xmlDataIslandName = function(pValue) {
    //    this._xmlDataIslandName = pValue;
    //    this.set_dataFilter("");
    //    if (pValue > "") {
    //        this.set_dataSource(document.getElementById(pValue));
    //    }
    this.set_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;
    this.buildXQueryFilter();
};
SIGI.DropDownList.prototype.get_displayDelimiter = function() {
    return this._displayDelimiter;
};
SIGI.DropDownList.prototype.set_displayDelimiter = function(pValue) {
    this._displayDelimiter = 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);
    if (this.get_element().simpleValidator) {
        //document.getElementById(this.get_element().simpleValidator).style.display = this.get_element().style.display;
        document.getElementById(this.get_element().simpleValidator).style.display = "none";
    }
};
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) {
        if (this._initialDisplay.toLowerCase() == "none") {
            this.get_element().style.display = "block";
        }
        else {
            this.get_element().style.display = this._initialDisplay;
        }
    }
    else {
        this.get_element().style.display = "none";
    }
    if (this.get_element().simpleValidator) {
        //document.getElementById(this.get_element().simpleValidator).style.display = this.get_element().style.display;
        document.getElementById(this.get_element().simpleValidator).style.display = "none";
    }
    if (this.get_element().label) {
        if (SIGI.Validation.hideLabelWithElement(this.get_element())) {
            try {
                this.get_element().label.style.display = this.get_element().style.display;
                this.get_element().label.className = "FormLabel";
            }
            catch (e) {
                document.getElementById(this.get_element().label).style.display = this.get_element().style.display;
                document.getElementById(this.get_element().label).className = "FormLabel";
            }
        }
    }
};
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();
};

/************************************************/
/*    Client filter function     */
/************************************************/

SIGI.DropDownList.prototype.buildXQueryFilter = function() {
    if (this._dataFilter != "") {
        //split by ; into name-value pair
        if (document.implementation.hasFeature("XPath", "3.0")) {
            this._dataFilterXQueryByAttributeValue = "//row[";
        }
        else {
            this._dataFilterXQueryByAttributeValue = "//ROW[";
        }
        /*
        This is support for entering filters in field:value;field:value format
        Implementing this created a problem with backwards compatibility, where
        developers were already using XPath format.  This code will remain in comment block
        but we're not supporting anything but XPath format for now

        var filters = this._dataFilter.split(";");
        for (i = 0; i < filters.length; i++) {
        var pair = filters[i].split(":");
        if (pair.length < 2) {
        pair = filters[i].split("=");
        }
        pair[0] = pair[0].replace(/@/g, "");
        pair[1] = pair[1].replace(/'/g, "");
        this._dataFilterXQueryByAttributeValue += "@" + pair[0] + "='" + pair[1] + "' and ";
        }
        this._dataFilterXQueryByAttributeValue = this._dataFilterXQueryByAttributeValue.substring(0, this._dataFilterXQueryByAttributeValue.length - 5);
        
        */
        this._dataFilterXQueryByAttributeValue += this._dataFilter;

        this._dataFilterXQueryByAttributeValue += "]";
    }
    else {
        if (document.implementation.hasFeature("XPath", "3.0")) {
            this._dataFilterXQueryByAttributeValue = "//row";
        }
        else {
            this._dataFilterXQueryByAttributeValue = "//ROW";
        }
    }
    this._dataFilterXQueryByElementValue = this._dataFilterXQueryByAttributeValue.replace(/@/g, "");
};

SIGI.DropDownList.prototype.dataBind = function() {
    //var xmlTable = SIGI.XML.getXmlDataIsland(this._dataSource);

    /*
    xmlDoc.selectNodes(xpath); 
    Firefox, Chrome, Opera and Safari use the evaluate() method to select nodes from the XML document:
    xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE,null); 
    */

    if (this._items[this.get_selectedIndex()] != null) {
        var existingValue = this._items[this.get_selectedIndex()]._value;
    }
    if (this._xmlDataIsland != 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;
        //First get the set of nodes that meet the search criteria
        try {
            nodes = SIGI.XML.selectNodes(this._dataFilterXQueryByElementValue, this._xmlDataIsland);
            if (nodes == null || nodes.length == 0) {
                nodes = SIGI.XML.selectNodes(this._dataFilterXQueryByAttributeValue, this._xmlDataIsland);
            }
        }
        catch (exc) {
            nodes = SIGI.XML.selectNodes("", this._xmlDataIsland);
        }

        var dataTextFieldArray = this._dataTextField.split(";");
        //next parse the display text and value from each node and build them into list items
        for (var i = 0; i < nodes.length; i++) {
            var value = "";
            var text = "";
            var selected = false;
            var currNodes = null;
            var currNode = null;

            //try to pull the value from the node attributes
            value = nodes[i].getAttribute(this._dataValueField);
            if (value == null) {
                //failing that, try to pull it from a child element
                currNodes = SIGI.XML.selectNodes(this._dataValueField, nodes[i]);
                if (currNodes.length > 0) {
                    currNode = currNodes[0];
                    if (currNode.text) {
                        value = currNode.text;
                    }
                    else {
                        value = currNode.innerHTML;
                    }
                }
            }

            //for display text, loop the split string of display fields
            for (j = 0; j < dataTextFieldArray.length; j++) {
                //try to pull the value from the node attributes
                textValToUse = nodes[i].getAttribute(dataTextFieldArray[j]);
                if (textValToUse == null) {
                    //failing that, try to pull it from a child element
                    currNodes = SIGI.XML.selectNodes(dataTextFieldArray[j], nodes[i]);
                    if (currNodes.length > 0) {
                        currNode = currNodes[0];
                        if (currNode.text) {
                            textValToUse = currNode.text;
                        }
                        else {
                            textValToUse = currNode.innerHTML;
                        }
                    }
                }
                text += textValToUse + this._displayDelimiter;
            }
            text = text.substring(0, text.length - this._displayDelimiter.length);
            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.loadOptions = function(dataSourceUrl, optionNodeName) {
    var xmlDoc;
    var xmlDoc2;
    this._items.length = 1;

    if (optionNodeName == null) {
        optionNodeName = "LookupData"
    }
    if (window.ActiveXObject) {
        var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        xmlHttp.open("POST", dataSourceUrl, false);
        xmlHttp.send(null);

        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc = xmlHttp.responseXML;

        var nodes = xmlDoc.getElementsByTagName(optionNodeName); //second level

        for (var i = 0; i < nodes.length; i++) {
            var node = nodes.item(i);
            var newOption = new Option();
            if (node.childNodes.item(1).text.length > 0) {
                this._items[i + 1] = new Option(node.childNodes.item(1).text, node.childNodes.item(0).text);
            }
        }
    }
    else if (window.XMLHttpRequest) {
        var xmlHttp2 = new XMLHttpRequest();
        xmlHttp2.open("GET", dataSourceUrl, false);
        xmlHttp2.send(null);

        xmlDoc2 = xmlHttp2.responseXML;

        var nodes = xmlDoc2.getElementsByTagName(optionNodeName); //second level

        for (var i = 0; i < nodes.length; i++) {
            var node = nodes.item(i);
            var newOption = new Option();

            //if ( node.childNodes[1].childNodes[0].nodeValue != null ) FOR DEFECT #16782
            if (node.childNodes[1].childNodes[0] != null) {
                this._items[i + 1] = new Option(node.childNodes[1].childNodes[0].nodeValue, node.childNodes[0].childNodes[0].nodeValue);
            }
        }
    }
    else {
        alert('Your browser does not support some functionality required by this page. To continue please upgrade to the latest release of your browser software.');
        return;
    }

    if (this._items.length == 2) {
        var hiddenControlId = this.id;
        hiddenControlId = hiddenControlId.replace(/Display/, "");
        var hiddenControlItem = document.getElementById(hiddenControlId);

        if (hiddenControlItem != null) {
            hiddenControlItem.value = this._items[1].value;
        }

        this._items[1].selected = true;
        this.disabled = false;
    }
    else {
        this._items[0].selected = true;
    }
};

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');


    /************************************************/
    /*    Client filter support*/
    /************************************************/
    //    this._enableClientFilters = this.get_element().getAttribute("enableClientFilters").toBoolean();
    //    if (this._enableClientFilters) {
    this.set_dataTextField(this.get_element().getAttribute("dataTextField"));
    this.set_dataValueField(this.get_element().getAttribute("dataValueField"));
    this.set_xmlDataIslandName(this.get_element().getAttribute("xmlDataIslandName"));
    this.set_displayDelimiter(this.get_element().getAttribute("displayDelimiter"));
    //    }

    /************************************************/
    /*    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;
};


