var comboAll= false; // true = all select boxes; false = only select boxes with the attribute combo="combo"
var forceMatch= true; // true = typed value must exist in the drop-down on blur; false = any value accepted on blur.
var forceAutoMatch= false; // true = only allows values in the drop-down to be typed; false = any value accepted.
window.onload=initCbo; // initalizes the script.
var f;
function initCbo(){
if(document.layers){return false}
var output='';
for(var f=0;f<document.forms.length;f++){
var s=document.forms[f].elements;
for(var i=0;i<s.length;i++){
if(s[i].type == 'select-one' && (s[i].combo=='combo' || comboAll==true)){
output='';
s[i].style.position='absolute';
s[i].style.left=0;
s[i].style.top=0;
// s[i].style.width='100%';
s[i].style.zIndex='1';
s[i].style.clip='rect(auto,auto,auto,'+(s[i].clientWidth-19)+')';
s[i].onfocus="window.clearTimeout(window.tmr"+s[i].name+")";
s[i].onblur="checkInList(\'"+s[i].name+"\',this.form)";
s[i].onchange='setTextBox(this,this.form)';
output+='<input type="text" name="'+s[i].name+'Box" style="position: absolute; left:0px; top:0px; width: 100%; z-index: 2;clip:rect(auto,'+(s[i].clientWidth-19)+',auto,auto)" onblur="checkInList(\''+s[i].name+'\',this.form)" ONKEYUP="autoComplete(this,this.form(\''+s[i].name+'\'),\'text\')" onNotInList="'+s[i].onNotInList+'" value="'+s[i][s[i].selectedIndex].text+'">';
output='<span style="position:relative">'+s[i].outerHTML+output+'</span> ';
s[i].outerHTML=output;
}
}
}
}
function checkInList(listName,f){
if(forceMatch){
var found=false;
var box=f[listName+"Box"];
var list=f[listName];
for(var i=0;i<list.length;i++){
if(box.value==list[i].text || box.value==''){
found=true;
break;
}
}
if(!found){
window.f=f;
eval("window.tmr"+listName+"=setTimeout(\"notInList('"+box.name+"')\",100)");
}
}
}
function notInList(box){
alert('The value you typed could not be found in the list!');
eval(f[box].onNotInList);
f[box].select();
f[box].focus();
}
function setTextBox(list,f){
var box = f[list.name+"Box"]
box.value=list[list.selectedIndex].text;
box.select();
box.focus();
}
function autoComplete (field, select, property) {
var found = false;
for (var i=0;i<select.options.length;i++) {
if(select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
found=true; break;
}
}
if (found) { select.selectedIndex = i; }
else { select.selectedIndex = -1; }
if (field.createTextRange) {
if (forceAutoMatch && !found) {
field.value=field.value.substring(0,field.value.length-1);
return;
}
var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
if (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 = newValue;
var rNew = field.createTextRange();
rNew.moveStart('character', oldValue.length) ;
rNew.select();
}
}
}
}