$(document).ready(function(){

    $forms = $('#form');

    $forms.bind('submit', function(){

 
        var $button = $('button',this).attr('disabled',true);

        var params = $(this.elements).serialize();

        var self = this;
        $.ajax({

            // Usando metodo Post
            type: 'POST',

            // this.action pega o script para onde vai ser enviado os dados
             url: this.action,

            // os dados que pegamos com a função serialize()
            data: params,

            // Antes de enviar
            beforeSend: function(){
                // mostro a div loading
                $('#loading').show();

                // html(): equivalente ao innerHTML
                $('#loading').html("Carregando...");
            },
            success: function(txt){
                // Ativo o botão usando a função attr()
                $button.attr('disabled',false);

                // Escrevo a mensagem
                $('#loading').html(txt);

                // Limpo o formulário
                self.reset();
            },

            // Se acontecer algum erro é executada essa função
            error: function(txt){
                $('#loading').html(txt);
            }
        })
        return false;
    });
});
