﻿var timerId = 0;

function OnClickLogoff() {
    FazerPostBack("FazerLogoff","");
}

function OnClickIndique() {
    window.open(RetEndBase() + "PopIndique.aspx", "PopCod", "scrollbars=0,width=350,height=142,location=0,menubar=0,resizable=0", "");
}

function OnClickIndiqueProduto(CodProduto, DescProduto) {
    window.open(RetEndBase() + "PopIndiqueProduto.aspx?CodProduto=" + CodProduto + "&DescProduto=" + DescProduto, "PopCod", "scrollbars=0,width=360,height=323,location=0,menubar=0,resizable=0", "");
}

function OnClickBuscar() {
    var CodSetor = $("selectSetorBusca").value;
    var TextoPesquisa = $("tbBuscar").value.trim();
    
    CodSetor = "0" + CodSetor;

    TextoPesquisa = TextoPesquisa.replace('\'', '');
    while (TextoPesquisa.indexOf(' ') > -1) {
        TextoPesquisa = TextoPesquisa.replace(' ', '+');
    }
    
    window.location = RetEndBase() + "Busca/" + TextoPesquisa.replace(" ", "+") + (CodSetor != "00" ? "?CodSetor=" + CodSetor : "");
}

//window.onkeypress = keypress;

function keypress(e) { MudarTeclaDigitada(e, 0); }

function OnKeyUpBuscar(e) {
    var compBotao = $("bbBuscar");
    var comp = $("tbBuscar");
    
    if (PegarCodigoTecla(e) != 27) {
        if (PegarCodigoTecla(e) == 13) {
            if (timerId != 0) {
                clearTimeout(timerId);
                timerId = 0;
            }
            MudarTeclaDigitada(e,0);
            compBotao.focus();
            compBotao.click();
        }  
        else {
            if (comp.value.length > 3) {
                if (timerId != 0) { clearTimeout (timerId);	 timerId = 0; }
	            timerId = setTimeout("EfetivarBusca()", 500);
            }
            else {
                if (timerId != 0) { clearTimeout (timerId);	}
                $('PopUpBusca').style.display = 'none';
            }
        }
    }
}

function EfetivarBusca() {
  var comp = $("tbBuscar");
  WebService.RetPopupPesquisa(comp.value,SucceededCallback_PopUpPesquisa,FailedCallback_PopUpPesquisa);
}

function SucceededCallback_PopUpPesquisa(result, eventArgs) {
    var dr;
    var compPopup = $('PopUpBusca');
    var ik;
    
    dr = eval(result);
    
    compPopup.style.display = 'none';
    compPopup.innerHTML = '';
    
    
    if (dr.Rows.length > 0) {
        for (ik = 0; ik < dr.Rows.length; ik++) {
            linha = dr.Rows[ik];
            //alert(linha);
            if (ik > 0) { 
                compPopup.innerHTML += '<div style="height: 1px; border-top: 1px solid silver; _margin-bottom: -15px;"></div>';
            }
            
            compPopup.innerHTML += '<br/><div><a href="' + linha.Endereco +  '">' + linha.B1_DESC.trim() + '</a></div><br/>';
        }
        compPopup.style.display = 'block';
    }
    else {
        compPopup.style.display = 'none';
    }
}

//Essa função é chamada caso o WebService falhe.
function FailedCallback_PopUpPesquisa(error)
{
    var compPopup = $('PopUpBusca');
    if (timerId != 0) { clearTimeout (timerId);	 timerId = 0; }
    compPopup.style.display = 'none';
    
    if (error.get_statusCode() != 0) {
        alert("[" + new String(error.get_statusCode()) + "]: " + error.get_message());
    }
}

function OnBlurBuscar() {
    /*
    var comp = document.getElementById("tbBuscar");
    if (comp.value == "") {
      comp.value = "Buscar...";
    }
    */
    /*
    if (timerId != 0) { clearTimeout (timerId);	 timerId = 0; }
    $('PopUpBusca').style.display = 'none';
    */
}

function OnFocusBuscar() {
    /*
    var comp = $("tbBuscar");
    var compPopup = $('PopUpBusca');
    
    var comp = document.getElementById("tbBuscar");
    if (comp.value == "Buscar...") {
      comp.value = "";
    }
    
    if (compPopup.style.display == 'none') {
        if (comp.value.length > 3) {
            if (timerId != 0) { clearTimeout (timerId);	}
	        timerId = setTimeout("EfetivarBusca()", 500);
        }
    }
    */
}
/*
function ConfigSiteParaResolucao() {
    var w = screen.width;
    var h = screen.height;

    if (w < 1024) {
      $("central_resolucao").className = "";
      $("central_resolucao").style.width = "970px";
    }
}
*/

function OnMouseOverCarrinho() {
    $("popupCarrinho").style.display = "block";
}

function OnMouseOutCarrinho() {
    $("popupCarrinho").style.display = "none";
}

function OnClickMinhaConta() {
    FazerPostBack("FazerLogon","");
}

function OnClickAtendimento() {
    window.location = RetEndBase() + 'SuporteAtendimento.aspx';
}

function OnClickMeusPedidos() {
    window.location = RetEndBase() + 'HistoricoPedido.aspx';
}

function OnClickBonus() {
    window.location = RetEndBase() + 'InfoBonus.aspx';
}

function OnClickDuvidas() {
    window.location = RetEndBase() + 'SuportePerguntasFrequentes.aspx';
}
