
// FUNCIÓN QUE CREA EL OBJETO XMLHttp
function ObtenerXMLHttp ()
{
	// Variable booleana para comprobar una instancia ActiveX valida
	var xmlhttp = false;
	
	// Comprobamos si es Internet Explorer
	try 
	{
		// Versión de JavaScript mayor que 5
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
		// Sino utilizamos el objeto ActiveX
		try 
		{
			// Internet Explorer
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (E) 
		{
			// No Internet Explorer
			xmlhttp = false;
		}
	}
	
	// Si no estamos utilizando Internet Explorer utilizamos el objeto Javascript
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
		xmlhttp = new XMLHttpRequest();
	}

	return xmlhttp;
}

// FUNCIÓN PARA PROCESAR UN XMLHttpRequest
// USO
// CargarAJAX("pagina", "busqueda=" + nombre + "&criterio=distintos", muestraResultados);
// FUNCION LLAMADA AL OBTENER LA RESPUESTA
// function muestraResultados(cadenaRespuesta){}
function CargarAJAX (url, cadenaParametros, funcionResultadoAJAX, argFuncionResultadoAJAX1, argFuncionResultadoAJAX2)
{
	// Obtenemos el objeto XMLHttp
	var xmlhttp = ObtenerXMLHttp ();
	var handlerFunction = RecogerRespuestaAJAX(xmlhttp, funcionResultadoAJAX, argFuncionResultadoAJAX1, argFuncionResultadoAJAX2);
	if (handlerFunction)
	{
		xmlhttp.onreadystatechange = handlerFunction;
	}
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	// Hacemos la petición adjuntando los parámetros
	xmlhttp.send(cadenaParametros);
}

// FUNCIÓN QUE RECOGE LA RESPUESTA DE LA PETICIÓN AJAX
function RecogerRespuestaAJAX (req, funcionResultadoAJAX, argFuncionResultadoAJAX1, argFuncionResultadoAJAX2) 
{
	if (funcionResultadoAJAX)
	{
		return function()
		{
			// SI LA PETICION HA FINALIZADO
			if(req.readyState == 4)
			{
				// SI SE HA RECIBIDO LA RESPUESTA
				if(req.status == 200)
				{
					var tipoRespuesta = req.getResponseHeader("Content-Type");
					
					if(tipoRespuesta.indexOf("html") != -1) // HTML
					{
						funcionResultadoAJAX(req.responseText, argFuncionResultadoAJAX1, argFuncionResultadoAJAX2);
					}
					else  // XML
					{
						funcionResultadoAJAX(req.responseXML, argFuncionResultadoAJAX1, argFuncionResultadoAJAX2);
					}
		
				}
				else
				{
					//alert("PROBLEMA RECIBIENDO DATOS " + req.status);
				}
			}
		}
	}
}

function MostrarResultado(cadenaRespuesta)
{
	document.getElementById("contenido").innerHTML = cadenaRespuesta;
}

function MostrarResultadoImg(cadenaRespuesta)
{
	document.getElementById("publi").innerHTML = cadenaRespuesta;
}
