﻿
var lagTime = 700;
var interruptableTimeout = undefined;
var lastQuery = undefined;
var anyResult = false;

function txtInput_Change() {

    clearTimeout(interruptableTimeout);
    if ($('#txtInput').val().length >= 3) {

        var thisQuery = $('#txtInput').val();
        interruptableTimeout = setTimeout(
			function () {
			    interruptableTimeout = undefined;
			    startAjax(thisQuery);
			}
			, lagTime);
    }
    else {
        hideResult();
    }
}

function startAjax(query) {
    if (query != lastQuery) {
        lastQuery = query;
        $('#divComboResults').html('<img src="/img/progress.gif"/>');
        $('#combosearchicon').show();
        $('#divComboResults').show();
        $.get("/ajax/combosearchresults.aspx?query=" + query, function (response) {
            gotResult(response);
        });
    }
    else {
        showResult();
    }
}

function gotResult(response) {
    $('#divComboResults').html(response);
    anyResult = true;
    showResult();
}
function hideResult() {
    $('#divComboResults').hide(600);
}
function showResult() {
    $('#combosearchicon').hide();
    if (anyResult) {
        $('#divComboResults').show(200);
    }
}
