/************************************************
* SMART SELECT
* allows searching beyond first keystroke in <select> lists
* assumes the <option>s are sorted alphabetically
*
* implement with window.onload = attachSmartSelect;
*/
function selKeyPress(e) {
var ek = e ? e.which : event.keyCode;
// get char from keycode, add to cache
var key = String.fromCharCode(ek);
window.keyCache.push(key);
smartSelect(this);
// clear old timer, set new timer to expire the cache
window.clearTimeout(window.keyTimer);
window.keyTimer = window.setTimeout("resetKeyCache();", 3000);
return false;
}
function resetKeyCache(e) {
if (navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
e = event ? event : null;
var ek = e ? e.keyCode : null;
}
else {
if (e) {
ek = e.which;
}
}
if (!e || ek == 0 || ek == 46) {
// delete key pressed
window.keyCache.length = 0;
window.MX = null;
return false;
}
return false;
}
function smartSelect(el) {
// select the option most closely matching cache
// loop through cache
var oCache = window.keyCache;
mx = window.MX?window.MX:0;
cacheLoop:for (var cx = 0; cx < oCache.length; cx++) {
// get substring of cached chars to match
subCache = oCache.join("").substring(0, cx + 1);
// loop through options, starting at last match
optLoop:for (var ox = mx; ox < el.options.length; ox++) {
subTxt = el.options[ox].text.substring(0, cx + 1);
// quit if previous n - 1 chars don't match
if (subTxt.substring(0, cx - 1).toLowerCase() !=
subCache.substring(0, cx - 1).toLowerCase()) {
// remove last bad char from cache in case it was a miskey
oCache.pop();
break cacheLoop;
}
// see if cached chars == option chars
if (subCache.toLowerCase() == subTxt.toLowerCase()) {
// we have a match! store the index
mx = ox;
break optLoop;
}
}
}
// select the best match
el.selectedIndex = mx;
window.MX = mx;
}
function attachSmartSelect() {
// set up the keyCache
window.keyCache = [];
window.MX = 0;
// attach to all select lists
var f = document.forms;
for (var fx = 0; fx < f.length; fx++) {
var els = f[fx].elements;
for (var ex = 0; ex < els.length; ex++) {
if (els[ex].tagName.toLowerCase() == "select") {
els[ex].onkeypress = selKeyPress;
els[ex].onkeyup = resetKeyCache;
els[ex].onblur = resetKeyCache;
}
}
}
}
/*
*
**************************************************/