// REQUIRES: `form` variable to be instantiated inside page including this file
// `form` has to be a DOM object - form that contains `more` combo boxes
var form;

var MoreCombos = {};

function addMoreCombo(cbName)
{
    MoreCombos[cbName] = {};
    
    $(form[cbName]).change(function(e){
        var value = $(this).val();
        if(!value)
        {
            this.selectedIndex = 0;
            alert("Select items(s) from the list!");
            return;
        }
        
        try
        {
            if(value == "none")
            {
                // clear all
                for(var item in MoreCombos[cbName])
                    MoreCombos[cbName][item] = undefined;
            }
            else
            {
                // clear `None`
                MoreCombos[cbName]["none"] = undefined;
            }
            
            MoreCombos[cbName][value] = $("option[@value="+value+"]", this).text();
            
            
            this.selectedIndex = 0;
            updateList(cbName);
        }
        catch(e){}
    });
}

function updateList(cbName)
{
    var list = $("#"+cbName)[0];
    $(list).empty();
    for(var item in MoreCombos[cbName])
        if(MoreCombos[cbName][item])
            $(list).append("<p><label class=\"label\">&nbsp;</label>"+MoreCombos[cbName][item]+" <a href=\"javascript: restore('"+cbName+"', '"+item+"')\">x</a></p>");
}

function restore(cbName, item)
{
    MoreCombos[cbName][item] = undefined;
    updateList(cbName);
}