/**
 * Cria a testeira
 */
function CriaTesteira(Elementos, Div){
    //CRIA OS ELEMENTOS DAS IMAGENS
    for(var i = 0; i < Elementos.length; i++)
        $("." + Div).append("<div><a href='" + Elementos[i][1] + "' target='" + Elementos[i][2] + "'><img alt='' border='0' src='img/testeira/" + Elementos[i][0] + "' /></a></div>");

    //CRIA A TESTEIRA
    $('.' + Div).before('<div id="nav_' + Div + '">').cycle({
        fx:     'fade',
        pager:  '#nav_' + Div
    });
}

/**
 * Cria o link dos favoritos
 */
function CreateBookmarkLink()
{
    var url      = "http://www.nexcamp.com.br";
    var title    = "Nexcamp - Soluções em Telefonia";
    if (window.sidebar)
    {
        window.sidebar.addPanel(title, url,"");
    }
    else if(window.opera && window.print)
    {
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all)
    {
        window.external.AddFavorite(url, title);
    }
    else
        {
            alert("Para adicionar nos favoritos:\nClique em na estrela da sua barra de URL.")
        }
}

/**
 * Adiciona ao carrinho de compras
 */
function AddCarrinho(CodProduto)
{
    if($("#AdicionadoCarrinho").attr("rel") == "S")
    {
        alert("Entre no seu carrinho faça o login\ne altere a quantidade desse produto .");
        return false;
    }

    //AJAX
    $.ajax(
    {
        type: "POST",
        datatype: "JSON",
        cache: false,
        url: "ajax/carrinho.php",
        data: "CodProduto=" + CodProduto,
        error: function(erro)
        {
            alert("Erro ao adicionar no carrinho.\nPor favor, tente novamente.");
            return false;
        },
        success: function(Dados)
        {
            //TENTA ALOCAR O PARSE DO JSON
            try
            {
                var Result = $.parseJSON(Dados);
            }
            catch(err)
            {
                alert("Erro ao adicionar no carrinho.\nPor favor, tente novamente.");
                return false;
            }

            //VERIFICA O RETORNO
            if(Result != null)
            {
                //ANIMACAO
                $('.InsereCarrinho').show();
                var widthCarrinho = $("#cart_block").css("width");
                var heightCarrinho = $("#cart_block").css("height");

                var posLeft = $("#cart_block").offset().left;
                var posTop = $("#cart_block").offset().top

                var posLeftCar = $(".InsereCarrinho").offset().left;
                var posLeftFinal = posLeft - posLeftCar;

                var posTopCar = $(".InsereCarrinho").offset().top;
                var posTopDif = posTopCar - posTop;

                $('.InsereCarrinho').animate({width:widthCarrinho, height:heightCarrinho, marginTop:((-posTopDif) + 7), marginLeft:(posLeftFinal + 97)}, 500, function(){
                    $('.InsereCarrinho').animate({opacity:0}, 500, function(){
                        $('.InsereCarrinho').hide();

                        //ANIMA CARRINHO
                        $('#cart_block_products').animate({height:0}, 500, function(){
                            $('#cart_block_products').html("&nbsp;");
                            var AlturaCarrinho = Result.Linhas * 15;
                            $('#cart_block_products').animate({height:AlturaCarrinho}, 500, function(){
                                $('#cart_block_products').html(Result.Produtos);
                                $('#cart_block_total').html(Result.ValorTotal);
                                $('.ajax_cart_no_product').html((Result.ValorTotal == "R$ 0,00")?"(vazio)":Result.ValorTotal);

                                $("#ComprarBotao").attr("src", "img/comprar_disabled.png");
                                $("#AdicionadoCarrinho").attr("rel", "S");
                            });
                        });
                    });
                });

                return false;
            }
            else
            {
                alert("Erro ao adicionar no carrinho.\nPor favor, tente novamente.");
                return false;
            }
        }
    });
}

/**
 * Remove o aparelho do carrinho
 */
function RemoveProduto(CodAparelho)
{
    //AJAX
    $.ajax(
    {
        type: "POST",
        datatype: "JSON",
        cache: false,
        url: "ajax/removecarrinho.php",
        data: "CodProduto=" + CodAparelho,
        error: function(erro)
        {
            alert("Erro ao remover do carrinho.\nPor favor, tente novamente.");
            return false;
        },
        success: function(Dados)
        {
            //TENTA ALOCAR O PARSE DO JSON
            try
            {
                var Result = $.parseJSON(Dados);
            }
            catch(err)
            {
                alert("Erro ao remover do carrinho.\nPor favor, tente novamente.");
                return false;
            }

            //VERIFICA O RETORNO
            if(Result != null)
            {
                //ANIMA CARRINHO
                $('.InsereCarrinho').css("width", "1px");
                $('.InsereCarrinho').css("height", "1px");
                $('.InsereCarrinho').css("margin-left", "");
                $('.InsereCarrinho').css("margin-top", "");
                $('.InsereCarrinho').css("opacity", "1");
                $('.InsereCarrinho').hide();

                $('#cart_block_products').animate({height:0}, 500, function(){
                    $('#cart_block_products').html("&nbsp;");
                    var AlturaCarrinho = Result.Linhas * 15;
                    $('#cart_block_products').animate({height:AlturaCarrinho}, 500, function(){
                        $('#cart_block_products').html(Result.Produtos);
                        $('#cart_block_total').html(Result.ValorTotal);
                        $('.ajax_cart_no_product').html((Result.ValorTotal == "R$ 0,00")?"(vazio)":Result.ValorTotal);

                        if(CodAparelho == $("#CodAparelho").val())
                        {
                            $("#AdicionadoCarrinho").attr("rel", "N");
                            $("#ComprarBotao").attr("src", "img/comprar.png");
                        }
                    });
                });
            }
            else
            {
                alert("Erro ao remover do carrinho.\nPor favor, tente novamente.");
                return false;
            }
        }
    });
}

/**
 * Apaga do carrinho
 */
function ApagaCarrinho(CodAparelho)
{
    //AJAX
    $.ajax(
    {
        type: "POST",
        datatype: "JSON",
        cache: false,
        url: "ajax/removecarrinho.php",
        data: "CodProduto=" + CodAparelho,
        error: function(erro)
        {
            alert("Erro ao remover do carrinho.\nPor favor, tente novamente.");
            return false;
        },
        success: function(Dados)
        {
            //TENTA ALOCAR O PARSE DO JSON
            try
            {
                var Result = $.parseJSON(Dados);
            }
            catch(err)
            {
                alert("Erro ao remover do carrinho.\nPor favor, tente novamente.");
                return false;
            }

            //VERIFICA O RETORNO
            if(Result != null)
            {
                //ANIMA CARRINHO
                $('.InsereCarrinho').css("width", "1px");
                $('.InsereCarrinho').css("height", "1px");
                $('.InsereCarrinho').css("margin-left", "");
                $('.InsereCarrinho').css("margin-top", "");
                $('.InsereCarrinho').css("opacity", "1");
                $('.InsereCarrinho').hide();

                $('#cart_block_products').animate({height:0}, 500, function(){
                    $('#cart_block_products').html("&nbsp;");
                    var AlturaCarrinho = Result.Linhas * 15;
                    $('#cart_block_products').animate({height:AlturaCarrinho}, 500, function(){
                        $('#cart_block_products').html(Result.Produtos);
                        $('#cart_block_total').html(Result.ValorTotal);
                        $('.ajax_cart_no_product').html((Result.ValorTotal == "R$ 0,00")?"(vazio)":Result.ValorTotal);

                        window.location.reload();
                    });
                });

                /*/ANIMA PAGINA CARRINHO
                $('#Linha_' + CodAparelho).hide("slow");
                $('#TotalCarrinhoPagina').html(Result.ValorTotal);
                $('#LinhaTotalProdutos').html((Result.Linhas > 0 && Result.ValorTotal != "R$ 0,00")?Result.Linhas:"0");

                //REMOVE DO INPUT
                $('#ProdutoCarrinho_1_' + CodAparelho).remove();
                $('#ProdutoCarrinho_2_' + CodAparelho).remove();
                $('#ProdutoCarrinho_3_' + CodAparelho).remove();
                $('#ProdutoCarrinho_4_' + CodAparelho).remove();

                if(Result.ValorTotal == "R$ 0,00")
                {
                    $("#LinhaFinalizar").html("&nbsp;");
                    $("#LinhaTotal").hide();
                }*/
            }
            else
            {
                alert("Erro ao remover do carrinho.\nPor favor, tente novamente.");
                return false;
            }
        }
    });
}

/**
 * Posta para a pagina de esqueci a senha
 */
function EsqueciSenha()
{
    document.getElementById("formJaSouCadastrado").action = "esquecisenha.php";
    document.getElementById("formJaSouCadastrado").submit();
    return false;
}

/**
 * Muda o tipo de pessoa
 */
function MudaTipoPessoa(Tipo)
{
    document.getElementById("TipoPessoa1").style.display = "";
    if(Tipo == "J")
        document.getElementById("TipoPessoa1").innerHTML = "CNPJ";
    else
        document.getElementById("TipoPessoa1").innerHTML = "CPF";
}

/**
 * Muda a quantidade do carrinho
 */
function MudaQuantidade(Quantidade, ID)
{
    //AJAX
    $.ajax(
    {
        type: "POST",
        cache: false,
        url: "ajax/mudaquantidade.php",
        data: "QTDE=" + Quantidade + "&ID=" + ID
    });
}

function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function leech(v){
    v=v.replace(/o/gi,"0")
    v=v.replace(/i/gi,"1")
    v=v.replace(/z/gi,"2")
    v=v.replace(/e/gi,"3")
    v=v.replace(/a/gi,"4")
    v=v.replace(/s/gi,"5")
    v=v.replace(/t/gi,"7")
    return v
}

function soNumeros(v){
    return v.replace(/\D/g,"")
}
