<!-- 
/**
	This javascript creates the "auto-complete" behavior for web forms
	displayed in Internet Explorer (one may call the behavior "type-ahead", "find-as-you-type" etc.
	
	This script is freeware by author Mingyi Liu and comes with no warranty whatsoever.
	in no case should the author be held liable for anything including loss/damage of any
	kind as the result of using this script.
	
	The author hereby grants you license to freely distribute or modify the scriipt
	for whatever purposes.  For any bug report or feature request, please send
	email to mingyiliu@yahoo.com 
*/

var keyTime, keyStr, allOpts, lastKey, selectTime;
var agt = navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

function dropdown() { //constructor for autocomplete dropdown
	keyTime = 0;
	keyStr = "";
	allOpts = new Array();
	lastKey = "";
}

function populateOptions(frmelement) {
	dropdown();
	for(var i = 0; i < eval("document."+frmelement+".options.length"); i++)
		allOpts[i] = eval("document."+frmelement+".options["+i+"].text.toLowerCase()");
}

function setSelection(frmelement) {
	// only deals with IE as firefox behaves well anyway, other browser can be added
	if (is_ie) {
		var keycode = window.event.keyCode;
		var currentKey = unescape('%' + keycode.toString(16)).toLowerCase();
		var idx, currentSIdx = eval("document."+frmelement+".selectedIndex");
		
		if (keycode == 8) {	//cancel the backspace browser behavior
			window.event.returnValue = false;
			keyStr = keyStr.substring(0, (keyStr.length-1));
		} else if (keycode == 27) keyStr = "";	//reset keyStr if escape key pressed
		else if (currentKey.indexOf("%") < 0) keyStr += currentKey;
		
		var newTime = new Date().getTime();
		if (keyTime != null && newTime - keyTime < 700) {	//if two keys were pressed between 0.7 seconds (can be customized), do type-ahead
			idx = findIdx();
		} else {	//unfortunately we seem to have to handle default browser behavior too
			var selectFirst = true;
			if (keyStr == lastKey) {
				idx = currentSIdx + 1;
				if (idx < allOpts.length && allOpts[idx].charAt(0) == lastKey)
					selectFirst = false;
			}
			if (selectFirst) idx = findIdx();
			lastKey = currentKey;
		}
		if (idx >= 0) {
			eval("document."+frmelement+".options["+currentSIdx+"].selected = false");
			eval("document."+frmelement+".options["+idx+"].selected = true");
		}
	}
}

function findIdx() {
/**
	full scan to find the smallest idx that match string keyStr (case-insensitive)
	if you get rid of all the toLowerCase() calls in this script, it'd become case
	sensitive match
*/
	var idx = -1, len = keyStr.length;
	for(var i = 0; i < allOpts.length; i++) {
		var str = allOpts[i].substring(0, len);
		if (keyStr == str) { idx = i; break; }
	}
	return idx;
}

function setTime() {
	keyTime = new Date().getTime();
	return false;
}

-->