/*
 * Indique este post
 */
$(document).ready(function(){
    $("form[name='formIndique']").submit(function(){
        var thisForm = $(this);
        $.ajax({
            type: "post",
            data: thisForm.serialize(),
            url: "./indiquePost.php",
            success: function (result) {
                if (result == 'OK') {
                    thisForm.clearForm();
                    alert('E-mail enviado com sucesso.');
                    thisForm.toggle().prev().find("li.ativo").removeClass().addClass("indique");
                }
                else {
                    alert(result);
                }
            },
            error: function () {
                alert('Erro durante o envio.');
            }
        });
        return false;
    });
});

function indiquePost (elem) {
    if ($(elem).attr('class') != 'ativo') {
        $(elem).removeClass().addClass("ativo").parent().parent().find('form').toggle();
    }
    else {
        $(elem).removeClass().addClass("indique").parent().parent().find('form').toggle();
    }
}

$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form') {
            return $(':input',this).clearForm();
        }
        if (type == 'text' || type == 'password' || tag == 'textarea') {
            this.value = '';
        }
        else if (type == 'checkbox' || type == 'radio') {
            this.checked = false;
        }
        else if (tag == 'select') {
            this.selectedIndex = -1;
        }
    });
};
// END Indique este post
