/* public */
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 == true) {
			selectedItems += linkedControl.options[i].value + ",";
		}
	}
	selectedItems = selectedItems.substr(0, selectedItems.length - 1).split(",");

	// Clear the linkedControl list	
	linkedControl.options.length=0;
	
	if(linkedControl.childNodes)
	{
		for(var i = linkedControl.childNodes.length-1; i>=0; i--) 
		{
			if(linkedControl.childNodes[i].tagName=='OPTGROUP')
			{
				linkedControl.removeChild(linkedControl.childNodes[i]);
			}
		}
	}
	
	if (showAll.length > 0) 
	{
		// Add the 'All' option - this must be done before option groups are added
		addToListTop(linkedControl, showAll, "");
	}
	
	// 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 == true) {
			addItems(masterControl.options[i].value, linkedControl);
		}
	}

	// Sort the linkedControl only if there are no option groups
	var allOptGroups = linkedControl.getElementsByTagName('OPTGROUP');
	
	if(allOptGroups && allOptGroups.length<1)
	{
		if (showAll.length > 0) 
		{
			// remove the 'All' option before sorting
			linkedControl.options[0]=null;
		}
		
		SortList(linkedControl);
		
		if (showAll.length > 0) 
		{
			// Add the 'All' option back
			addToListTop(linkedControl, showAll, "");
			// clear the linked list selection (Safari bug)	
			linkedControl.value='';	
		}		
	}
	else
	{
		// we are not sorting, but have to clear selection (Safari bug)
		linkedControl.value='';	
	}
	
	// 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.selectedIndex = j;
			}
		}
	}
}

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].setAttribute('selected', false); // ie6 bug workaround
	}
}

function SelectControlItem(value, linkedControl) {
	for (var j = 0; j < linkedControl.options.length; j++) {
		if (linkedControl.options[j].value == value) {
			linkedControl.options[j].setAttribute('selected', true); // ie6 bug workaround
			break;
		}
	}	
}

/* private */
function isOther(name) {	
	if ((String(name).indexOf("Regional") == 0) || (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);
	}
}

