//##################
// Validation Script
//##################
$(document).ready(function() {
	// validate signup form on keyup and submit
	var validator = $("#frmContato").validate({
			onkeyup: false,
			onfocusout: false,
			onfocus: false,
			
			rules: {
				nomForm: { required: true },
				emlForm: { required: true, email: true },
				msgForm: { required: true }
			},
			
			//Error messages
			messages: {
				nomForm: { required: "Informar um nome" },
				emlForm: { required: "Informar um email válido" },
				msgForm: { required: "Informar uma mensagem" }
			},
			
			//Where the errors are going to be placed
			//in the html
			errorPlacement: function(error, element) {},
			
			highlight: function(element, errorClass) {
				var aErrorMessage = validator.customMessage(element.name, "required");

				$(element).attr("title", aErrorMessage);
				setTooltip(element);
				
				$(element).addClass(errorClass);
			},
			
			unhighlight: function(element, errorClass) {
				$(element).attr("title", "");
				setTooltip(element);
				
				$(element).removeClass(errorClass);
			},

			
			//Before submiting, check the non-standard rules
			submitHandler: function(form) {
				//Pega as informações
				var aVars = new Object();
				
				if ($("#codCliente").length > 0) aVars.codCliente = $("#codCliente").val();
				if ($("#nomCliente").length > 0) aVars.nomCliente = $("#nomCliente").val();
				if ($("#emlCliente").length > 0) aVars.emlCliente = $("#emlCliente").val();
				
				aVars.nomForm = $("#nomForm").val();
				aVars.emlForm = $("#emlForm").val();
				aVars.assForm = $("#assForm").val();
				aVars.msgForm = $("#msgForm").val();
				
				//Faz POST via Ajax
				showQuickLoading("#boxBtEnviarMensagem", "enviando mensagem", true);
				
				$.post(
					"./lib/frm.superachei.fale_conosco.php",
					aVars,
					function(data) {
						
						hideQuickLoading();
						
						switch (data["retorno"]) {
							case 0: alert("Ocorreu um erro ao tentar enviar sua mensagem!");
											break;
							
							case 1: window.location.href = "fale_conosco.php?a=1";
											break;
						}
					},
					"json"
				);
				
				return false;
			}
	});
	
	$("#frmCadastroImovel").bind("invalid-form.validate", function() {
		alert("Existem campos inválidos no formulário!");
	});
});
//##################
//##################
