function goCarrinho(idproduto){

    $("div#panel").slideDown("slow");
    $("#toggleC a").toggle();
    ajaxpage("/portal/carrinho/"+idproduto+"/1","panel");

}

function goRemoverProduto(idproduto){

    document.getElementById("pedqtde_"+idproduto).value = 0;
    ajaxpost('pedido_recalcular', 'panel')

}

function goLogin(){
    
    $("#frmLogin").validate({
        //        errorLabelContainer: $("#errorDivLogin"),
        highlight: function(input) {
            $(input).addClass("error-highlight");
        },
        unhighlight: function(input) {
            $(input).removeClass("error-highlight");
        },
        rules: {
            txtLoginEmail: {
                required: true,
                email: true
            },
            txtPwdEmail: {
                required: true,
                minlength: 4
            }
        },
        messages: {
            txtLoginEmail: {
                required: "[ Preencha o Email ] ",
                email: "[ O Email informado é inválido ] "
            },
            txtPwdEmail: {
                required: "[ Preencha a Senha ] ",
                minlength: "[ A Senha deve ter, no minimo, 4 caracteres ] "
            }
        },
        submitHandler: function(form) {
            ajaxpost("frmLogin","panel");
        }
    });


}

function goContato(){

    $("#formContato").validate({
        errorLabelContainer: $("#errorDivContato"),
        highlight: function(input) {
            $(input).addClass("error-highlight");
        },
        unhighlight: function(input) {
            $(input).removeClass("error-highlight");
        },
        rules: {
            txtContatoNome: {
                required: true
            },
            txtContatoEmail: {
                required: true,
                email: true
            },
            txtContatoDDDFone: {
                required: true,
                number: true,
                maxlength: 2
            },
            txtContatoFone: {
                required: true,
                number: true,
                maxlength: 8
            },
            txtContatoMensagem: {
                required: true
            }
        },
        messages: {
            txtContatoNome: {
                required: "[Preencha o Nome]"
            },
            txtContatoEmail: {
                required: "[Preencha o Email]",
                email: "[O Email informado é inválido]"
            },
            txtContatoDDDFone: {
                required: "Preencha o DDD<br>",
                number: "DDD deve conter apenas números<br>",
                maxlength: "DDD deve ter no máximo 2 digitos<br>"
            },
            txtContatoFone: {
                required: "Preencha o Telefone<br>",
                number: "Telefone deve conter apenas números<br>",
                maxlength: "Telefone deve ter no máximo 8 digitos<br>"
            },
            txtContatoMensagem: {
                required: "[Preencha a Mensagem]"
            }

        },
        submitHandler: function(form) {

            $.blockUI({
                message: "Aguarde...",
                overlayCSS: {
                    cursor: 'pointer'
                },
                css: {
                    cursor: 'default',
                    width: '1000px',
                    top:  ($(window).height() - 500) /2 + 'px',
                    left: ($(window).width() - 1000) /2 + 'px' //'50px',
                },
                onBlock: function() {
                    ajaxpost("formContato", "divBlockMsg");
                }
            });

            $('.blockOverlay').attr('title','Clique para fechar').click($.unblockUI);
            
        }
    });

}


function goCadastrar2(){

    $("#frmCadastro1").validate({
        errorLabelContainer: $("#errorDivCadastro"),
        highlight: function(input) {
            $(input).addClass("error-highlight");
        },
        unhighlight: function(input) {
            $(input).removeClass("error-highlight");
        },
        rules: {
            txtCadastroNome: {
                required: true
            },
            txtCadastroCPF: {
                required: true,
                number: true,
                minlength: 11
            },
            txtCadastroDtNascimento:{
                required: true
            },
            txtCadastroEmail: {
                required: true,
                email: true
            },
            txtPassCadastro: {
                required: true,
                minlength: 4
            },
            txtPassConfCadastro: {
                required: true,
                minlength: 4,
                equalTo: "#txtPassCadastro"
            }
        },
        messages: {
            txtCadastroNome: {
                required: "[Preencha o Nome]"
            },
            txtCadastroCPF: {
                required: "[Preencha o CPF]",
                number: "[CPF deve conter apenas números]",
                minlength: "[CPF deve conter 11 digitos]"
            },
            txtCadastroDtNascimento:{
                required: "[Informe a Data de Nascimento]"
            },
            txtCadastroEmail: {
                required: "[Preencha o Email]",
                email: "[O Email informado é inválido]"
            },
            txtPassCadastro: {
                required: "[Preencha a Senha]",
                minlength: "[A senha deve conter no mínimo 4 caracteres]"
            },
            txtPassConfCadastro: {
                required: "[Confirme a Senha]",
                minlength: "[A senha deve conter no mínimo 4 caracteres]",
                equalTo: "[A senha deve ser igual a anterior]"
            }
        },
        submitHandler: function(form) {
            ajaxpost("frmCadastro1","panel");
        }
    });

}

function goCadastrar(){

    $("#frmCadastro").validate({
        //        errorLabelContainer: $("#errorDivCadastro"),
        highlight: function(input) {
            $(input).addClass("error-highlight");
        },
        unhighlight: function(input) {
            $(input).removeClass("error-highlight");
        },
        rules: {
            txtCadastroEmail: {
                required: true,
                email: true
            }
        },
        messages: {
            txtCadastroEmail: {
                required: "[ Preencha o Email ]",
                email: "[ O Email informado é inválido ]"
            }
        },
        submitHandler: function(form) {
            ajaxpost("frmCadastro","panel");
        }
    });

}

function goFormaPagamento(){ 

    ajaxpost('formFormaPagamento', 'panel');

}

function goDesbloqueiaEndereco(){ 

    var form = document.getElementById("frmEndereco");
    for(i=0; i < form.elements.length; i++)
        if(form.elements[i].type == "text")
            if( document.getElementById('chkEndereco').checked == true)
                form.elements[i].disabled = true;
            else
                form.elements[i].disabled = false;

}

function goPesquisarCep(){

    cep = document.getElementById("txtCep").value;
    if(trim(cep)){
        ajaxpage("/portal/cep/"+cep, "pesquisacep");
    }
}
 
function goRetornarCep(logradouro, bairro, cidade, uf){
    document.getElementById("txtuf").value = uf;
    document.getElementById("txtcidade").value = cidade;
    document.getElementById("txtbairro").value = bairro;
    document.getElementById("txtlogradouro").value = logradouro;
    document.getElementById("txtnumero").focus();
}

function goValidarEndereco(div){

    if(div == "")
        div = "panel";

    $("#frmEndereco").validate({
        errorLabelContainer: $("#errorDiv"),
        highlight: function(input) {
            $(input).addClass("error-highlight");
        },
        unhighlight: function(input) {
            $(input).removeClass("error-highlight");
        },
        rules: {
            txtCep: {
                required: true,
                number: true,
                maxlength: 8
            },
            txtlogradouro: "required",
            txtnumero: {
                required: true,
                number: true
            },
            txtbairro: "required",
            txtcidade: "required",
            txtuf: {
                required: true,
                minlength: 2
            }
        },
        messages: {
            txtCep: {
                required: "Preencha o CEP<br>",
                number: "CEP deve conter apenas números<br>",
                maxlength: "CEP deve ter no máximo 8 digitos<br>"
            },
            txtlogradouro: "Preencha o Logradouro<br>",
            txtnumero: {
                required: "Preencha o Número<br>",
                number: "Número deve conter apenas números<br>"
            },
            txtbairro: "Preencha o Bairro<br>",
            txtcidade: "Preencha a Cidade<br>",
            txtuf: {
                required: "Preencha o Estado<br>",
                maxlength: "Estado deve ter apenas dois caracteres<br>"
            }
        },
        submitHandler: function(form) {
            ajaxpost("frmEndereco",div);
        }
    });
}

function goEnderecoEntrega(){

    var chk = document.getElementById('chkEndereco').checked;
    if(chk == true){
        ajaxpost("frmEndereco","panel");
    } else {
        goValidarEndereco();
    }
}

function trocaCampoCep(){
    
    if(document.getElementById('cep1').value.length == 5)
        document.getElementById('cep2').focus(); 

}

function goFecharJanela(){
    
        $.unblockUI();
        return false;
    

}

function goFecharPainel(){
    $("div#panel").slideUp("slow");
    $("#toggleC a").toggle();
}
function goAbrirPainel(){
    $("div#panel").slideDown("slow");
    $("#toggleC a").toggle();
}

function goFinalizarPedido(){ 

    //    $('#test').click(function() {
    $.blockUI({
        message: $('#question'),
        css: {
            width: '350px',
            backgroundColor: '#fff',
            color: '#666'
        }
    });
    //    });

    //    $.growlUI('Growl Notification', 'Have a nice day!');

    $('#yes').click(function() {
        // update the block message
        $.blockUI({
            message: "<h3>Registrando o seu pedido.<br>Aguarde...</h3>",
            css: {
                backgroundColor: '#f00',
                color: '#fff'
            }
        });
        setTimeout(function() {
            $.unblockUI({
                onUnblock: function(){
                    ajaxpost('formConfirmaCompra','panel');
                }
            });
        }, 3000);
    });

    $('#no').click(function() {
        $.unblockUI();
        return false;
    });
}

function goParcelasPagSeguro(){

    $.blockUI({
        message: $('#parcelasPagSeguro'),
        css: {
            width: '350px',
            height:'520px',
            top: '20px',
            backgroundColor: '#fff',
            color: '#666'
        }
    });
    $('.blockOverlay').attr('title','Clique para fechar').click($.unblockUI);

}

function goDetalhesPedido(idpedido){

    //    $(document).ajaxStop($.unblockUI);
    //$('#detalhePedido_'+idpedido).click(function() {
    $.blockUI({
        message: "Aguarde...",
        overlayCSS: {
            cursor: 'pointer'
        },
        css: {
            cursor: 'default',
            width: '1000px',
            top:  ($(window).height() - 500) /2 + 'px',
            left: ($(window).width() - 1000) /2 + 'px' //'50px',
        },
        onBlock: function() {
            ajaxpage('/portal/mostrarpedido/'+idpedido, "divBlockMsg");
        }
    });
        
    $('.blockOverlay').attr('title','Clique para fechar').click($.unblockUI);
        
//});

}

function goFecharDetalhesPedido(){

    $.unblockUI();

}

function goRastrearPedido(numeroremessa){

    window.open("http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_TIPO=001&P_COD_UNI="+numeroremessa);

}

function goAlterarDadosCadastrais(){
    
    $("#frm").validate({
        errorLabelContainer: $("#errorDivCadastro"),
        highlight: function(input) {
            $(input).addClass("error-highlight");
        },
        unhighlight: function(input) {
            $(input).removeClass("error-highlight");
        },
        rules: {
            txtCadastroNome: {
                required: true
            },
            txtCadastroCPF: {
                required: true,
                number: true,
                minlength: 11
            },
            txtCadastroDtNascimento:{
                required: true
            }
        },
        messages: {
            txtCadastroNome: {
                required: "[Preencha o Nome]"
            },
            txtCadastroCPF: {
                required: "[Preencha o CPF]",
                number: "[CPF deve conter apenas números]",
                minlength: "[CPF deve conter 11 digitos]"
            },
            txtCadastroDtNascimento:{
                required: "[Informe a Data de Nascimento]"
            }
        },
        submitHandler: function(form) {
            ajaxpost("frm","conteudologado");
        }
    });

}

function goAlterarDadosAcesso(){

    $("#frm").validate({
        errorLabelContainer: $("#errorDivCadastro"),
        highlight: function(input) {
            $(input).addClass("error-highlight");
        },
        unhighlight: function(input) {
            $(input).removeClass("error-highlight");
        },
        rules: {
            txtCadastroSenha: {
                required: true,
                minlength: 4
            },
            txtCadastroConfSenha: {
                required: true,
                minlength: 4,
                equalTo: "#txtCadastroSenha"
            },
            txtCadastroSenhaAtual: {
                required: true,
                minlength: 4
            }
        },
        messages: {
            txtCadastroSenha: {
                required: "[Preencha a Nova Senha]",
                minlength: "[A nova senha deve conter no mínimo 4 caracteres]"
            },
            txtCadastroConfSenha: {
                required: "[Confirme a Nova Senha]",
                minlength: "[A nova senha deve conter no mínimo 4 caracteres]",
                equalTo: "[A nova senha deve ser igual a anterior]"
            },
            txtCadastroSenhaAtual: {
                required: "[Preencha a Senha Atual]",
                minlength: "[A senha atual deve conter no mínimo 4 caracteres]"
            }
        },
        submitHandler: function(form) {
            ajaxpost("frm","conteudologado");
        }
    });

}

function goResponderEnquete(){
    //    var idresposta = $('input:radio[name=enqcodigo]:checked').val();
    //    var idenquete = $('#enqIdf').val();
    //    var data = $('#frmEnquete').serialize();




    $.blockUI({
        message: "Aguarde...",
        overlayCSS: {
            cursor: 'pointer'
        },
        css: {
            cursor: 'default',
            width: '400px',
            top:  ($(window).height() - 500) /2 + 'px',
            left: ($(window).width() - 400) /2 + 'px' //'50px',
        },
        onBlock: function() {
            ajaxpost("frmEnquete","divBlockMsg");
        }
    });

    $('.blockOverlay').attr('title','Clique para fechar').click($.unblockUI);

//
//
//    $.ajax({
//        type: 'POST',
//        url: '/portal/responderenquete/'+idresposta+'/'+idenquete,
//        data: data,
//        cache: false,
//        success: function(retorno) {
//            // unblock when remote call returns
//            $('#boxEnquetes').html(retorno);
//            $.blockUI({
//                message: $('#boxEnquetes'),
//                css: {
//                    width: '500px',
//                    top: '20px',
//                    left: '200px'
//                }
//            });
//
//            $('.blockOverlay').attr('title','Clique para fechar').click($.unblockUI);
//        }
//    });
//


}

function goExibirObreiro(id){

    $.blockUI({
        message: "Aguarde...",
        overlayCSS: {
            cursor: 'pointer'
        },
        css: {
            cursor: 'default',
            width: '650px',
            zIndex: '999999999',
            top:  ($(window).height() - 550) /2 + 'px',
            left: ($(window).width() - 650) /2 + 'px' //'50px',
        },
        onBlock: function() {
            ajaxpage("/portal/exibirObreiro/"+id,"divBlockMsg");
        }
    });

    $('.blockOverlay').attr('title','Clique para fechar').click($.unblockUI);

    $('#no').click(function() {
        $.unblockUI();
        return false;
    });
}

function goResultadoEnquete(){
    
    $.blockUI({
        message: "Aguarde...",
        overlayCSS: {
            cursor: 'pointer'
        },
        css: {
            cursor: 'default',
            width: '400px',
            top:  ($(window).height() - 500) /2 + 'px',
            left: ($(window).width() - 400) /2 + 'px' //'50px',
        },
        onBlock: function() {
            ajaxpage("/portal/resultadoenquete","divBlockMsg");
        }
    });

    $('.blockOverlay').attr('title','Clique para fechar').click($.unblockUI);
}

function gerarGrafico(id)
{   
    $("#"+id).visualize({
        type: 'bar',
        width: '360px'
    });
    $("#"+id).hide();
}


function trim(str){
    while (str.charAt(str.length - 1)==" ")
        str = str.substring(0, str.length - 1);
    while (str.charAt(0)==" ")
        str = str.substring(1, str.length);
    return str;
}

