function selectAllOptions(obj) {
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
	}
}
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
}
function moveAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		moveSelectedOptions(from,to);
	} else if (arguments.length==3) {
		moveSelectedOptions(from,to,arguments[2]);
	} else if (arguments.length==4) {
		moveSelectedOptions(from,to,arguments[2],arguments[3]);
	}
}
function moveAllOptions2(from,from2,to,to2) {
	selectAllOptions(from);
	if (arguments.length==2) {
		moveSelectedOptions2(from,from2,to,to2);
	} else if (arguments.length==5) {
		moveSelectedOptions2(from,from2,to,to2,arguments[4]);
	} else if (arguments.length==6) {
		moveSelectedOptions2(from,from2,to,to2,arguments[4],arguments[5]);
	}
}
function moveSelectedOptions(from,to) {
	// Unselect matching options, if required
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
		}
	}
	// Move them over
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
			to.options[index] = new Option( o.text, o.value, false, false);
		}
	}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
		}
	}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(from);
		sortSelect(to);
	}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
}
function moveSelectedOptions2(from, from2,to, to2) {
	// Unselect matching options, if required
	if (arguments.length>5) {
		var regex = arguments[5];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
		}
	}
	// Move them over
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
			to.options[index] = new Option( o.text, o.value, false, false);
			if (to2 != null) {
				if (!hasOptions(to2)) { var index = 0; } else { var index=to2.options.length; }
				to2.options[index] = new Option( o.text, o.value, false, false);
			}
		}
	}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			if (from2 != null)
				from2.options[i] = null;
		}
	}
	if ((arguments.length<4) || (arguments[4]==true)) {
		sortSelect(from);
		if (from2 != null)
			sortSelect(from2);
		sortSelect(to);
		if (to2 != null) 
			sortSelect(to2);
	}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	if (to2 != null) 
		to2.selectedIndex = -1;
	if (from2 != null)
		from2.selectedIndex = -1;
}
function sortSelect(obj) {
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
	}
	if (o.length==0) { return; }
	o = o.sort(
		function(a,b) {
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			}
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}
function swapOptions(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 removeOptions(obj){
	for (var i=(obj.options.length-1); i>=0; i--) {
		if (obj.options[i].selected) {
			obj.options[i] = null;
		}
	}
}
function addOptions(obj, text, value) {
	var new_option = document.createElement('option');
    new_option.text = text;
    new_option.value = value;

    try {
    	obj.add(new_option, null); // standards compliant; doesn't work in IE
  	} catch(ex) {
    	obj.add(new_option); // IE only
  	}
}
function moveOptionUp(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i-1].selected) {
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
			}
		}
	}
}
function moveOptionDown(obj) {
	if (!hasOptions(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) {
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
			}
		}
	}
}
function inputAdd(inputBoxId, selectBoxId) {
	var inputBox = document.getElementById(inputBoxId);
	var selectBox = document.getElementById(selectBoxId);
	
	addOptions(selectBox, inputBox.value, inputBox.value);
}
function removeFromSelect(selectBoxId) {
	var selectBox = document.getElementById(selectBoxId);
	
	removeOptions(selectBox);
}
function removeAllFromSelect(selectBoxId) {
	var selectBox = document.getElementById(selectBoxId);
	selectAllOptions(selectBox);
	
	removeFromSelect(selectBoxId);
}

