$().ready(function() {

	$("#Micuenta, #Micuenta1, #Micuenta2, #Micuenta3").blur(function() {
		$("#Micuenta").valid();
		$("#Micuenta1").valid();
		$("#Micuenta2").valid();
		$("#Micuenta3").valid();
	});

	// this one requires the text "buga", we define a default message, too
	$.validator.addMethod("nifcif", function(value) {
		return (isValidCif(value) || isValidNif(value));
	}, 'Please enter valid NIF/CIF!');

	$.validator.addMethod("validarccc", function(value) {
		var c1 = "";
		var c2 = "";
		var c3 = "";
		var c3_aux = "";
		var c4 = "";
		var chk = document.getElementById("Pago1").checked;

		c1 = document.getElementById("Micuenta").value;
		c2 = document.getElementById("Micuenta1").value;
		c3 = document.getElementById("Micuenta2").value;
		c4 = document.getElementById("Micuenta3").value;	
		c3_aux = obtenerDigito("00" + c1 + c2);
		c3_aux = c3_aux + "" + obtenerDigito(c4);

		return (!chk || (c3 == c3_aux));						
	}, '');		
	
	var validatorInscrip = $("#FormInscrip").validate({
	  	rules: {
			Email3: {
				equalTo: "#Email2"
			},
			Email2: {
				email: true,
				required: true
			},
			Pago: "required",
			Nif: "nifcif",
			Micuenta: "validarccc",
			Micuenta1: "validarccc",
			Micuenta2: "validarccc",
			Micuenta3: "validarccc"
			/*Nif: function(value, element) {
			if(/^([0-9]{8})*[a-zA-Z]+$/.test(value)){
			var numero = value.substr(0,value.length-1);
			var let = value.substr(value.length-1,1);
			numero = numero % 23;
			var letra='TRWAGMYFPDXBNJZSQVHLCKET';
			letra=letra.substring(numero,numero+1);
			if (letra==let)
			return true;
			return false;
			}
			return this.optional(element);
			}*/

		},
		messages: {
			First: "Indica tu nombre",
			Last: "Indica tu apellido",
			Nif: "Indica tu nif",
			Direccion2: "Indica tu direccion",
			Cpostal2: "Indica tu CP",
			Ciudad2: "Indica tu ciudad",
			Provincia2: "Indica tu provincia",
			Telefono: "Indica tu telefono/movil",
			Email2: "Indica un correo valido",
			Email3: "No coincide",
			Fin: "Indica tu fin de carrera"
		}
	}
	
	);

	// check if confirm password is still valid after password changed
	$("#Nif________________________________").blur(function() {
		if (!validar_nif_correcto()) {
			document.getElementById("Nif").value = "";
			document.getElementById("Nif").focus();
		}
	});
				   
	// validate the comment form when it is submitted
	$("#commentForm").validate();
	
	// validate signup form on keyup and submit
	$("#signupForm").validate({
		rules: {
			firstname: "required",
			lastname: "required",
			username: {
				required: true,
				minlength: 2
			},
			password: {
				required: true,
				minlength: 5
			},
			confirm_password: {
				required: true,
				minlength: 5,
				equalTo: "#password"
			},
			email: {
				required: true,
				email: true
			},
			topic: {
				required: "#newsletter:checked",
				minlength: 2
			},
			agree: "required"
		},
		messages: {
			firstname: "Please enter your firstname",
			lastname: "Please enter your lastname",
			username: {
				required: "Please enter a username",
				minlength: "Your username must consist of at least 2 characters"
			},
			password: {
				required: "Please provide a password",
				minlength: "Your password must be at least 5 characters long"
			},
			confirm_password: {
				required: "Please provide a password",
				minlength: "Your password must be at least 5 characters long",
				equalTo: "Please enter the same password as above"
			},
			email: "Please enter a valid email address",
			agree: "Please accept our policy"
		}
	});
	
	// propose username by combining first- and lastname
	$("#username").focus(function() {
		var firstname = $("#firstname").val();
		var lastname = $("#lastname").val();
		if(firstname && lastname && !this.value) {
			this.value = firstname + "." + lastname;
		}
	});
	
	// check if confirm password is still valid after password changed
	$("#password").blur(function() {
		$("#confirm_password").valid();
	});
	
	//code to hide topic selection, disable for demo
	var newsletter = $("#newsletter");
	// newsletter topics are optional, hide at first
	var inital = newsletter.is(":checked");
	var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
	var topicInputs = topics.find("input").attr("disabled", !inital);
	// show when newsletter is checked
	newsletter.click(function() {
		topics[this.checked ? "removeClass" : "addClass"]("gray");
		topicInputs.attr("disabled", !this.checked);
	});
	
	
 $("#parent1").css("display","all");
 $(".aboveage1").click(function(){
    	if ($('input[name=age1]:checked').val() == "Contado: 1 cuota" ) {
        	$("#PagoTarjeta").slideDown("fast"); //Slide Down Effect
        } else {
            $("#PagoTarjeta").slideUp("fast");	//Slide Up Effect
        }
     });
		
		

	
});
