// prepare the form when the DOM is ready
$(document).ready(function() {
    var options = {
        beforeSubmit:  showRequest,  // pre-submit callback
        success:       showResponse  // post-submit callback
    };

    // bind to the form's submit event
    $('#formcontact').submit(function() {
        // inside event callbacks 'this' is the DOM element so we first
        // wrap it in a jQuery object and then invoke ajaxSubmit
        $(this).ajaxSubmit(options);

        // !!! Important !!!
        // always return false to prevent standard browser submit and page navigation
        return false;
    });
});

// pre-submit callback
function showRequest(formData, jqForm, options) {
	// Validacao!
	var form = jqForm[0]; 
    if (!form.message.value || !form.email.value || !form.name.value) { 
        alert('Please enter a value for both Name, E-mail and Message'); 
        return false; 
    }
	
    $("#wrapBox").hide("fast");
    $("#loader").show("fast");
    return true;
}

// post-submit callback
function showResponse(responseText, statusText)  {
	$("#name").val("");
	$("#email").val("");
	$("#subject").val("");
	$("#message").val("");
    alert(responseText);
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
    //    '\n\nThe output div should have already been updated with the responseText.');
    $("#wrapBox").show("fast");
    $("#loader").hide("fast");
}
