﻿// ---------------------------------------------------------------------------------------------------------
// --- Bismillahirrahmanirrahim
// --- Yeri görğü ve arasındakileri yaratana hamd olsun.
// --- Ne yaparsan yap Allah'ın adıyla başla.
// ---------------------------------------------------------------------------------------------------------
// --- Kodlayan : M.Kaan GİRTİ // Firma : Eriş UN // Departman : Bilgi İşlem // Tarih : 02 Şubat 2010
// ---------------------------------------------------------------------------------------------------------

var q = document.getElementById("searchKey");
function Search() {
    if (q.value != "") {
        window.location = "../search.aspx?q=" + q.value;
    }
}

var lastString = "";
function SearchTimer() {
    var sfilter = document.getElementById("SearchFilter");
    if (lastString != q.value) {
        XmlExecute("../searchQuery.aspx?q=" + q.value, SearchQueryExecute, "");
        lastString = q.value;
    }
    if (q.value == "") { sfilter.style.display = "none"; }
}

function SearchQueryExecute() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        if (200 == xmlHttp.status) {
            var sfilter = document.getElementById("SearchFilter");
            var getData = xmlHttp.responseXML.getElementsByTagName("ALLDATA")[0].childNodes[0].nodeValue;
            if (getData != "") {
                var valueSplit = getData.split("~");
                var valueCount = valueSplit.length - 1;
                if (valueCount > 1) {
                    document.getElementById("SearchKeys").innerHTML = "";
                    for (v = 0; v < valueCount; v++) {
                        datSplit = valueSplit[v].split("*");
                        var newLi = document.createElement("li");
                        newLi.innerHTML = '<table cellpadding="0" cellspacing="0"><tr><td width="90%" onclick="javascript:sfUseKey(this);">' + datSplit[0] + '</td><td width="10%" align="right"><nobr>' + datSplit[1] + '</nobr></td></tr></table>';
                        document.getElementById("SearchKeys").appendChild(newLi);
                    }
                    sfilter.style.display = "block";
                }
            }
            else {
                sfilter.style.display = "none";
            }
        }
    }
}

function sfUseKey(getValue) {
    q.value = getValue.innerHTML;
    Search();
}
document.getElementById("searchKey").value = document.getElementById("searchKey").value.replace(/&quot;/gi, String.fromCharCode(34));

function reSizeTag(getTag) {
    var myWidth = 0, myHeight = 0;
    if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    else if (typeof (window.innerWidth) == "number") {
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
    //------
    var newX = ((myWidth / 2) + Number(document.getElementById("SearchAdd").innerHTML));
    if (myWidth > 800) {
        document.getElementById("SearchFilter").style.left = newX + "px";
    }
}

$(document).ready(function() {
    var sfilter = document.getElementById("SearchFilter");
    sfilter.style.display = "none";
    resizeTime = setInterval(reSizeTag, 1);
    Timer = setInterval(SearchTimer, "100");
});

document.body.onclick = function() {
    document.getElementById("SearchFilter").style.display = "none";
}

var nowKey = -1;

function ClearBackground() {
    var sKey = document.getElementById("SearchKeys");
    var totalKey = sKey.childNodes.length;
    if (totalKey > 0) {
        for (i = 0; i < totalKey; i++) { sKey.childNodes[i].style.backgroundColor = ""; }
    }
    nowKey = -1;
}

function SearchKeyPress(evt) {
    evt = (evt) ? evt : (window.event) ? event : null;
    var charCode = 0;
    if (evt) {
        charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    }
    
    var sfilter = document.getElementById("SearchFilter");
    if (sfilter.style.display == "block") {
        var sKey = document.getElementById("SearchKeys");
        var totalKey = sKey.childNodes.length;
        if (totalKey > 0) {
            for (i = 0; i < totalKey; i++) { sKey.childNodes[i].style.backgroundColor = ""; }
        }
        /******/
        if (charCode == 38) {
            nowKey = nowKey - 1;
        }
        else if (charCode == 40) {
            nowKey = nowKey + 1;
        }
        else if (charCode == 13) {
            var getText = sKey.childNodes[nowKey].childNodes[0].childNodes[0].childNodes[0].childNodes[0].innerHTML;
            q.value = getText;
            sfilter.style.display == "none";
            Search();
        }
        if (nowKey > (totalKey - 1)) { nowKey = 0; }
        if (nowKey < 0) { nowKey = (totalKey - 1); }
        sKey.childNodes[nowKey].style.backgroundColor = "#E9E9E9";
    }
    else {
        if (charCode == 13) {
            Search();
        }
    }
}
