﻿function UpdateSearchBox(e, nMethod) {
    // update searchbox with selected item from listbox
    // nMethod: 1=arrow selection, 2=click selection, 3=enter
    var oTxt = document.getElementById(ctxtSearch_ec);
    var oLb = document.getElementById(clbSug_ec);
    var oSearch = document.getElementById(cbtnSearch_ec);
    var oLocation = document.getElementById(ccboBooks_ec);
    oTxt.value = oLb.value;
    switch (nMethod) {
        case 2:
            oLb.style.top = '-200px';
            oLb.style.display = 'none';
            break;
        default:
            switch (e.keyCode) {
                case 13:
                    oLocation.focus();
                    oLb.style.top = '-200px';
                    oLb.style.display = 'none';
                    return false;
                    break;
                case 9:
                    oLb.style.top = '-200px';
                    oLb.style.display = 'none';
                    oLocation.focus();
                    break;
            }
            break;
    }
}


function ShowSuggestions(e) {
    var iKeyCode = e.keyCode;
    if (iKeyCode != 8 && iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        switch (iKeyCode) {
            case 13:
                var oSearch = document.getElementById(cbtnSearch_ec);
                oSearch.click();
                break;
            case 40:
                //down arrow
                var oLB = document.getElementById(clbSug_ec);
                var oTxt = document.getElementById(ctxtSearch_ec);
                if (oLB.style.display != 'none') {
                    oLB.focus();
                    oLB.options[0].selected = true;
                    oTxt.value = oLB.value;
                }
                break;
        }
    }
    else {
        var oTxt = document.getElementById(ctxtSearch_ec);
        var oTbl = document.getElementById('tblSB');
        if (oTbl != null) {
            document.getElementById(chidLeft_ec).value = oTbl.offsetLeft + oTxt.offsetLeft + 53;
            document.getElementById(chidTop_ec).value = oTbl.offsetTop + oTxt.offsetTop + 95;
            document.getElementById(chidSearch_ec).value = oTxt.value;
            document.getElementById(cbtnUpdate_ec).click();
            }
        else {
            oTbl = document.getElementById('tblSearchFor');
            document.getElementById(chidLeft_ec).value = oTbl.offsetLeft + oTxt.offsetLeft + 377;
            document.getElementById(chidTop_ec).value = oTbl.offsetTop + oTxt.offsetTop + 32;
            document.getElementById(chidSearch_ec).value = oTxt.value;
            document.getElementById(cbtnUpdate_ec).click();
        }
        oTxt.focus();
    }
}
