﻿var pos = 0;
var count = 0;

function noenter(url, key) {
    f = document.forms[0];
    suggcont = document.getElementById("suggestion");
    if (suggcont.style.display == "") {
        if (key == 13) {
            choiceclick(document.getElementById(pos));
            if (f.ddlBusca.value.length == 0) {
                window.location.href = url + '/busca.aspx?Query=ProductPage&keyWord=' + f.txtBusca.value;
            }
            else {
                window.location.href = url + '/busca.aspx?Query=ProductPage&ProdTypeId=' + f.ddlBusca.value + '&keyWord=' + f.txtBusca.value;
            }
            return false;
        }
        else {
            return true;
        }
    }
    else {
        if (key == 13) {
            if (f.ddlBusca.value.length == 0) {
                window.location.href = url + '/busca.aspx?Query=ProductPage&keyWord=' + f.txtBusca.value;
            }
            else {
                window.location.href = url + '/busca.aspx?Query=ProductPage&ProdTypeId=' + f.ddlBusca.value + '&keyWord=' + f.txtBusca.value;
            }
            return false;
        }
        else {
            return true;
        }
    }
}

document.onclick = function() {
    f = document.forms[0];
    closechoices();
    if (f.txtBusca.value.length > 0) {
        if (f.ddlBusca.value.length == 0) {
            window.location.href = uri + 'busca.aspx?Query=ProductPage&keyWord=' + f.txtBusca.value;
        }
        else {
            window.location.href = uri + 'busca.aspx?Query=ProductPage&ProdTypeId=' + f.ddlBusca.value + '&keyWord=' + f.txtBusca.value;
        }
    }
}

var uri = "";

function suggest(url, key, query) {
    uri = url;
    if (key == 38) {
        goPrev();
    }
    else if (key == 40) {
        goNext();
    }
    else if (key != 13) {
        if (query.length > 2) {
            t = new Date();
            if (document.forms[0].ddlBusca.length > 0) {
                doRequest(url + 'server.aspx?Action=busca&linha=' + document.forms[0].ddlBusca.value + '&kit=' + query + '&iesux=' + t.getTime(), update);
            }
            else {
                doRequest(url + 'server.aspx?Action=busca&kit=' + query + '&iesux=' + t.getTime(), update);
            }
        }
        else {
            closechoices();
        }
    }
}

function update(result) {
    arr = new Array();
    arr = result.split(',');

    if (arr.length > 10) {
        count = 10;
    }
    else {
        count = arr.length;
    }

    suggdiv = document.getElementById("suggestion");
    if (arr.length > 0) {
        suggdiv.innerHTML = '';

        document.getElementById("suggestion").style.display = '';

        for (i = 1; i <= arr.length; i++) {
            novo = document.createElement("div");
            suggdiv.appendChild(novo);
            novo.id = i;
            novo.setAttribute("style", "border-bottom: dashed #C00 1px");
            novo.style.height = "55px";
            novo.style.padding = "5px";
            novo.onmouseover = function() { select(this, true); }
            novo.onmouseout = function() { unselect(this, true); }
            novo.onclick = function() { choiceclick(this); }
            novo.innerHTML = arr[i - 1];
        }
    }
}

function select(obj, mouse) {
    obj.style.backgroundColor = '#c00';
    obj.style.color = '#ffffff';
    if (mouse) {
        pos = obj.id;
        unselectAllOther(pos);
    }
}

function unselect(obj, mouse) {
    obj.style.backgroundColor = '#ffffff';
    obj.style.color = '#c00';
    if (mouse) {
        pos = 0;
    }
}

function goPrev() {
    if (count > 0) {
        if (document.getElementById(pos)) {
            unselect(document.getElementById(pos));
            pos--;
            if (document.getElementById(pos)) {
                select(document.getElementById(pos));
            } else {
                pos = 0;
            }
        } else {
            pos = count;
            select(document.getElementById(count));
        }
    }
}

function goNext() {
    if (pos <= count && count > 0) {
        if (document.getElementById(pos)) {
            unselect(document.getElementById(pos));
        }
        pos++;
        if (document.getElementById(pos)) {
            select(document.getElementById(pos));
        } else {
            pos = 0;
        }
    }
}

function choiceclick(obj) {
    valor = obj.innerHTML;
    arr = new Array();
    arr = valor.split('> ');
    document.getElementById("txtBusca").value = arr[1];
    count = 0;
    pos = 0;
    suggcont = document.getElementById("suggestion");
    suggcont.style.display = "none";
    document.getElementById("txtBusca").focus();
}

function closechoices() {
    suggcont = document.getElementById("suggestion");
    if (suggcont.style.display == "") {
        count = 0;
        pos = 0;
        suggcont.style.display = "none";
    }
}

function unselectAllOther(id) {
    for (i = 1; i <= count; i++) {
        if (i != id) {
            document.getElementById(i).style.backgroundColor = '#ffffff';
            document.getElementById(i).style.color = '#c00';
        }
    }
}