/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function nuevoAjax(){ 
	/* Crea el objeto AJAX. */
	var xmlhttp=false; 
	try { 
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e){ 
		try{ 
			// Creacion del objet AJAX para IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); } 

	return xmlhttp; 
} 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*	FUNCIONES PARA CREAR Y GUARDAR DATOS DE UN CAMPO SELECT (COMBO)		*/
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function CambiaComboTipos(){
	// Funcion encargada de cambiar el texto comun de la fila por un campo select que conserve
	// el valor que tenia ese campo 
	
	valor_tipo=document.getElementById("selTipo").options[document.getElementById("selTipo").selectedIndex].value;
	valor_material=document.getElementById("selTipoMaterial").options[document.getElementById("selTipoMaterial").selectedIndex].value;
	valor_nivel=document.getElementById("selNivel").options[document.getElementById("selNivel").selectedIndex].value;
	
	var filaNiv=document.getElementById("fila_niveles");
	var filaMat=document.getElementById("fila_materiales");
				
	var codigo='';
	var ajaxA=nuevoAjax();
		
	var sURL="ajax/genera_combo_niveles.asp?nombreselect=selNivel&tipo="+valor_tipo+"&material="+valor_material;
			
	ajaxA.open("GET", sURL, true);
	ajaxA.onreadystatechange=function(){
	//alert(ajax.readyState);  
		
		if (ajaxA.readyState==1){
			// Mientras carga, pongo una opción que diga "Espere..."
			filaNiv.innerHTML="<span style='font-size: 0.8em; font-color: #FFFFFF;'><b>Espere...</b></span>";
		}
		if (ajaxA.readyState==4){ 
			codigo = ajaxA.responseText;
			
				
			filaNiv.innerHTML = ajaxA.responseText;	
			for(iCont=0;iCont<document.getElementById("selNivel").length;iCont++){
				if(document.getElementById("selNivel").options[iCont].value==valor_nivel){
					document.getElementById("selNivel").options[iCont].selected=true;
				}
			}
		
			
	
			var codigo='';
			var ajaxB=nuevoAjax();
			
			var sURL="ajax/genera_combo_materiales.asp?nombreselect=selTipoMaterial&tipo="+valor_tipo+"&nivel="+valor_nivel;
					
			ajaxB.open("GET", sURL, true);
			ajaxB.onreadystatechange=function(){
			//alert(ajax.readyState);  
			
			if (ajaxB.readyState==1){
					// Mientras carga, pongo una opción que diga "Espere..."
					filaMat.innerHTML="<span style='font-size: 0.8em; font-color: #FFFFFF;'><b>Espere...</b></span>";
				}
				if (ajaxB.readyState==4){ 
					codigo = ajaxB.responseText;					
					filaMat.innerHTML = ajaxB.responseText;	
					for(iCont=0;iCont<document.getElementById("selTipoMaterial").length;iCont++){
						if(document.getElementById("selTipoMaterial").options[iCont].value==valor_material){
							document.getElementById("selTipoMaterial").options[iCont].selected=true;
						}
					}
				}
			}
			
			ajaxB.send(null);	
				
					
			//document.getElementById(elemento).focus();
		}
	}
		
	ajaxA.send(null);	
		
		
	//fila.innerHTML="ajax/genera_combo.asp?nombreselect="+elemento+"&sid="+sid
	
}

function CambiaComboNiveles(){
	// Funcion encargada de cambiar el texto comun de la fila por un campo select que conserve
	// el valor que tenia ese campo 
	
	//valor_tipo=document.getElementById("selTipo").options[document.getElementById("selTipo").selectedIndex].value;
	valor_material=document.getElementById("selTipoMaterial").options[document.getElementById("selTipoMaterial").selectedIndex].value;
	valor_nivel=document.getElementById("selNivel").options[document.getElementById("selNivel").selectedIndex].value;
		
	//var filaTip=document.getElementById("fila_tipos");
	var filaMat=document.getElementById("fila_materiales");
				
	var codigo='';
	var ajaxB=nuevoAjax();
			
	var sURL="ajax/genera_combo_materiales.asp?nombreselect=selTipoMaterial&tipo="+valor_tipo+"&nivel="+valor_nivel;
				
	ajaxB.open("GET", sURL, true);
	ajaxB.onreadystatechange=function(){
			
		if (ajaxB.readyState==1){
			// Mientras carga, pongo una opción que diga "Espere..."
			filaMat.innerHTML="<span style='font-size: 0.8em; font-color: #FFFFFF;'><b>Espere...</b></span>";
		}
		if (ajaxB.readyState==4){ 
			codigo = ajaxB.responseText;					
			filaMat.innerHTML = ajaxB.responseText;	
			for(iCont=0;iCont<document.getElementById("selTipoMaterial").length;iCont++){
				if(document.getElementById("selTipoMaterial").options[iCont].value==valor_material){
					document.getElementById("selTipoMaterial").options[iCont].selected=true;
				}
			}
		}
	}
			
	ajaxB.send(null);	
	
}


function CambiaComboMateriales(){
	// Funcion encargada de cambiar el texto comun de la fila por un campo select que conserve
	// el valor que tenia ese campo 
	
/*	valor_tipo=document.getElementById("selTipo").options[document.getElementById("selTipo").selectedIndex].value;
	valor_material=document.getElementById("selTipoMaterial").options[document.getElementById("selTipoMaterial").selectedIndex].value;
	valor_nivel=document.getElementById("selNivel").options[document.getElementById("selNivel").selectedIndex].value;
		
	var filaTip=document.getElementById("fila_tipos");
	var filaNiv=document.getElementById("fila_niveles");
				
	var codigo='';
	var ajaxA=nuevoAjax();
		
	var sURL="ajax/genera_combo_tipos.asp?nombreselect=selTipo&nivel="+valor_nivel+"&material="+valor_material;
			
	ajaxA.open("GET", sURL, true);
	ajaxA.onreadystatechange=function(){
	//alert(ajax.readyState);  
		
		if (ajaxA.readyState==1){
			// Mientras carga, pongo una opción que diga "Espere..."
			filaTip.innerHTML="<span style='font-size: 0.8em; font-color: #FFFFFF;'><b>Espere...</b></span>";
		}
		if (ajaxA.readyState==4){ 
			codigo = ajaxA.responseText;
				
			filaTip.innerHTML = ajaxA.responseText;	
			for(iCont=0;iCont<document.getElementById("selTipo").length;iCont++){
				if(document.getElementById("selTipo").options[iCont].value==valor_tipo){
					document.getElementById("selTipo").options[iCont].selected=true;
				}
			}
			
	
			var codigo='';
			var ajaxB=nuevoAjax();
			
			var sURL="ajax/genera_combo_niveles.asp?nombreselect=selNivel&tipo="+valor_tipo+"&material="+valor_material;
				
			ajaxB.open("GET", sURL, true);
			ajaxB.onreadystatechange=function(){
			//alert(ajax.readyState);  
			
				if (ajaxB.readyState==1){
					// Mientras carga, pongo una opción que diga "Espere..."
					filaNiv.innerHTML="<span style='font-size: 0.8em; font-color: #FFFFFF;'><b>Espere...</b></span>";
				}
				if (ajaxB.readyState==4){ 
					codigo = ajaxB.responseText;					
					filaNiv.innerHTML = ajaxB.responseText;	
					for(iCont=0;iCont<document.getElementById("selNivel").length;iCont++){
						if(document.getElementById("selNivel").options[iCont].value==valor_nivel){
							document.getElementById("selNivel").options[iCont].selected=true;
						}
					}
				}
			}
			
			ajaxB.send(null);	
			
					
			//document.getElementById(elemento).focus();
		}
	}
		
	ajaxA.send(null);
	*/
}


function CaracteresHTML(sCadena){
	var sAux="á,é,í,ó,ú,Á,É,Í,Ó,Ú";
	var vCaracteres=sAux.split(',');
	
	sAux="&aacute;,&eacute;,&iacute;,&oacute;,&uacute;,&Aacute;,&Eacute;,&Iacute;,&Oacute;,&Uacute;";
	var vValoresHTML=sAux.split(',');
	
	var sAux2;
	sAux=sCadena;
	for(i=0; i<vCaracteres.length; i++){		
		while(sAux.indexOf(vCaracteres[i])>0){
			sAux2=sAux.replace(vCaracteres[i],vValoresHTML[i]);
			sAux=sAux2;
		}
	}
	return sAux;
}

function SustituyeComillas(sTexto){
	var sAux=sTexto;
	var sAux2;
		
	while(sAux.indexOf('"')>=0){
		sAux2=sAux.replace('"','#comilla_doble#');
		sAux=sAux2;
	}
		
	while(sAux.indexOf('\'')>=0){
		sAux2=sAux.replace('\'','#comilla_simple#');
		sAux=sAux2;
	}
	
	return sAux;
}


function RestituyeComillas(sTexto){
	var sAux=sTexto;
	var sAux2;
		
	while(sAux.indexOf('#comilla_doble#')>=0){
		sAux2=sAux.replace('#comilla_doble#','"');
		sAux=sAux2;
	}
		
	while(sAux.indexOf('#comilla_simple#')>=0){
		sAux2=sAux.replace('#comilla_simple#','\'');
		sAux=sAux2;
	}
	
	return sAux;
}

function limpia_formulario(){
	/*document.getElementById("selTipo").selectedIndex=0;
	document.getElementById("selNivel").selectedIndex=0;
	document.getElementById("selTipoMaterial").selectedIndex=0;
	CambiaComboTipos();
	   
	document.getElementById("txtTituloBuscar").value="";
	document.getElementById("txtAutorBuscar").value="";
	alert(document.getElementById("txtAutorBuscar").value);
	document.getElementById("txtISBNBuscar").value="";
	document.getElementById("txtPalClaveBuscar").value="";*/
	document.location.href="busqueda_avanzada.asp";
}

