var tString = '';
var lsf = 0;	// last successful index
         
         
function evalKey() {
    // Global Variables
    var i = 0; 
    var success = false;
    var elem = event.srcElement;
    var tLowElemText = '';
    // Get the unicode char of the keypress
    var eCode = event.keyCode;
    // Check if it's a vaid ASCII Character
    if (eCode == 27){
        tString = ''; 
        elem.selectedIndex = 0;
        }
    else if ( (eCode > 31) && (eCode < 122))
        {
        // Convert the Code to the corresponding character and add to searchstring
        tString += String.fromCharCode(eCode);
        // ... and perform the search starting from the top element in the listbox
        while (success == false) 	
            {
                i = 0;
                // Convert everything to lowercase; allows an easy comparison
                var tLowString = tString.toLowerCase();
                // Compose the regexp searchstring ...
                var rExpr = eval("/^" +  tLowString + "/");
                while ((i < elem.length)&& (success == false) )	
                    {
                        tLowElemText = elem.options[i].text.toLowerCase();
                        // success: Position the listbox on the (first) found element
                        if (tLowElemText.search(rExpr) != -1)
                            {
                                elem.selectedIndex = i;
                                success = true;
                            }
                        else {
                                i++; }
                             } // while i < elem.length
                        // if nothing is found in the entire list, the last character of the
                        //searchstring is removed to allow typing the correct 'next' character
                        if (success == false) {
                            tString = tString.substr(0, tString.length-1);
                     }
              } // while success = false
        } 
        // Just for demo use: updates the span displaying the typed
        // characters; remove from code before re-using
//        lchr.innerText =String.fromCharCode(eCode);
//        tsf.innerText = tString;
         
        window.event.returnValue = false;
        window.event.cancelBubble = true;
}
