function atendimento(root_path) {
    window.open(root_path+"/msn/index/", "Autodata", "status=no, width=500, height=500");
}

function rastreio_correios(rastreio) {
    window.open("http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_TIPO=001&P_COD_UNI="+rastreio, "Autodata", "status=no, width=500, height=500");
}

function comentarios(root_path, pedi1_cod) {
    window.open(root_path+"/produtos/comentarios/"+pedi1_cod, "Comentários", "status=no, scrollbars = yes, width=620, height=500");
}

function indicar(root_path, pedi1_cod) {
    window.open(root_path+"/produtos/indicar/"+pedi1_cod, "Indicar", "status=no, scrollbars = yes, width=620, height=500");
}

function showOfertasLancamentos(cate1_cod, tipo, root_path, page){
    $.ajax({
        url: root_path + "/ajax/ofertasLancamentos/"+ cate1_cod +"/"+ tipo +"/"+ page +"/",
        cache: false,
        success: function(html){
            $("#ofertasLancamentos").html(html);
        }
    });
}

function checaLogin(){
    frm = document.frmLoginDo;
    erro = 0;

    if ((frm.email.value.length < 1) || (frm.email.value=="E-mail")){
        alert('Preencha seu E-mail.');
        frm.email.focus();
        erro = 1;
    }
    if ((frm.senha.value.length < 1) && (erro == 0)){
        alert('Preencha o campo senha.');
        frm.senha.focus();
        erro = 1;
    }

    if (erro == 0){
        frm.submit();
    }
}

function formatar(src, mask){
    var i = src.value.length;
    var saida = mask.substring(0,1);
    var texto = mask.substring(i)
	
    if (texto.substring(0,1) != saida){
        src.value += texto.substring(0,1);
    }
}

function favorito(root_path, prod1_cod){
    $.ajax({
        url: root_path + "/ajax/addFavorito/"+ prod1_cod +"/",
        cache: false,
        success: function(html){
            if (html == '0') {
                alert('Você precisa estar logado para utilizar os favoritos.');
            }
            else if (html == '1') {
                alert('Este produto já está em seus favoritos.');
            }
            else {
                alert('Produto cadastrado em seus favoritos.');
            }
        }
    });
}

function placeholder(obj, txt){
    if (obj.value == txt){
        obj.value = '';
    }
}

function placeholder2(obj, txt){
    if (obj.value == ''){
        obj.value = txt;
    }
}

function enviarContato(){
    frm = document.frmContato;
    erro = 0;
/*
    if (frm.nome.value.length < 1){
        alert('Preencha seu nome.');
        frm.rem_nome.focus();
        erro = 1;
    }
    if ((frm.email.value.length < 1) && (erro == 0)){
        alert('Preencha seu e-mail.');
        frm.rem_email.focus();
        erro = 1;
    }
*/
    if (erro == 0){
        frm.submit();
    }
}

function enviarAssistencia(){
    frm = document.frmAssistencia;
    erro = 0;

    if ((frm.nome.value.length < 1) || (frm.nome.value=="Nome")){
        alert('Preencha seu nome.');
        frm.nome.focus();
        erro = 1;
    }
    if (((frm.empresa.value.length < 1) || (frm.empresa.value=="Empresa")) && (erro == 0)){
        alert('Preencha o campo Empresa.');
        frm.empresa.focus();
        erro = 1;
    }
    if (((frm.email.value.length < 1) || (frm.email.value=="E-mail")) && (erro == 0)){
        alert('Preencha seu e-mail.');
        frm.email.focus();
        erro = 1;
    }
    if (((frm.telefone.value.length < 1) || (frm.telefone.value=="Telefone")) && (erro == 0)){
        alert('Preencha seu Telefone.');
        frm.telefone.focus();
        erro = 1;
    }
    if (((frm.cidade.value.length < 1) || (frm.cidade.value=="Cidade")) && (erro == 0)){
        alert('Preencha o campo Cidade.');
        frm.cidade.focus();
        erro = 1;
    }
    if (((frm.endereco.value.length < 1) || (frm.endereco.value=="Endereço")) && (erro == 0)){
        alert('Preencha seu Endereço.');
        frm.endereco.focus();
        erro = 1;
    }
    if ((frm.cpf.value.length < 1) || (frm.cpf.value=="CPF (apenas números)") && (erro == 0)){
        alert('Preencha seu CPF.');
        frm.cpf.focus();
        erro = 1;
    }

    if (erro == 0){
        frm.submit();
    }
}

function enviarSenha(){
    if (document.frmSenha.email.value.length < 1){
        alert("Digite seu e-mail para recuperar sua senha.");
    }
    else {
        document.frmSenha.submit();
    }
}

function enviarIndique(){
    frm = document.frmIndique;
    erro = 0;
	
    if (frm.rem_nome.value.length < 1){
        alert('Preencha seu nome.');
        frm.rem_nome.focus();
        erro = 1;
    }
    if ((frm.rem_email.value.length < 1) && (erro == 0)){
        alert('Preencha seu e-mail.');
        frm.rem_email.focus();
        erro = 1;
    }
    if ((frm.des_nome.value.length < 1) && (erro == 0)){
        alert('Preencha o nome de seu amigo.');
        frm.des_nome.focus();
        erro = 1;
    }
    if ((frm.des_email.value.length < 1) && (erro == 0)){
        alert('Preencha o e-mail de seu amigo.');
        frm.des_email.focus();
        erro = 1;
    }

    if (erro == 0){
        frm.submit();
    }
}

function inteiro(e){
    var tecla = (window.event)?event.keyCode:e.which;

    if((tecla > 47 && tecla < 58) || (tecla > 95 && tecla < 106) || tecla == 0){
        return (true);
    }
    else{
        if (tecla != 8){
            return (false);
        }
        else {
            return (true);
        }
    }
}

function produtos_index(){
    cate1_cod 	=	$('#fieldCate1_cod').val();
    cate1_key 	=	$('#fieldCate1_key').val();
    view	=	$('#fieldView').val();
    ordem	=	$('#fieldOrdem').val();
    root_path	=	$('#rootPath').val();
	
    hp = root_path + "/produtos/index/" + cate1_cod + "/" + cate1_key + "/" + view + "/" + ordem + "/";
	
    document.location = hp;
}

function produtos_favoritos(){
    view		=	$('#fieldView').val();
    ordem		=	$('#fieldOrdem').val();
    root_path           =	$('#rootPath').val();
	
    hp = root_path + "/produtos/favoritos/" + view + "/" + ordem + "/";
	
    document.location = hp;
}

function produtos_lista(){
    tipo	 	=	$('#fieldTipo').val();
    view		=	$('#fieldView').val();
    ordem		=	$('#fieldOrdem').val();
    root_path           =	$('#rootPath').val();
	
    hp = root_path + "/produtos/lista/" + tipo + "/" + view + "/" + ordem + "/";
	
    document.location = hp;
}

function produtos_busca(){
    tags1_cod 	=	$('#fieldTags1_cod').val();
    tags1_key	=	$('#fieldTags1_key').val();
    query	=	$('#fieldQuery').val();
    view	=	$('#fieldView').val();
    ordem	=	$('#fieldOrdem').val();
    root_path	=	$('#rootPath').val();
	
    hp = root_path + "/produtos/busca/" + tags1_cod + "/" + tags1_key + "/" + view + "/" + ordem;
	
    $('#frmViewOrder').attr('action',hp);
    $('#frmViewOrder').submit();
}

function buscaGoTo(pag){
    tags1_cod 	=	$('#fieldTags1_cod').val();
    tags1_key	=	$('#fieldTags1_key').val();
    view	=	$('#fieldView').val();
    ordem	=	$('#fieldOrdem').val();
    root_path	=	$('#rootPath').val();
	
    hp = root_path + "/produtos/busca/" + tags1_cod + "/" + tags1_key + "/" + view + "/" + ordem + "/" + pag + "/";
	
    $('#frmViewOrder').attr('action',hp);
    $('#frmViewOrder').submit();
}

function sendLogin(root_path){
    erro = 0;
	
    if ($('#fldEmail').val().length < 1){
        alert('Preencha o campo E-mail');
        $('#fldEmail').focus();
        erro = 1;
    }
    else {
        if ($('#fldCompra1').attr('checked')){
            if ($('#fldCep').val().length < 8){
                alert('Preencha o campo CEP corretamente.');
                $('#fldCep').focus()
                erro = 1;
            }
            $('#frmLogin').attr("action",root_path + "/cadastro/index/");
        }
        else if ($('#fldCompra2').attr('checked')){
            if ($('#fldSenha').val().length < 1){
                alert('Preencha o campo SENHA.');
                $('#fldSenha').focus();
                erro = 1;
            }
            $('#frmLogin').attr("action",root_path + "/login/do/");
        }
        else {
            alert('Selecione se você já é cliente ou se esta é sua primeira compra.');
            erro = 1;
        }
    }
	
    if (erro == 0){
        $('#frmLogin').submit();
    }
	
}

function tipoPessoa(){
    if ($('#pessFisica').attr('checked')){
        $('#txtCpf').html('<strong>CPF:</strong>');
        $('#txtRg').html('RG:');
        $('#fldCpf').attr('maxlength','11');
    }
    else {
        $('#txtCpf').html('<strong>CNPJ:</strong>');
        $('#txtRg').html('Insc. Estadual:');
        $('#fldCpf').attr('maxlength','14');
    }
}

function enviarCadastro(){
    frm = document.frmCadastro;
	
    erro = 0;
/*
    if (frm.clie1_nom.value.length < 1){
        alert("Preencha o campo 'Seu nome'.");
        frm.clie1_nom.focus();
        erro = 1;
    }
    if ((frm.clie1_cpf.value.length < 1) && (erro == 0)){
        alert("Preencha o campo 'CPF'.");
        frm.clie1_cpf.focus();
        erro = 1;
    }
    if ((frm.clie1_cpf.value.length != $('#fldCpf').attr("maxlength")) && (erro == 0)){
        if ($('#fldCpf').attr("maxlength") == 11){
            alert("O campo CPF deve ter 11 números.");
        }
        else {
            alert("O campo CNPJ deve ter 14 números");
        }
        frm.clie1_cpf.focus();
        erro = 1;
    }
    if ((frm.clie1_ema.value.length < 1) && (erro == 0)){
        alert("Preencha o campo 'E-mail'.");
        frm.clie1_ema.focus();
        erro = 1;
    }

    if ((frm.ende1_cep.value.length < 8) && (erro == 0)){
        alert("O campo CEP deve ter 8 números.");
        frm.ende1_cep.focus();
        erro = 1;
    }
    if ((frm.clie1_sen.value.length < 6) && (erro == 0)){
        alert("O campo Senha deve ter no mínimo 6 caracteres.");
        frm.clie1_sen.focus();
        erro = 1;
    }
    if ((frm.clie1_sen.value != frm.clie1_sen2.value) && (erro == 0)){
        alert("A confirmação de senha não confere.");
        frm.clie1_sen2.focus();
        erro = 1;
    }
    if ((frm.ende1_end.value.length < 1) && (erro == 0)){
        alert("Preencha o campo 'Endereço'.");
        frm.ende1_end.focus();
        erro = 1;
    }
    if ((frm.ende1_cid.value.length < 1) && (erro == 0)){
        alert("Preencha o campo 'Cidade'.");
        frm.ende1_cid.focus();
        erro = 1;
    }
    if ((frm.ende1_tel_pri.value.length < 1) && (erro == 0)){
        alert("Preencha o campo 'Telefone'.");
        frm.ende1_tel_pri.focus();
        erro = 1;
    }
    if (($("#frmCadastro .concordo:checked").length < 1) && (erro == 0)){
        alert("Você deve concordar com os nossos termos de uso.");
        frm.concordo_check.focus();
        erro = 1;
    }
    */
    if (erro == 0){
        frm.submit();
    }
}

// FUNCOES DO CARRINHO DE COMPRAS
function addCarrinho(prod1_cod, root_path){
	
    var retorno;
    var variacao;
	
    if ($('#variacaoPrincipal').length > 0){
        //alert($('#variacaoPrincipal').val());
        if ($('#variacaoPrincipal').val() > 0){
            retorno = true;
            variacao = $('#variacaoPrincipal').val();
        } else {
            retorno = false;
        }
    } else {
        retorno = true;
        variacao = 0;
    }
	
    if (retorno){
        document.location = root_path + "/carrinho/add/" + prod1_cod + "/" + variacao + "/";
    } else {
        $('#variacaoPrincipal').focus();
    }
}

function addCarrinhoQtd(prod1_cod, root_path){

    var retorno;
    var qtd1_prod;
    var variacao;

    if ($('#variacaoPrincipal').length > 0){
        if ($('#variacaoPrincipal').val() > 0){
            retorno = true;
            variacao = $('#variacaoPrincipal').val();
        } else {
            retorno = false;
            $('#variacaoPrincipal').focus();
        }
    } else {
        retorno = true;
        variacao = 0;
    }

    if (retorno){
        if ($('#quantidadePrincipal').val() > 0){
            qtd1_prod = $('#quantidadePrincipal').val();
            document.location = root_path + "/carrinho/add_quantidade/" + prod1_cod + "/" + qtd1_prod + "/" + variacao + "/";
        } else {
            $('#quantidadePrincipal').focus();
        }
    }
}

function consultaProduto(prod1_cod, root_path){
    document.location = root_path + "/contato/index/" + prod1_cod + "/";
}

function addCasada(casa1_cod, prod1, prod2, root_path){
	
    // VERIFICA PRODUTO UM
    objName = '#var_' + casa1_cod + '_1';
    if ($(objName).length > 0){
        if ($(objName).val() > 0){
            retorno1 = true;
            variacao1 = $(objName).val();
        }
        else {
            retorno1 = false;
        }
    }
    else {
        retorno1 = true;
        variacao1 = 0;
    }
	
    // VERIFICA PRODUTO DOIS
    objName = '#var_' + casa1_cod + '_2';
    if ($(objName).length > 0){
        if ($(objName).val() > 0){
            retorno2 = true;
            variacao2 = $(objName).val();
        }
        else {
            retorno2 = false;
        }
    }
    else {
        retorno2 = true;
        variacao2 = 0;
    }
	
    if ((retorno1 == true) && (retorno2 == true)){
        document.location = root_path + "/carrinho/add/" + prod1 + "/" + variacao1 + "/" + prod2 + "/" + variacao2 + "/";
    }
    else {
        alert('Selecione as op&ccedil;&otilde;es desejadas');
        $(objName).focus();
    }
	
}

function calcularFrete(root_path){
    cep = $('#cepDestino').val();
	
    if (cep != ''){
        $('#valorFrete').html("<p align='center'><img src='"+ root_path +"/admin/img/common/ajax-loader.gif' /><br />Aguarde Carregando</p>");
		
        $.ajax({
            url: root_path + "/ajax/calculaFrete/"+ cep +"/",
            cache: false,
            success: function(html){
                $("#valorFrete").html(html);
            }
        });
    }
}

function showFrete(root_path){
    valor = $('#valorSubTotal').val();
    valor = valor.replace(',','.');
	
    if ($('#opFreteSedex').attr('checked') == true){
        valorFrete = $('#opFreteSedex').attr('title');
        opcao = $('#opFreteSedex').val();
    }
    else {
        valorFrete = $('#opFretePac').attr('title');
        opcao = $('#opFretePac').val();
    }

    valorFrete = valorFrete.replace(',','.');

    // GRAVA A OPï¿½ï¿½O DESEJADA
    $.ajax({
        url: root_path + "/ajax/gravaFrete/"+ opcao +"/"+ valorFrete +"/",
        cache: false
    });

    valor = parseFloat(valor);
    valorFrete = parseFloat(valorFrete);

    valorTotal = valor + valorFrete;
    valorTotal = valorTotal.toFixed(2);

    valorFrete = "" + valorFrete;
    valorTotal = "" + valorTotal;

    $('#txtPreco').html('R$ ' + valorFrete.replace('.',','));
    $('#txtValorTotal').html('R$ ' + valorTotal.replace('.',','));
}

function alterarQuantidade(){
    $('#fieldAcao').val('2');
    $('#frmCarrinho').submit();
}

function excluirCarrinho(prod1_cod, vari1_cod){
    if (confirm('Deseja realmente retirar este item de seu carrinho de compras?')){
        $('#fieldProd').val(prod1_cod);
        $('#fieldVari').val(vari1_cod);
        $('#fieldAcao').val('3');

        $('#frmCarrinho').submit();
    }
}

function addCarrinhoQtdRelacionado(prod1_cod, root_path){

    var retorno;
    var qtd1_prod;
    var variacao;

    if ($('#vari_prod_rela' + prod1_cod + '').length > 0){
        if ($('#vari_prod_rela' + prod1_cod + '').val() > 0){
            retorno = true;
            variacao = $('#vari_prod_rela' + prod1_cod + '').val();
        } else {
            retorno = false;
            $('#vari_prod_rela' + prod1_cod + '').focus();
        }
    } else {
        retorno = true;
        variacao = 0;
    }

    if (retorno){
        if ($('#qtd_prod_rela' + prod1_cod + '').val() > 0){
            qtd1_prod = $('#qtd_prod_rela' + prod1_cod + '').val();
            document.location = root_path + "/carrinho/add_quantidade/" + prod1_cod + "/" + qtd1_prod + "/" + variacao + "/";
        } else {
            $('#qtd_prod_rela' + prod1_cod + '').focus();
        }
    }
}
