function ocultarSiguientes(elemento){
	idItem = "#" + $j(elemento).attr("id");
	
	$j(idItem + " option").each(function(){
		idSiguiente = $j(this).attr("siguiente");
		$j("#bloque" + idSiguiente).hide();
		
		ocultarSiguientes($j("#pregunta" + idSiguiente));
		
	});
}

$j(document).ready(function(){
	$j("select.preguntas").change(function(){
		ocultarSiguientes(this);
		
		idItem = "#" + $j(this).attr("id");
		siguiente = $j(idItem + " option:selected").attr("siguiente");
		
		//alert(siguiente);
		if(siguiente != 0){
			$j("#bloque" + siguiente).show();
		}
		
		if (($j(idItem + " option:selected").attr("segmento")==0) || ($j(idItem + " option:selected").attr("segmento") == undefined)){
			segmento = "";
		}else{
			segmento = $j(idItem + " option:selected").attr("segmento");
		}
		$j("#segmento").val(segmento);
	});
});




var JSPage = Class.create();

JSPage.prototype = {
	initialize : function() {
		Object.extend(window, this);
		
		Event.observe("btnEnviar", "click", window.enviar);
		//Event.observe("particular", "click", window.particular);
		//Event.observe("profesional", "click", window.profesional);
	},
	
	profesional : function(){
		$("perfil").selectedIndex = 0;
		$("perfil").disabled = false;
	},
	
	particular : function(){
		$("perfil").selectedIndex = -1;
		$("perfil").disabled = true;
	},
	
	validar : function(){
		error = false;
		$$("input.required").each(function(item){
			if((item.value == "") && !(error)){
				alert("Los campos marcados con un (*) son obligatorios");
				item.focus();
				error = true;
			}
		});
		
		$$("input.nif").each(function(item){
			if((valida_nif_cif_nie(item.value) <= 0) && !(error)){
				alert("El NIF debe contener un valor correcto");
				item.focus();
				error = true;
			}
		});
		
		$$("select.required").each(function(item){
			if((item.value == "") && !(error)){
				alert("Los campos marcados con un (*) son obligatorios");
				item.focus();
				error = true;
			}
		});
		
		if(!error && (!$("generoHombre").checked) && (!$("generoMujer").checked)){
			alert("Los campos marcados con un (*) son obligatorios");
			$("generoHombre").focus();
			error = true;
		}
		
		if(!error){
			if(!$("checkPolitPriv").checked){
				alert("Debe aceptar la política de privacidad de datos");
				error = true;
			}
		}
		
		return !error;
	},
	
	enviar : function(){
		if(window.validar()){
			$("formContacta").submit();			
		}		
	}	
}

Event.observe(window,"load",function(){
	jsPage = new JSPage();
});


