$(document).ready(function() {
	$('#prueba-popup').click(function (e) {
		popup();
	});
	$('.submitAssociateForm').click(function (e) {
		init(e);
	});
		
	function init(e) {
		e.preventDefault();
		e.stopPropagation();
		cleanData();
		sendAjax();
	}
	});

function popup() {		
	$('<div id="velo"></div>').prependTo('body');
	var velo = $('div#velo');
	var modal = $('div#modal');
	
	$(velo).fadeIn();
	$(modal).fadeIn();
		
	$(velo).click(function() {
		$('div#modal').fadeOut(function() {
			$(velo).fadeOut();
		});
	});
	
	$('div#modal a.cerrar').click(function() {
		$('div#modal').fadeOut(function() {
			$(velo).fadeOut();
		});
	});
	cleanData();
}
	
	function cleanData() {
		$("#error_name").html('');
		$("#error_phone").html('');
		$("#error_mail").html('');
		$("#formulario-ok").css("display", "none");
		$(".formularioClass").css("display", "block");
	}
	
	function sendAjax() {
		var clase = ".submitAssociateForm";
		var form = $('.submitAssociateForm');
		var name = $(".formName").val();
		var phone = $(".formPhone").val();
		var email = $(".formEmail").val();
		$.post("/fundacionrecover/es/participa/asociate.html", {formName: name, formPhone: phone, formEmail: email}, function (data) {
			var errorName = $(data).find("errorname").text();
			var errorPhone = $(data).find("errorphone").text();
			var errorMail = $(data).find("erroremail").text();
			if (errorName != "") {
				$("#error_name").html(errorName);
				var error = true;
			}
			if (errorPhone != "") {
				$("#error_phone").html(errorPhone);
				var error = true;
			}
			if (errorMail != "") {
				$("#error_mail").html(errorMail);
				var error = true;
			}
			if (!error) {
				$("#formulario-ok").css("display", "block");
				$(".formularioClass").css("display", "none");
			}
			form.attr("action", "#");
		});
	}
