function newAjax()
{
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	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)
		{
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
		}
	}
	return xmlhttp;
}

function searchInArray(array, data)
{
	// 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])
	{
		// alert (array[x] + ' = ' + data + '[' + x + ']');
		if(array[x]==data) return x;
		x++;
	}
	return null;
}

function fetchAjaxResponse(baseUrl, listSelects, idSelectSource, selectTargetValue, readOnly) {
	// Obtengo la posicion que ocupa el select que debe ser cargado en el array declarado mas arriba
	var posSelectTarget=searchInArray(listSelects, idSelectSource)+1;
	// alert ('posSelectTarget = ' + posSelectTarget);
	// Obtengo el select que el usuario modifico
	var selectSource=document.getElementById(idSelectSource);
	// Obtengo la opcion que el usuario selecciono
	var optionSelected=selectSource.options[selectSource.selectedIndex].value;
	// Si el usuario eligio la opcion "Elige", no voy al servidor y pongo los selects siguientes en estado "Selecciona opcion..."
	if(optionSelected==0)
	{
		var x=posSelectTarget, selectActual=null;
		// Busco todos los selects siguientes al que inicio el evento onChange y les cambio el estado y deshabilito
		while(listSelects[x])
		{
			selectActual=document.getElementById(listSelects[x]);
			selectActual.length=0;

			var newOption=document.createElement("option"); newOption.value=0; newOption.innerHTML=selectTextDefault; // "Select Option...";
			selectActual.appendChild(newOption);
			selectActual.disabled=true;
			x++;
		}
	}
	// Compruebo que el select modificado no sea el ultimo de la cadena
	else if(idSelectSource!=listSelects[listSelects.length-1])
	{
		// Obtengo el elemento del select que debo cargar
		var idSelectTarget = listSelects[posSelectTarget];
		// alert ('idSelectTarget = ' + idSelectTarget);
		var typeContent = selectsContentSource[posSelectTarget-1];
		var selectDestino=document.getElementById(idSelectTarget);
		// alert ('selectDestino = ' + selectDestino);
		// Creo el nuevo objeto AJAX y envio al servidor el ID del select a cargar y la opcion seleccionada del select origen
		var ajax=newAjax(); // ../content/dependentselectsprocess.php scriptProcess+ "&selectSource="+idSelectSource+
		ajax.open("GET", baseUrl+"ajaxselectsresponse/get/"+typeContent+"/"+optionSelected+"/"+selectTargetValue+"/"+readOnly, true);
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==1)
			{
				// Mientras carga elimino la opcion "Selecciona Opcion..." y pongo una que dice "Cargando..."
				selectDestino.length=0;
				var newOption=document.createElement("option"); newOption.value=0; newOption.innerHTML="Loading...";
				selectDestino.appendChild(newOption); selectDestino.disabled=true;
			}
			if (ajax.readyState==4)
			{
				selectDestino.parentNode.innerHTML=ajax.responseText;
			}
		};
		ajax.send(null);
	}
}
