﻿var typeAheadData =
{
    keyStrokes: "",
    focusDDLId: "",
    ResetOnNewDDLRequest: function(id) {
        if (this.focusDDLId != id) {
            this.focusDDLId = id; this.keyStrokes = "";
        }
    }
};
function TADD_OnKeyDown(tb) {
    if (event.ctrlKey)
        return;

    typeAheadData.ResetOnNewDDLRequest(tb.id);

    switch (event.keyCode) {
    	 case 9:  //TFS 17601 - after tabbing it would clear out list box selection
    	 	 return;
        case 38:
        case 40:
            typeAheadData.keyStrokes = "";
            return;
        case 13:
            typeAheadData.keyStrokes = "";
            tb.fireEvent("onchange");
            return;
        case 8:
            if (typeAheadData.keyStrokes.length > 0) {
                typeAheadData.keyStrokes = typeAheadData.keyStrokes.substr(0,
        typeAheadData.keyStrokes.length - 1);
            }
            event.cancelBubble = true;
            event.returnValue = false;
            break;
        default:
            var c = '';
            if ((event.keyCode >= 96) && (event.keyCode <= 105)) //Numbers 0-9
            {
                c = (event.keyCode - 96).toString();
            }
            else {
                c = String.fromCharCode(event.keyCode).toLowerCase();
            }
            if (c != null) {
                typeAheadData.keyStrokes += c;
            }
            event.cancelBubble = true;
            event.returnValue = false;
            break;
    }
    if (TADD_SelectItem(typeAheadData) == false) {
        typeAheadData.keyStrokes = "";
        window.status = "Not found";
    }
    else {
        tb.fireEvent("onchange");
        window.status = "KeyStrokes: " + typeAheadData.keyStrokes;
    }
}
function TADD_SelectItem(typeAheadData) {
    var ddl = document.getElementById(typeAheadData.focusDDLId);
    ddl.selectedIndex = -1;
    if (typeAheadData.keyStrokes.length > 0) {
        for (i = 0; i < ddl.options.length; i++) {
            if ((ddl.options[i].text.length >= typeAheadData.keyStrokes.length)
      && (ddl.options[i].text.substr(0,
      typeAheadData.keyStrokes.length).toLowerCase() == typeAheadData.keyStrokes)) {
                ddl.selectedIndex = i;
                return true;
            }
        }
    }
    return false;
}
