function muestraCategorias(id)
{
	ocultarCapas("capaSelectMunicipios");
	ocultarCapas("capaSelectLocalidad");
	ocultarCapas("capaSelectTematicas");
	ocultarCapas("capaSelectSubTematicas");
	ocultarCapas("capaSelecTipo");
	ocultarCapas("capaSelectDestinoTuristico");
	ocultarCapas("capaSelectGeneral");
	ocultarCapas("capaBuscadorPalabras");
	ocultarCapas("capaBuscadorPalabrasExacta");
	ocultarCapas("capaSubCategorias");
	ocultarCapas("capaSubTipos");
	ocultarCapas("resultados_busqueda");
	ocultarCapas("resultados_ruta");
	
	
	if((id != null) && id != "")
	{
		id_obtenido = document.getElementById(id);
		if(id_obtenido != null && id_obtenido != '' && id_obtenido.value != null && id_obtenido.value != ''){
			mostrarCapas("cargandoForm");
			loadOptionsMunicipios('./palencia/include/opcionesCategoria.jsp?'+ id + "=" + escape(id_obtenido.value) ,generarOptions);
		}
	}
}

function ocultarCapas(idCapa)
{
	var obj = document.getElementById(idCapa);
	if(obj)
			obj.style.display='none';		
		

}

function mostrarCapas(idCapa)
{
	var obj = document.getElementById(idCapa);
	if(obj)
			obj.style.display='block';		
		

}





function  loadOptionsMunicipios(url,procesoReqCambio)
{    	
	// Puede que no se soporte nativamente el objeto XMLHttpRequest
    if(window.XMLHttpRequest)
    {
    	reqOptCat=new XMLHttpRequest();	
    }
    else if(window.ActiveXObject)
    {
    	reqOptCat= new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(reqOptCat)
    {
    	reqOptCat.onreadystatechange=procesoReqCambio;
    	reqOptCat.open("POST",url,true);
    	reqOptCat.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	reqOptCat.send("id=3");
    }
}

function generarOptions()
{
	
	if(reqOptCat.readyState==4)
    {
    	// Comprobamos si ha habido exito
    	//alert(reqOptMun.status);
    	if(reqOptCat.status==200)
    	{
    		ocultarCapas("cargandoForm");
    		arrayOpciones = eval(reqOptCat.responseText);
    		//ponemos todos los select a 0
    		var select1 = document.getElementById("selectMunicipios");
    		if(select1 != null){
    			select1.options.length=0;
    		}
    		var select2 = document.getElementById("selectTipo");
			if(select2 != null){
				select2.options.length=0;
			}
			var select3 = document.getElementById("selectDestinoTuristico");
			if(select3 != null){
				select3.options.length=0;
			}
			var select4 = document.getElementById("selectGeneral");
			if(select4 != null){
				select4.options.length=0;
			}
			var inputPalabras = document.getElementById("buscadorPalabras");
			if(inputPalabras){
				inputPalabras.value="";
			}
			var select5 = document.getElementById("selectTematicas");
    		if(select5 != null){
    			select5.options.length=0;
    		}
			
			if(document.getElementById("selectMunicipios") != null){
				document.getElementById("selectMunicipios").options.length=0;
			}
			if(document.getElementById("selectTipo") != null){
				document.getElementById("selectTipo").options.length=0;
			}
			if(document.getElementById("selectDestinoTuristico") != null){
				document.getElementById("selectDestinoTuristico").options.length=0;
			}
			if(document.getElementById("selectGeneral") != null){
				document.getElementById("selectGeneral").options.length=0;
			}
			if(document.getElementById("inputOculto") != null){
				document.getElementById("inputOculto").innerHTML = "";
			}
			if(document.getElementById("capaSubTipos") != null){
				document.getElementById("capaSubTipos").innerHTML = "";
			}
			if(document.getElementById("selectTematicas") != null){
				document.getElementById("selectTematicas").options.length=0;
			}
			if(document.getElementById("exacta") != null){
				if(document.getElementById("exacta").type == "checkbox"){
					document.getElementById("exacta").checked = false;
				} 
			}
			
			
    		if (arrayOpciones.length == 1){
    			/*Pintamos municipios*/
    			if(arrayOpciones[0].municipios != null && (arrayOpciones[0].municipios.length >0)){
	    			var select = document.getElementById("selectMunicipios");
	    			var selectLoc = document.getElementById("selectLocalidad"); 
	    			
	    			select.options.length = 0;
	    			selectLoc.options.length = 0;
	    			selectLoc.options[0]=new Option("","");
	    			municipios = arrayOpciones[0].municipios;
	    			for (i = 0; i < municipios.length; i++){
	    				select.options[i]=new Option(municipios[i].value,municipios[i].value);
	    				localidades[municipios[i].name] = new Array();
	    				locali = municipios[i].localidades;
	    				if(locali != null && locali.length > 0){
	    					for (j = 0; j < locali.length; j++){
	    						/*selectLoc.options[selectLoc.options.length]=new Option(locali[j].name,locali[j].name);*/
	    						localidades[municipios[i].name].push(locali[j].name);
				    		}
				        }
	    			}
	    			mostrarCapas("capaSelectMunicipios");
	    			mostrarCapas("capaSelectLocalidad");
    			}
    			
    			
    			
    			/*Pintamos subtipos*/
    			if(arrayOpciones[0].subcategorias != null && (arrayOpciones[0].subcategorias.length > 0)){
	    			var select = document.getElementById("selectTipo");
	    			select.options.length=0;
	    			subcategorias = arrayOpciones[0].subcategorias;
	    			var capaSubtipos = document.getElementById("capaSubTipos");
	    			if(capaSubtipos != null){
	    				capaSubtipos.innerHTML = "";
	    			}
	    			for (i = 0; i < subcategorias.length; i++){
	    				select.options[i]=new Option(subcategorias[i].name,subcategorias[i].value);
	    				if((capaSubtipos != null) && (subcategorias[i].canales.length > 0)){
	    					contenidoInner  = "";
				        	for (j = 0; j < subcategorias[i].canales.length; j++){
				    			contenidoInner += '<input onClick="desSeleccionarCheckbox(\''+subcategorias[i].value+'\', this.value);" class="classLabelCheckbox" type="checkbox" name="subtipo" value="' + subcategorias[i].canales[j].value + '"/><label for="subtipo"  class="classLabelCheckbox">' + subcategorias[i].canales[j].name + '</label><div class="pie_bloque"><!-- no borrar --></div>';
				    		}
				        	capaSubtipos.innerHTML += '<div  id="' +subcategorias[i].value + '" style="display:none;overflow: auto;height:100px;">'+ contenidoInner + '<input type="checkbox" name="subtipo" value=""  class="classLabelCheckbox" onClick="desSeleccionarCheckbox(\''+subcategorias[i].value+'\', this.value);"/><label for="subtipo" class="classLabelCheckbox">Todo</label><div class="pie_bloque"><!-- no borrar --></div></div>';
	    				}
	    			}
	    			mostrarCapas("capaSelecTipo");
    			}
    			/*Pintamos destinos turisticos*/
    			if(arrayOpciones[0].destinos_turisticos != null && (arrayOpciones[0].destinos_turisticos.length > 0)){
	    			var select = document.getElementById("selectDestinoTuristico");
	    			select.options.length=1;
	    			select.options[0]=new Option("Todo","");
	    			destinos_turisticos = arrayOpciones[0].destinos_turisticos;
	    			for (i = 0; i < destinos_turisticos.length; i++){
	    				select.options[i+1]=new Option(destinos_turisticos[i].name,destinos_turisticos[i].value);
	    			}
	    			mostrarCapas("capaSelectDestinoTuristico");
	    		
	    			
	    			
    			}
    			/*Pintamos select si la opcion es recurso*/
    			if(arrayOpciones[0].recursos != null && (arrayOpciones[0].recursos.length >0)){
	    			var select = document.getElementById("selectGeneral");
	    			select.options.length=0;
	    			recursos = arrayOpciones[0].recursos;
	    			for (i = 0; i < recursos.length; i++){
	    				select.options[i]=new Option(recursos[i].name,recursos[i].value);
	    			}
	    			mostrarCapas("capaSelectGeneral");
	    		}
    			if(arrayOpciones[0].buscadorPalabras != null && (arrayOpciones[0].buscadorPalabras.length >0)){
    				mostrarCapas("capaBuscadorPalabras");
    				if(arrayOpciones[0].buscadorPalabras[0].exacta == "activo"){
    					mostrarCapas("capaBuscadorPalabrasExacta");
    				}
    			}
    			
    			var inputOculto = document.getElementById("inputOculto");
    			if(inputOculto != null){
    				inputOculto.innerHTML = '<input type="hidden"  id="nombreParametro" name="nombreParametro" value="' + arrayOpciones[0].nombreParametro + '"/>';
    			}
    			
    			/*Pintamos tematicass*/
    			if(arrayOpciones[0].tematicas != null && (arrayOpciones[0].tematicas.length >0)){
	    			var select = document.getElementById("selectTematicas");
	    			var selectLoc = document.getElementById("selectSubTematicas"); 
	    			
	    			select.options.length = 0;
	    			selectLoc.options.length = 0;
	    			selectLoc.options[0]=new Option("","");
	    			temati = arrayOpciones[0].tematicas;
	    			for (i = 0; i < temati.length; i++){
	    				select.options[i]=new Option(temati[i].name,temati[i].value);
	    				subtematicas[temati[i].value] = new Array();
	    				subtemati = temati[i].subtematicas;
	    				if(subtemati != null && subtemati.length > 0){
	    					for (j = 0; j < subtemati.length; j++){
	    						/*selectLoc.options[selectLoc.options.length]=new Option(locali[j].name,locali[j].name);*/
	    						subtematicas[temati[i].value].push(subtemati[j].name);
				    		}
				        }
	    			}
	    			mostrarCapas("capaSelectTematicas");
	    			mostrarCapas("capaSelectSubTematicas");
    			}
    			
    		}
    	
    		
    	}
		else
    	{
    		mostrarError("Problema al generar los options:\n"+reqOptCat.statusText);
    	}
	}
}




function muestraSubTipo(id){
	if(document.getElementById("capaSubTipos")!= null){
		ocultarCapas("capaSubTipos");
		
		var elemArray = document.getElementById("capaSubTipos").childNodes; 
		
		for(z = 0; z < elemArray.length; z++){
			var elem = elemArray[z].id;
			if(elem != null){
				limpiarCheckboxs(elem);
				if(elem == id){
					mostrarCapas(elem);
				}else{
					ocultarCapas(elem);
				}
			}
		}
		if(id != ''){
			mostrarCapas(id);
			mostrarCapas("capaSubTipos");
		}
	
	}
}

function desSeleccionarCheckbox(id, idActual){
	if(document.getElementById(id) != null){
		var checkbox = document.getElementById(id).getElementsByTagName("input");
		for(l = 0; l < checkbox.length; l++){
			if(idActual == ''){
				if(checkbox[l].value != "")
					checkbox[l].checked = false;
			}else{
				if(checkbox[l].value == "")
					checkbox[l].checked = false;
			}
		}
	}
}

function limpiarCheckboxs(id){
	if(document.getElementById(id) != null){
		var checkbox = document.getElementById(id).getElementsByTagName("input");
		for(l = 0; l < checkbox.length; l++){
			checkbox[l].checked = false;
		}
	}
}

function limpiarOptionRecursos(){
	var idSelectGeneral = document.getElementById("selectGeneral");
	if(idSelectGeneral != null){
		if(idSelectGeneral.options[0].selected){
			for (i=1;i<idSelectGeneral.options.length;i++){
				  idSelectGeneral.options[i].selected = false;
			}
		}
	}
}

function mostrar_localidades(id){
	var select_localidades = document.getElementById("selectLocalidad");
	if((select_localidades != null)  && (localidades != null) && (id != "")){
		select_localidades.options.length = 0;
		var i = 1;
		select_localidades.options[0] = new Option("","");
		if(localidades[replaceSpecialChars(id)] != null){
			for(z = 0 ; z <localidades[id].length; z++){
				select_localidades.options[i] = new Option(localidades[id][z],localidades[id][z]);
				i++;
			}
		}
	}else if(select_localidades != null){
		select_localidades.options.length = 0;
	}
}

function mostrar_tematicas(id){
	var select_tematicas = document.getElementById("selectSubTematicas");
	if((select_tematicas != null)  && (subtematicas != null) && (id != "")){
		select_tematicas.options.length = 0;
		var i = 1;
		select_tematicas.options[0] = new Option("","");
		if(subtematicas[replaceSpecialChars(id)] != null){
			for(z = 0 ; z <subtematicas[id].length; z++){
				select_tematicas.options[i] = new Option(subtematicas[id][z],subtematicas[id][z]);
				i++;
			}
		}
	}else if(select_tematicas != null){
		select_tematicas.options.length = 0;
	}
}

function replaceSpecialChars(str) {
	var specialCharsLet = new Array ("á","é","í","ó","ú","Á","É","Í","Ó","Ú","ü","Ü");
	var specialCharsVal = new Array("&aacute;","&eacute;","&iacute;","&oacute;","&uacute;","&Aacute;","&Eacute;","&Iacute;","&Oacute;","&Uacute;","&uuml;","&Uuml;");
	
	var regex;
	var returnString = str;
	for (var i = 0; i < specialCharsLet.length; i++) {
		regex = new RegExp("["+specialCharsLet[i]+"]", "g");
		returnString = returnString.replace(regex, specialCharsVal[i]);
		regex = null;
	}
	return returnString;
}
