jQuery().ready(function() {	
	
	jQuery.validator.addMethod("validateCodigoPostal", function(value) {
		spe_pais_geo = document.getElementById('InscripcionSpePaisGeo').value;
		if(spe_pais_geo == 'ESP') {
			regexp =/^(0[1-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-2])(\d{3})\b/
			return (regexp.test(value));
		}else if(spe_pais_geo == 'MEX') {
			var regexp =/^(0[1-9]|1[0-6]|[2-9][0-9])(\d{3})\b/
                        return (regexp.test(value));
		}else {
			regexp =/[0-9a-z-A-Z]{4}/
			return (regexp.test(value));
		}
	}, "<label class='error'>El Código Postal no es válido</label>");
	
	jQuery.validator.addMethod("validateAficiones", function(value) {
		cantidad_aficiones = document.getElementById('cantidad_aficiones').value;
		for (i=1; i <= cantidad_aficiones; i++) {
			if (document.getElementById('id_aficion_' + i).checked) {
				document.getElementById('error_aficion').style.display = 'none';
				return true;
			}
		}
		document.getElementById('error_aficion').style.display = 'block';
		return false;
		
	}, "");
	
	jQuery("#form_suscriptor_alta").validate({
		rules: {
			"data[Inscripcion][email]": {
				required: true,
				email: true
			},
			"data[Inscripcion][fecha_nacimiento]": {
				required: true
			},
			"data[Inscripcion][codigo_postal]": {
				required: true,
				validateCodigoPostal: true
			},
			"data[Inscripcion][rubro]": {
				required: true
			},
			"data[Inscripcion][cantidad_aficiones]": {
				validateAficiones: true
			}
		},
		messages: {
			"data[Inscripcion][email]": {
				required: "<br /><label class='error'>Ingrese una Dirección de Email</label>",
				email: "<br /><label class='error'>La Dirección de Email no es válida</label>"
			},
			"data[Inscripcion][fecha_nacimiento]": {
				required: "<br /><label class='error'>Seleccione su Fecha de Nacimiento</label>"
			},
			"data[Inscripcion][codigo_postal]": {
				required: "<br /><label class='error'>Ingrese un Código Postal</label>"
			},
			"data[Inscripcion][rubro]": {
				required: "<br /><label class='error'>Seleccione una Ocupación</label>"
			},
			"data[Inscripcion][cantidad_aficiones]": {
				validateAficiones: "<br /><label class='error'>Seleccione una Aficción</label>"
			}
		}

	} );
} );

