//	versión AJAX de ONE - QuimiNet
var ajxqn_http = new Array();
var ajxqn_error = false;
var ajxqn_max = 25;
String.prototype.htmlEntities = function () {
   return this.replace(/</g,'&lt;').replace(/>/g,'&gt;');
};
//var ajxqn_brw = ajxqn_browser();
function ajxqn_EscapeQQ( thing )
	{
	temp = new String(thing).replace(/'"'/g, '\\"');
	return temp;
	}
function ajxqn_Unescape( str )
	{
	// payload has slashes escaped with whacks
	temp2 = new String(str).replace( /\\\//g, "/" );
	return temp2;
	}
function ajxqn_echo_message(msg)
	{
	var defaultCss = "";
	defaultCss = "position:absolute;right:1px;top:1px;color:white;background-color:red;font-family:verdana;font-weight:lighter;";
	var div_tmp_AJX = document.getElementById("AJXmessage");
	if (div_tmp_AJX==null)
		{
		var un_div = document.createElement("DIV");
		un_div.id = "AJXmessage";
		un_div.style.cssText = defaultCss;
		if( document.body )
			ref_tmp = document.body.appendChild(un_div);
		div_tmp_AJX = document.getElementById("AJXmessage");
		}
	if( div_tmp_AJX )
		{
		div_tmp_AJX.innerHTML = msg;
		var my_scrollTop = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
		div_tmp_AJX.style.top = (0+my_scrollTop) + "px";
		//alert("top="+div_tmp_AJX.style.top);
		}
	else
		window.status = msg;
	}
function ajxqn_close_colsole()
	{
	var div_tmp_AJX = document.getElementById("AJXQN_console");
	if (div_tmp_AJX==null)
		return;
	div_tmp_AJX.style.display = "none";
	div_tmp_AJX.style.visibility = "hidden";
	}
function ajxqn_colsole(num_pet, echo, extra)
	{
	var div_tmp_AJX = document.getElementById("AJXQN_console");
	if (div_tmp_AJX==null)
		{
		var un_div = document.createElement("DIV");
		un_div.id = "AJXQN_console";
		un_div.style.cssText = "position:absolute; top:1px; left:1px;color:black;background-color:white;font-family:verdana;border:1px dotted darkblue;width:260px;z-index:11000;";
		if( document.body )
			ref_tmp = document.body.appendChild(un_div);
		div_tmp_AJX = document.getElementById("AJXQN_console");
		}
	if( div_tmp_AJX )
		{
		div_tmp_AJX.innerHTML = '';
		div_tmp_AJX.style.display = "block";
		div_tmp_AJX.style.visibility = "visible";
		var un_nota = document.createElement("DIV");
		var Stamp = new Date();
		var tm_tmp = '';
		var Hours;
		var Mins;
		var Segs;
		var Time;
		Hours = Stamp.getHours();
		if (Hours >= 12)
			Time = " P.M.";
		else
			Time = " A.M.";
		if (Hours > 12)
			Hours -= 12;
		if (Hours == 0)
			Hours = 12;
		Mins = Stamp.getMinutes();
		if (Mins < 10)
			Mins = "0" + Mins;
		Segs = Stamp.getSeconds();
		if ( Segs < 10 )
			Segs = "0" + Segs;
		tm_tmp += Hours + ":" + Mins + ":" + Segs + Time;
		sal_inner = "<strong>QuimiAjaX</strong>&nbsp;&nbsp;"+tm_tmp+'&nbsp;&nbsp;<a href="javascript:ajxqn_close_colsole();" title="CERRAR CONSOLA">CERRAR</a>';
		sal_inner += "<br>http.length="+ajxqn_http.length+"; Resp.length="+((new String(echo)).length)+"; Petición:"+num_pet;
		if( extra!=null && extra!="")
			sal_inner += "<br><strong>PlayLoad:</strong><br>"+extra;
		un_nota.innerHTML = sal_inner;
		un_nota.style.cssText = "width:auto;";
		ref_tmp = div_tmp_AJX.appendChild(un_nota);
		un_txt = document.createElement("TEXTAREA");
		un_txt.style.cssText = "height:100px;width:250px;border:1px dotted orange;overflow:scroll;";
		un_txt.id = "AJXQN_txt_console";
		//alert((new String(echo)).htmlEntities());
		if( echo==undefined )
			un_txt.value = "-- Respuesta NULA --";
		un_txt.innerHTML = echo.htmlEntities();
		ref_tmp = div_tmp_AJX.appendChild(un_txt);
		//alert("top="+div_tmp_AJX.style.top);
		}
	}
function ajxqn_getHTTPObject()
	{
	var xmlhttp = false;
	if(typeof XMLHttpRequest!='undefined')
		xmlhttp=new XMLHttpRequest();
	else 
		if(typeof ActiveXObject!='undefined')
	xmlhttp = (Number(navigator.appVersion.substr(0,3))>=5)? new ActiveXObject('Msxml2.XMLHTTP'):new ActiveXObject('Microsoft.XMLHTTP');
	if( xmlhttp==null )
		alert("Error, tu navegador no soporta contenido dinámico, lo sentimos...");
	
	/*
	if( window.XMLHttpRequest )	//	If the user is using Mozilla/Firefox/Safari
		{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.overrideMimeType('text/xml');
		}
	else	//	If the user is using IE
		{
		if ( window.ActiveXObject)
			{
			try
				{
				xmlhttp = new ActiveXObject( "Msxml2.XMLHTTP" )
				} 
			catch (e)
				{
				try
					{
					xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" )
					}
				catch (e)
					{
					alert("Error, tu navegador no soporta contenido dinámico, lo sentimos...");
					}
				}
			}
		}
	*/
	if( !xmlhttp )
		{
		ajxqn_echo_message('Falla, no es posible crear una instancia XML HTTP');
		return null;
		}
	if( xmlhttp.overrideMimeType )     //	control de un error conocido
		xmlhttp.overrideMimeType('text/xml');
	return xmlhttp;
	}
function ajxqn_primer_http_libre()	//	busca al primer objeto libre, regresa -1 si no hay ninguno libre
	{
	//alert("ajxqn_http.length="+ajxqn_http.length);
	if( ajxqn_http.length==0 )
		return -1;
	for( indice in ajxqn_http )
		{
		//	Libre;  Ocupado es <= 3
		if( !ajxqn_http[indice]["busy"] )
			{
			//ajxqn_colsole(indice, ajxqn_http.toString());
			return indice;
			}
		}
	return -1;
	}
function jsrsExecute (rspage, callback, func, parms, visibility)
	{
	var ajxqn_current;
	ajxqn_current = ajxqn_primer_http_libre();
	var data = null;
	//window.status = "ajxqn_current="+ajxqn_current;
	if( ajxqn_current == -1 )	//	Si no hay ninguno libre o todos están ocupados
		{
		ajxqn_current = ajxqn_http.length;
		ajxqn_http[ajxqn_current] = new Array();
		ajxqn_http[ajxqn_current]["obj"] = ajxqn_getHTTPObject();
		ajxqn_http[ajxqn_current]["busy"] = false;
		//window.status = "["+ajxqn_current+"].readyState="+ajxqn_http[ajxqn_current]["obj"].readyState;
		}
	/*
	else
		window.status = "reciclando:"+ajxqn_current;
	*/
	visibility = (visibility == null)? false : visibility;
	//alert("valueOf="+callback.valueOf());
	//alert("toString="+callback.toString());
	ajxqn_http[ajxqn_current]["obj"].onreadystatechange = function()
		{
		if( ajxqn_http[ajxqn_current]["obj"].readyState <= 3 )
			{
			ajxqn_echo_message("Cargando...");
			}
		if( ajxqn_http[ajxqn_current]["obj"].readyState == 4 )
			{
			ajxqn_echo_message("");
			if (ajxqn_http[ajxqn_current]["obj"].status == 200)
				{
				if( window.status == "Cargando..." )
					window.status = "";
				if( ajxqn_http[ajxqn_current]["obj"].responseText.indexOf("|*RESP_AJX*|") == -1 )//	-1 es que no esta
					{
					ajxqn_colsole(ajxqn_current,"La versión del servidor AJX-PHP no es compatible u ocurrió un Error\nLa respuesta fué:\n"+ajxqn_http[ajxqn_current]["obj"].responseText);
					}
				else
					{
					tmp_arr = ajxqn_http[ajxqn_current]["obj"].responseText.split("|*RESP_AJX*|");
					sal_tmp = ajxqn_Unescape( tmp_arr[1] );
					//	Llamar a la función que solicito el usu, pasandole como parametros la respuesta del Obj
					if( visibility )
						{
						ajxqn_colsole(ajxqn_current, sal_tmp, tmp_arr[0]);
						//alert(ajxqn_http[ajxqn_current]["obj"].getAllResponseHeaders());
						}
					callback( sal_tmp );
					}
				}
			else if( ajxqn_http[ajxqn_current]["obj"].status == 404 )
				{
				ajxqn_colsole(ajxqn_current,"La página solicitada no existe\nPor favor reporte este error al webMaster, gracias.");
				}
			delete ajxqn_http[ajxqn_current]["obj"];
			//ajxqn_http[ajxqn_current]["obj"] = null;
			ajxqn_http[ajxqn_current]["obj"] = ajxqn_getHTTPObject();
			ajxqn_http[ajxqn_current]["busy"] = false;
			}
		};
	//	Mandar la petición
	par_limpios = parms;
	par_limpios = ajxqn_EscapeQQ(par_limpios);
	
	data = 'func='+ func +'&parms='+ par_limpios;
	peticion_real = rspage + "?m=P";// + '?func='+ func +'&parms='+ par_limpios;
	
	ajxqn_http[ajxqn_current]["obj"].open('POST', peticion_real, true)
	ajxqn_http[ajxqn_current]["obj"].setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
    ajxqn_http[ajxqn_current]["obj"].send( data ); // data
	ajxqn_http[ajxqn_current]["busy"] = true;
	ajxqn_echo_message("Cargando...");
	return;
	}
