function LinkedControlChange(masterControl, linkedControl, showAll, addItems) {
	// 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 -= 1) {
		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, "");
	}
	// 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 addToList(list, text, value) {
	list.length++;
	list.options[list.length-1].value = value;
	list.options[list.length-1].text = text;
}

function addToListTop(list, text, value) {
	list.length++;
	for (i=list.length-1; i>0; i=i-1) {
		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 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 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 isOther(name) {
	if (String(name).indexOf("Other") == 0) {
		return true;
	} else {
		return false;
	}
}

function isLessThan(name1, name2) {
	if (isOther(name1) && !isOther(name2)) {
		return false;
	} else if (!isOther(name1) && isOther(name2)) {
		return true;
	} else {
		return (name1<name2);
	}
}

function checkInput(keywords, categories) {
	if (keywords.value.length>0) {
		return true;
	} else {
		for (var i = 0; i<categories.options.length; i++) {
			if (categories.options[i].selected == true && categories.options[i].value != 0) {
				return true;
			}
		}
	}
	alert("Please enter at least one keyword or select one sector to begin your search.");
	return false;
}

function getElement(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	} else if (document.all) {
		return document.all[id];
	} else if (document.layers) {
		var name = id;
		var index = name.indexOf('_');
		if (index>=0) {
			var len = name.length;
			name = name.substring(0, index)+":"+name.substring(index+1, len);
		}
		return document.forms[1].elements[name];
	}
	return id;
}