/******************************************************************************
	Please leave this intact when using this code =)
		
	developed and distributed on http://blog.jadecell.org
	copyright 2007

	This code can only be used on non-commercial websites
	- you can not claim this code as your own
	- you can not publish (media: books, cd's, magazines, etc) 
		this without my expressed WRITTEN consent
	- please let me know if you use this code 
		(so i know that its being put to good use)
													
	If you wish to use this for instruction, please be sure to let
		me know, things may have changed to make this more efficient or
		I may have additional tools to help you with this
********************************************************************************/

function moveModuleByValue(fromID,toID){
	//define the 'from' and 'to' pulldown (respectively)
	var from	= document.myform[fromID];
	var to		= document.myform[toID];
	
	//grab the selection in the 'from' pulldown
	var fromSelected = from.selectedIndex;
	
	//if there are no values to move in the 'from' pulldown create an alert
	if(fromSelected == -1){
		alert("No values left to be moved!");
	} else {
		//grab the value and text (respectively) from the 'from' pulldown
		fromValue = from[fromSelected].value;
		fromText = from[fromSelected].text;

		//if there are no selections in the two
		//	create a a brand new option
		if(to.length == 0){
			to.options[0] = new Option(
					fromText,		//the text property
					fromValue,		//the value property
					false,			//teh defaultSelected property
					false			//the selected property
				);
		} else {

			//since we know there is at least one value in the to pulldown, lets run a for loop
			for(toSelected=to.length-1;toSelected>=0;toSelected--){
				toCurrentValue = to[toSelected].value;	//current 'to' value used for comparison

				//if there is no next selection in the 'to' pulldown
				if((toSelected-1) < 0){

					//if the current 'to' value is at least the 'from' value
					if(toCurrentValue <= fromValue){
						//create a new option at the end
						//	(we know that 0 is the initial selection so we'll create one
						//		in array position 1 instead)
						to.options[1] = new Option (fromText,fromValue,false,false);
					} else {
						//otherwise we need to make the current one the second option and the
						//	new one (or the selected 'from' pulldown) the current option
						toText = to[toSelected].text;
						toValue = to[toSelected].value;
						to.options[1] = new Option (toText,toValue,false,false);
						to.options[0] = new Option (fromText,fromValue,false,false);
					}
				} else {
					//there is a next selection in the 'to' pulldown
					toNextValue = to[toSelected - 1].value;
					//if the 'from' selected value is in between the first and next value
					if(fromValue <= toCurrentValue && fromValue > toNextValue){
						//then make the current value a new option and have the new 'from' 
						//	text/value take its spot
						toCurrentText = to[toSelected].text;
						to.options[toSelected+1] = new Option (toCurrentText,toCurrentValue,false,false);
						to.options[toSelected] = new Option (fromText,fromValue,false,false);
						break;
					} else if(fromValue > toCurrentValue && toSelected == (to.length-1)){
						//or if the 'from' value is greater than the current 'to' value and
						//	the selected 'to' is the last selection, then add a new option at
						//	at the end
						to.options[toSelected + 1] = new Option (fromText,fromValue,false,false);
						break;
					} else if(fromValue < toNextValue && toSelected == 0){
						//or if the 'from' value is less than the next value and we've reached the
						//	first in the pulldown, then make a new option as the first
						to.options[toSelected] = new Option (fromText,fromValue,false,false);
						break;
					} else {
						//or for anything in between the first and last of the 'to' selections
						//	add the 
						toCurrentText = to[toSelected].text;
						toNextText = to[toSelected-1].text;
						to.options[toSelected+1] = new Option (toCurrentText,toCurrentValue,false,false);
						to.options[toSelected] = new Option (toNextText,toNextValue,false,false);
					}
				}
			}
		}
		
		//clear out this value in the from pulldown
		from.options[fromSelected] = null;
	}
}

function moveModuleByText(fromID,toID){
	//define the 'from' and 'to' pulldown (respectively)
	var from	= document.myform[fromID];
	var to		= document.myform[toID];
	
	//grab the selection in the 'from' pulldown
	var fromSelected = from.selectedIndex;
	
	//if there are no values to move in the 'from' pulldown create an alert
	if(fromSelected == -1){
		alert("No values left to be moved!");
	} else {
		//grab the value and text (respectively) from the 'from' pulldown
		fromValue = from[fromSelected].value;
		fromText = from[fromSelected].text;

		//if there are no selections in the two
		//	create a a brand new option
		if(to.length == 0){
			to.options[0] = new Option(
					fromText,		//the text property
					fromValue,		//the value property
					false,			//teh defaultSelected property
					false			//the selected property
				);
		} else {

			//since we know there is at least one value in the to pulldown, lets run a for loop
			for(toSelected=to.length-1;toSelected>=0;toSelected--){
				toCurrentValue = to[toSelected].value;	//current 'to' value used for comparison
				toCurrentText = to[toSelected].text;

				//if there is no next selection in the 'to' pulldown
				if((toSelected-1) < 0){

					//if the current 'to' Text is at least the 'from' Text
					if(toCurrentText <= fromText){
						//create a new option at the end
						//	(we know that 0 is the initial selection so we'll create one
						//		in array position 1 instead)
						to.options[1] = new Option (fromText,fromValue,false,false);
					} else {
						//otherwise we need to make the current one the second option and the
						//	new one (or the selected 'from' pulldown) the current option
						to.options[1] = new Option (toCurrentText,toCurrentValue,false,false);
						to.options[0] = new Option (fromText,fromValue,false,false);
					}
				} else {
					//there is a next selection in the 'to' pulldown
					toNextValue = to[toSelected - 1].value;
					toNextText = to[toSelected - 1].text;
					//if the 'from' selected Text is in between the first and next Text
					if(fromText <= toCurrentText && fromText > toNextText){
						//then make the current Text a new option and have the new 'from' 
						//	text/value take its spot
						to.options[toSelected+1] = new Option (toCurrentText,toCurrentValue,false,false);
						to.options[toSelected] = new Option (fromText,fromValue,false,false);
						break;
					} else if(fromText > toCurrentText && toSelected == (to.length-1)){
						//or if the 'from' Text is greater than the current 'to' Text and
						//	the selected 'to' is the last selection, then add a new option at
						//	at the end
						to.options[toSelected + 1] = new Option (fromText,fromValue,false,false);
						break;
					} else if(fromText < toNextText && toSelected == 0){
						//or if the 'from' Text is less than the next Text and we've reached the
						//	first in the pulldown, then make a new option as the first
						to.options[toSelected] = new Option (fromText,fromValue,false,false);
						break;
					} else {
						//or for anything in between the first and last of the 'to' selections
						//	add the 
						to.options[toSelected+1] = new Option (toCurrentText,toCurrentValue,false,false);
						to.options[toSelected] = new Option (toNextText,toNextValue,false,false);
					}
				}
			}
		}
		
		//clear out this Text in the from pulldown
		from.options[fromSelected] = null;
	}
}
