var req;
var layer;
function loadHTMLDoc(url) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function ejecuta_busqueda(input, response, where)
{
  if (response != ''){ 
  	anadirAHistorial(true);
	paginaActual = paginaActual+1;
    // Response mode
    list = document.getElementById(where);
	//alert("Ya tá");
	//alert(response);
	list.innerHTML = response;
	//alert("");
	//--------------cambiarVisibilidad('cargando',true,'hidden');
	sortables_init();
  }else{
	//--------------cambiarVisibilidad('cargando',true,'visible');
	list = document.getElementById(where);
	list.innerHTML = "Cargando ...<br>Espere unos segundos, por favor.";
	//--------------
	layer=where;
    // Input mode
	arr = input.split("|");	
	url = arr[0] + '.php?'; //'http://192.168.0.201/castelo/progpresupuestos/'
	arr = arr[1].split(":");
	for (i=0;i<arr.length;i++){
		data = arr[i].split(']');
		url += data[0] + '=' + data[1];
		if (i!=arr.length-1) url += '&';
	}
    loadHTMLDoc(url);
  }

}
function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...						
		    response  = req.responseText
			ejecuta_busqueda('',response,layer);
	  
        } else {
            alert("Hay un problema para realizar la búsqueda:\n" + req.statusText);
        }
    }
}
