jQuery(document).ready(function(){
	jQuery.validator.messages.required = "";
	jQuery.validator.messages.email = "";
	jQuery("#thnk_contacto").validate({
		errorClass: "thnk_contacto_error",
		onkeyup: false,
		onblur: false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
				? 'Hay un campo mal llenado. Revisa el campo marcado.'
				: 'Hay ' + errors + ' campos mal llenados.  Revisa los campos marcados.';
				jQuery("div.thnk_contacto_error span").html(message);
				jQuery("div.thnk_contacto_error").show();
			} else {
				jQuery("div.thnk_contacto_error").hide();
			}
		},
		submitHandler: function() {
			jQuery("div.thnk_contacto_error").hide();
			
			var pathContacto = jQuery("#thnk_pathcontacto").val();
			var param = {
				accion : "enviar",
				nombre : jQuery("#nombre").val(),
				email : jQuery("#email").val(),
				telefono : jQuery("#telefono").val(),
				comentario : jQuery("#comentario").val(),
			};
			jQuery.ajax({
				type: "POST",
				url: pathContacto + "/thnk-contacto.php",
				data: param,
				success: function(resp){
					if(resp=="ok"){
						alert("Mensaje enviado correctamente");
						jQuery("#nombre").val('');
						jQuery("#email").val('');
						jQuery("#telefono").val('');
						jQuery("#comentario").val('');
					}else{
						alert("No fue posible enviar el formulario.\n Por favor escr’benos a directiva@sindicatoclarochile.cl");
					}
				},
				error: function(){
					alert("No fue posible enviar el formulario.\n Por favor escr’benos a directiva@sindicatoclarochile.cl");
				}
			});
		}
	});
	
	jQuery("#thnk_contacto_btn_enviar").click(function(){
		jQuery("#thnk_contacto").submit();
	});
});

