﻿//ListBox.js
//Version: 1.0
//This script is created by Samir Nigam. Do not remove, modify, or hide the author information. keep it intact.
//Mail: nigam.samir@hotmail.com  

function ListBox(Arguments)
{
    //Public property Version.
    this.Version = '2.0';
	
	//Local variables.
    var Ids = 0;
    var EventHandlers = new Array();
			
	var Base = Arguments.Base ? Arguments.Base : document.documentElement;	
	var Multiselect = Arguments.Multiselect ? Arguments.Multiselect : false; 	
	var Size = !Arguments.Rows && Arguments.Rows > 5 ? Arguments.Rows : 6;	
	var FontSize = Arguments.FontSize ? Arguments.FontSize : '10pt';
	var FontHoverWeight = Arguments.FontHoverWeight ? Arguments.FontHoverWeight : 'normal';  
	var Width = Arguments.Width ? Arguments.Width : 300;
	var ListHeight = Arguments.ListHeight ?  Arguments.ListHeight : null;
	var NormalItemColor = Arguments.NormalItemColor ? Arguments.NormalItemColor : '#204060';
	var NormalItemBackColor = Arguments.NormalItemBackColor ? Arguments.NormalItemBackColor : '#FCFDFE';
	var AlternateItemColor = Arguments.AlternateItemColor ? Arguments.AlternateItemColor : '#204060';
	var AlternateItemBackColor = Arguments.AlternateItemBackColor ? Arguments.AlternateItemBackColor : NormalItemBackColor;
	var SelectedItemColor = Arguments.SelectedItemColor ? Arguments.SelectedItemColor : '#2B5682';
	var SelectedItemBackColor = Arguments.SelectedItemBackColor ? Arguments.SelectedItemBackColor : '#DFEAF4';
	var HoverItemColor = Arguments.HoverItemColor ? Arguments.HoverItemColor : '#204060';
	var HoverItemBackColor = Arguments.HoverItemBackColor ? Arguments.HoverItemBackColor : '#C2D7EB';
	var HoverBorderdColor = Arguments.HoverBorderdColor ? Arguments.HoverBorderdColor : '#C2D7EB';
	var ListBorder = Arguments.ListBorder ? Arguments.ListBorder : '1px solid #C6D9EC';  	
	var ItemSpanClass = Arguments.ItemSpanClass ? Arguments.ItemSpanClass : '';
	var MsgNoResults = Arguments.MsgNoResults ? Arguments.MsgNoResults : ''; 
	var TopMsgNoResults = Arguments.TopMsgNoResults ? Arguments.TopMsgNoResults : null; 
	var ClickEventHandler = Arguments.ClickEventHandler ? Arguments.ClickEventHandler : function(){ }; 	
	
	var SelectedIndex = -1;
	var BeforeSelItemColor;
	var BeforeSelBgColor;
	var BeforeBorderCol;
	var BeforeFontWeight;
 
	//Create div for list box.
    //tm var ListBoxDiv = document.createElement('div');              
    var ListBoxDiv = Base;   
    ListBoxDiv.style.zIndex = 100000; 
	ListBoxDiv.style.backgroundColor = NormalItemBackColor;	
    ListBoxDiv.style.textAlign = 'left';
    ListBoxDiv.style.verticalAlign = 'top';
    ListBoxDiv.style.cursor = 'default';
    ListBoxDiv.style.border = ListBorder;        
    ListBoxDiv.style.width = Width + 'px';            
	ListBoxDiv.style.height = (Size * 22) + 'px';
	if (ListHeight) 
	  ListBoxDiv.style.height = ListHeight + 'px';
	ListBoxDiv.style.overflow = 'auto'; 
	
	var MsgSpan = null;	
	
	this.ShowNoResultMsg = function()
	{
	  if (MsgNoResults != '')
	  {
	    MsgSpan = document.createElement('span');	   
	    MsgSpan.innerHTML = MsgNoResults;  
	    MsgSpan.style.display = 'block'; 
	    MsgSpan.style.position = 'absolute';
	    MsgSpan.style.textAlign = 'center';        
	    MsgSpan.style.left = '0px';
	    if (TopMsgNoResults)
	      MsgSpan.style.top = TopMsgNoResults + 'px';
	    else
	      MsgSpan.style.top = parseInt(ListBoxDiv.style.height,10) / 2 + 'px';
	    MsgSpan.style.width = Width + 'px';	    	   	   
	    ListBoxDiv.appendChild(MsgSpan); 	    
	  }  		
	} 
		
    this.AddItem = function(_Text, _Value, _Checked)
	{
        var Item = null;
		var CheckBox = null;        
        var Span = null;
       		
       	if (MsgSpan)            	      	
	      MsgSpan.style.top = '-250px'; 
	           		
        Item = document.createElement('div'); 
        Item.style.paddingLeft = '4px';
        Item.style.paddingRigth = '4px';       
        Item.style.backgroundColor = Ids % 2 == 0 ? NormalItemBackColor : AlternateItemBackColor;
        Item.style.color = Ids % 2 == 0 ? NormalItemColor : AlternateItemColor;;
	    Item.style.fontWeight = 'normal';
	    Item.style.fontFamily = 'Arial';
	    Item.style.fontSize =  FontSize;
        Item.style.textAlign = 'left';
        Item.style.verticalAlign = 'middle'; 
        Item.style.cursor = 'pointer';
        Item.style.borderTop = Ids % 2 == 0 ? '1px solid ' + NormalItemBackColor : '1px solid ' + AlternateItemBackColor;
        Item.style.borderBottom = Ids % 2 == 0 ? '1px solid ' + NormalItemBackColor : '1px solid ' + AlternateItemBackColor;
        Item.style.overflow = 'hidden';
        Item.style.textOverflow = 'ellipsis';       
		Item.ItemIndex = Ids;			
		
        CheckBox = document.createElement('input');       
        if (!Multiselect) 
          CheckBox.type = 'radio';        
        else
          CheckBox.type = 'checkbox';        
        CheckBox.style.display = Multiselect ? '' : 'none';       
        Item.appendChild(CheckBox);        
        			
        Span = document.createElement('span');
        Span.innerHTML = _Text;     
		Span.value = _Value;     
        Span.title = _Text;          
        Span.setAttribute("class", ItemSpanClass); 
	    Item.appendChild(Span);  
	    
	    if (_Checked)
        {   
          OnMouseOver(CheckBox, Item);       
          CheckBox.checked = _Checked;                              
          OnClick(CheckBox, Item);                                     
        }            	    	    
	    ListBoxDiv.appendChild(Item);		      	     	   
		
	    //Register events.	    
	    WireUpEventHandler(Item, 'mouseover', function(){ OnMouseOver(CheckBox, Item); });
	    WireUpEventHandler(Item, 'mouseout', function(){ OnMouseOut(CheckBox, Item); });
	    WireUpEventHandler(Item, 'selectstart', function(){ return false; });
	    if (!Multiselect) {
	      WireUpEventHandler(Item, 'click', function(){ OnClick(CheckBox, Item); });	    
	      WireUpEventHandler(Item, 'dblclick', function(){ ClickEventHandler(CheckBox, {ItemIndex: Item.ItemIndex }); });
	    } else {  
	      WireUpEventHandler(CheckBox, 'click', function(){ OnClick(CheckBox, Item); });
	      WireUpEventHandler(CheckBox, 'click', function(){ ClickEventHandler(CheckBox, { IsSelected: CheckBox.checked, Text: _Text, Value: _Value, ItemIndex: Item.ItemIndex }); });   
	    }  	        
	    
	    Ids++;
	}
	
    //Public method GetItems.
    this.GetItems = function()
	{
        var Items = new Array();
		
		var Divs = ListBoxDiv.getElementsByTagName('div');
		
        for(var n = 0; n < Divs.length; ++n)    
			Items.push({IsSelected: Divs[n].childNodes[0].checked, Text: Divs[n].childNodes[1].innerHTML, Value: Divs[n].childNodes[1].value, ItemIndex: Divs[n].ItemIndex});  
       		
        return Items;
    }
     
	
    //Public method Dispose.
	this.Dispose = function()
	{
	    this.DeleteItems(); 
	    while(EventHandlers.length > 0)
	        DetachEventHandler(EventHandlers.pop());	        		
				
	   //tm Base.removeChild(ListBoxDiv);	  
	}
	
	//Public method Contains.
	this.Contains = function(Index)
	{
		return typeof(Index) == 'number' && ListBoxDiv.childNodes[Index] ? true : false;
	}
	
	//Public method GetItem.
	this.GetItem = function(Index)
	{	    
	    var Divs = ListBoxDiv.getElementsByTagName('div');
		
	    return this.Contains(Index) ? { IsSelected: Divs[Index].childNodes[0].checked, Text: Divs[Index].childNodes[1].innerHTML, Value: Divs[Index].childNodes[1].value, ItemIndex: Index} : null;
	}		
	
	//Public method DeleteItem.
	this.DeleteItem = function(Index)
	{
	    if(!this.Contains(Index)) return false;
	    
	    try
	    {
			ListBoxDiv.removeChild(ListBoxDiv.childNodes[Index]);
			if (this.GetTotalItems()<1) 
	          this.ShowNoResultMsg();   
	    }
	    catch(err)
	    {
			return false;
	    }
	    
	    return true;
	}
	
	//Public method DeleteItems.
	this.DeleteItems = function()
	{			
	    var ItemsRemoved = 0;
	    
	    for(var n = ListBoxDiv.childNodes.length - 1; n >= 0; --n)   
	        try
	        {
				ListBoxDiv.removeChild(ListBoxDiv.childNodes[n]);
				ItemsRemoved++;
	        }
	        catch(err)
	        {
			    break;
	        }  	  	        	      	  
	      
    //   this.RestoreStartPosScrollBar();
	   SelectedIndex = -1; 
	   Ids = 0;	 
	   
	   this.ShowNoResultMsg(); 	    
	   	   	     
	   return ItemsRemoved;
	}
	
	this.RestoreStartPosScrollBar = function()
	{
	    ListBoxDiv.style.overflow = ie() ? '' : 'hidden';                 	   	   	  
        ListBoxDiv.scrollTop = 0;	     
        ListBoxDiv.style.overflow = 'auto';         
	} 
	
	this.GetSelectedIndex = function()
	{
	  return SelectedIndex; 
	}
	
	this.GetSelectedValue = function()
	{	
	  var index = this.GetSelectedIndex();
	  if (index > -1) {
	    var item = this.GetItem(index);	    
	    return item.Value;  
	  } else
	    return '';
	} 
	
	this.GetSelectedText = function()
	{	
	  var index = this.GetSelectedIndex();
	  if (index > -1) {
	    var item = this.GetItem(index);	    
	    return item.Text;  
	  } else
	    return '';
	} 
	
	//Public method GetTotalItems.
	this.GetTotalItems = function()
	{
	    return ListBoxDiv.childNodes.length;
	}
	
	this.GetTotalItemsHeight = function()
	{
	    var Divs = ListBoxDiv.getElementsByTagName('div');
		var totalHeight = 0;
        for(var n = 0; n < Divs.length; ++n)  
          totalHeight = totalHeight + Divs[n].clientHeight;
        return totalHeight;   
	}
	
	function ClearLastSelected()	
	{
	   var Divs = ListBoxDiv.getElementsByTagName('div');	
	   if ((SelectedIndex > - 1) && (SelectedIndex < Divs.length))
	   {
	     var cb = Divs[SelectedIndex].childNodes[0];
	     cb.checked = false;
	     var item = Divs[SelectedIndex];	    
	     item.style.color = BeforeSelItemColor;
	     var span = item.childNodes[1];
		 if (span)
		   span.style.color = item.style.color;
	     item.style.backgroundColor = BeforeSelBgColor;	     	  
	     item.style.borderTopColor = BeforeBorderCol;
	     item.style.borderBottomColor = BeforeBorderCol; 
		 item.style.fontWeight = BeforeFontWeight;	     
	   }  
	} 
	
    //Item mouseover event handler.
    var OnMouseOver = function(CheckBox, Item)
    {       
        if(CheckBox.checked) return;
				
        Item.bgColor = Item.style.backgroundColor;
	    Item.fColor = Item.style.color;
	    Item.bColor = Item.style.borderTopColor;
        Item.style.backgroundColor = HoverItemBackColor;
		Item.style.color = HoverItemColor;	
		var span = Item.childNodes[1];
		if (span)
		  span.style.color = Item.style.color;			
		Item.style.borderTopColor = Item.style.borderBottomColor = HoverBorderdColor;
		Item.style.fontWeight = FontHoverWeight;
    }
    
    //Item mouseout event handler.
    var OnMouseOut = function(CheckBox, Item)
    {
        if(CheckBox.checked) return;
				
		Item.style.backgroundColor = Item.bgColor;
	    Item.style.color = Item.fColor;
	    var span = Item.childNodes[1];
		if (span)
		  span.style.color = Item.style.color;
	    Item.style.borderTopColor = Item.style.borderBottomColor = Item.bColor;
		Item.style.fontWeight = 'normal';
    }
    
    //CheckBox click event handler.
	var OnClick = function(CheckBox, Item)
	{	  
	      if (!Multiselect) {   	       
	        if (SelectedIndex > - 1)		                 
	          ClearLastSelected();           	         	        	        
	        CheckBox.checked = true;   
	        SelectedIndex = Item.ItemIndex; 
	        BeforeSelBgColor = Item.bgColor;	        
	        BeforeSelItemColor = Item.fColor;
	        BeforeBorderCol = Item.bColor;
	        BeforeFontWeight =  'normal';	          	        
	      }
	      
	      if(CheckBox.checked)
          {
		    Item.style.backgroundColor = SelectedItemBackColor;
			Item.style.color = SelectedItemColor;
			var span = Item.childNodes[1];
			if (span)
			  span.style.color = Item.style.color; 				 		   
			Item.style.borderTopColor = Item.style.borderBottomColor = SelectedItemBackColor;
          }
          else
          {
            Item.style.backgroundColor = HoverItemBackColor;
		    Item.style.color = HoverItemColor;
			Item.style.borderTopColor = Item.style.borderBottomColor = HoverBorderdColor;
          }      
	} 
			
    //Private anonymous method to wire up event handlers.
	var WireUpEventHandler = function(Target, Event, Listener)
	{
	    //Register event.	    
	    if(Target.addEventListener)	   
			Target.addEventListener(Event, Listener, false);	    
	    else if(Target.attachEvent)	   
			Target.attachEvent('on' + Event, Listener);
	    else 
	    {
			Event = 'on' + Event;
			alert(Event);
			Target.Event = Listener;	 
		}
		
	    //Collect event information through object literal.
	    var EVENT = { Target: Target, Event: Event, Listener: Listener }
	    EventHandlers.push(EVENT);
	}
	
	//Private anonymous  method to detach event handlers.
	var DetachEventHandler = function(EVENT)
	{
	    if(EVENT.Target.removeEventListener)	   
			EVENT.Target.removeEventListener(EVENT.Event, EVENT.Listener, false);	    
	    else if(EVENT.Target.detachEvent)	   
			EVENT.Target.detachEvent('on' + EVENT.Event, EVENT.Listener);
	    else 
		{
			EVENT.Event = 'on' + EVENT.Event;
			EVENT.Target.EVENT.Event = null;	 
	    }
	}
	 
	WireUpEventHandler(ListBoxDiv, 'contextmenu', function(){ return false; });		
    //tm Base.appendChild(ListBoxDiv);   
}  

