// Javascript document
$(function(){
	$('#txtTelefone').mask('(99)9999-9999');
	
	$("#contact-form").validationEngine('attach', {
		autoHidePrompt: true,
		autoHideDelay: 4000,
		onValidationComplete: function(form, status) {
			if (status == true) {
				$loading = $('#loading');
				$loading.modal();
				
				$.ajax({
					type: 'post',
					url: 'processaForms/formmail.php',
					async: false,
					data: $('#contact-form').serialize(),
					success: function(data){
						$loading
							.find('img')
							.remove()
							.end()
							.find('h3')
							.html(data)
							.after('<a href="javascript:;" onclick="closeModal();" class="close">Fechar</a>');
					},
					error: function(data){
						$loading.find('h3').html(data);
						$.modal.close();
					}
				});
			}
		}
	});
});

function closeModal() {
	$.modal.close();
	$('input:text, textarea').val('');
}

function beforeCall(form, options){
	$('#loading').modal({
		escClose:false
	});
    return true;
}

// Called once the server replies to the ajax form validation request
function ajaxValidationCallback(status, form, json, options){
	console.log(status);
	console.log(form);
	console.log(json);
	console.log(options);
	
    if (status === true) {
    	$.ajax({
			type: 'post',
			url: 'processaForms/formmail.php',
			async: false,
			data: $('#contact-form').serialize(),
			success: function(data){
				$loading
					.find('img')
					.remove()
					.end()
					.find('h3')
					.html(data)
					.after('<a href="javascript:;" onclick="closeModal();" class="close">Fechar</a>');
			},
			error: function(data){
				$loading.find('h3').html(data);
				$.modal.close();
			}
		});
    } else {
    	
    }
}
