$(function() {
	$('.error').hide();

	$("#buttonLimpiar").click(function() {
		$('.error').hide();
		$("input#nombre").focus();
	});

	$("#buttonSubmit").click(function() {
		$('.error').hide();
		
		var nombre = $("input#nombre").val();
		if (nombre == "") {
			$("label#nombre_error").show();
			$("input#nombre").focus();
			return false;
		}

		var apellidos = $("input#apellidos").val();
		if (apellidos == "") {
			$("label#apellidos_error").show();
			$("input#apellidos").focus();
			return false;
		}

		var telefono = $("input#telefono").val();
		if (telefono == "") {
			$("label#telefono_error").show();
			$("input#telefono").focus();
			return false;
		}

		var email = $("input#email").val();
		if (email == "") {
			$("label#email_error").show();
			$("input#email").focus();
			return false;
		}

		if (!validar_correo(email)) {
			$("label#email_error_validacion").show();
			$("input#email").focus();
			return false;
		}

		var password1 = $("input#password1").val();
		if (password1 == "") {
			$("label#password1_error").show();
			$("input#password1").focus();
			return false;
		}

		var password2 = $("input#password2").val();
		if (password2 == "") {
			$("label#password2_error").show();
			$("input#password2").focus();
			return false;
		}

		if (password1 != password2) {
			$("label#password1_error_distintas").show();
			$("input#password1").focus();
			return false;
		}

		$("#buttonSubmit").attr("disabled", "true");
		
		var dataString = 'email=' + email + '&telefono=' + telefono + '&nombre=' + nombre + '&apellidos=' + apellidos + '&password1=' + password1 + '&password2=' + password2;

		$.ajax( {
			type : "POST",
			url : "inscripcionUsuarios.registrar.php",
			data : dataString,   
			cache: false,   
			contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1",
	        dataType: "Text",
	        success : function(data) {
				mostrarAlert('Correcto',data);
				$("#buttonSubmit").removeAttr('disabled');
			},
			error : function() {
				mostrarAlert('Error','Se ha producido un error.');
				$("#buttonSubmit").removeAttr('disabled');
			}
		});
		return false;
	});
});
runOnLoad(function() {
	$("input#nombre").select().focus();
});
