// written by davidd
// no time for documentation......

function moveOption(leftObj,rightObj,strName,boolRemoveSelected,intForcedIndex,boolReturnValue){
	
	var boolAddOption 	= true;
	var boolReturnValue	= ( arguments.length == moveOption.length && boolReturnValue );
	
	if( intForcedIndex != undefined )
		intIndex = intForcedIndex;
	else
		intIndex = leftObj.selectedIndex;
		
	
	if( leftObj.length == 0 || leftObj.options[intIndex].value == '' || leftObj.options[intIndex].text == 'undefined' )
		alert('You must select ' + strName + ' or submit the form to continue.');
	
	else
	{		
		optionX = new Option( leftObj.options[intIndex].text, leftObj.options[intIndex].value );
		
		
		for( i = 0; i < rightObj.options.length; i++ ){
		
			if( leftObj.options[intIndex].value == rightObj.options[i].value ){
				
				boolAddOption = false;
				break;
			}
		}
		
		if( boolAddOption ){
			rightObj.options[rightObj.length] 				= optionX;
			rightObj.options[rightObj.length-1].selected 	= true;
			//leftObj.options[0].selected 					= true;
		}
		else {
			
			if( leftObj.options[intIndex] )
				alert("You have already selected " + leftObj.options[intIndex].text  + ".\nPlease make another selection.");
		
			if( boolReturnValue ) 
				return false;
		}
		
		
		
		if( boolRemoveSelected ){
			
			intOldIndex 				= intIndex;
			leftObj.options[intIndex] 	= null;
			intNewIndex 				= Math.min( leftObj.options.length-1, intOldIndex );
			
			//if( leftObj.options.length ) 
				//leftObj.options[intNewIndex].selected = true;
		}
	}
	
	if( boolReturnValue ) 
		return true;			
}

function moveAll(leftObj,rightObj,strName,boolRemoveSelected){
	
	var boolAddOption = true;
	
	//if(leftObj.length == 0 || leftObj.options[leftObj.selectedIndex].value == '' || leftObj.options[leftObj.selectedIndex].text == 'undefined')
	if(	leftObj.length == 0 || leftObj.selectedIndex == -1 ) {
		alert('You must select ' + strName + ' or submit the form to continue.');
	}
	else
	{				
		//for( j = leftObj.options.length-1; j >= 0 ; j-- ){
		for( j = 0; j < leftObj.options.length; j++ ){
			
			if( leftObj.options[j] && leftObj.options[j].selected == true ){
				
				boolReturnValue = moveOption(leftObj,rightObj,strName,boolRemoveSelected,j, true );
				j--;				
				if(! boolReturnValue ) 
					return;
			}
		}
		
	}
	
}

function moveUp(selectedObj){
	
	if( selectedObj.selectedIndex == -1  ) {
		alert( "Please make a selection to move" );
	}
	else {
		var num = selectedObj.selectedIndex;
		var optionX = new Option(selectedObj.options[num].text, selectedObj.options[num].value);
			
		if (num == 0)
			alert(optionX.text + " is already at the top of the list.");
		else
		{
			selectedObj.options[num]= new Option(selectedObj.options[num-1].text, selectedObj.options[num-1].value);
			selectedObj.options[num-1]=optionX;
			selectedObj.options[num-1].selected=true;
		}		
	}
}

function moveDown(selectedObj){
	
	if( selectedObj.selectedIndex == -1  ) {
		alert( "Please make a selection to move" );
	}
	else {
		var num = selectedObj.selectedIndex;
		var optionX = new Option(selectedObj.options[num].text, selectedObj.options[num].value);
		
		if (num == selectedObj.length-1)
			alert(optionX.text + " is already at the bottom of the list.");
		else
		{
			selectedObj.options[num]= new Option(selectedObj.options[num+1].text, selectedObj.options[num+1].value);
			selectedObj.options[num+1]=optionX;
			selectedObj.options[num+1].selected=true;
		}
	}
}

function removeAll(leftObj,rightObj,strName){
	
	if(	leftObj.length == 0 || leftObj.selectedIndex == -1 ) {
		alert('You must select ' + strName + ' or submit the form to continue.');
	}
	else
	{				
		for( j = leftObj.options.length-1; j >= 0 ; j-- ){
			
			if( leftObj.options[j] && leftObj.options[j].selected == true ){
				
				intOldIndex 							= leftObj.selectedIndex;
				leftObj.options[leftObj.selectedIndex] 	= null;
				intNewIndex 							= Math.min( leftObj.options.length-1, intOldIndex );
			}
		}
	}
	
	if(leftObj.options.length) 
		leftObj.options[intNewIndex].selected = true;
	
	return;		
}

function selectAll( selObj ){
	
	
	if( selObj.length != 0 ) {		
		
		for( i = 0; i < selObj.options.length; i++ ){
	
			selObj.options[i].selected = true;
		}
	}		
}

function getValues( leftObj, inputName ){
	
	var hasValue = "";
	
	if( leftObj.length )
	{
		for( i = 0; i < leftObj.length; i++ )
		{
			if(i == 0)
				hasValue = leftObj.options[i].value;
			else
				hasValue = hasValue + "," + leftObj.options[i].value;
		}				
	}
		
	inputName.value = hasValue;
}


function getValuesInArray( leftObj, aryArray, intIndex, inputName, boolRemoveDuplicates ){
	
	var aryHasValue = new Array();
	
	if( leftObj.length )
	{
		for(i=0;i < leftObj.length;i++) {
			
			val = aryArray[leftObj.options[i].value][intIndex];
			
			aryHasValue.push( val );
		}	
	}
	
	if( boolRemoveDuplicates )
		aryHasValue = removeDuplicates ( aryHasValue );
		
	inputName.value = aryHasValue.join();
}


function removeDuplicates ( aryToClean ) {
	
	var aryTemp = new Array();
  	
	aryToClean.sort();
  	
	for( i=0; i<aryToClean.length; i++ ) {
  		
		if( aryToClean[i] == aryToClean[i+1] ) 
			continue;
			
  		aryTemp[aryTemp.length] = aryToClean[i];
  	}
  	
	return aryTemp;
}


function sendSortOrderForm( objForm, objSelect, objText ) {			
			
	getValues( objSelect, objText );
	
	objForm.submit();
}

