
function MoveDropDown(tsource, tdest){
	var thelength = 0;
	var returnvalue;
	for (var i = tsource.length - 1; i >= 0; i--) {
		if (tsource.options[i].selected) {
			returnvalue = tsource.options[i].value;

			tdest.length ++;
			thelength = tdest.length;
			
			tdest.options[thelength - 1].text = tsource.options[i].text;
			tdest.options[thelength - 1].value = tsource.options[i].value;
			
		
			var oldlength = tsource.length;
			for (var j = i; tsource.length > j + 1; j++) {
				tsource.options[j].value = tsource.options[j + 1].value;
				tsource.options[j].text = tsource.options[j + 1].text;
			}
			
			tsource.options[j].value = "";
			tsource.options[j].text = "";
			tsource.length = tsource.length - 1;
		}
	}
	return (returnvalue);
}
function MoveDropDown2(tsource, tdest){
	for (var i = 0; i < tsource.length; i++) {
		if (tsource.options[i].selected) {
			AddIf(tdest,tsource.options[i].text,tsource.options[i].value)
		}
	}
	RemoveSelected(tsource);
}

function SelectOption(tsource, thevalue, nocase){
	tsource.selectedIndex = -1;
	var hit = 0;
	for (var i = 0; i < tsource.length; i++) {
		if (listfind(thevalue.toLowerCase(),tsource.options[i].value.toLowerCase())>0 || tsource.options[i].value.toLowerCase() == thevalue.toLowerCase()) {
			tsource.options[i].selected = true;
			if (hit == 0 ){
//				alert(i + ' ' + tsource[i].text);
				tsource.selectedIndex = i;
				hit = 1;
			}
		}
	}
}
function RemoveSelected(tsource){

	for (var x = tsource.length-1; x>= 0; x--){
		if (tsource.options[x].selected){
			for (var j = x; tsource.length > j + 1; j++) {
				tsource.options[j].value = tsource.options[j + 1].value;
				tsource.options[j].text = tsource.options[j + 1].text;
			}
			
			tsource.options[j].value = "";
			tsource.options[j].text = "";
			tsource.length = tsource.length - 1;
		}
	}
	tsource.selectedIndex = -1;
}

function ChangeSelected(tsource, thetext, thevalue){

	for (var x = tsource.length-1; x>= 0; x--){
		if (tsource.options[x].selected){
			tsource.options[x].value = thevalue;
			tsource.options[x].text = thetext;
		}
	}
}

function AddToSelect(destdrop, thetext, thevalue){
	destdrop.length ++;
	thelength = destdrop.length;
	destdrop.options[thelength - 1].text = thetext;
	destdrop.options[thelength - 1].value = thevalue;
	return (1);
}


function SelectAll(tsource){
	for (var x = 0; x < tsource.length; x++){
		tsource.options[x].selected = true;
	}
}

function RemoveAll(formelement){
	var thelength = formelement.length
	for (var i = 0; thelength > i; i++) {
		RemoveLast(formelement);
	}
}


function RemoveLast(formelement){
	var thelength = formelement.length;
	
	if (thelength >= 1){
		formelement.options[thelength - 1].value = "";
		formelement.options[thelength - 1].text = "";
		formelement.length = thelength - 1;	
	}
}

function MoveUpDown(source, whichway){
	s = source.selectedIndex;
	if (s == 0 && whichway == 'up'){
		return;
	}
	if (s == (source.length-1) && whichway == 'down'){
		return;
	}
	if(whichway == 'up'){
		offset = -1;
	}else{
		offset = 1;
	}
	v = source.options[s + offset].value;
	t = source.options[s+ offset].text;
	source.options[s + offset].value = source.options[s].value;
	source.options[s + offset].text = source.options[s].text;
	source.options[s].value = v;
	source.options[s].text = t;
	source.options[s + offset].selected = true;
	source.options[s].selected = false;
}