var destino = 1;
var cuadrosarr = new Array();

var XmlHttpObj;

var Utf8 = {

    //Convierte de UTF-8 a ISO
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function CreateXmlHttpObj()
{
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
		if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}


function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}



function StateChangeHandler()
{
	if(XmlHttpObj.readyState == 4)
	{
		if(XmlHttpObj.status == 200)
		{
			if (destino == 1)
				ParseDatosExpo(XmlHttpObj.responseXML.documentElement);
			if (destino == 2)
				ParseCuadrosExpo(XmlHttpObj.responseXML.documentElement);
			if (destino == 3)
				ParseBuscarExpo(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("Código de error: "  + XmlHttpObj.status);
		}
	}
}

/* *************************************
************************************** */

function GetDatosExpo(idexpo) {

	var requestUrl;
     destino = 1;

	if (IdExpoActiva != null)
		document.getElementById('enlace'+IdExpoActiva).className = "menusub";
	document.getElementById('enlace'+idexpo).className = "menusubon";
	IdExpoActiva = idexpo;

	requestUrl = "datosexpoxml.php" + "?IdExposicion=" + encodeURIComponent(idexpo);

	CreateXmlHttpObj();
	
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandler;
		XmlHttpObj.open( "GET", requestUrl, true );
		XmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		XmlHttpObj.send('');
           document.getElementById('EnlacePestanaExpo4').onclick = function(){GetCuadrosExpo(idexpo);}
           document.getElementById('imgInfoLateral').src = 'imginfo.php?IdExpo='+idexpo;

	}
}



function ParseDatosExpo(ExpoXML) {	

	var DatosExpo = ExpoXML.getElementsByTagName('exposicion')[0];

	ActivaPantallaDatosExpo();
	document.getElementById('divDatosExpo1').innerHTML = GetInnerText(DatosExpo.getElementsByTagName('presentacion')[0]);
	document.getElementById('divDatosExpo2').innerHTML = GetInnerText(DatosExpo.getElementsByTagName('biografia')[0]);
	document.getElementById('divDatosExpo3').innerHTML = "<b>"+GetInnerText(DatosExpo.getElementsByTagName('sala')[0])+"</b>";
	document.getElementById('divDatosExpo3').innerHTML += "<br><br><b>Dirección</b><br>";
	document.getElementById('divDatosExpo3').innerHTML += GetInnerText(DatosExpo.getElementsByTagName('direccion')[0]);
	document.getElementById('divDatosExpo3').innerHTML += "<br><br><b>Horario</b><br>";
	document.getElementById('divDatosExpo3').innerHTML += GetInnerText(DatosExpo.getElementsByTagName('horario')[0]);
}




function GetCuadrosExpo(idexpo) {

	var requestUrl;
	destino = 2;
	
	for (i = 0; i < cuadrosarr.length; i++)
		delete cuadrosarr[i];
	cuadrosarr.length = 0;

	requestUrl = "cuadrosexpoxml.php" + "?IdExposicion=" + encodeURIComponent(idexpo);

	CreateXmlHttpObj();
	
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandler;
		XmlHttpObj.open( "GET", requestUrl, true );
		XmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		XmlHttpObj.send('');		
	}
}


function ParseCuadrosExpo(ExpoXML) {	

	var CuadrosExpo = ExpoXML.getElementsByTagName('cuadro');

	for (var count = 0; count < CuadrosExpo.length; count++) {
		cuadrosarr[count] = new Object();
		cuadrosarr[count].idcuadro = GetInnerText(CuadrosExpo[count].getElementsByTagName('idcuadro')[0]);
		cuadrosarr[count].titulo = GetInnerText(CuadrosExpo[count].getElementsByTagName('titulo')[0]);
		cuadrosarr[count].tecnica = GetInnerText(CuadrosExpo[count].getElementsByTagName('tecnica')[0]);
		cuadrosarr[count].autor = GetInnerText(CuadrosExpo[count].getElementsByTagName('autor')[0]);
		cuadrosarr[count].tamanio = GetInnerText(CuadrosExpo[count].getElementsByTagName('tamanio')[0]);
		cuadrosarr[count].textopie = GetInnerText(CuadrosExpo[count].getElementsByTagName('textopie')[0]);
		cuadrosarr[count].imagenp = GetInnerText(CuadrosExpo[count].getElementsByTagName('imagenp')[0]);
		cuadrosarr[count].imageng = GetInnerText(CuadrosExpo[count].getElementsByTagName('imageng')[0]);
	}

	imprimeThumb();
	ActivaPantallaCuadrosExpo();
}




function BuscarExpo(textobuscar) {

	var requestUrl;
     destino = 3;

	requestUrl = "busquedaexpoxml.php" + "?textobuscar=" + encodeURIComponent(textobuscar);

	CreateXmlHttpObj();
	
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandler;
		XmlHttpObj.open( "GET", requestUrl, true );
		XmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		XmlHttpObj.send('');
	}
}


function ParseBuscarExpo (ExpoXML) {	

	var CuadrosExpo = ExpoXML.getElementsByTagName('exposicion');
	var htmlExpo = '';
	var id;
	var nombre;
	var hasta;

	htmlExpo +=''+
		'<table width="162" border="0" cellspacing="0" cellpadding="0" >';
/*              '<tr bgcolor="#FFFFFF">'+
                '<td width="5">&nbsp;</td>'+
                '<td width="18"><img src="img/icono1.gif" width="17" height="13"></td>'+
                '<td width="5" class="menusub">&nbsp;</td>'+
                '<td width="134" class="menusub" align="left" class="menuprincipal"><b>Encontradas</b></td>'+
              '</tr>';
*/
	for (var count = 0; count < CuadrosExpo.length; count++) {
		id = GetInnerText(CuadrosExpo[count].getElementsByTagName('id')[0]);
		nombre = GetInnerText(CuadrosExpo[count].getElementsByTagName('nombre')[0]);
		hasta = GetInnerText(CuadrosExpo[count].getElementsByTagName('hasta')[0]);
		hasta = hasta.substring(0,4);

		htmlExpo += ''+
              '<tr bgcolor="#D9D9D9">'+
                '<td width="5">&nbsp;</td>'+
                '<td align="right" valign="top" style="padding-top:3px;"><img src="img/icono2.gif" width="11" height="11"></td>'+
                '<td class="menusub">&nbsp;</td>'+
                '<td class="menusub" align="left"><a id="enlace'+id+'" class="menusub" href="#" onClick="GetDatosExpo('+id+');">'+nombre+'</a></td>'+
              '</tr>';

	}
	htmlExpo += '</table>';

	document.getElementById('tblExpoBuscadas').innerHTML = htmlExpo;
}
