/*
** Class Name	 : ListBox
** Parameters	 : 
**				   string ListBoxID  : The ID for the ListBox Control
** Description	 : handle all functionality for ListBox Control
** Author		 : elhussein
*/
function ListBox(ListBoxID)
{
	// Class Properties
	this.ListBoxControl  = document.all[ListBoxID];	// private
	this.MSG_NO_SELECTION	  = "Please Select Item first";
	this.MSG_ITEM_DUPLICATION = "Duplicated Item";
	
	// Overridable Methods
	this.OnClear;
	this.OnAdd;
	this.OnRemove;

	// Class Method
	// Add
	this.Add = function( Text , Value , ValidateTextOcurrance ){
		ValidateTextOcurrance = ( ValidateTextOcurrance == null )? false : ValidateTextOcurrance;
			
		if( ValidateTextOcurrance 
			&& this.Contains(Text) ){
			alert(this.MSG_ITEM_DUPLICATION);
			return;
		}
				
		this.ListBoxControl.options[this.Length()] = new Option(Text ,Value);
		
		if( this.OnAdd != null )
			this.OnAdd();
	}
	
	// Remove
	this.RemoveSelected = function(){
		if( this.ListBoxControl.selectedIndex != -1 )
			this.RemoveAt( this.ListBoxControl.selectedIndex );			
		else
			alert(this.MSG_NO_SELECTION);
	}
	
	// RemoveAt
	this.RemoveAt = function(index){
		this.ListBoxControl.options[index] = null;
		
		if( this.OnRemove != null )
			this.OnRemove();
	}
	
	// Clear
	this.Clear = function(){
		while( this.Length() !=0 )
			this.ListBoxControl[this.Length()-1] = null;
			
		if( this.OnClear != null )
			this.OnClear();
	}
	
	// Contains
	this.ContainsText = function(Text){
		for(var i=0; i<this.Length(); i++){
			if( this.ListBoxControl.options[i].text == Text ){
				return true;
			}
		}
			
		return false;
	}
	
	// Contains
	this.Contains = function(Value){
		for(var i=0; i<this.Length(); i++){
			if( this.ListBoxControl.options[i].value == Value ){
				return true;
			}
		}
			
		return false;
	}

	// Disable
	this.Disable = function(){
		this.ListBoxControl.disabled = true;
	}
	
	// Enable
	this.Enable = function(){
		this.ListBoxControl.disabled = false;
	}
	
	// FindByText
	// return		: return Option Item
	//				  or null value
	this.FindByText = function(Text){
		for(var i=0; i<this.Length(); i++){
			if( this.ListBoxControl.options[i].text == Text ){
				return this.ListBoxControl.options[i];
			}
		}
			
		return null;
	}
	
	// FindByValue
	// return		: return Option Item
	//				  or null value
	this.FindByValue = function(Value){
		for(var i=0; i<this.Length(); i++){
			if( this.ListBoxControl.options[i].value == Value ){
				return this.ListBoxControl.options[i];
			}
		}
			
		return null;
	}
	
	// IndexOf
	this.IndexOf = function(Text , Value){
		for(var i=0; i<this.Length(); i++){
			if( this.ListBoxControl.options[i].value == Value 
				&& this.ListBoxControl.options[i].text == Text ){
				return i;
			}
		}
			
		return -1;
	}
	
	// Insert
	this.Insert = function(index , Text, Value, ValidateTextOcurrance){
		if( index ==0 && this.Length() == 0 ){
			this.Add(Text, Value , ValidateTextOcurrance);
			return;
		}
	
        if( index >= this.Length() ){
                alert("Exception: index Out of Range");
                return;
        }

		ValidateTextOcurrance = ( ValidateTextOcurrance == null )? false : ValidateTextOcurrance;
        this.Add(Text, Value , ValidateTextOcurrance);

        for( var i=this.Length()-1; i>index; i-- )
			 this.Swap(i, i-1);
	}
	
	// Length
	this.Length = function(){
		return this.ListBoxControl.length;
	}
	
	// Select
	this.Select = function( index ){
		this.ListBoxControl.options[index].selected = true;
	}
	
	// SelectAll
	this.SelectAll = function(){
		for( var i=0; i<this.Length(); i++ )
			 this.Select(i);
	}

	// Swap
    this.Swap = function(index1, index2){
        var tmpItemValue = this.ListBoxControl.options[index1].value;
        var tmpItemText = this.ListBoxControl.options[index1].text;

		this.ListBoxControl.options[index1].text = this.ListBoxControl.options[index2].text;
		this.ListBoxControl.options[index1].value = this.ListBoxControl.options[index2].value;

		this.ListBoxControl.options[index2].text = tmpItemText;
		this.ListBoxControl.options[index2].value = tmpItemValue;
    }
    
	// UnSelect
	this.UnSelect = function( index ){
		this.ListBoxControl.options[index].selected = false;
	}
	
	// SelectAll
	this.UnSelectAll = function(){
		for( var i=0; i<this.Length(); i++ )
			 this.UnSelect(i);
	}

}

