/*
 * Combo Box Widget
 * ----------------
 * Adapted from JBEL, the JavaScript Browser Extension Library.
 * Copyright (c) 2004-2005 Tim Vasil.  All rights reserved.
 * Licensed to Active Retention LLC.
 */

//=============================================================================
// ComboBox class:  implements combox box functionality
// Throws all exceptions
// <select> tag should have the following attributes:
//		inputBoxName
//		inputBoxValue
//		maxlength
//		class
// <select> tag must have width set to a specific number of pixels
//=============================================================================

function ComboBox(oSelectBox)
{
	var boxWidth = parseInt(oSelectBox.style.width);
	oSelectBox.comboBox = this;
	oSelectBox.style.position = 'absolute';
	oSelectBox.style.width = '100%';
	oSelectBox.style.zIndex = '1';
	oSelectBox.style.clip = 'rect(auto,auto,auto,' + (boxWidth - 18) + ')';
	//oSelectBox.onfocus = "window.clearTimeout(window['tmr" + oSelectBox.name +"'])";
	oSelectBox.onchange = "ComboBox__setTextBox(this,this.form)";
	var sReadOnly = (oSelectBox.disabled) ? " readonly='true' " : "";

    var sInputBox = '<input ' + sReadOnly + ' comboName="' + oSelectBox.name + '" type="text" name="' + oSelectBox.inputBoxName + '" class="' + oSelectBox.className + '" value="' + CoreLib_escapeHtml(oSelectBox.inputBoxValue) + '" maxlength="' + oSelectBox.maxlength + '" style="position: absolute; width: ' + (boxWidth - 16) + '; z-index: 2;clip:rect(auto,' + (boxWidth-18) + ',auto,auto)" ONKEYUP="ComboBox__autoComplete(this, this.form(\'' + oSelectBox.name + '\'),\'text\')" />';
    var sOutput = '<span style="position:relative;width:'+boxWidth+'px;height:22px">' + oSelectBox.outerHTML + sInputBox + '</span>';
    oSelectBox.outerHTML = sOutput;
    CoreLib_getElementById(oSelectBox.name).selectedIndex = -1;
    ComboBox__autoComplete(CoreLib_getElementById(oSelectBox.inputBoxName), CoreLib_getElementById(oSelectBox.name), 'text');
}

/*static*/ function ComboBox__setTextBox(list,f)
{
	var box = f[list.inputBoxName]
	box.value=list[list.selectedIndex].text;
	box.select();
	box.focus();
}

/*static*/ function ComboBox__autoComplete(field, select, property)
{
	var found = false;
	if (field.value.length > 0)
	{
		for (var i=0;i<select.options.length;i++)
		{
			if(select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0)
			{
				found=true; break;
			}
		}
	}
	
	select.selectedIndex = (found) ? i : -1;
	var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;16;20;144;17;18;91;93";
	if (!event || cursorKeys.indexOf(event.keyCode+";") == -1)
	{
		var r1 = field.createTextRange();
		var oldValue = r1.text;
		var newValue = found ? select.options[i][property] : oldValue;
		if (newValue != field.value)
		{
			field.value = oldValue + newValue.substr(oldValue.length);
			var rNew = field.createTextRange();
			rNew.moveStart('character', oldValue.length);
			rNew.select();
		}
	}
}
