// SELECT BOX
// +------------------------------------------------------------------------------------------------------------------------------+
// | 
// +------------------------------------------------------------------------------------------------------------------------------+
// | AUTHORS: 
// | Matt Kruse (matt@mattkruse.com) - http://www.mattkruse.com/
// | Neil Champion (neil@cambridgenewmedia.co.uk) - http://www.cambridgenewmedia.co.uk
// | Macromedia (JumpMenu) - http://www.macromedia.com
// +------------------------------------------------------------------------------------------------------------------------------+
// | INSTRUCTIONS:
// +------------------------------------------------------------------------------------------------------------------------------+
// | COMMENTS:
// +------------------------------------------------------------------------------------------------------------------------------+
// | FILES REQUIRED FOR PROCESSING
// | Please read the instructions and comments for all included files
// | none
// +------------------------------------------------------------------------------------------------------------------------------+
// | VERSION HISTORY:
// | v 1.0     |     03.01.2004     |     Original version
// +------------------------------------------------------------------------------------------------------------------------------+

var selected1 = false;
var selected2 = false;
var i = 0;
var o = false;
//
function unselect (field) {
	if (field.selectedIndex != -1) {field.selectedIndex = -1;}
}

function selectUnselectMatchingOptions(obj,regex,which,only) {
	if (window.RegExp) {
		if (which == "select") {
			selected1=true;
			selected2=false;
		}
		else if (which == "unselect") {
			selected1=false;
			selected2=true;
		}
		else{return;}
		var re = new RegExp(regex);
		for (i=0;i<obj.options.length;i++) {
			if (re.test(obj.options[i].text)) {obj.options[i].selected = selected1;}
			else{
				if (only == true) {obj.options[i].selected = selected2;}
			}
		}
	}
}

function selectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",false);
}

function selectOnlyMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",true);
}

function unSelectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"unselect",false);
}

function sortSelect(obj) {
	o = new Array();
	if(obj.options==null) {return;}
	for (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 (i=0;i<o.length;i++) {o
		bj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	}
}

function selectAllOptions(obj) {
	for (i=0;i<obj.options.length;i++) {
		obj.options[i].selected = true;
	}
}

function moveSelectedOptions(from,to) {
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {unSelectMatchingOptions(from,regex);}
	}
	for (i=0;i<from.options.length;i++) {
		o = from.options[i];
		if (o.selected) {to.options[to.options.length] = new Option( o.text, o.value, false, false);}
	}
	for (i=(from.options.length-1);i>=0;i--) {
		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 copySelectedOptions(from,to) {
	var options = new Object();
	for (i=0;i<to.options.length;i++) {
		options[to.options[i].value] = to.options[i].text;
	}
	for (i=0;i<from.options.length;i++) {
		o = from.options[i];
		if (o.selected) {
			if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {to.options[to.options.length] = new Option( o.text, o.value, false, false);}
		}
	}
	if ((arguments.length<3) ||(arguments[2]==true)) {sortSelect(to);}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
}

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 copyAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {copySelectedOptions(from,to);}
	else if (arguments.length==3) {copySelectedOptions(from,to,arguments[2]);}
}

function swapOptions(obj,i,j) {
	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 moveOptionUp(obj) {
	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) {
	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 removeSelectedOptions(from) {
	for (i=(from.options.length-1);i>=0;i--) {
		o=from.options[i];
		if (o.selected) {from.options[i] = null;}
	}
	from.selectedIndex = -1;
}

function removeAllOptions(from) {
	for (i=(from.options.length-1);i>=0;i--) {
		from.options[i] = null;
	}
	from.selectedIndex = -1;
}

function addOption(obj,text,value,selected) {
	if (obj!=null && obj.options!=null) {obj.options[obj.options.length] = new Option(text, value, false, selected);}
}

//Jumps to location specified in the value attribute of the selected field
//v3.0
function MM_jumpMenu(targ,selObj,restore){ //
	if (selObj.options[selObj.selectedIndex].value != '') {eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'")};
	if (restore) selObj.selectedIndex=0;
}

