var xhr = new Array(); // ARRAY OF XML-HTTP REQUESTS
var xi = new Array(0); // ARRAY OF XML-HTTP REQUEST INDEXES
var cont = new Array;
var textog = new Array;
xi[0] = 1; // FIRST INDEX SET TO 1 MAKING IT AVAILABLE
//contenedor

function objetoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
  		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function cargar_dato(dato,contenedor,texto){
	cont = document.getElementById(contenedor);
  //  alert('CONT '+cont);
  //document.formgeneral.submit();
	ajax=objetoAjax();
//    ajax.open("GET", dato,true);
	ajax.open('POST','../php/' , true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	ajax.send(dato);
    ajax.onreadystatechange = function(){
		
		if (ajax.readyState == 4){
				if (ajax.status == 200) {
					funciones=separarjava(ajax.responseText);	
					cont.innerHTML = ajax.responseText;
					if(funciones){eval(funciones);}
					}
				else if(ajax.status==404){ cont.innerHTML = "La página no existe";}
				else {cont.innerHTML = "Error:".ajax.status; }

		}
		//else if (ajax.readyState == 1){	cont.innerHTML ='<img src="../img/loading2.gif" > - '+texto;}
		}
	
	//ajax.send(null);
	
}

function cargar_dato2(dato,contenedor,texto){
	cont = document.getElementById(contenedor);
   // alert('CONT '+cont);
  //document.formgeneral.submit();
	ajax=objetoAjax();
//    ajax.open("GET", dato,true);
	ajax.open('POST','../privado/inicio.php' , true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	ajax.send(dato);
    ajax.onreadystatechange = function(){
		
		if (ajax.readyState == 4){
				if (ajax.status == 200) {
					funciones=separarjava(ajax.responseText);	
					cont.innerHTML = ajax.responseText;
					if(funciones){eval(funciones);}
					}
				else if(ajax.status==404){ cont.innerHTML = "La página no existe";}
				else {cont.innerHTML = "Error:".ajax.status; }

		}
		//else if (ajax.readyState == 1){	cont.innerHTML ='<img src="../img/loading2.gif" > - '+texto;}
		}
	
	//ajax.send(null);
	
}

//Funcion AJAX MultiHilo
var xhr = new Array(); // ARRAY OF XML-HTTP REQUESTS
var xi = new Array(0); // ARRAY OF XML-HTTP REQUEST INDEXES
xi[0] = 1; // FIRST INDEX SET TO 1 MAKING IT AVAILABLE
var textog = new Array();
var cont = new Array();


function xhrRequest(type) {
if (!type) {
type = 'html';
}


// xhrsend IS THE xi POSITION THAT GETS PASSED BACK
// INITIALIZED TO THE LENGTH OF THE ARRAY(LAST POSITION + 1)
// IN CASE A FREE RESOURCE ISN'T FOUND IN THE LOOP
var xhrsend = xi.length;

// GO THROUGH AVAILABLE xi VALUES
for (var i=0; i<xi.length; i++) {


// IF IT'S 1 (AVAILABLE), ALLOCATE IT FOR USE AND BREAK
if (xi[i] == 1) {
xi[i] = 0;
xhrsend = i;
break;
}
}


// SET TO 0 SINCE IT'S NOW ALLOCATED FOR USE
xi[xhrsend] = 0;


// SET UP THE REQUEST
if (window.ActiveXObject) {
try {
xhr[xhrsend] = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhr[xhrsend] = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
} else if (window.XMLHttpRequest) {
xhr[xhrsend] = new XMLHttpRequest();
if (xhr[xhrsend].overrideMimeType) {
xhr[xhrsend].overrideMimeType('text/' + type);
}
}
return (xhrsend);
}


function cargar_datos(url,contenedor,texto) {

var xhri = xhrRequest('html');
textog[xhri]=texto;
cont[xhri] = contenedor;
//document.getElementById('infocarga').style.display='block';
//document.getElementById('infocarga').innerHTML ="<img src='../img/loading6.gif' ><span class='textocarga'>"+textog[xhri]+"</span>";
xhr[xhri].open('POST','../privado/inicio.php' , true);
xhr[xhri].setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xhr[xhri].send(url);
xhr[xhri].onreadystatechange = function() {
if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
funciones=separarjava(xhr[xhri].responseText);	
document.getElementById(cont[xhri]).innerHTML=xhr[xhri].responseText;
if(funciones){eval(funciones);}
//document.getElementById('infocarga').style.display='none';
xi[xhri] = 1;
xhr[xhri] = null;
}
else {
	}
};
//xhr[xhri].send(null);
}

function cargar_datos2(url,contenedor,texto) {

var xhri = xhrRequest('html');
textog[xhri]=texto;
cont[xhri] = contenedor;
//alert (cont[xhri]);
document.getElementById(cont[xhri]).innerHTML ='<img src="../img/loading5.gif" > <span class="loading">'+textog[xhri]+'</span>';
xhr[xhri].open('POST','../php/motor.php' , true);
xhr[xhri].setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xhr[xhri].send(url);
//xhr[xhri].open('GET', url, true);
xhr[xhri].onreadystatechange = function() {
if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
document.getElementById(cont[xhri]).innerHTML=xhr[xhri].responseText;
xi[xhri] = 1;
xhr[xhri] = null;
}
else {
	//document.getElementById(cont[xhri]).innerHTML ='<img src="../img/loading2.gif" >  '+textog[xhri];
	}
};
//xhr[xhri].send(null);
}

//Esta funcion es solo para cargar los Items y la primera pagina.
function cargarsubmenu(url,contenedor,texto,id){
var xhri = xhrRequest('html');
textog[xhri]=texto;
cont[xhri] = contenedor;
pestanaseleccionada('1',id);
//document.getElementById(cont[xhri]).innerHTML ='<img src="../img/loading2.gif" > '+textog[xhri];
xhr[xhri].open('POST','../php/motor.php' , true);
xhr[xhri].setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xhr[xhri].send(url);
xhr[xhri].onreadystatechange = function() {
if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
//funciones=separarjava(xhr[xhri].responseText);	
//alert(funciones);
document.getElementById(cont[xhri]).innerHTML=xhr[xhri].responseText;
//if(funciones){eval(funciones);}
xi[xhri] = 1;
xhr[xhri] = null;
cargar_datos('modulo=Modulos&op=operacion','div_cuerpo','Cargando');
}
else {
	}
};
	}

function separarjava(texto){
	var p=0;
	var funciones='';
	while (1){
	inicio=texto.indexOf('<script',p);
	fin=texto.indexOf('</script>',p+7);
	if (inicio !='-1'){
	funciones=funciones+texto.substring(inicio+8,fin);
	}
	else {break;}
	p=fin;
	}
return funciones;
}
