/****************************************************
 * Interacción con el serividor
 ****************************************************/
// Guarda una refrencia al objeto XMLHttpRequest
var xmlHttp = creaObjetoXmlHttpRequest();
var agt=navigator.userAgent.toLowerCase();

// Devuelve el objeto XMLHttpRequest
function creaObjetoXmlHttpRequest()
{
/*	if (agt.indexOf("msie")!=-1 && document.all && agt.indexOf("opera") == -1 && agt.indexOf("mac") == -1)
	{
		eval('try{new ActiveXObject(agt.indexOf("msie 5")!=-1?"Microsoft.XMLHTTP":"Msxml2.XMLHTTP")}catch(e){location="html/es/noactivex.html"}');
	}
*/	// Si el navegador es Internet Explorer
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	// Si el navegador es Mozilla u otro
	else
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlHttp = null;
		}
	}
	// Devuelve el objeto creado o muestra un mensaje de error
	if (!xmlHttp)
	{
		alert("Error al crear el objeto XMLHttpRequest.");
	}
	else
	{
		return xmlHttp;
	}
}

function enviaPeticion(url, queryString, metodo, funcionRespuesta)
{
	if (xmlHttp == null)
	{
		xmlHttp = creaObjetoXmlHttpRequest();
	}
	// Continuamos sólo si el objeto xmlHttp no está ocupado
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		// Codificamos los parámetros a enviar
		var cadenaPostEnc = queryString+"&r="+Math.random();//encodeURIComponent(queryString);

		// Llamamos a la página PHP
		xmlHttp.open(metodo, url+"?"+cadenaPostEnc, true);
		// Definimos el método para recoger la respuesta
		xmlHttp.onreadystatechange = funcionRespuesta;
		// Enviamos la petición
		xmlHttp.send(null);
	}
	else
	{
		// Si la conexión está ocupada, lo intentamos más tarde
		estaFuncion = 'enviaPeticion('+url+','+queryString+','+metodo+','+functionPeticion+','+funcionRespuesta+')';
		setTimeout(estaFunction, 500);
	};
}

