var xml = function() {
    var self = this;

    this.file   = null;
    this.ret    = null;
    this.xml    = '';
    this.xmlDoc = null;
    this.transf = 'GET';
    this.debug = false;
    this.returnType = 'xml';

    var xmlhttp = null;

    this.load = _load;

    function _load() {
        if (self.file && self.ret) {
            try {
                self.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e1) {
                try {
                    self.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e2) {
                    if (!self.xmlhttp && typeof XMLHttpRequest!='undefined') {
                        self.xmlhttp = new XMLHttpRequest();
                    }
                }
            }
            if (self.xmlhttp != null) {
                self.xmlhttp.onreadystatechange = _testReadyStateSend;
                self.xmlhttp.open(self.transf,self.file,true);
                if (this.transf == 'POST'){
                    self.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                }
                self.xmlhttp.send(self.xml);
            }
        } else {
            return false;
        }
    };

    function _testReadyStateSend() {
        if (self.xmlhttp.readyState == 4) {
            if (self.xmlhttp.status == '200') {
                _evalInternalGet();
            } else {
                //alert('Página não encontrada');
                return false;
            }
        }
    };

    function _evalReturn() {
        var retFunc = '';
        if (typeof(self.ret) == 'function') {
            eval('new self.ret');
        }
    };

    function _evalInternalGet() {
        self.xmlDoc = (self.returnType == 'xml' ? self.xmlhttp.responseXML : self.xmlhttp.responseText);
        if (self.debug){
            var container = document.getElementsByTagName("BODY");
            if (container.length > 0){
                var newDiv = document.createElement("DIV");
                newDiv.setAttribute("style", "position:absolute; width:640; left:150; top:300; background: #FFFFFF; border: 2px solid #000000");
                var txt = document.createTextNode(self.xmlhttp.responseText);
                newDiv.appendChild(txt);
                container[0].appendChild(newDiv);
            }
        }
        _evalReturn();
    };
};

var resultado = null;
var selecionado = -1;
var maximo = 0;
var produto = new Array(10);

function getResults(string, kc) {
    var div = document.getElementById("float_results");    
    document.busca.keyword.setAttribute('autocomplete', 'off');            
    if((string.length < 3) || (document.busca.key.value != 8)){        
        div.style.display = "none";        
        selecionado = -1;    
    }else{        
        if((div.style.display == "block") && ((kc == 38) || (kc == 40) || (kc == 13))){
            if(kc != 13){
                return mudarCaixa(kc);
            }else{
                loadResult(produto[selecionado]);
            }
        }else{
            selecionado = -1;
            var objPagina = new xml();
            objPagina.file = "float_results/xml_results.asp?string="+string;
            objPagina.ret = function () {
                var pagina = objPagina.xmlDoc.getElementsByTagName('item');
                var buffer = "";                
                if (pagina.length > 0) {
                    for (var i = 0; i < pagina.length; i++) {
                        var result  = pagina[i].getElementsByTagName('result')[0].firstChild.nodeValue;
                        var desc = pagina[i].getElementsByTagName('desc')[0].firstChild.nodeValue;
                        buffer += "<a href='#' onclick='loadResult(\""+desc+"\");' ";
                        buffer += "onmouseover='clearResult("+i+");'><font size='2'>"+result+"</font></a>";
                        maximo = i;
                        produto[i] = desc;
                    }
                    div.style.display = "block";
                
                    if(maximo == 9){
                        buffer += "<strong><em><a href='#' onclick='loadResult(999999);' ";
                        buffer += "onmouseover='clearResult("+i+");'>MAIS RESULTADOS...</a></em></strong>";
                        maximo += 1;
                    }
                    
                    buffer += "<div class='about'>-- www.ferrs.com.br --</div>";
                }else{
                    div.style.display = "none";
                }                
                div.innerHTML = buffer;
            }            
        objPagina.load();
        }    
    }
};

function loadResult(string) {
    if(isNaN(string) || string == 999999){
        document.busca.submit();
    }else{
        document.location.href='product.asp?id='+string;
    }
};

var intblur = 0;

function resultBlur(origem) {
    if(origem == 2){
        intblur += 1;
        if (intblur <= 1){
            setTimeout("resultBlur(2)",1000);
        }else{
                document.getElementById("float_results").style.display = "none";
                intblur = 0;
        }
    }else{        
        document.getElementById("float_results").style.display = "none";
    }
};
            
function mudarCaixa(kc){
    if(kc == 40){        
        if(selecionado == -1){
            selecionado = 0;
            pintarSelecionado(selecionado);
            return false;            
        }else{
            if(selecionado < maximo){
                selecionado++;
                pintarSelecionado(selecionado);
                return false;
            }else{
                selecionado = -1;    
                pintarSelecionado(selecionado);
                return false;
            }
        }
    }else{        
        if(kc == 38){
            if(selecionado == -1){
                selecionado = maximo;
                pintarSelecionado(selecionado);
                return false;
            }else{    
                if(selecionado > 0){
                    selecionado--;
                    pintarSelecionado(selecionado);
                    return false;
                }else{
                    selecionado = -1;
                    pintarSelecionado(selecionado);
                    return false;
                }
            }
        }
    }
};

function pintarSelecionado(indice){
    var float = document.getElementById("float_results");
    //var campo = document.busca.keyword;
    for(var i=0; i<=maximo; i++){
        float.getElementsByTagName('a')[i].className = 'nohovered';    
        if(selecionado != -1) {
            float.getElementsByTagName('a')[selecionado].className = 'hovered';
            //campo.value = float.getElementsByTagName('a')[selecionado].firstChild.nodeValue;
        }
    }
};

function clearResult(opcao){
    var float = document.getElementById("float_results");
    selecionado = opcao;
    for(var i=0; i<=maximo; i++){
        float.getElementsByTagName('a')[i].className = 'nohovered';
        if(selecionado == opcao) {
            float.getElementsByTagName('a')[selecionado].className = 'hovered';
        }
    }
};

