function atendimento(root_path,msn) {
    window.open(root_path+"/msn/index/"+msn, "Ideal_Cartuchos", "status=no, width=500, 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 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 inteiro(e){
    var tecla = (window.event)?event.keyCode:e.which;

    if((tecla > 47 && tecla < 58) || 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_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(){
    var frm = document.getElementById("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/CNPJ'.");
        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.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_cep.value.length < 8) && (erro == 0)){
        alert("O campo CEP deve ter 8 números.");
        frm.ende1_cep.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_num.value.length < 1) && (erro == 0)){
        alert("Preencha o campo 'Número'.");
        frm.ende1_num.focus();
        erro = 1;
    }
	
    if ((frm.ende1_bai.value.length < 1) && (erro == 0)){
        alert("Preencha o campo 'Bairro'.");
        frm.ende1_bai.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 1'.");
        frm.ende1_tel_pri.focus();
        erro = 1;
    }
	
    if (erro == 0){
        frm.submit();
    }
	
}

// FUNCOES DO CARRINHO DE COMPRAS
function addCarrinho(prod1_cod, root_path){




if ($("#variacaoPrincipal").length > 0) {
       if ($("input[@name='variacaoPrincipal']:checked").val() > 0){
 
            retorno = true;
            prod1_cod = prod1_cod+"/"+$("input[@name='variacaoPrincipal']:checked").val();


       } else {
            retorno = false;
        }
}else {
          retorno = true;
}
  
     
	
    if (retorno){
        document.location = root_path + "/carrinho/add/" + prod1_cod ;
    }
    else {
        alert("Selecione a opção desejada");
        
    }
}

function addCasada(casa1_cod, prod1, prod2, root_path){
    document.location = root_path + "/carrinho/add/" + prod1 + "/" + variacao1 + "/" + prod2 + "/" + variacao2 + "/";
}

function calcularFrete(root_path){
    cep = $('#endereco .input_cep').val();

    if (cep != ''){
        $('#cep').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){
                $("#cep").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 validaEmail(email) {
    if (window.RegExp) {
        var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
        var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
        var reg1 = new RegExp(reg1str);
        var reg2 = new RegExp(reg2str);
        if (!reg1.test(email) && reg2.test(email)) {
            return true;
        }
        return false;
    } else {
        if(email.indexOf("@") >= 0)
            return true;
        return false;
    }
}
