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; 
}

var listadoSelects=new Array();
listadoSelects[0]="pais";
listadoSelects[1]="departamento";
listadoSelects[2]="ciudad";

function buscarEnArray(array, dato){
	var x=0;
	while(array[x]){
		if(array[x]==dato) return x;
		x++;
	}
	return null;
}

function cargaComplementos(idSelectOrigen){
	var posicionSelectDestino=buscarEnArray(listadoSelects, idSelectOrigen)+1;
	var selectOrigen=document.getElementById(idSelectOrigen);
	var opcionSeleccionada=selectOrigen.options[selectOrigen.selectedIndex].value;
	if(opcionSeleccionada==0){
		var x=posicionSelectDestino, selectActual=null;
		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++;
		}
	}
	else if(idSelectOrigen!=listadoSelects[listadoSelects.length-1]){
		var idSelectDestino=listadoSelects[posicionSelectDestino];
		var selectDestino=document.getElementById(idSelectDestino);


		var ajax=nuevoAjax();
		ajax.open("GET", "php/usuario/selectsCargaComplementos.php?select="+idSelectDestino+"&opcion="+opcionSeleccionada, 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=='pais'){
					if(opcionSeleccionada==42){
						document.getElementById('departamento').disabled=false;
						document.getElementById('departamento').selectedIndex=0;
						//document.getElementById('ciudad').disabled=true;
						document.getElementById('ciudad').selectedIndex=0;
						//document.getElementById('otrodepartamento').disabled=true;
						d//ocument.getElementById('otraciudad').disabled=true;
					}
					else
					{
						//document.getElementById('departamento').disabled=true;
						//document.getElementById('ciudad').disabled=true;
						document.getElementById('otrodepartamento').disabled=false;
						document.getElementById('otraciudad').disabled=false;
					}
				}
				else if (idSelectOrigen=='departamento'){	
					document.getElementById('ciudad').selectedIndex=0;
					document.getElementById('ciudad').disabled=false;
				}	
			}
			if (ajax.readyState==4){
				selectDestino.innerHTML=ajax.responseText;
			} 
		}
		ajax.send(null);
	}
}

