$(document).ready(function(){
    $("#telefone").mask("(99) 9999-9999");
    $("#fone").mask("(99) 9999-9999");
    $("#cep").mask("99.999-999");
    $("#cnpjSoftwareHouse").mask("99.999.999/9999-99");
    $("#buscar").click (function() {
        location.href= "index.php?busca=" + $("#busca").val();
    });
    $("#busca").keypress (function(e) {
        if (e.which == 13){
            $("#buscar").click();
        }
    });
    $(".cnpjLiberacao").mask("99.999.999/9999-99");
	
    $("#inscricaoCNPJ").mask("99.999.999/9999-99");
    $("#inscricaoCPF").mask("999.999.999-99");
	
    $("input:radio[name=tipo]").click (function() {
        if($("input:radio[name=tipo]").filter(":checked").val() == "juridica"){
            $("#juridico").attr("checked");
            $("#boxInscricao").html("CNPJ");
            $("#boxNome").html("Razão");
            $("#inscricaoCNPJ").val("");
            $("#inscricaoCPF").val("");
            $("#inscricaoCNPJ").show();
            $("#inscricaoCPF").hide();
        } else {
            $("#fisica").attr("checked");
            $("#boxInscricao").html("CPF");
            $("#boxNome").html("Nome");
            $("#inscricao").val("");
            $("#inscricaoCNPJ").val("");
            $("#inscricaoCPF").val("");
            $("#inscricaoCNPJ").hide();
            $("#inscricaoCPF").show();
        }
    });
    $("input:radio[id=juridico]").click();
	
    $("input:radio[name=tipoPagto]").click (function() {
        if($("input:radio[name=tipoPagto]").filter(":checked").val() == "pagSeguro"){
            $("#boxDescriAcrescimo").html("Tarifa Opção <strong>PagSeguro</strong>");
            $("#boxDescriTotal").html("Total");
            $.post("calculaAcrescimo.php?tipo=valor",{
                total: $("#totalCarrinho").val()
            },function(retorno){
                $("#valorAcrescimo").val(retorno);
                $("#boxValorAcrescimo").html("R$ " + retorno);
            });
            $.post("calculaAcrescimo.php?tipo=total",{
                total: $("#totalCarrinho").val()
            },function(retorno){
                $("#boxTotalAcrescimo").html("R$ " + retorno);
            });
        } else {
            $("#boxDescriAcrescimo").html("");
            $("#boxDescriTotal").html("");
            $("#boxValorAcrescimo").html("");
            $("#boxTotalAcrescimo").html("");
            $("#valorAcrescimo").val("");
        }
    });
    $("input:radio[id=pagSeguro]").click();
	
    $("#enviar").click (function(){
        var mens = "";
		
        var produtos = $("#produtos").val();
        var quantidades = $("#quantidades").val();
        var prod = new Array();
        var quant = new Array();
        prod = produtos.split("|");
        quant = quantidades.split("|");
        /*for(var i=0; i<prod.length-1; i++){
            if(prod[i] != ""){
                for(var x=0; x<quant[i]; x++){
                    if($("#cnpjLiberacao_" + prod[i] + "_" + (x+1)).val() == ""){
                        mens = mens + "Favor Informar o CNPJ de Liberação " + (x+1) + "!<br/>";
                    } else {
                        $.ajax({
                            url: "concluir_pedidoFuncoes.php?tipo=validarCNPJ",
                            type: "post",
                            async: false,
                            data: "cnpj=" + $("#cnpjLiberacao_" + prod[i] + "_" + (x+1)).val(),
                            success: function(retorno){
                                if(retorno != ""){
                                    mens = mens + "CNPJ de Liberação " + (x+1) + " Inválido!<br/>";
                                }
                            }
                        });
                    }
                }
            }
        }
        */
        if($("#nome").val() == ""){
            mens = mens + "Favor Informar o seu Nome! <br/>";
        }
        if($("input:radio[name=tipo]").filter(":checked").val() == "juridica"){
            if($("#inscricaoCNPJ").val() == ""){
                mens = mens + "Favor Informar o CNPJ! <br/>";
            } else if($("#inscricaoCNPJ").val() != ""){
                $.ajax({
                    url: "concluir_pedidoFuncoes.php?tipo=validarCNPJ",
                    type: "post",
                    async: false,
                    data: "cnpj=" + $("#inscricaoCNPJ").val(),
                    success: function(retorno){
                        if(retorno != ""){
                            mens = mens + "Favor Informar um CNPJ Válido!<br/>";
                        }
                    }
                });
            }
        } else {
            if($("#inscricaoCPF").val() == ""){
                mens = mens + "Favor Informar o CPF! <br/>";
            } else if($("#inscricaoCPF").val() != ""){
                $.ajax({
                    url: "concluir_pedidoFuncoes.php?tipo=validarCPF",
                    type: "post",
                    async: false,
                    data: "cpf=" + $("#inscricaoCPF").val(),
                    success: function(retorno){
                        if(retorno != ""){
                            mens = mens + "Favor Informar um CPF Válido!<br/>";
                        }
                    }
                });
            }
        }
        if($("#endereco").val() == ""){
            mens = mens + "Favor Informar o seu Endereço! <br/>";
        }
        if($("#numero").val() == ""){
            mens = mens + "Favor Informar o Número do Endereço! <br/>";
        }
        if($("#bairro").val() == ""){
            mens = mens + "Favor Informar o seu Bairro! <br/>";
        }
        if($("#cidade").val() == ""){
            mens = mens + "Favor Informar a sua Cidade! <br/>";
        }
        if($("#cep").val() == ""){
            mens = mens + "Favor Informar o seu CEP! <br/>";
        }
        if($("#estado").val() == ""){
            mens = mens + "Favor Informar o seu Estado! <br/>";
        }
        if($("#fone").val() == ""){
            mens = mens + "Favor Informar o seu Telefone! <br/>";
        }
        if($("#email").val() == ""){
            mens = mens + "Favor Informar o seu E-mail! <br/>";
        } else {
            $.ajax({
                url: "concluir_pedidoFuncoes.php?tipo=validarEmail",
                type: "post",
                async: false,
                data: "email=" + $("#email").val(),
                success: function(retorno){
                    if(retorno != ""){
                        mens = mens + "Favor Informar um E-mail Válido!<br/>";
                    }
                }
            });
        }
        /*if($("#cnpjSoftwareHouse").val() != ""){
            $.ajax({
                url: "concluir_pedidoFuncoes.php?tipo=validarCNPJ",
                type: "post",
                async: false,
                data: "cnpj=" + $("#cnpjSoftwareHouse").val(),
                success: function(retorno){
                    if(retorno != ""){
                        mens = mens + "CNPJ da Software House Inválido!<br/>";
                    }
                }
            });
        }
        */
        if(mens == ""){
            $("#form_enviar").submit();
        } else {
            $("#msg").html(mens);
        }
    });
    /*
     *
     * INICIO
     * CONFIGURAÇÕES PARA IMAGEM
     *
     */
    $(".paging").show();
    $(".paging a:first").addClass("active");

    var imageWidth = $(".window").width();
    var imageSum = $(".image_reel img").size();
    var imageReelWidth = imageWidth * imageSum;

    $(".image_reel").css({
        'width' : imageReelWidth
    });

    rotate = function(){
        var triggerID = $active.attr("rel") - 1; //Get number of times to slide
        var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

        $(".paging a").removeClass('active'); //Remove all active class
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

        //Slider Animation
        $(".image_reel").animate({
            left: -image_reelPosition
        }, 500 );

    };

    rotateSwitch = function(){
        play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
            $active = $('.paging a.active').next();
            if ( $active.length === 0) { //If paging reaches the end...
                $active = $('.paging a:first'); //go back to first
            }
            rotate(); //Trigger the paging and slider function
        }, 5000); //Timer speed in milliseconds (3 seconds)
    };

    rotateSwitch(); //Run function on launch

    //On Hover
    $(".image_reel a").hover(function() {
        clearInterval(play); //Stop the rotation
    }, function() {
        rotateSwitch(); //Resume rotation
    });

    //On Click
    $(".paging a").click(function() {
        $active = $(this); //Activate the clicked paging
        //Reset Timer
        clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor
    });
    /*
     *
     * FIM
     * CONFIGURAÇÕES PARA IMAGEM
     *
     */

    $("#enviarFaleConosco").click (function(){
        var mens = "";
        if($("#nome").val() == ""){
            mens = mens + "Favor Informar o seu Nome!<br/>";
        }
        if($("#email").val() == ""){
            mens = mens + "Favor Informar o seu E-mail!<br/>";
        }
        if($("#mensagem").val() == ""){
            mens = mens + "Favor Informar a Mensagem!<br/>";
        }
        if(mens == ""){
            $("#retorno").ajaxStart(function(){
                $(this).html("<img src=\"admin/load.gif\" alt=\"Executando\" style=\"border: 0px;\" />");
            });
            $.post("indexFuncoes.php?tipo=enviar",{
                nome: $("#nome").val(),
                cidade: $("#cidade").val(),
                estado: $("#estado").val(),
                telefone: $("#telefone").val(),
                email: $("#email").val(),
                mensagem: $("#mensagem").val()
            },function(retorno){
                limparFaleConosco();
                $("#retorno").html(retorno);
            });
        } else {
            $("#retorno").html(mens);
        }
    });
});
function dep(departamento){
    location.href= "index.php?busca=" + $("#busca").val() + "&dep=" + departamento;
}
function atualizaQuant(carrinho,produto,quant){
    $("#msg").ajaxStart(function(){
        $(this).html("<img src=\"load.gif\" alt=\"Executando\" style=\"border: 0px;\" />");
    });
    $.ajax({
        type: "POST",
        url: "atualizaquant.php",
        data: "carrinho=" + carrinho +
        "&produto=" + produto +
        "&quant=" + quant,
        success: function(msg){
            $("#msg").html(msg);
            $("#boxTotalCarrinho").load("atualizaquant.php?tipo=getTotal&carrinho=" + carrinho);
        }
    });
}
function limparFaleConosco(){
    $("#nome").val("");
    $("#cidade").val("");
    $("#estado").val("");
    $("#telefone").val("");
    $("#email").val("");
    $("#mensagem").val("");
    $("#nome").focus();
}
