// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 

function getElementLeft(Elem) {
	/*if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.pageX;
	} else {
*/		var elem;
		Elem=elemento;
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		xPos = elem.offsetLeft;
		tempEl = elem.offsetParent;
  		while (tempEl != null) {
  			xPos += tempEl.offsetLeft;
	  		tempEl = tempEl.offsetParent;
  		}
		return xPos;
	//}
}


function getElementTop(Elem) {
/*	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.pageY;
	} else {*/
	Elem=elemento;
		if(document.getElementById) {	
			var elem = document.getElementById(Elem);
		} else if (document.all) {
			var elem = document.all[Elem];
		}
		yPos = elem.offsetTop;
		tempEl = elem.offsetParent;
		while (tempEl != null) {
  			yPos += tempEl.offsetTop;
	  		tempEl = tempEl.offsetParent;
  		}
		return yPos;
	//}
}

function reallocate(e){
	
axisx=getElementLeft(elemento);	
axisy=getElementTop(elemento);	
	
	
	
	axisx=axisx-195;
	axisy=axisy-180;
	
	document.getElementById("divMessage").style.top=axisy+"px";
	document.getElementById("divMessage").style.left=axisx+"px";
	

}



// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
	  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
 	{
  	xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
 	}
 	catch (e)
 	{
	try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
	}
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
   // return the created object or display an error message
  if (!xmlHttp)
 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function process(id)
{ hide();
elemento = "evento"+id;  

  // proceed only if the xmlHttp object isn't busy
  
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // retrieve the name typed by the user on the form
    name = id;
    // execute the quickstart.php page from the server
   
	{xmlHttp.open("GET", "evento_query.php?id=" + name, true); 
	
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
	
    xmlHttp.send(null);
	}
	
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('process(id)', 1000);
}

// executed automatically when a message is received from the server
 function handleServerResponse() 
{ 
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      var xmlResponse = xmlHttp.responseXML;
	  
      // obtain the document element (the root element) of the XML structure
      xmlRoot = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element
		
  	  helloMessage ="";
	  helloMessage = helloMessage + "<font color=#FFFFFF>" + repchars(xmlRoot.getElementsByTagName("nombre")[0].firstChild.data) + "\n" +"</font>";
	  helloMessage = helloMessage + "<br>" + "<font size=-2;>fecha: </font>" + xmlRoot.getElementsByTagName("fecha")[0].firstChild.data + "\n";
	  helloMessage = helloMessage + "<br>" + "<font size=-2;>hora: </font>" + xmlRoot.getElementsByTagName("hora")[0].firstChild.data;
	  helloMessage = helloMessage + "<br>" + "<font size=-2;>precio: </font>" + xmlRoot.getElementsByTagName("precio")[0].firstChild.data + " €";
	  helloMessage = helloMessage + "<br>" + repchars(xmlRoot.getElementsByTagName("descripcion")[0].firstChild.data);
  	  helloMessage = helloMessage + "<br>" + "<center><font size=-2;>menu:</font><br><a onClick='window.open(\"" + repchars(xmlRoot.getElementsByTagName("menu")[0].firstChild.data) + "\",null)'><img src='images/pdf.jpg' /></a> </center>";
	 
	  
	   // update the client display using the data received from the server
      document.getElementById("divMessage").innerHTML = 
                                            '<b> ' + helloMessage + '</b>';
		
		//document.getElementById("divMessage").style.top=top;
		//document.getElementById("divMessage").style.left=left;
		document.getElementById("divMessage").style.visibility='visible';
		reallocate();
		
      // restart sequence
      //setTimeout('process()', 1000);
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}
function hide (){
	document.getElementById("divMessage").style.visibility='hidden';
}



function repchars(cadena) {
	
	real = new Array(12);
	real[0]="ñ";
	real[1]="á";
	real[2]="é";
	real[3]="í";
	real[4]="ó";
	real[5]="ú";
	real[6]="Ñ";
	real[7]="Á";
	real[8]="É";
	real[9]="Í";
	real[10]="Ó";
	real[11]="Ú";
	
	fake = new Array(12);
	fake[0] ="+-n@-+";
	fake[1] ="+-a@-+";
	fake[2] ="+-e@-+";
	fake[3] ="+-i@-+";
	fake[4] ="+-o@-+";
	fake[5] ="+-u@-+";
	fake[6] ="+-N@-+";
	fake[7] ="+-A@-+";
	fake[8] ="+-E@-+";
	fake[9] ="+-I@-+";
	fake[10] ="+-O@-+";
	fake[11] ="+-U@-+";
	
	for (i=0;i<fake.length-1;i++) {
		cadena = cadena.replace (fake[i],real[i]);
	}

	return cadena; 
	}