function nuevoAjax()
{ 
	/* 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) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); } 

	return xmlhttp; 
}

function cargaContenido(selectACargar, iIdIdioma)
{
	// Recibo el número correspondiente al combo que se debe llenar de datos
	var selectAnterior=selectACargar-1; // Obtengo el número del combo que activó el evento onChange
	// Extraigo el valor del combo que se ha cambiado
	var valor=document.getElementById("sector"+selectAnterior).options[document.getElementById("sector"+selectAnterior).selectedIndex].value;
	var elemento, i;
	
	if(valor!=0)
	{
		ajax=nuevoAjax();
		// Envio al servidor el valor seleccionado y el combo al cual se le deben poner los datos
		ajax.open("GET", "inc/aplicacionesPerso/solicitudAgente/subsectores.asp?valor="+valor, true);
		ajax.onreadystatechange=function() 
		{ 
			if (ajax.readyState == 1)
			{
				// Mientras carga elimino la opcion "Seleccione" y pongo una que dice "Cargando..."
				elemento = document.getElementById("sector"+selectACargar);
				elemento.length=0;
				var opcionCargando=document.createElement("option"); 
				opcionCargando.value=0; 
				if (iIdIdioma == 1) opcionCargando.innerHTML="Cargando...";
				if (iIdIdioma == 2) opcionCargando.innerHTML="Loading...";
				elemento.appendChild(opcionCargando); 
				elemento.disabled = true;	
			}
			if (ajax.readyState == 4)
			{
				// Coloco en la fila contenedora los datos que recibo del servidor
				elemento = document.getElementById("sector"+selectACargar);
				for (i = elemento.options.length-1; i>0; i--)	elemento.options[i]=null
			  var selectedArray = ajax.responseText.split("##")	
				for (i =0; i < selectedArray.length; i++)
				{ 
					elemento.options[i] = new Option(selectedArray[i].split("||")[1], selectedArray[i].split("||")[0]);
				}
				elemento.options[0].selected = true
				
				elemento.disabled = false;
			} 
		}
		ajax.send(null);
	}
	
	actualizarFormulario(selectACargar, iIdIdioma);
	
}

function actualizarFormulario(selectACargar, iIdIdioma) {
	
	/* Colocamos mediante los whiles los selects en "Seleccione" cuando el select anterior
	ha quedado en estado "Seleccione" */
	var x=selectACargar-1, y=null, valor=null, elemento=null;
	while(x < selectACargar)
	{
		valor = document.getElementById("sector"+x).options[document.getElementById("sector"+x).selectedIndex].value;
		if(valor == 0)
		{
			while(x < selectACargar) 
			{
				y = x+1;
				elemento=document.getElementById("sector"+y);
				elemento.length=0;
				var opcionSelecciona=document.createElement("option"); 
				opcionSelecciona.value=0; 
				if (iIdIdioma == 1) opcionSelecciona.innerHTML="Seleccione";
				if (iIdIdioma == 2) opcionSelecciona.innerHTML="Select";
				elemento.appendChild(opcionSelecciona); 
				elemento.disabled=true;
				x++;
			}
		}
		x++;
	}	
	
}

function vaciarElemento(oCampo) {
	
	// Vaciamos el combo
	var elemento = document.getElementById(oCampo);
	for (var i = elemento.options.length-1; i>0; i--)	elemento.options[i]=null
	elemento.disabled = true;
	
}

