function nuevoAjax(){ 
	var xmlhttp=false;
	try{
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
		}
	}
	return xmlhttp; 
}

function enviarDatosFecha(fecha2,fecha,nombre){
	divResultado = document.getElementById('resultado');
	ajax=objetoAjax();
	ajax.open("POST", "datosfecha.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
		divResultado.innerHTML = ajax.responseText
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("fecha2="+fecha2+"&fecha="+fecha+"&nombre="+nombre)
}

function listavehiculo(linea,modelo,marca){
	divResultado = document.getElementById('content');
	ajax=objetoAjax();
	ajax.open("POST", "php/consultas/buscarMarcaVehiculo/listarMarcaVehiculo.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
		divResultado.innerHTML = ajax.responseText
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");	
    ajax.send("linea="+linea+"&modelo="+modelo+"&marca="+marca)
}


// Declaro los selects que componen el documento HTML. Su atributo ID debe figurar aqui.
var listadoSelects=new Array();
listadoSelects[0]="select1";
listadoSelects[1]="select2";
listadoSelects[2]="select3";

function buscarEnArray(array, dato){
	// Retorna el indice de la posicion donde se encuentra el elemento en el array o null si no se encuentra
	var x=0;
	while(array[x]){
		if(array[x]==dato) return x;
		x++;
	}
	return null;
}

function cargaContenido(idSelectOrigen){
	// Obtengo la posicion que ocupa el select que debe ser cargado en el array declarado mas arriba

	var posicionSelectDestino=buscarEnArray(listadoSelects, idSelectOrigen)+1;
	// Obtengo el select que el usuario modifico
	var selectOrigen=document.getElementById(idSelectOrigen);
	// Obtengo la opcion que el usuario selecciono
	var opcionSeleccionada=selectOrigen.options[selectOrigen.selectedIndex].value;
	// Si el usuario eligio la opcion "Elige", no voy al servidor y pongo los selects siguientes en estado "Selecciona opcion..."
	if(opcionSeleccionada==0){
		var x=posicionSelectDestino, selectActual=null;
		// Busco todos los selects siguientes al que inicio el evento onChange y les cambio el estado y deshabilito
		while(listadoSelects[x]){
			selectActual=document.getElementById(listadoSelects[x]);
			selectActual.length=0;
			
			var nuevaOpcion=document.createElement("option"); nuevaOpcion.value=0; nuevaOpcion.innerHTML="Seleccione";
			selectActual.appendChild(nuevaOpcion);	selectActual.disabled=true;			
			
			x++;
		}
	}
	// Compruebo que el select modificado no sea el ultimo de la cadena
	else if(idSelectOrigen!=listadoSelects[listadoSelects.length-1]){
		// Obtengo el elemento del select que debo cargar
		var idSelectDestino=listadoSelects[posicionSelectDestino];
		var selectDestino=document.getElementById(idSelectDestino);
		idMarca=document.getElementById('select1');		
		idMarcaSelect = idMarca.options[idMarca.selectedIndex].value;	
		// Creo el nuevo objeto AJAX y envio al servidor el ID del select a cargar y la opcion seleccionada del select origen
		var ajax=nuevoAjax();
		ajax.open("GET", "php/consultas/busquedaMedidaLlanta/select_dependientes_3_niveles.php?select="+idSelectDestino+"&opcion="+opcionSeleccionada + "&idMarca=" + idMarcaSelect, true);
		ajax.onreadystatechange=function(){ 
			if (ajax.readyState==1){
				selectDestino.length=0;
				var nuevaOpcion=document.createElement("option"); nuevaOpcion.value=0; nuevaOpcion.innerHTML="Cargando...";
				selectDestino.appendChild(nuevaOpcion); selectDestino.disabled=true;	
				
						if (idSelectOrigen=='select1'){
						document.getElementById('select2').disabled=true;
						document.getElementById('select2').selectedIndex=0;
						document.getElementById('select3').disabled=true;
						document.getElementById('select3').selectedIndex=0;
						}else if (idSelectOrigen=='select2'){	
						
						document.getElementById('select3').selectedIndex=0;
						document.getElementById('select3').disabled=true;
						}
				
			}
			if (ajax.readyState==4){
				selectDestino.parentNode.innerHTML=ajax.responseText;
			} 
		}
		ajax.send(null);
	}
}