// Phil S, Apr 2007

function trim(inputString) {
	if (typeof inputString!='string') {
		return inputString;
	} 
	var retValue=inputString; 
	var ch=retValue.substring(0, 1); 
	while (ch==' ') {
		retValue=retValue.substring(1,retValue.length); 
		ch=retValue.substring(0, 1);
	} 
	ch=retValue.substring(retValue.length-1, retValue.length); 
	while (ch==' ') {
		retValue=retValue.substring(0, retValue.length-1); 
		ch=retValue.substring(retValue.length-1,retValue.length);
	} 
	while (retValue.indexOf('  ')!=-1) {
		retValue=retValue.substring(0,retValue.indexOf('  ')) + retValue.substring(retValue.indexOf('  ')+1,retValue.length);
	} 
	return retValue;
}

function addValue(theForm,theSourceField,theDestField,text,val,show) {
	var thisitem;
	var newoption; 
	var isNew = true; 
	var boxLength = document[theForm][theDestField].length; 
	for (var i = 0; i < boxLength; i++) {
		thisitem = document[theForm][theDestField].options[i].text; 
		if (thisitem == text) {
			isNew = false; 
			i = boxLength;
			alert("You have already selected this item");
		}
	} 
	if (isNew) {
		var newoption = new Option(text, val, false, false); 
		document[theForm][theDestField].options[boxLength] = newoption;
		boxLength++;
	} 
	if (document.getElementById("selectionTotal")) {
		document.getElementById("selectionTotal").innerHTML = boxLength;
	}
}

function addItem(theForm,theSourceField,theDestField) {
	var fld=document[theForm][theSourceField];
	if (fld.selectedIndex >= 0) {			
		var si=fld.selectedIndex;
		var st=trim(fld.options[si].text);
		var sv=fld.options[si].value;
		addValue(theForm,theSourceField,theDestField,st,sv,true);
		document[theForm][theSourceField].selectedIndex=-1;
	}
	else {
		alert("Please choose an item to add");
	}
}

function addItemAll(theForm,theSourceField,theDestField) {
	var fld=document[theForm][theSourceField];
	var bl=fld.length;
	as=new Array();
	if (fld.selectedIndex >= 0) {		
		for (var i=0;i<bl;i++) {
			if (fld.options[i].selected) {
				as[i]=fld.options[i].value;
				//alert(fld.selectedIndex);
			}
		}
		for (var i=0;i<bl;i++) {
			for (var x=0;x<as.length;x++) {
				if (fld.options[i].value==as[x]) {
				//alert(fld.options[i].value);
				 //alert(fld.selectedIndex);
					//var si=fld.selectedIndex;
					var st=trim(fld.options[i].text);
					var sv=fld.options[i].value;
					addValue(theForm,theSourceField,theDestField,st,sv,true);
					document[theForm][theSourceField].selectedIndex=-1;
				}
				bl=fld.length;
			}
		}
			
	}
	else {
		alert("Please choose an item to add");
	}
}

function removeItem(theForm,theSourceField,theDestField) {
	var fld=document[theForm][theDestField];
	var bl=fld.length;
	as=new Array();
	if (fld.selectedIndex >= 0) {		
		for (var i=0;i<bl;i++) {
			if (fld.options[i].selected) {
				as[i]=fld.options[i].value;
				//alert(as[i]);
			}
		}
		for (var i=0;i<bl;i++) {
			for (var x=0;x<as.length;x++) {
				if (fld.options[i].value==as[x]) {
					fld.options[i]=null;
				}
				bl=fld.length;
			}
		}
		if (document.getElementById("selectionTotal")) {
			document.getElementById("selectionTotal").innerHTML = bl;
		}
	}
	else {
		alert("Please choose an item to remove");
	}
}

function hasItems(obj) {
	if (obj!=null && obj.options!=null) { 
		return true; 
	}
	return false;
}
	
function swapItems(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
}
	
function moveItemUp(theform,thefield) {
	var obj = document[theform][thefield];
	if (!hasItems(obj)) { 
		return; 
	}
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i-1].selected) {
				swapItems(obj,i,i-1);
				obj.options[i-1].selected = true;
			}
		}
	}
}
	
function moveItemDown(theform,thefield) {
	var obj = document[theform][thefield];
	if (!hasItems(obj)) { 
		return; 
	}
	for (i=obj.options.length-1; i>=0; i--) {
		if (obj.options[i].selected) {
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
				swapItems(obj,i,i+1);
				obj.options[i+1].selected = true;
			}
		}
	}
}
	
function verifySortBox(theform,thefield) {
	var obj = document[theform][thefield];
	if (!hasItems(obj)) { 
		return; 
	}
	for (i=0; i < obj.options.length; i++) {
		obj.options[i].selected = true;
	}
	//document[theform].submit();
}
