// JavaScript Document

function moveOver(sourceId, targetId, noSelText) {
	var sourceBox = document.getElementById(sourceId);
	var targetBox = document.getElementById(targetId);
	var boxLength = targetBox.length;
	var selectedItem = sourceBox.selectedIndex;
	var selectedText = sourceBox.options[selectedItem].text;
	var selectedValue = sourceBox.options[selectedItem].value;
	var arrSelected = getSelectedItems(targetId, noSelText);
	var newoption = null;
	var isNew = true;
	var thisitem = 0;
	if (boxLength != 0) {
		for (var i = 0; i < boxLength; i++) {
			thisitem = targetBox.options[i].text;
			if (thisitem == selectedText) {
				isNew = false;
				break;
			}
		}
	} 
	if (isNew) {
		arrSelected[arrSelected.length] = selectedValue;
		targetBox.length = 0;
		var targetBoxLength = targetBox.length;
		var fullSourceList = window['full_'+sourceId];
		var fullListLength = fullSourceList.length
		for (var i = 0; i < fullListLength; i++) {

			for (var x = 0; x < arrSelected.length; x++) {
				if (fullSourceList[i]['value'] == arrSelected[x]) {
					newoption = new Option(fullSourceList[i]['text'], fullSourceList[i]['value'], false, false);
					targetBox.options[targetBoxLength] = newoption;
				}
			}
			targetBoxLength = targetBox.length;
		}
		sourceBox.options[selectedItem] = null;
	}
	sourceBox.selectedIndex=-1;
}

function removeMe(sourceId, targetId, noSelText) {
	var sourceBox = document.getElementById(sourceId);
	var targetBox = document.getElementById(targetId);
	var boxLength = targetBox.length;
	var arrSelected = new Array();
	var count = 0;
	for (var i = 0; i < boxLength; i++) {
		if (targetBox.options[i].selected) {
			arrSelected[count] = targetBox.options[i].value;
		}
		count++;
	}
	for (var i = 0; i < boxLength; i++) {
		for (var x = 0; x < arrSelected.length; x++) {
			if (targetBox.options[i].value == arrSelected[x]) {
				targetBox.options[i] = null;
			}
		}
		boxLength = targetBox.length;
	}
	hideSelectedItems(sourceId, targetId, noSelText)
}

function getSelectedItems(targetId, noSelText) {
	var targetBox = document.getElementById(targetId);
	var targetBoxLength = targetBox.length;
	var arrSelected = new Array();
	var newoption = null;
	var count = 0;
	if (targetBoxLength > 0) {
		for (var i = 0; i < targetBoxLength; i++) {
			if (targetBox.options[i].value > 0) arrSelected[count] = targetBox.options[i].value;
			count++;
		}
	} else {
		newoption = new Option(noSelText, 0, false, false);
		targetBox.options[targetBoxLength] = newoption;
	}
	return arrSelected;
}

function hideSelectedItems(sourceId, targetId, noSelText) {
	var fullSourceList = window['full_'+sourceId];
	var fullListLength = fullSourceList.length
	if (!fullListLength) getSourceItems(sourceId);
	var fullSourceList = window['full_'+sourceId];
	var fullListLength = fullSourceList.length
	var sourceBox = document.getElementById(sourceId);
	var targetBox = document.getElementById(targetId);
	var arrSelected = getSelectedItems(targetId, noSelText);
	var newoption = null;
	sourceBox.length = 0;
	var sourceBoxLength = sourceBox.length;
	for (var i = 0; i < fullListLength; i++) {
		newoption = new Option(fullSourceList[i]['text'], fullSourceList[i]['value'], false, false);
		sourceBox.options[sourceBoxLength] = newoption;
		for (var x = 0; x < arrSelected.length; x++) {
			if (fullSourceList[i]['value'] == arrSelected[x]) {
				sourceBox.options[sourceBoxLength] = null;
			}
		}
		sourceBoxLength = sourceBox.length;
	}
	return;
}

function getSourceItems(sourceId) {
	var sourceBox = document.getElementById(sourceId);
	var sourceBoxLength = sourceBox.length;
	var fullSourceList = new Array();
	for (var i = 0; i < sourceBoxLength; i++) {
		fullSourceList[i] = new Array(2);
		fullSourceList[i]['value'] = sourceBox.options[i].value;
		fullSourceList[i]['text'] = sourceBox.options[i].text;
	}
	window['full_'+sourceId] = fullSourceList;
}

function selectAll(sourceId, targetId, noSelText) {
	var sourceBox = document.getElementById(sourceId);
	var targetBox = document.getElementById(targetId);
	var newoption = null;
	targetBox.length = 0;
	var targetBoxLength = targetBox.length;
	var fullSourceList = window['full_'+sourceId];
	var fullListLength = fullSourceList.length
	for (var i = 0; i < fullListLength; i++) {
		newoption = new Option (fullSourceList[i]['text'], fullSourceList[i]['value'], false, false);
		targetBox.options[targetBoxLength] = newoption;
		targetBoxLength = targetBox.length;
	}
	hideSelectedItems(sourceId, targetId, noSelText)
}

function removeAll(sourceId, targetId, noSelText) {
	var targetBox = document.getElementById(targetId);
	targetBox.length = 0;
	hideSelectedItems(sourceId, targetId, noSelText)
}

