/*
** Class Name	 : ListBoxAdapter
** Parameters	 : 
**				   string srcListBoxID  : The ID for the source ListBox
**				   string distListBoxID : The ID for the distination ListBox
				   string hiddenInputID : The ID for the Hidden Input
** Description	 : take the selected item from the source listbox
**				   and add or delete it to the distination listBox
** Author		 : elhussein
*/
function ListBoxAdapter(srcListBoxID, distListBoxID, hiddenInputID)
{
	// Class Properties
	this.MSG_NO_SELECTION		= "Please Select Item first";
	this.MSG_ITEM_DUPLICATION	= "Duplicated Item";

	this.srcListBox  = document.all[srcListBoxID];
	this.distListBox = document.all[distListBoxID];
	this.hiddenInput = document.all[hiddenInputID];

	// Class Method
	// Add
	this.Add = function(){
		if( this.srcListBox.selectedIndex != -1 ){
			var selectedItem = this.srcListBox.options[this.srcListBox.selectedIndex];
			
			if( !this.Contains(selectedItem.value) ){
				this.distListBox.options[this.distListBox.length] = new Option(selectedItem.text ,selectedItem.value);
				this.changeHiddenInputValue();
			}else{
				alert(this.MSG_ITEM_DUPLICATION);
			}
			
		}else{
			alert(this.MSG_NO_SELECTION);
		}
	}
	
	// Remove
	this.Remove = function(){
		if( this.distListBox.selectedIndex != -1 )
			this.RemoveAt( this.distListBox.selectedIndex );			
		else
			alert(this.MSG_NO_SELECTION);
	}
	
	// RemoveAt
	this.RemoveAt = function(index){
		this.distListBox.options[index] = null;
		this.changeHiddenInputValue();
	}
	
	// Clear
	this.Clear = function(){
		while( this.distListBox.length !=0 )
			this.distListBox[this.distListBox.length-1] = null;
		
		this.changeHiddenInputValue();
	}
	
	// AddAll
	this.AddAll = function(){
		this.Clear();

		for(var i=0; i<this.srcListBox.length; i++){
			var selectedItem = this.srcListBox.options[i];
			this.distListBox.options[i] = new Option(selectedItem.text ,selectedItem.value);
		}
		
		this.changeHiddenInputValue();
	}
	
	// Contains
	this.Contains = function(value){
		for(var i=0; i<this.distListBox.length; i++){
			if( this.distListBox.options[i].value == value ){
				return true;
			}
		}
			
		return false;
	}
	
	// Private : changeHiddenInputValue
	this.changeHiddenInputValue = function(){
		this.hiddenInput.value = "";

		for(var i=0; i<this.distListBox.length; i++){
			if( this.hiddenInput.value.length > 0 )
				this.hiddenInput.value += ","
			this.hiddenInput.value += this.distListBox.options[i].value;
		}
	}
}
