﻿function initComboBox(elm)
{
	if (window._comboBoxFunctions == null) {
		window._comboBoxFunctions =
		{
			_onChangeHandler: [],
			_onFocusHandler: [],
			_parent: [],

			getEnabled: function()		{ return !this.disabled; },
			setEnabled: function(val)	{ this.disabled = !val; },
			
			getReadOnly: function()		{ return this.disabled; },
			setReadOnly: function(val)	
			{
				if (val != this.getReadOnly())
				{
					if (val)
						with (this) {disabled = true; this._tabIndex = tabIndex; tabIndex = -1; className += " ReadOnly";}
					else
						with (this) {disabled = false; tabIndex = this._tabIndex; className = className.replace("ReadOnly", "");}
				}
			},

			getInitValue: function() 	{ return this._initValue; },
			setInitValue: function(val) { this._initValue = val; },

			getValue: function() 		{ return this.value; },
			setValue: function(val) 	
			{
				var oldVal = this.value;
				this.value = val;
				if (this.selectedIndex == -1)
					this.value = oldVal;
			},

			getValueText: function()	{ return this.childNodes[this.selectedIndex].text; },
			setValueText: function(val)	{ this.childNodes[this.selectedIndex].text = val; },		

			getValueDesc: function()	{ return this.childNodes[this.selectedIndex].getAttribute("desc"); },
			setValueDesc: function(val)	{ this.childNodes[this.selectedIndex].setAttribute("desc", val); },	
		
			getValueAttr: function(attr)		{ return this.childNodes[this.selectedIndex].getAttribute(attr); },
			setValueAttr: function(attr, val)	{ this.childNodes[this.selectedIndex].setAttribute(attr, val); },		

			clear: function()			{ this._cachedValue = this.getValue(); this.setValue(""); },
			reset: function()			{ this.setValue(this.getInitValue()); },
			restore: function()			{ if (this._cachedValue != null) this.setValue(this._cachedValue); },

			getItem: function(index)		{ return this.childNodes[index]; },
			getItemValue: function(index)	{ return this.childNodes[index].value; },
			getItemText: function(index)	{ return this.childNodes[index].text; },

			getItemByValue: function(value)	
			{
				for (var i = 0; i < this.childNodes.length; i++)
					if (this.childNodes[i].value == value)
						return this.childNodes[i];
				return null;		
			},

			createItem: function(value, text, desc) 
			{
				var item = document.createElement("OPTION");
				item.value = value; 
				item.appendChild(document.createTextNode(text ? text : value));
				if (desc)
					item.setAttribute("desc", desc);							
				return item;
			},

			addItem: function(value, text, desc)	
			{
				return this.appendChild(this.createItem(value, text, desc));
			},

			addOrSetItem: function(value, text, desc, sorted, select)
			{
				this.setValue(value);
				if (this.getValue() == value)
					return;

				return this.appendItem(this.createItem(value, text, desc), sorted, select);
			},

			appendItem: function(item, sorted, select)
			{
				var refItem = null;
				if (sorted) 
				{
					var itemText = item.text.toUpperCase(); 
					for (var i = 0; i < this.childNodes.length; i++)
					{
						refItem = this.childNodes[i];
						if (refItem.text.toUpperCase() > itemText)
							break;
						else
							refItem = null;
					}
				}

				if (refItem)				
					this.insertBeforeItem(item, refItem, select);
				else
				{
					this.appendChild(item);				
					if (select)
						item.selected = true;	
				}	

				return item;	
			},

			insertBeforeItem: function(item, refItem, select)
			{
				this.insertBefore(item, refItem);
				if (select)	
				{			
					this.selectedIndex = refItem.index - 1; 
					item.selected = true;
				}	
				return item;		
			},

			deleteItem: function(index, selectNext)
			{	
				if (index == null && this.selectedIndex != -1)
					index = this.selectedIndex;
			
				return this.removeItem(this.childNodes[index]);	
			},

			deleteItems: function()
			{	
				this.selectedIndex = -1;
				while (this.hasChildNodes())
					this.removeChild(this.lastChild);         
			},

			deleteItemsByValues: function(vals)
			{	
				for (var i = 0; i < vals.length; i++)
				{
					this.setValue(vals[i]);
					if (this.getValue() == vals[i])
						this.deleteItem();	
				}		
			},

			removeItem: function(item, selectNext)
			{	
				if (selectNext)
				{
					var itemToSelect = item.nextSibling;						
					if (!itemToSelect)
						itemToSelect = item.previousSibling;
					if (itemToSelect)
						itemToSelect.selected = true;
				}					
				return this.removeChild(item);    
			},

			getItemCount: function()
			{
				return this.childNodes.length;		
			},

			copyItems: function(source)
			{
				this.deleteItems();
				for (var i = 0; i < source.childNodes.length; i++)
				{
					var item = source.childNodes[i];
					this.addItem(item.value, item.text, item.getAttribute("desc"));
				}	
			},

			onchange: function() 
			{
				if (this._onChangeHandler)
					window.postAction("try {" + this.id + "._onChangeHandler(); } catch(e) {displayErr('ComboBox_OnChange', e);}");		
			},

			onfocus: function(eventObj) 
			{		
				try 
				{
					if (this._onFocusHandler)		
						this._onFocusHandler();
				} 
				catch(e) {displayErr("ComboBox_OnFocus", e);}
			}
			
		};
	}

	// save a copy of any pre-initialized handlers
	var customOnClickHandler = elm.onclick;
	var customOnFocusHandler = elm.onfocus;
	var customOnChangeHandler = elm.onchange;
	var initOnChangeHandler = elm._onChangeHandler;
	var initOnFocusHandler = elm._onFocusHandler;

	// add interfaces
	elm = extendElement(elm);
	elm = Object.extend(elm, window._comboBoxFunctions);

	// initialize 
	elm._tabIndex = elm.tabIndex;
	elm._initValue = elm.value;
	
	// auto event wire up
	elm._onChangeHandler = (initOnChangeHandler ? initOnChangeHandler : getEventHandler(elm.id, "OnChange"));
	elm._onFocusHandler = (initOnFocusHandler ? initOnFocusHandler : getEventHandler(elm.id, "OnFocus"));
	
	// allow direct override of handlers
	if (customOnClickHandler)
		elm.onclick = customOnClickHandler;
	if (customOnFocusHandler)
		elm.onfocus = customOnFocusHandler;
	if (customOnChangeHandler)
		elm.onchange = customOnChangeHandler;

	return elm;
}

