function HTML_Form_Listbox_AddItem(list, key, text, noCheck)
{
	if (typeof(list) == "string") list = HTML_Form_Field(list);
	if (!noCheck) {
		var index = HTML_Form_Listbox_FindItem(list, key);
		if (index >= 0) {
			list.options[index].text = text;
			return true;
		}
	}
	if (list.options.add)
	{
		newOpt = document.createElement("OPTION");
		newOpt.value = key;
		newOpt.text = text;
		list.options.add(newOpt, list.length);
	} else {
	    var optionObj = new Option(text, key, false, false);
    	list.options[list.length] = optionObj;
	}
}

function HTML_Form_Listbox_AddArray(list, values, noCheck)
{
	if (typeof(list) == "string") list = HTML_Form_Field(list);
	var key; for (key in values) HTML_Form_Listbox_AddItem(list, key, values[key], noCheck);
}

function HTML_Form_Listbox_RemoveItem(list, key)
{
	if (typeof(list) == "string") list = HTML_Form_Field(list);
	var index = HTML_Form_Listbox_FindItem(list, key);
	if (index == -1) return false;
	
	if (list.options.remove) list.options.remove(index);
	else list.options[index] = null;
}

function HTML_Form_Listbox_RemoveAll(list)
{
	if (typeof(list) == "string") list = HTML_Form_Field(list);
	list.options.length = 0;
}

function HTML_Form_Listbox_FindItem(list, key)
{
	for (var i = 0; i < list.length; i++) if (list.options[i].value == key)	return i;
	return -1;		
}

function HTML_Form_Listbox_RemoveSelected(list)
{
	if (typeof(list) == "string") list = HTML_Form_Field(list);	
	var list = HTML_Form_Field(listName);
	var selectedItem = list.selectedIndex;
	var maxItems = list.length-1;
	if ((maxItems < 0) || (selectedItem < 0)) return;
	if (list.options.remove) list.options.remove(selectedItem);
	else list.options[selectedItem] = null;
}
