$(document).ready(function() {
    $('html').click(function() {
        if($("#autocompleteResults").css("display") == "block") {
            $("#autocompleteResults").css("display","none");
        }
    });
});

var timer;
function getSuggests(search) {
    if(search.length > 1) {
        clearTimeout(timer);
        timer = setTimeout("ajaxLiveSearch('"+search+"')", 250);  /* 0,25 sek */
    }
    else {
        ajaxLiveSearch(" ");
    }
}

function ajaxLiveSearch(search) {
    var xmlHttp = null;
    if(typeof XMLHttpRequest != 'undefined') {
        xmlHttp = new XMLHttpRequest();
    }
    if(!xmlHttp) {
        try {
            xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                xmlHttp  = null;
            }
        }
    }

    if(xmlHttp) {
        var url = "autoSuggest.php";
        var params = "search="+search;
        
        xmlHttp.open("POST", url, true);
        
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");
    
        xmlHttp.onreadystatechange = function () {
            if(xmlHttp.readyState == 4) {
            
                if(xmlHttp.responseText != '') {
                    document.getElementById("autocompleteResults").innerHTML = xmlHttp.responseText;
                    $("#autocompleteResults").css("display","block");
                }
                else {
                    $("#autocompleteResults").css("display","none");
                }
            }
        };
        xmlHttp.send(params);
    }
}
