var txtCodigo = null; //Variavel usada para retornar a referencia da caixa de texto usada
var lblDescricao = null;
var lblPreco = null;
var txtQuantidade = null;
var txtEmail = null;
var lblEmailExiste = null;
var lblEmbalagem = null;

function MensagemCEP(txt)
{
    alert('teste');
    SmxTI.LojaVirtual.WUC.Endereco.RetornarMenssagemCEP(txt.value,MensagemCEP_Callback);
}

function MensagemCEP_Callback(res)
{
    if(res != null)
    {
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblDadoValidacaoEndereco').value = res;
    }
}

function VerificarCepExiste(txt)
{
    if(txt.value != "")
    {
        SmxTI.LojaVirtual.WUC.Endereco.RetornoDadosCEP(txt.value,VerificarCepExiste_Callback);
    }
    else
    {
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblEndereco').innerHTML = "";
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblBairro').innerHTML   = "";
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblCidade').innerHTML   = "";
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblEstado').innerHTML   = "";
    }
}

function VerificarCepExiste_Callback(res)
{
    //Usando innerHTML devido ao Firefox
    var Resposta = res.value;
    
    if(Resposta == "")
    {
        alert("CEP Inválido!");
        
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_txtCEP').value          = "";
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblEndereco').innerHTML = "";
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblBairro').innerHTML   = "";
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblCidade').innerHTML   = "";
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblEstado').innerHTML   = "";
    }
    else
    {
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_txtCEP').value          = Resposta.split(';')[0];
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblEndereco').innerHTML = Resposta.split(';')[1];
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblBairro').innerHTML   = Resposta.split(';')[2];
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblCidade').innerHTML   = Resposta.split(';')[3];
        document.getElementById('ctl00_ContentPlaceHolder1_Endereco1_lblEstado').innerHTML   = Resposta.split(';')[4];
    }
}



function VerificarEmailExiste(txtEmail) {
    var lblEmailExiste = document.getElementById('ctl00_ContentPlaceHolder1_lblEmailExistente');
    
    if (replaceAll(txtEmail.value, ' ','') != '') {
        jQuery.get("ValidacaoEmailJaExiste.ashx",
                    { Email: txtEmail.value },
                    function(retorno) {
                        if (retorno != '') {
                            alert(retorno);
                            txtEmail.focus();
                        }

                        lblEmailExiste.innerText = retorno;
                    });
    }
    else {
        alert('Digite o email');
        lblEmailExiste.innerText = '';
        txtEmail.focus();
    }

    return false;
}

function VerificarEmailExisteTrocaEmail(txt)
{
    txtEmail = txt;
    
    if(txt.value != "")
    {
        SmxTI.LojaVirtual.AlterarEmail.EmailExiste(txt.value, VerificarEmailExiste_Callback);
    }
}

function VerificarEmailExisteTrocaEmail_Callback(res)
{
    var lblEmailExiste = document.getElementById('ctl00_ContentPlaceHolder1_lblEmailExistente');
    
    if(lblEmailExiste != null)
    {
        if(res.value)
        {
            lblEmailExiste.innerText = "Este email já existe. Digite outro!";
        }
        else
        {
            lblEmailExiste.innerText = "";
        }
    }
}

function VerificarEmailExiste_Callback(res)
{
    var lblEmailExiste = document.getElementById('ctl00_ContentPlaceHolder1_lblEmailExistente');
    
    if(lblEmailExiste != null)
    {
        if(res.value)
        {
            lblEmailExiste.innerText = "Este email já existe. Digite outro!";
        }
        else
        {
            lblEmailExiste.innerText = "";
        }
    }
}

function RetornarDescricaoProduto(txt) {
    //alert("Hello world 1");
    txtCodigo = txt;
    lblDescricao = document.getElementById(txtCodigo.id.replace('txtCodigo', 'lblDescricao'));
    lblEmbalagem = document.getElementById(txtCodigo.id.replace('txtCodigo', 'lblEmbalagem'));
    lblPreco = document.getElementById(txtCodigo.id.replace('txtCodigo', 'lblPreco'));
    txtQuantidade = document.getElementById(txtCodigo.id.replace('txtCodigo', 'txtQuantidade'));

    if (replaceAll(txtCodigo.value, ' ', '') == '000000') 
    {
        txtQuantidade.value = '';
        lblDescricao.innerHTML = '';
        lblEmbalagem.innerHTML = '';
        lblPreco.innerHTML = '';

        txtCodigo.value = '';
        txtCodigo.focus();

        return false;
    }
    else if (replaceAll(txtCodigo.value, ' ', '') != '') 
    {
        jQuery.get("CompraPorCodigoHandler.ashx",
                    { CodigoProduto: txtCodigo.value, TipoBusca: 'produto' },
                    function(data) {

                        if (data != '')
                        {
                            if (txtCodigo.value != '')
                            {
                                //Alterado Felipe de innerText para innerHTML
                                if (lblDescricao != null) {
                                    //Verifica se o produto tem saldo em estoque
                                    if (data == 'sem_saldo') {
                                        txtQuantidade.value = '';
                                        lblDescricao.innerHTML = '';
                                        lblEmbalagem.innerHTML = '';
                                        lblPreco.innerHTML = '';

                                        alert('Produto indisponível para compra.');

                                        txtCodigo.value = '';
                                        txtCodigo.focus();
                                    }
                                    else {
                                        txtQuantidade.disabled = false;

                                        lblDescricao.innerHTML = data.split(';')[0];
                                        lblEmbalagem.innerHTML = data.split(';')[2];
                                        lblPreco.innerHTML = data.split(';')[1];

                                        if (txtQuantidade.value == '') {
                                            txtQuantidade.value = '1';
                                        }

                                        txtQuantidade.select();
                                    }
                                }
                            }
                            else {
                                txtQuantidade.value = '';
                                lblDescricao.innerHTML = '';
                                lblEmbalagem.innerHTML = '';
                                lblPreco.innerHTML = '';

                                txtCodigo.value = '';
                                txtCodigo.focus();
                            }
                        }
                        else {
                            alert('Produto não encontrado.');

                            txtCodigo.focus();
                            txtCodigo.value = '';
                            txtQuantidade.value = '';
                            lblDescricao.innerHTML = '';
                            lblEmbalagem.innerHTML = '';
                        }
                    });
    }
    else 
    {
        lblPreco.innerHTML = '';
        txtQuantidade.value = '';
        lblDescricao.innerHTML = '';
        lblEmbalagem.innerHTML = '';
    }
}

function CalcularTotalCompraPorCodigo(txtQuantidade, txtCodigo, lblPreco) {
    var txtCodigo = document.getElementById(txtCodigo);
    var lblPreco = document.getElementById(lblPreco);
    
    if (txtQuantidade.value != '')
    {
        if (!isNaN(txtQuantidade.value)) {
            jQuery.get("CompraPorCodigoHandler.ashx",
                    { CodigoProduto: txtCodigo.value, TipoBusca: 'quantidade', Quantidade: txtQuantidade.value },
                    function(data) {
                        if (data != '') {
                            if (txtCodigo.value != '') {
                                //Alterado Felipe de innerText para innerHTML
                                if (lblDescricao != null) {
                                    //Verifica se o produto tem saldo em estoque
                                    if (data == 'sem_saldo') {
                                        alert('Produto indisponível para compra.');

                                        lblPreco.innerHTML = '';

                                        if (txtQuantidade.value != '') {
                                            txtQuantidade.value = '';
                                        }

                                        lblDescricao.innerHTML = '';
                                        lblEmbalagem.innerHTML = '';

                                        txtCodigo.value = '';
                                        txtCodigo.focus();
                                    }
                                    else {
                                        txtQuantidade.disabled = false;

                                        lblPreco.innerHTML = data;
                                    }
                                }
                            }
                            else {
                                txtQuantidade.value = '';
                                lblDescricao.innerHTML = '';
                                lblEmbalagem.innerHTML = '';
                                lblPreco.innerHTML = '';

                                txtCodigo.value = '';
                                txtCodigo.focus();
                            }
                        }
                    });
        }
        else {
            alert('Quantidade inválida.');

            txtQuantidade.focus();
        }
    }
    else {
        if (txtCodigo.value != '') {
            txtQuantidade.value = '1';
            txtQuantidade.onblur();
        }
    }
}

function ComprarProdutoPorAaZ(idproduto, qtd, tipoEstatistica, idEstatistica) {
    var txtQuantidade = null;
    var quantidade = null;

    txtQuantidade = document.getElementById(qtd);

    if (txtQuantidade != null) {
        quantidade = txtQuantidade.value;
    }
    else {
        quantidade = 1;
    }

    if (!isNaN(quantidade)) {
        jQuery.get("CompraPorAaZ.ashx",
                    { CodigoProduto: idproduto, Quantidade: quantidade, Etp: tipoEstatistica, Eid: idEstatistica },
                    function(retorno) {
                    if (retorno != '') 
                        {
                            alert(retorno);
                        }
                    });
    }
    else {
        alert('Quantidade inválida');
        
        if (txtQuantidade != null) {
            txtQuantidade.focus();
        }
    }
    
    return false;
}

function ComprarProdutoPorAaZAtualizarSacola(idproduto, qtd, IDSubTotalSacola, IDQuantidadeSacola, tipoEstatistica, idEstatistica) {
    var txtQuantidade = null;
    var quantidade = null;
    var lblSubTotalSacola = null;
    var lblQuantidade = null;

    txtQuantidade = document.getElementById(qtd);
    lblSubTotalSacola = document.getElementById(IDSubTotalSacola);
    lblQuantidade = document.getElementById(IDQuantidadeSacola);
    
    if (txtQuantidade != null) {
        quantidade = txtQuantidade.value;
    }
    else {
        quantidade = 1;
    }

    if (!isNaN(quantidade)) 
    {
        jQuery.get("CompraPorAaZ.ashx",
                    { CodigoProduto: idproduto, Quantidade: quantidade, uptscl: '1', Etp: tipoEstatistica, Eid: idEstatistica },
                    function(retorno) {
                        if (retorno != '') 
                        {
                            alert(retorno.split('|')[0]);
                            lblQuantidade.innerHTML = retorno.split('|')[1];
                            lblSubTotalSacola.innerHTML = retorno.split('|')[2];
                        }
                    });
    }
    else 
    {
        alert('Quantidade inválida');

        if (txtQuantidade != null) {
            txtQuantidade.focus();
        }
    }

    return false;
}
