var type;
var val = "";

function searching(typ) {
    var request = false;
    type = typ;
    


    if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        request = new XMLHttpRequest();
    }


    var keyword = document.getElementById(typ + '_keyword');
    keyword = keyword.value;

    var url = "./ajax/search.php?keywords=" + keyword;
    
    request.open("GET", url, true);
    request.onreadystatechange = function() {

        //ziskani a parsovani xml
        var new_div;
        var i;


        var xml = request.responseXML;
        data = xml.getElementsByTagName("items");


        //vyprazdneni divu s vysledky
        var div = document.getElementById(type + "_search");
        div.innerHTML = "";


        //vytvareni a vkladani novych divu s obsahem
        for (i = 0; i < data.length; i++) {
            new_div = document.createElement("div");
            new_div.setAttribute("class", "search_div");
            new_div.setAttribute("id", "d_" + i);
            new_div.innerHTML = data[i].firstChild.nodeValue;

            //prirazeni kazdemu divu onmouseover, onmouseout
            new_div.onmouseover = function() {
                this.style.backgroundColor = "#7fabd8";
                val = this.innerHTML;
            }

            new_div.onmouseout = function() {
                this.style.backgroundColor = "#f7f7f7";
                val = "";
            }


            div.appendChild(new_div);
        }

        div.style.visibility = "visible";

        if (!data.length) {
            div.style.visibility = "hidden";    
        }
  

    }

    request.send(null);
}


function out(type) {
    var div = document.getElementById(type + "_search");
    div.style.visibility = "hidden";

    //doplneni do pole pokud klikl na nejakou polozku
    if (val) {
        var text = document.getElementById(type + "_keyword")
        text.value = val;
    }

}
