/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function(){
    $("#carrinhoForm").submit(function(){
        //alert($(this).serialize())
        $.ajax({
            url: '../carrinhocompra/carrinho.php',
            data: $(this).serialize()+"&req=ajax",
            type: "POST",
            success: function(rtn){
                if(!isNaN(rtn)){
                    $("#prodCarrinho").show();
                    $("#qntProdCarrinho").html(rtn);
                    alert("Produto adicionado no carrinho com sucesso.");
                } else {
                    alert(rtn)
                }
            }
        });

        return false;
    })

    $(".btn_excluir").click(function(){
        var patt1=new RegExp("[0-9].*");
        var result = patt1.exec($(this).attr("id"));
        if(confirm('Deseja realmente retirar este produto do carrinho ?')){
            $.ajax({
                url: '../carrinhocompra/carrinho.php',
                data: 'indexId='+result+'&acao=2&req=ajax',
                type: 'POST',
                success: function(rtn){
                    if(rtn != 0){
                        $("#linha_"+result).remove();
                    } else {
                        $("#linha_"+result).remove();
                        $("#linha_topo").remove();
                        $("#carrinho").html("Não há produtos no carrinho");
                        $("#enviar").remove();
                    }
                }
            });
        }
    });
    /*
    */
})

