var peticion_http; 
var camp_list;
var sel_lista;
var origen_list;

function cargaContenido(url, metodo, funcion) { 
	peticion_http = inicializa_xhr(); 
	if(peticion_http) { 
		peticion_http.onreadystatechange = funcion; 
		peticion_http.open(metodo, url, true); 
		peticion_http.send(null); 
	} 
} 

function inicializa_xhr() { 
	if (window.XMLHttpRequest) { // Mozilla, Safari, ... 
		return new XMLHttpRequest(); 
	} 
	else if (window.ActiveXObject) { // IE 
		try 
        {
            return new ActiveXObject ("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            return new ActiveXObject ("Microsoft.XMLHTTP");
            
        }
	} 
} 

function muestraLista() { 
	var objSelect = document.getElementById(camp_list);
	if(peticion_http.readyState == 4) { 
		if (peticion_http.status == 200) {
			objSelect.length = 0;
			var resp = peticion_http.responseText.split("|");
			var option = new Option("Todos","");
			try {
			   objSelect.add(option,null);
			} catch (e) {
			   objSelect.add(option,-1);
			}
			
			for(i=0;i<resp.length; i++){
				value = parseInt(resp[i]);
				i++;
				text = resp[i];
				if(text != undefined){
					option = new Option(text,value);
					try {
					   objSelect.add(option,null);
					} catch (e) {
					   objSelect.add(option,-1);
					}
			}
		}
			limpiarCampos();
		}
	} else if(peticion_http.readyState == 1){
			var option = new Option("Cargando...","");
			try {
			   objSelect.add(option,null);
			} catch (e) {
			   objSelect.add(option,-1);
			}
		}
}

function mostrar(list, campo, dest) { 
	camp_list = dest;
	var lista = document.getElementById(list);
	if(dest) cargaContenido("src/datos.php?cp="+campo+"&id="+lista.value, "GET", muestraLista); 
}

function limpiarCampos(){
	if(camp_list == 'list_dptos'){
		document.getElementById('list_ciudades').length = 0;
		document.getElementById('list_zonas').length = 0;
	}else if(camp_list == 'list_ciudades'){
		document.getElementById('list_zonas').length = 0;
	}else if(camp_list == 'list_paises'){
		document.getElementById('list_dptos').length = 0;
		document.getElementById('list_ciudades').length = 0;
		document.getElementById('list_zonas').length = 0;
	}
}