// add an option element
function addOption(objSelect, sText, sValue)
{
	var objOption = document.createElement('option');
	objOption.text = sText;
	objOption.value = sValue;
	try
	{
		objSelect.add(objOption, null); // standards compliant; doesn't work in IE
	}
	catch(ex)
	{
		objSelect.add(objOption); // IE only
	}
}

// remove all option elements
function removeAllOptions(objSelect)
{
	objSelect.length = 0;
}

// this method displays what is selected in the combo box in the input div
function displaySelected(obj, divName)
{
	var objDiv = document.getElementById(divName);
	var sSelectedItems = "";
	for (var iLoop=0; iLoop<obj.length; iLoop++)
	{
		if (obj.options[iLoop].selected)
		{
			sSelectedItems += obj.options[iLoop].text + ', ';
		}
	}
	if (sSelectedItems != '')
	{
		objDiv.innerHTML = "<b><u>Selected:</u></b> " + sSelectedItems.substring(0, sSelectedItems.length-2);
	}
	else
	{
		objDiv.innerHTML = "Hold down the 'Ctrl' key and click to select multiple items.";
	}
}

// select the input option
function selectOption(sId, sValue, bText)
{
	var ele = document.getElementById(sId);
	if (!ele || sValue == '') return;
	if (ele.type == 'select-one')
	{
		for (var iLoop = 0; iLoop < ele.length; iLoop++)
		{
			var sOption = bText? ele.options[iLoop].text : ele.options[iLoop].value;
			if (sOption == sValue)
			{
				ele.selectedIndex = iLoop;
				break;
			} 
		}
	}
	else if (ele.type == 'select-multiple')
	{
		sValue = sValue.split(', ');
		for (var iLoop = 0; iLoop < ele.length; iLoop++)
		{
			for (var jLoop = 0; jLoop < sValue.length; jLoop++)
			{
				var sOption = bText? ele.options[iLoop].text : ele.options[iLoop].value;
				if (sOption == sValue[jLoop]) ele.options[iLoop].selected = 1;
			}
		}
	}
}

// select or unselect all options
function selectAllOptions(bSelect, obj)
{
	// validation
	if (!obj) {	alert("Object not found!"); return; }
	// select/unselect all
	for (var iLoop = 0; iLoop < obj.length; iLoop++)
		obj[iLoop].checked = bSelect;
}

// return all selected options in a string
function getSelectedOptions(obj)
{
	// validation
	if (!obj) {	alert("Object not found!"); return; }
	// build select options string
	var sReturn = "";
	for (var iLoop = 0; iLoop < obj.length; iLoop++)
		if (obj[iLoop].checked) sReturn += obj[iLoop].value + ", ";
	// tidy up and return
	if (sReturn != "") sReturn = sReturn.substring(0, sReturn.length-2);
	return sReturn;
}

function gotoOption(sValue, sId, iOffset)
{
	var obj = document.getElementById(sId);
	var sSearch = sValue.toLowerCase();
	var sSearchLength = sSearch.length;
	for (var iLoop = iOffset; iLoop < obj.options.length; iLoop++)
	{
		var sText = obj[iLoop].text.toLowerCase();
		if (sText.length > sSearchLength)
		{
			if (sSearch == sText.substring(0, sSearchLength))
			{
				obj[iLoop].selected = true;
				break;
			}
		}
	}
}
