//EVENTOS
function CargarEventos()
{
        CargarAJAX("/museoreinasofia/live/noticias.html?b=1", MostrarEventos);
        CargarAJAX("/museoreinasofia/live/noticias_en.html?b=1", ShowEvents);
}

// Cargamos el contenido de la caja de eventos
window.onload = CargarEventos;

function MostrarEventos(str) {
        var d = document.getElementById("contenedorEventos");
        if (d) {
               d.innerHTML = str;
        }
}

function ShowEvents(str) {
        var d = document.getElementById("eventContainer");
        if (d) {
               d.innerHTML = str;
        }
}

// ---------------------------------------------------

function CargarCalendario(anyo, mes)
{
	var cont = document.getElementById("calendar");
	cont.innerHTML = "<img src=\"/museoreinasofia/live/images/loading.gif\" style=\"margin:70px 0px 0px 55px;\" alt=\"Cargando...\" />";
	CargarAJAX("/museoreinasofia/live/agenda/calendario.html?y=" + anyo + "&m=" + mes, MostrarCalendario);
}


function MostrarCalendario(str)
{
	var cont = document.getElementById("calendar");
	cont.innerHTML = str;
}

//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
function CargarAJAX (url, funcionResultadoAJAX, argFuncionResultadoAJAX1, argFuncionResultadoAJAX2)
{
	// Obtenemos el objeto XMLHttp
	var xmlhttp = ObtenerXMLHttp ();
	var handlerFunction = RecogerRespuestaAJAX(xmlhttp, funcionResultadoAJAX, argFuncionResultadoAJAX1, argFuncionResultadoAJAX2);
	if (handlerFunction)
	{
		xmlhttp.onreadystatechange = handlerFunction;
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	// Hacemos la petición adjuntando los parámetros
	xmlhttp.send(null);
}

// FUNCIÓN QUE RECOGE LA RESPUESTA DE LAPETICIÓ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 LOS DATOS");
				}
			}
		}
	}
}
