﻿var GetListItemsType = {
    SELECTED: true,
    ALL: false
};

function ItemSelectionManager(selectedItemList, optionalItemList, addButton, removeButton, clearButton) {
    this.SelectedItemList = selectedItemList;
    this.OptionalItemList = optionalItemList;

    AddHandler(addButton, 'click', CreateDelegate(this, this.AddButtonClickHandler));
    AddHandler(removeButton, 'click', CreateDelegate(this, this.RemoveButtonClickHandler));
    AddHandler(clearButton, 'click', CreateDelegate(this, this.ClearButtonClickHandler));
    AddHandler(document.forms[0], 'submit', CreateDelegate(this, this.FormSubmitHandler));
}

ItemSelectionManager.prototype.ClearButtonClickHandler = function () {
    this.MoveSelectedItems(this.SelectedItemList, this.OptionalItemList, GetListItemsType.ALL);
};

ItemSelectionManager.prototype.AddButtonClickHandler = function () {
    this.MoveSelectedItems(this.OptionalItemList, this.SelectedItemList, GetListItemsType.SELECTED);
};

ItemSelectionManager.prototype.RemoveButtonClickHandler = function () {
    this.MoveSelectedItems(this.SelectedItemList, this.OptionalItemList, GetListItemsType.SELECTED);
};

ItemSelectionManager.prototype.FormSubmitHandler = function () {
    for (index = 0; index < this.SelectedItemList.options.length; index++) {
        this.SelectedItemList.options[index].selected = true;
    }

    return true;
};

ItemSelectionManager.prototype.MoveSelectedItems = function (fromList, toList, selected) {
    var selectedItems;

    if (selected) {
        selectedItems = this.GetSelectedItems(fromList);
    }
    else {
        selectedItems = this.GetAllItems(fromList);
    }

    for (index = 0; index < selectedItems.length; index++) {
        var option = new Option(selectedItems[index].Text, selectedItems[index].Value);
        option.selected = true;
        toList.add(option);
    }

    for (index = selectedItems.length - 1; index >= 0; index--) {
        fromList.remove(selectedItems[index].Index);
    }
};

ItemSelectionManager.prototype.GetSelectedItems = function (list) {
    var selectedItems = new Array();

    for (index = 0; index < list.options.length; index++) {
        if (list.options[index].selected) {
            selectedItems.push(new ListOptionItem(list.options[index].text, list.options[index].value, index));
        }
    }

    return selectedItems;
};

ItemSelectionManager.prototype.GetAllItems = function (list) {
    var selectedItems = new Array();

    for (index = 0; index < list.options.length; index++) {
        selectedItems.push(new ListOptionItem(list.options[index].text, list.options[index].value, index));
    }

    return selectedItems;
};

function ListOptionItem(text, value, index) {
    this.Text = null;
    this.Value = null;
    this.Index = null;

    if (text != null && text != undefined && text != "") { this.Text = text; }
    if (value != null && value != undefined && value != "") { this.Value = value; }
    if (index != null && index != undefined && index != "") { this.Index = index; }
}


