function addToList(list, text, value) {
	list.length++;
	list.options[list.length-1].value = value;
	list.options[list.length-1].text  = text;
}

function isOtherSector(sectorName) {	
	if (String(sectorName).indexOf("Other") == 0) {
		return true;
	} else {
		return false;
	}
}

function isLessThan(sectorName1, sectorName2) {
	if (isOtherSector(sectorName1) && !isOtherSector(sectorName2)) {
		return false;
	} else if (!isOtherSector(sectorName1) && isOtherSector(sectorName2)) {
		return true;
	} else {
		return (sectorName1 < sectorName2);
	}
}

function sortList(list) {
	for (var i = 0; i < (list.length - 1); i++) {
		for (var j = i + 1; j < list.length; j++) {
			if (isLessThan(list.options[j].text, list.options[i].text)) {
				var tempValue = list.options[i].value;
				var tempText  = list.options[i].text;
				list.options[i].value = list.options[j].value;
				list.options[i].text  = list.options[j].text;
				list.options[j].value = tempValue;
				list.options[j].text  = tempText;
			}
		}
	}
}

function addToListTop(list, text, value) {
	list.length++;
	for (i = list.length-1; i > 0; i--) {
		list.options[i].value = list.options[i-1].value;
		list.options[i].text  = list.options[i-1].text;
	}
	list.options[0].value = value;
	list.options[0].text  = text;
}

function option_link(masterControl, linkedControl, showAll, addItems, selected) {
	selected == "" || selected == null ? selected = '' : selected = selected;
	// Collect all the currently selected linkedControl items for later reselection
	var selectedItems = "";
	for (var i = 0; i < linkedControl.options.length; i++) {
		if (linkedControl.options[i].selected) {
			selectedItems += linkedControl.options[i].value + ",";
		}
	}
	selectedItems = selectedItems.substr(0, selectedItems.length - 1).split(",");

	// Clear the linkedControl list
	for (var i = linkedControl.options.length; i >= 0; i--) {
		linkedControl.options[i] = null;
	}

	// Repopulate the linkedControl list depending on the selected items in the masterControl
	for (var i = 0; i < masterControl.options.length; i++) {
		if (masterControl.options[i].selected) {
			addItems(masterControl.options[i].value, linkedControl);
		}
	}

	// Sort the linkedControl
	sortList(linkedControl);

	if (showAll.length > 0) {
		// Add the 'All' option
		addToListTop(linkedControl, showAll, "" + selected + "");
	}

	// Reselect the linkedControl
	clearControl(linkedControl);
	for (var i = 0; i < selectedItems.length; i++) {
		for (var j = 0; j < linkedControl.options.length; j++) {
			if (linkedControl.options[j].value == selectedItems[i]) {
				linkedControl.options[j].selected = true;				
			}
		}
	}
}

function clearControl(ctl) {
	for (var i = 0; i < ctl.options.length; i++) {
		ctl.options[i].selected = false;
	}
}

function selectControlItem(value, linkedControl) {
	for (var j = 0; j < linkedControl.options.length; j++) {
		if (linkedControl.options[j].value == value) {
			linkedControl.options[j].selected = true;
			break;
		}
	}
}

function checkInput(keywords, categories) {
	var isValid = false;
	if (keywords.value.length > 0) {
		isValid = true;
	} else {
		for (var i = 0; i < categories.options.length; i++) {
			if (categories.options[i].selected == true && categories.options[i].value != 0) {
				isValid = true;
				break;
			}
		}
	}
	if (!isValid) {
		alert("Please enter at least one keyword or select one sector to begin your search.");
	}
	return isValid;
}

