/*<!---

This creates an object for working with
select boxes and options.
Var List
var SelUtility= new SelectUtility;
--->*/

//
SelectUtility = function ()
	{	var self=this;
		this.TheSelectBox=arguments[0];
		if (1> arguments.length)
		{
			this.TheOptions=arguments[1];
		}
		
		//clear select box
		this.clear = 
			function(TheSelectBox) 
			{
				document.getElementById(TheSelectBox).options.length=0;
			};
			
		//// collapse the drop down box
		this.collapse = 
			function(TheSelectBox)
			{
				document.getElementById(TheSelectBox).style.display='none';
			};
			
		/// Epand the drop down box
		this.expand = 
			function(TheSelectBox)
			{
				document.getElementById(TheSelectBox).style.display='';
			};
		
		//// Hide the drop down box
		this.hide = 
			function(TheSelectBox) 
			{
				document.getElementById(TheSelectBox).style.visibility='hidden';
			};
			
		/// show the drop down box
		this.show = 
			function(TheSelectBox) 
			{
				document.getElementById(TheSelectBox).style.visibility='visible';
			};
		
		///fill the drop down box from an array
		// requires SelectBox,TheOptions
		this.fill = 
			function (TheSelectBox,TheOptions)
			{
				var OptionsList = document.getElementById(TheSelectBox);
				if(OptionsList==null){
					OptionsList=document.getElementByName(TheSelectBox);
					}
				OptionsList.options.length=0;
		
				for(var i=0;i < TheOptions.length;i++)
				{
					OptionsList.options[i]=new Option(TheOptions[i][1], TheOptions[i][0]);	
				}
			};
		
		// requires SelectBox,TheOptions
		this.append = 
			function (TheSelectBox,TheOptions)
			{
				var OptionsList = document.getElementById(TheSelectBox);
		
				var X=OptionsList.length;
		
				for(var i=0;i < TheOptions.length;i++)
				{
					OptionsList.options[X]=new Option(TheOptions[i][1], TheOptions[i][2]);
					X++;
				}
			};
		
		this.empty =
			function(TheSelectBox)
			{
				if(document.getElementById(TheSelectBox)!=null){
				document.getElementById(TheSelectBox).options.length=0;
				}
				else{
					document.getElementByName(TheSelectBox).options.length=0;
					}
			};
		
		
		this.getAll=
			function(TheSelectBox,ThefillBox)
			{
				var OptionsList = document.getElementById(TheSelectBox);
				var NewOptionsList=document.getElementById(ThefillBox);
		
				NewOptionsList.options.length=0;
		
				for(var i=0;i < OptionsList.options.length;i++)
				{
					NewOptionsList.options[i]=new Option(OptionsList.options[i].text, OptionsList.options[i].value);
				}
			};
			
		
		/*<!---*
			Sets the Selected option based on the value of the option
			@requires @TheSelectBox,@TheOption
			@TheOption=The value to search for
		--->*/
		this.setSelected = 
			function(TheSelectBox,TheOption)
			{
				var OptionsList = document.getElementById(TheSelectBox);
		
				for(var i=0;i < OptionsList.length;i++)
				{
					if(OptionsList.options[i].value==TheOption)
					{
						OptionsList.options[i].selected=true;
					}	
				}
/*<!---*
			Gets the Selected option based on the selected index of the option
			@requires @TheSelectBox
			@TheSelectBox=element ID
			returns array of value and options text
--->*/
		this.getSelected = 
			function(TheSelectBox)
			{
				var OptionsList = document.getElementById(TheSelectBox);
		
				for(var i=0;i < OptionsList.length;i++)
				{
					if(OptionsList.options[i].selected==true)
					{
						var TheValue= new array(OptionsList.options[i].value,OptionsList.options[i].text);
						
					}	
				}
				
				return TheValue; 
			}; 
		};
		
		/*
Move from on select list to another
conatins Five functions
getSelectedList
getTargetList
moveAllOptions
moveSelected
returnAllOptions
*/
	this.getSelectedList = function(sSource)
		{
			return document.getElementById(sSource);
		};
		
	this.getTargetList = function(sTarget)
		{
			return document.getElementById(sTarget);
		};
	

	this.moveAllOptions = function(sSource,sTarget)
		{
			
		 var oSrcList = self.getSelectedList(sSource);
		 var oTargetList = self.getTargetList(sTarget);
		 var sSelectList = '';

		 for(var i =0; i < oTargetList.length; i++)
		 	{
			 
				 sSelectList += oTargetList.options[i].value + ',';
			 }

		var i=0;
		 while(oSrcList.length !=0)
		 	{
				
				if(sSelectList.indexOf(oSrcList.options[i].value) == -1)
					{ 

						

						oTargetList.options[oTargetList.length]=new Option(oSrcList.options[i].text,oSrcList.options[i].value)

								oSrcList.options[i]=null;
					}
					
				else{
						oSrcList.options[i]=null;
						
					}
		
			
			}
			
		 this.sortOptions(oTargetList);
		 oSrcList=null;
		 oTargetList = null;
		 
		};
		
		
	this.moveSelected = function(sSource,sTarget)
		{
			
		 var oSrcList = self.getSelectedList(sSource);
		 var oTargetList = self.getTargetList(sTarget);
		 var sSelectList = '';
		var nLastSelectedIndex = 0;
		
		 for(var i=0; i < oTargetList.length; i++)
		 	{
				 sSelectList += oTargetList.options[i].value + ',';
			 }
		 
		
		var x = new Array(0);
		
		for(var i=0; i < oSrcList.length; i++)
		 	{
				if(oSrcList.options[i].selected==true && sSelectList.indexOf(oSrcList.options[i].value) == -1)
					{ 
						oTargetList.options[oTargetList.length]=new Option(oSrcList.options[i].text,oSrcList.options[i].value)
					//remove options		
						x.push(oSrcList.options[i].value);
						
					}
			}
			
		for(var j=0; j < x.length;j++)
			{ 
				for(var i=0; i < oSrcList.length; i++)
						{
							if(oSrcList.options[i].selected==true)
								{ 
									//remove options		
									oSrcList.options[i]=null;
									nLastSelectedIndex=i;
								}
						}
			}	
		
			oSrcList.options.selectedIndex=nLastSelectedIndex;
			
	this.sortOptions(oTargetList);

 /*	
		 // Remove pointers
		 	oSrcList=null;
			
		 var list='';
	
		 for(var i =0; i< oTargetList.length;i++)
		 	{
				list+=oTargetList.options[i].index+':'+oTargetList.options[i].value+ ',';
				
				}
				alert(list);
*/
		 oTargetList = null;
		};
		
	this.sortArrayNoCase = function(x,y)
	{ 
      var a = x.toUpperCase(); 
      var b = y.toUpperCase(); 
      	if (a > b) 
         	return 1 
     	 if (a < b) 
         return -1 
      return 0; 
    }
	
	this.sortOptions = function(oOptionList)
		{	
			var aTempText = new Array(1);
			var aTempValue = new Array(1);
			var	aClone= new Array(1);
			
			
			for (var i=0; i < oOptionList.length; i++)
				{
					aTempText[i]=oOptionList.options[i].text;
					aClone[i]=oOptionList.options[i].text;
					aTempValue[i]=oOptionList.options[i].value;
				}
				
	
			aClone.sort(this.sortArrayNoCase);
			//alert(aClone.join() + '\n' +aTempText.join())
				
	
			for(var i = 0;  i < aClone.length; i++)
				{
						
					//oOptionList.options[i].text=aClone[i];
						
					for(j=0; j < aTempText.length; j++)
						{
							if (aTempText[j]==aClone[i])
								{
									oOptionList.options[i].text=aClone[i];
									oOptionList.options[i].value = aTempValue[j];
									
										break;
								} 
						}
				}
			
			//oOptionList=null;
		}
	
	this.findValue = function(sElm,sValue)
		{
			var SelectBox = document.getElementById(sElm);
			var ReturnValue = -1;
			
			for(var i=0;i < SelectBox.length; i++)
				{
				
				//alert(SelectBox.options[i].value+" : " + sValue);
					if(SelectBox.options[i].value == sValue)
						{
							ReturnValue = i;
							break;
						}
				
				}
				
			return ReturnValue;
		}
	
//this is the end	
}

