// Select controls: find, filter, multi-select checkboxes and radio buttons


// If no select passed used current select.
// If string passed then it is the ID.
var eCurrentSelect;
function getSelect(eSelect)
{
	if (eSelect == null)
		return eCurrentSelect;
	else
		if (typeof eSelect == 'string')
			return document.getElementById(eSelect);
		else
			return eSelect;
}


// This will get the URL for viewing/editing of the current selected option.
function getSelectOpenOptionURL(eSelect)
{
	eSelect = getSelect(eSelect);
	var sOpenOptionURL = getAttributeValue(eSelect, 'openoptionurl');
	if (sOpenOptionURL != '')
		return sOpenOptionURL.replace(/\[Value\]/g, eSelect.value).replace(/%5bValue%5d/g, eSelect.value);
	else
		return '';
}


// This will set the corresponding filter value for the openned select option in the newly openned window.
// This is called in the onload of the window.
function setSelectOpenOptionFilter(oWindow)
{
	eSelect = getSelect(null);
	
	var sFilterControlId = getAttributeValue(eSelect, 'filtercontrolid');
	if (sFilterControlId != '')
	{
		var sFilterValue = document.getElementById(sFilterControlId).value;
		if (sFilterValue != '')
		{
			var oWindowFilterControl = oWindow.document.getElementById(sFilterControlId);
			if (oWindowFilterControl != null)
			{
				oWindowFilterControl.value = sFilterValue;
				oWindow.refreshSelect(oWindowFilterControl);
			}
		}
	}
}



// This will open a new window and load the selected option for viewing/editing.
function openSelectOption(eSelect)
{
	eSelect = getSelect(eSelect);
	
	var sOpenOptionURL = getSelectOpenOptionURL(eSelect);
	if (sOpenOptionURL != '' && getAttributeValue(eSelect, 'allowviewoptions') != 'false')
	{
		var sValue = eSelect.value;
		if (sValue != '' || getAttributeValue(eSelect, 'allownewoptions') != 'false')
		{
			eCurrentSelect = eSelect;
			var oWindow = window.open(sOpenOptionURL, '_blank', '', false);
			// fullscreen=no, left=, right=, width=, height=, channelmode=no, directories=no, titlebar=yes, location=no, menubar=no, toolbar=no, scrollbars=yes, status=no, resizable=no, copyhistory=no
//			oWindow.openerSelect = eSelect.id;
		}
	}
}


// This function is called once an option has been edited.
function loadSelectOption(eSelect, sValue)
{
	eSelect = getSelect(eSelect);

	eSelect.value = sValue;
	if (sValue != '')
	{
		var sDataSourceURL = getAttributeValue(eSelect, 'datasourceurl')
		if (sDataSourceURL != '')
		{
			sDataSourceURL = sDataSourceURL.replace(/\[Value\]/g, sValue).replace(/%5bValue%5d/g, sValue);
			sDataSourceURL = sDataSourceURL.replace(/\[FilterValue\]/g, 'null').replace(/%5bFilterValue%5d/g, 'null');
			sDataSourceURL = sDataSourceURL.replace(/\[AllFilterValues\]/g, 'true').replace(/%5bAllFilterValues%5d/g, 'true');

			var oXML = loadXML(sDataSourceURL);
			var oItems = oXML.documentElement.childNodes;
			if (oItems.length == 1)
			{
				var oItem = oItems.item(0);
				var sFilterControlId = getAttributeValue(eSelect, 'filtercontrolid');
				if (sFilterControlId != '')
				{
					var eFilter = document.getElementById(sFilterControlId);
					var sFilterValue = getAttributeValue(oItem, 'filtervalue');
					if (eSelect.selectedIndex > 0)
						eSelect.options.item(eSelect.selectedIndex).setAttribute('filtervalue', sFilterValue);
					loadSelectOption(eFilter, sFilterValue);
					loadSelect(eSelect, true);
					eSelect.value = sValue;
				}
				else
				{
					var sText = oItem.childNodes.item(0).nodeValue;
					var eOption;
					if (sValue == eSelect.value)
					{
						eOption = eSelect.options.item(eSelect.selectedIndex);
						eOption.innerHTML = '';
					}
					else
					{
						eOption = document.createElement('option');
						eOption.setAttribute('value', sValue);
						eSelect.appendChild(eOption);
						eSelect.value = sValue;
					}
					var eText = document.createTextNode(sText);
					eOption.appendChild(eText);
				}
			}
		}
	}
	refreshSelect(eSelect);
	refreshFilteredControl(eSelect);
}


// This function will load the options if not already loaded.
function loadSelect(eSelect, bClearFirst)
{
	eSelect = getSelect(eSelect);

	var sDataSourceURL = getAttributeValue(eSelect, 'datasourceurl');
	if (sDataSourceURL != '')
	{
		var eFilterOption;
		var sFilterValue = '';
		var sFilterControlId = getAttributeValue(eSelect, 'filtercontrolid');
		if (sFilterControlId != '')
		{
			var eFilter = document.getElementById(sFilterControlId);
			if (eFilter.selectedIndex == -1)
				return;
			eFilterOption = eFilter.options.item(eFilter.selectedIndex);
			sFilterValue = eFilterOption.value;
			if (bClearFirst == false)
			{
				var sLoaded = getAttributeValue(eFilterOption, 'loaded');
				if (sLoaded != 'false')
					return;
			}
		}
		else
			if (bClearFirst == false)
			{
				var sLoaded = getAttributeValue(eSelect, 'loaded');
				if (sLoaded != 'false')
					return;
			}
		
		if (bClearFirst == true)
			clearSelectOptions(eSelect, sFilterValue);
		
		sDataSourceURL = sDataSourceURL.replace(/\[Value\]/g, 'null').replace(/%5bValue%5d/g, 'null');
		sDataSourceURL = sDataSourceURL.replace(/\[FilterValue\]/g, (sFilterValue == '' ? 'null' : sFilterValue)).replace(/%5bFilterValue%5d/g, (sFilterValue == '' ? 'null' : sFilterValue));
		sDataSourceURL = sDataSourceURL.replace(/\[AllFilterValues\]/g, 'false').replace(/%5bAllFilterValues%5d/g, 'false');

		var sFilteredControlId = getAttributeValue(eSelect, 'filteredcontrolid');
		var oXML = loadXML(sDataSourceURL);
		var oItems = oXML.documentElement.childNodes;
		for (var i = 0; i < oItems.length; i++)
		{
			var oItem = oItems.item(i);
			var sValue = getAttributeValue(oItem, 'value');
			var sText = oItem.childNodes.item(0).nodeValue;
			var eOption = document.createElement('option');
			eOption.setAttribute('value', sValue);
			eOption.setAttribute('filtervalue', sFilterValue);
			if (sFilteredControlId != '')
				eOption.setAttribute('loaded', 'false');
			var eText = document.createTextNode(sText);
			eOption.appendChild(eText);
			eSelect.appendChild(eOption);
		}
		
		if (sFilterValue != '')
			eFilterOption.setAttribute('loaded', 'true');
		else
			eSelect.setAttribute('loaded', 'true');
	}
}


// This will refresh the select control.
// It will initialise the associated filter control and find controls.
// It will also setup a replacement control if multi-select or has a filter.
function refreshSelect(eSelect)
{
	eSelect = getSelect(eSelect);

	loadSelect(eSelect, false);

	var sSelectId = eSelect.id
	var sFilterControlId = getAttributeValue(eSelect, 'filtercontrolid');
	var sFilterValue = '';
	var sFilterText = '';
	if (sFilterControlId != '')
	{
		var eFilter = document.getElementById(sFilterControlId);
		if (getAttributeValue(eFilter, 'filteredcontrolid') == '')
		{
			// Initialise Filter Control
			eFilter.setAttribute('filteredcontrolid', eSelect.id);
			eFilter.onchange = onSelectChange;

			if (eFilter.options.item(0).value == '')
				if (eSelect.multiple == true)
				{
					var sShowSelectedOnlyText = 'Show Selected Only';
					eFilter.options.item(0).text = sShowSelectedOnlyText;
					var sReplacementControlId = getAttributeValue(eFilter, 'replacementcontrolid');
					if (sReplacementControlId != '')
						document.getElementById(sReplacementControlId).options.item(0).text = sShowSelectedOnlyText;
					if (eFilter.selectedIndex == 0)
						setFindControlText(eFilter, true);
				}
		}
		sFilterValue = eFilter.value;
		if (eFilter.selectedIndex >= 0)
			sFilterText = eFilter.options.item(eFilter.selectedIndex).text;
	}

	if (eSelect.multiple == false && (eSelect.selectedIndex == -1 || sFilterValue != getAttributeValue(eSelect.options.item(eSelect.selectedIndex), 'filtervalue')))
		eSelect.value = '';
	
	var bisRadioButtonControl = isRadioButtonControl(eSelect);
	if (sFilterControlId != '' || eSelect.multiple == true || bisRadioButtonControl == true)
	{
		var sReplacementControlId = sSelectId + '_ReplacementControl';
		var eReplacementControl = document.getElementById(sReplacementControlId);
		if (eReplacementControl == null)
		{
			// Initialise Replacement Control
			if (eSelect.multiple == true || bisRadioButtonControl == true)
				eReplacementControl = document.createElement('div');
			else
			{
				eReplacementControl = document.createElement('select');
				eReplacementControl.onchange = onSelectChange;
			}
			eReplacementControl.className = eSelect.className;
			eReplacementControl.id = sReplacementControlId;
			eReplacementControl.setAttribute('replacedcontrolid', eSelect.id);
			eSelect.setAttribute('replacementcontrolid', eReplacementControl.id);
		}
		eReplacementControl.innerHTML = '';
		for (var j = 0; j < eSelect.length; j++)
		{
			var eSelectOption = eSelect.options.item(j);
			var sOptionValue = eSelectOption.value;
			var sOptionText = eSelectOption.text;
			if (sOptionText.substring(0, sFilterText.length + 3) == sFilterText + ' - ')
				sOptionText = sOptionText.substring(sFilterText.length + 3);
			else if (sOptionText.substring(sOptionText.length - sFilterText.length - 3) == ' - ' + sFilterText)
				sOptionText = sOptionText.substring(0, sOptionText.length - sFilterText.length - 3);
			var sOptionFilterValue = getAttributeValue(eSelectOption, 'filtervalue');
			if (eSelect.multiple == true)
			{
				if (sOptionValue != '' && ((sFilterControlId != '' && sFilterValue == '' && eSelectOption.selected) || sFilterValue == sOptionFilterValue))
				{
					var eCheckBoxOption = document.createElement('div');
					var sCheckboxId = sReplacementControlId + j;
					var eCheckBox = document.createElement('input');
					eCheckBox.type = 'checkbox';
					eCheckBox.id = sCheckboxId;
					eCheckBox.onclick = new Function('onclick', 'document.getElementById(\'' + sSelectId + '\').options.item(' + j.toString() + ').selected = this.checked;');
					eCheckBox.setAttribute('value', sOptionValue);
					eCheckBoxOption.appendChild(eCheckBox);
					var eText = document.createElement('span');
					eText.onclick = new Function('onclick', 'document.getElementById(\'' + sCheckboxId + '\').click()');
					eText.appendChild(document.createTextNode(sOptionText));
					eCheckBoxOption.appendChild(eText);
					eReplacementControl.appendChild(eCheckBoxOption);
					eCheckBox.checked = eSelectOption.selected;
				}
			}
			else
			{
				if (eSelectOption.selected == true && sOptionFilterValue != sFilterValue)
					eSelectOption.selected = false;
				
				if (sOptionValue == '' || sFilterValue == sOptionFilterValue)
					if (bisRadioButtonControl == true)
					{
						var eRadioButtonOption = document.createElement('div');
						var sRadioButtonId = sReplacementControlId + j;
						var eRadioButton;
						try
						{
							// implemented due to bug in IE - wouldn't setAttributes name and checked
							eRadioButton = document.createElement('<input type="radio" name="' + sReplacementControlId + '"' + ((eSelectOption.selected == true) ? ' checked="checked"' : '') + ' />');
						}
						catch(err)
						{
							eRadioButton = document.createElement('input');
							eRadioButton.type = 'radio';
							eRadioButton.name = sReplacementControlId;
							eRadioButton.checked = eSelectOption.selected;
						}
						eRadioButton.value = sOptionValue;
						eRadioButton.id = sRadioButtonId;
						eRadioButton.onclick = onRadioButtonClick;
						eRadioButtonOption.appendChild(eRadioButton);
						var eText = document.createElement('span');
						eText.onclick = new Function('onclick', 'document.getElementById(\'' + sRadioButtonId + '\').click()');
						eText.appendChild(document.createTextNode(sOptionText));
						eRadioButtonOption.appendChild(eText);
						eReplacementControl.appendChild(eRadioButtonOption);
					}
					else
					{
						var eOption = document.createElement('option');
						eOption.setAttribute('value', sOptionValue);
						eOption.selected = eSelectOption.selected;
						var eText = document.createTextNode(sOptionText);
						eOption.appendChild(eText);
						eReplacementControl.appendChild(eOption);
					}
			}
		}
		eSelect.parentNode.insertBefore(eReplacementControl, eSelect);
		eSelect.style.display = 'none';
	}
	
	if (eSelect.multiple == false && bisRadioButtonControl == false)
	{
		var sFindControlId = sSelectId + '_FindControl';
		var eFindControl = document.getElementById(sFindControlId);
		var eDropDownControl;
		if (eFindControl == null)
		{
			// Initialise Find Control
			var eFindControl = document.createElement('input');
			eFindControl.type = 'text';
			eFindControl.id = sFindControlId;
			eFindControl.className = 'DropDown ' + eSelect.className;
			eFindControl.setAttribute('autocomplete', 'off');
			eFindControl.onkeydown = onFindControlKeyDown;
			eFindControl.onkeyup = onFindControlKeyUp;
			eFindControl.onblur = onFindControlBlur;
			eFindControl.onfocus = onFindControlFocus;
			eFindControl.onclick = onFindControlClick;
			eDropDownControl = document.getElementById(sSelectId + (getAttributeValue(eSelect, 'filtercontrolid') == '' ? '' : '_ReplacementControl'));
			eDropDownControl.parentNode.insertBefore(eFindControl, eDropDownControl);
			eDropDownControl.setAttribute('findcontrolid', eFindControl.id);
			eDropDownControl.style.display = 'none';
			eDropDownControl.style.position = 'absolute';
			eDropDownControl.style.top = (calculateElementTop(eFindControl) + eFindControl.offsetHeight).toString() + 'px';
			eDropDownControl.style.left = calculateElementLeft(eFindControl).toString() + 'px';
			eDropDownControl.style.width = eFindControl.offsetWidth.toString() + 'px';
			eDropDownControl.onblur = onDropDownBlur;
			eDropDownControl.onfocus = onDropDownFocus;
			eDropDownControl.onchange = addEvent(onSelectChange, eDropDownControl.onchange);
			eDropDownControl.onclick = onDropDownClick;
			eFindControl.setAttribute('dropdowncontrolid', eDropDownControl.id);
		}
		else
			eDropDownControl = document.getElementById(getAttributeValue(eFindControl, 'dropdowncontrolid'));
		var iSize = eDropDownControl.length + 1;
		eDropDownControl.size = (iSize > 15 ? 15 : iSize);

		if (eSelect.selectedIndex == 0 || (eSelect.selectedIndex > 0 && sFilterValue == getAttributeValue(eSelect.options.item(eSelect.selectedIndex), 'filtervalue')))
			eFindControl.value = eSelect.options.item(eSelect.selectedIndex).text;
		else
			eFindControl.value = '';
	}
}


// This will clear all the options for the specified filter.
// It also clears the related options of any filtered controls.
function clearSelectOptions(eSelect, sFilterValue)
{
	eSelect = getSelect(eSelect);

	var sFilteredControlId = getAttributeValue(eSelect, 'filteredcontrolid');
	var eFilteredControl;
	if (sFilteredControlId != '')
		eFilteredControl = document.getElementById(sFilteredControlId);
	
	for (var i = 0; i < eSelect.length; i++)
	{
		var eOption = eSelect.options.item(i);
		if ((i != 0 || eSelect.multiple == true) && (sFilterValue == '' || sFilterValue == getAttributeValue(eOption, 'filtervalue')))
		{
			if (sFilteredControlId != '')
				clearSelectOptions(eFilteredControl, eOption.value);
			eSelect.remove(i--);
		}
	}
}


// Determines if the select is to be displayed as a radio list.
function isRadioButtonControl(eSelect)
{
	return (typeof aRadioButtonControls != 'undefined' && aRadioButtonControls.indexOf(eSelect.id) >= 0)
}


// This will return the value from the selected radio button.
function getRadioButtonControlValue(eRadioButtonControl)
{
	var aRadioButtons = document.getElementsByName(eRadioButtonControl.id);
	for (var i = 0; i < aRadioButtons.length; i++)
	{
		var eRadioButton = aRadioButtons.item(i);
		if (eRadioButton.checked == true)
			return eRadioButton.value;
	}
	return '';
}


// This is called when the drop down changes.
function setFindControlText(eDropDownControl, bForceSet)
{
	var sReplacementControlId = getAttributeValue(eDropDownControl, 'replacementcontrolid');
	if (sReplacementControlId != '')
		eDropDownControl = document.getElementById(sReplacementControlId);
	var sFindControlId = getAttributeValue(eDropDownControl, 'findcontrolid');
	if (sFindControlId != '')
	{
		var iLastIndex = eDropDownControl.options.length - 1;
		if (eDropDownControl.selectedIndex < iLastIndex && getAttributeValue(eDropDownControl.options.item(iLastIndex), 'new') == 'true')
			eDropDownControl.remove(iLastIndex);
			
		var eFindControl = document.getElementById(sFindControlId);
		if (bForceSet == true || getAttributeValue(eDropDownControl, 'limittolist') != 'false')
			if (eDropDownControl.selectedIndex >= 0)
				eFindControl.value = eDropDownControl.options.item(eDropDownControl.selectedIndex).text;
			else
				eFindControl.value = '';
		else
			if (eFindControl.value != eDropDownControl.options.item(eDropDownControl.selectedIndex).text)
			{
				// If not limit to list then add the new option.
				var eOption = document.createElement('option');
				eOption.setAttribute('value', eFindControl.value);
				eOption.setAttribute('new', 'true');
				var eText = document.createTextNode(eFindControl.value);
				eOption.appendChild(eText);
				eDropDownControl.appendChild(eOption);
				eDropDownControl.selectedIndex = eDropDownControl.options.length - 1;
			}
	}
}


// This is called when the replacement control value changes.
function setReplacedControlValue(eReplacementControl)
{
	var sReplacedControlId = getAttributeValue(eReplacementControl, 'replacedcontrolid');
	if (sReplacedControlId != '')
	{
		var eReplacedControl = document.getElementById(sReplacedControlId);
		if (isRadioButtonControl(eReplacedControl) == true)
		{
			eReplacedControl.value = getRadioButtonControlValue(eReplacementControl);
		}
		else
			eReplacedControl.value = eReplacementControl.value;
	}
}


// This is called when the filter control value changes.
function refreshFilteredControl(eFilterControl)
{
	var sReplacedControlId = getAttributeValue(eFilterControl, 'replacedcontrolid');
	if (sReplacedControlId != '')
		eFilterControl = document.getElementById(sReplacedControlId);
	var sFilteredControlId = getAttributeValue(eFilterControl, 'filteredcontrolid');
	if (sFilteredControlId != '')
	{
		var eFilteredControl = document.getElementById(sFilteredControlId);
		refreshSelect(eFilteredControl);
		refreshFilteredControl(eFilteredControl);
	}
}


// This is called when an alpha, numeric, space, backspace, delete keys, up or down arrows are pressed.
// Also when find control is clicked on.
function openDropDown(eFindControl)
{
	document.getElementById(getAttributeValue(eFindControl, 'dropdowncontrolid')).style.display = 'block';
}


// This is called when the find control and drop down lose focus or when the tab or escape key are pressed.
function closeDropDown(eFindControl, bCheckFocus)
{
	if (typeof eFindControl == 'string')
		if (eFindControl == '')
			return;
		else
			eFindControl = document.getElementById(eFindControl);
	
	if (bCheckFocus == false || getAttributeValue(eFindControl, 'hasfocus') == 'false')
		document.getElementById(getAttributeValue(eFindControl, 'dropdowncontrolid')).style.display = 'none';
}


// When the select changes. If drop down then close it. Update associated controls.
function onSelectChange(e)
{
	var eSelectControl;
	if (window.event) // IE
		eSelectControl = window.event.srcElement;
	else
		eSelectControl = e.target;

	closeDropDown(getAttributeValue(eSelectControl, 'findcontrolid'), false);
	setReplacedControlValue(eSelectControl);
	refreshFilteredControl(eSelectControl);
}


// When the select changes. If drop down then close it. Update associated controls.
function onRadioButtonClick(e)
{
	var eRadioButton;
	if (window.event) // IE
		eRadioButton = window.event.srcElement;
	else
		if (typeof e.target.name == 'undefined') // span clicked in firefox
			eRadioButton = e.target.parentNode.childNodes.item(0);
		else // radio clicked in firefox
			eRadioButton = e.target;

	eRadioButton.checked = true;
	var eRadioButtonControl = eRadioButton.parentNode.parentNode;
	setReplacedControlValue(eRadioButtonControl);
	refreshFilteredControl(eRadioButtonControl);
	
	return true;
}


// When drop down loses focus set the hasFocus attribute.
// Update the find control and initiate the close of the drop down.
function onDropDownBlur(e)
{
	var eDropDownControl;
	if (window.event) // IE
		eDropDownControl = window.event.srcElement;
	else
		eDropDownControl = e.target;
	
	setFindControlText(eDropDownControl, false);
	var eFindControl = document.getElementById(getAttributeValue(eDropDownControl, 'findcontrolid'))
	// This attribute is used in closeDropDown to determine if the find control or drop down have focus.
	eFindControl.setAttribute('hasfocus', 'false');
	// Timer used due to focus issues. Focus may go to drop down so we don't want to close.
	setTimeout('closeDropDown(\'' + eFindControl.id + '\', true);', 100);
}


// When drop down gets focus set the hasFocus attribute.
function onDropDownFocus(e)
{
	var eDropDownControl;
	if (window.event) // IE
		eDropDownControl = window.event.srcElement;
	else
		eDropDownControl = e.target;
	
	// This attribute is used in closeDropDown to determine if the find control or drop down have focus.
	document.getElementById(getAttributeValue(eDropDownControl, 'findcontrolid')).setAttribute('hasfocus', 'true');
}


// Update and set the focus back to the find control so key presses work.
function onDropDownClick(e)
{
	var eDropDownControl;
	if (window.event) // IE
		eDropDownControl = window.event.srcElement;
	else
		eDropDownControl = e.target;
	
	if (eDropDownControl.tagName.toLowerCase() == 'option')
	{
		var eOption = eDropDownControl;
		eDropDownControl = eOption.parentNode;
		// The onFindControlBlur function is called prior to this
		// which sets the dropdown index to another value if not limited to list.
		// This works for FF but not for IE.
		if (eOption.selected == false)
			eDropDownControl.value = eOption.value;
	}
	
	setFindControlText(eDropDownControl, true);
	var eFindControl = document.getElementById(getAttributeValue(eDropDownControl, 'findcontrolid'))
	closeDropDown(eFindControl, false);
	eFindControl.focus();
}


// When find control loses focus set the hasFocus attribute.
// Initiate the close of the drop down.
function onFindControlBlur(e)
{
	var eFindControl;
	if (window.event) // IE
		eFindControl = window.event.srcElement;
	else
		eFindControl = e.target;
	
	setFindControlText(document.getElementById(getAttributeValue(eFindControl, 'dropdowncontrolid')), false);
	// This attribute is used in closeDropDown to determine if the find control or drop down have focus.
	eFindControl.setAttribute('hasfocus', 'false');
	// Timer used due to focus issues. Focus may go to drop down so we don't want to close.
	setTimeout('closeDropDown(\'' + eFindControl.id + '\', true);', 100);
}


// When find control gets focus set the hasFocus attribute and select text.
function onFindControlFocus(e)
{
	var eFindControl;
	if (window.event) // IE
		eFindControl = window.event.srcElement;
	else
		eFindControl = e.target;
	
	eFindControl.setAttribute('hasfocus', 'true');
	eFindControl.select();
}


// When click open the drop down.
function onFindControlClick(e)
{
	var eFindControl;
	if (window.event) // IE
		eFindControl = window.event.srcElement;
	else
		eFindControl = e.target;
	
	openDropDown(eFindControl);
}


// When editing key press open the drop down and find the first matching option.
function onFindControlKeyUp(e)
{
	var iKeyCode;
	var eFindControl;
	if (window.event) // IE
	{
		iKeyCode = window.event.keyCode;
		eFindControl = window.event.srcElement;
	}
	else
	{
		iKeyCode = e.which;
		eFindControl = e.target;
	}
	if (iKeyCode == 8 || iKeyCode == 46 || iKeyCode == 32 || iKeyCode >= 48) //Delete or Backspace or Space or alpha-numeric
	{
		openDropDown(eFindControl);
		var eDropDownControl = document.getElementById(getAttributeValue(eFindControl, 'dropdowncontrolid'));
		for (var i = 0; i < eDropDownControl.length; i++)
		{
			if (eDropDownControl.options.item(i).text.toLowerCase().indexOf(eFindControl.value.toLowerCase()) == 0)
				break;
		}
		if (i == eDropDownControl.length)
			i = 0;
		if (eDropDownControl.selectedIndex != i)
		{
			eDropDownControl.selectedIndex = i;
			setReplacedControlValue(eDropDownControl);
			refreshFilteredControl(eDropDownControl);
		}
	}
}


// When up or down arrow then open the drop down or select the previous or next option.
// When tab or escape key then close the drop down.
// Note: Tab key especially important so to close drop down before it gets focus.
function onFindControlKeyDown(e)
{
	var iKeyCode;
	var eFindControl;
	if (window.event) // IE
	{
		iKeyCode = window.event.keyCode;
		eFindControl = window.event.srcElement;
	}
	else
	{
		iKeyCode = e.which;
		eFindControl = e.target;
	}

	switch (iKeyCode)
	{
		case 38: // Up Arrow
		case 40: // Down Arrow
		{
			var eDropDownControl = document.getElementById(getAttributeValue(eFindControl, 'dropdowncontrolid'));
			if (eDropDownControl.style.display == 'none')
				openDropDown(eFindControl);
			else
			{
				if (eDropDownControl.selectedIndex >= 0 && eDropDownControl.options.item(eDropDownControl.selectedIndex).text == eFindControl.value)
				{
					var i = eDropDownControl.selectedIndex;
					if (iKeyCode == 38 && i > 0)
						i -= 1;
					if (iKeyCode == 40 && i < eDropDownControl.length - 1)
						i += 1;
					if (eDropDownControl.selectedIndex != i)
					{
						eDropDownControl.selectedIndex = i;
						setFindControlText(eDropDownControl, true);
						setReplacedControlValue(eDropDownControl);
						refreshFilteredControl(eDropDownControl);
					}
				}
				else
						setFindControlText(eDropDownControl, true);
			}
			break;
		}
		case 9: // Tab key
		case 27: // Escape key
		{
			closeDropDown(eFindControl, false);
			break;
		}
	}
}
