﻿function ListUtil() 
{
	if(typeof ListUtil.__initialized=="undefined")
	{
		ListUtil.prototype.getList = function(list)
		{
			var oList;
			if(typeof list == 'object')
			{
				oList = list;
			}
			else if(typeof list != 'undefined')
			{
				oList = document.getElementById(list);
			}
			return oList;
		}
		
		ListUtil.prototype.createList = function(id)
		{
			var oList = document.createElement('select');
			oList.setAttribute('id', ((id) ? id : ''));
			
			return oList;
		}
		
		ListUtil.prototype.createItem = function(text, value)
		{
			var item = document.createElement('option');
			item.appendChild(document.createTextNode(((text) ? text : '')));
			//item.setAttribute('value', ((value) ? escape(value) : ''));
			item.setAttribute('value', ((value) ? value : ''));
				
			return item;
		}
	
		ListUtil.prototype.selectedItem = function(list)
		{
			var result = null;
			var oList = this.getList(list);
			if(oList && oList.options && oList.selectedIndex > -1)
			{
				result = oList.options[oList.selectedIndex];
			}
			return result;	
		}
		
		ListUtil.prototype.selectedItemValue = function(list)
		{
			var result = null;
			var oList = this.getList(list);
			if(oList && oList.options && oList.selectedIndex > -1)
			{
				result = oList.options[oList.selectedIndex].value;
			}
			return result;	
		}
		
		ListUtil.prototype.selectedItemText = function(list)
		{
			var result = null;
			var oList = this.getList(list);
			if(oList && oList.options && oList.selectedIndex > -1)
			{
				result = oList.options[oList.selectedIndex].text;
			}
			return result;	
		}
		
		ListUtil.prototype.itemAtIndex = function(list, index)
		{
			var result = null;
			var oList = this.getList(list);
			if(oList && oList.options && oList.options.length > index && index > -1)
			{
				result = oList.options[index];
			}
			return result;
		}
		
		ListUtil.prototype.itemWithValue = function(list, value)
		{
			var result = null;
			if(value && typeof value != 'object')
			{
				var oList = this.getList(list);
				if(oList && oList.options && oList.options.length)
				{
					for(var i = 0; i < oList.options.length; i++)
					{
						if(oList.options[i].value && oList.options[i].value == value)
						{
							result = oList.options[i];
							break;
						}
					}
				}
			}
			return result;
		}
		
		ListUtil.prototype.itemWithText = function(list, text)
		{
			var result = null;
			if(text && typeof text != 'object')
			{
				var oList = this.getList(list);
				if(oList && oList.options && oList.options.length)
				{
					for(var i = 0; i < oList.options.length; i++)
					{
						if(oList.options[i].text && oList.options[i].text == text)
						{
							result = oList.options[i];
							break;
						}
					}
				}
			}
			return result;
		}
		
		
		ListUtil.prototype.setSelectedItem = function(list, item, fireChangeEvent)
		{
			var itemSelected = false;
			var oList = this.getList(list);	
			var option;
			if(oList && item && oList.options && oList.options.length)
			{
				var t;
				var v;
				if(item != null && typeof item == 'object')
				{
					if(item.value)
						v = item.value;
					if(item.innerText)
						t = item.innerText;
				}
				else if(item != null)
				{
					v = item;
					t = item;
				}
			
				if(v && t)
				{
					for(var i = 0; i < oList.options.length; i++)
					{
						var opt = oList.options[i];
						if(typeof item == 'object')
						{
							if(opt.value.toLowerCase() == v.toLowerCase() && opt.text.toLowerCase() == t.toLowerCase())
							{
								this.setSelectedIndex(oList, i, fireChangeEvent);
								option = opt;
								itemSelected = true;
								break;
							}
						}
						else
						{
							if(opt.value.toLowerCase() == v.toLowerCase() || opt.text.toLowerCase() == t.toLowerCase())
							{
								this.setSelectedIndex(oList, i, fireChangeEvent);
								option = opt;
								itemSelected = true;
								break;
							}
						}
					}
				}
			}
			if(!itemSelected) this.setSelectedIndex(oList, 0, fireChangeEvent)
			return option;
		}
		
		ListUtil.prototype.setSelectedIndex = function(list, index, fireChangeEvent)
		{
			var oList = this.getList(list);
			if(oList && !isNaN(index) && oList.options && oList.options.length && oList.options.length > index)
			{
				if(oList.options[index])
					setTimeout(function()
					{
						if(oList.options[index])
						{
							oList.options[index].selected = true;
							if(fireChangeEvent)
								ListUtil.fireChangeEvent(oList);
						}
					}, 50);
			}
		}
		
		ListUtil.prototype.selectLastItem = function(list, fireChangeEvent)
		{
			var oList = this.getList(list);
			if(oList && oList.options && oList.options.length)
				this.setSelectedIndex(oList, oList.options.length - 1, fireChangeEvent);
		}
		
		ListUtil.prototype.clearSelectedIndex = function(list)
		{
			var oList = this.getList(list);
			if(oList && oList.options)
				oList.setSelectedIndex(oList, -1);
		}
		
		ListUtil.prototype.populateListFromItems = function(list, items)
		{
			var oList = this.getList(list);
			if(oList && items && oList.options && items.length)
			{
				this.clearList(oList);
				for(var i = 0; i < items.length; i++)
				{
					oList.appendChild(items[i]);
				}
				oList.disabled = false;
			}
		}
		
		ListUtil.prototype.populateListFromArray = function(list, itemArray)
		{
			var oList = this.getList(list);
			if(oList && itemArray && oList.options && itemArray.length)
			{
				this.clearList(oList);
				for(var i = 0; i < itemArray.length; i++)
					oList.appendChild(this.createItem(itemArray[i], itemArray[i]));
				oList.disabled = false;
			}
		}
		
		ListUtil.prototype.populateListFromObjectArray = function(list, objectArray, textKey, valueKey)
		{
			var oList = this.getList(list);
			if(oList && objectArray && oList.options && objectArray.length)
			{
				this.clearList(oList);
				for(var i = 0; i < objectArray.length; i++)
					oList.appendChild(this.createItem(objectArray[i][textKey], objectArray[i][valueKey]));
				oList.disabled = false;
			}
		}
		
		ListUtil.prototype.addItem = function(list, item)
		{
			var oList = this.getList(list);
			if(oList && oList.options)
				oList.appendChild(item)
		}
		
		ListUtil.prototype.insertItem = function(list, item, index)
		{
			var oList = this.getList(list);
			if(oList && oList.options)
			{
				if(index >= oList.options.length)
					oList.appendChild(item)
				else
				{
					var prev = oList.options[index];
					oList.insertBefore(item, prev);
				}
			}
		}
		
		ListUtil.prototype.removeItem = function(list, index)
		{
			var oList = this.getList(list);
			if(oList && oList.options && oList.options.length - 1 >= index  && index > -1)
			{
				try
				{
					oList.remove(index);
				}
				catch(e){}
			}
		}
		
		ListUtil.prototype.clearList = function(list)
		{
			var oList = this.getList(list);
			if(oList && oList.options && oList.options.length > 0)
			{
				for(var i = oList.options.length - 1; i > -1; i--)
				{
					this.removeItem(oList, i);
				}
			}
		}
		
		ListUtil.prototype.listLoading = function(list)
		{
			var oList = ListUtil.getList(list);
			if(oList && oList.options)
			{
				oList.loading = true;
				ListUtil.clearList(oList);
				ListUtil.populateListFromObjectArray(oList, [{'text': 'loading...', 'value': ''}], 'text', 'value');
				oList.disabled = true;
			}
		}
		
		ListUtil.prototype.fireChangeEvent = function(list)
		{
			var oList = this.getList(list);
			if(oList)
			{
				if(oList.dispatchEvent)
				{
					var evt = document.createEvent('HTMLEvents');
					evt.initEvent('change', true, true);
					oList.dispatchEvent(evt);
				}
				else
					oList.fireEvent('onchange');
			}
		}
		

		ListUtil.__initialized = true;
	}
}
var ListUtil = new ListUtil();

