﻿var supressDropDownClosing = false;

function Post_DDMS_OnClientDropDownClosing(sender, eventArgs) {
    eventArgs.set_cancel(supressDropDownClosing);
}

function Post_DDMS_OnClientSelectedIndexChanging(sender, eventArgs) {
    eventArgs.set_cancel(supressDropDownClosing);
}

function Post_DDMS_OnClientDropDownOpening(sender, eventArgs) {
    supressDropDownClosing = true;
}

function Post_DDMS_OnClientBlur(sender) {
    supressDropDownClosing = false;
    sender.toggleDropDown();
}

function Post_DDMS_SelectedItems(ControlID, ValueControlID, DefaultText) {
    var combo = $find(ControlID);

    var items = combo.get_items();

    var selectedItemsTexts = "";
    var selectedItemsValues = "";

    var itemsCount = items.get_count();

    for (var itemIndex = 0; itemIndex < itemsCount; itemIndex++) {
        var item = items.getItem(itemIndex);

        var checkbox = Post_DDMS_GetItemCheckBox(item);

        //Check whether the Item's CheckBox) is checked.
        if (checkbox.checked) {
            selectedItemsTexts += item.get_text() + ", ";
            selectedItemsValues += item.get_value() + ", ";
        }
    }

    selectedItemsTexts = selectedItemsTexts.substring(0, selectedItemsTexts.length - 2);
    selectedItemsValues = selectedItemsValues.substring(0, selectedItemsValues.length - 2);

    //Set the text of the RadComboBox with the texts of the selected Items, separated by ','.
    combo.set_text(selectedItemsTexts);

    //Set the comboValue hidden field value with values of the selected Items, separated by ','.
    document.getElementById(ValueControlID).value = selectedItemsValues;

    //Clear the selection that RadComboBox has made internally.
    if (selectedItemsValues == "") {
        combo.clearSelection();
        combo.set_text(DefaultText);
    }
}

function Post_DDMS_GetItemCheckBox(item) {
    //Get the 'div' representing the current RadComboBox Item.
    var itemDiv = item.get_element();

    //Get the collection of all 'input' elements in the 'div' (which are contained in the Item).
    var inputs = itemDiv.getElementsByTagName("input");

    for (var inputIndex = 0; inputIndex < inputs.length; inputIndex++) {
        var input = inputs[inputIndex];

        //Check the type of the current 'input' element.
        if (input.type == "checkbox") {
            return input;
        }
    }

    return null;
}


