//HTTP States
var STATE_UNINITIALIZED = 0;
var STATE_LOADING 		= 1;
var STATE_LOADED 		= 2;
var STATE_INTERACTIVE 	= 3;
var STATE_COMPLETE 		= 4;

//Response codes
var RESPONSE_OK 		= 200;
var RESPONSE_NOT_FOUND 	= 404;

var elementId;

function createRequestObject() 
{
    var tmpXmlHttpObject;
    
    //depending on what the browser supports, use the right way to create the XMLHttpRequest object
    if (window.XMLHttpRequest) 
    { 
        // Mozilla, Safari would use this method ...
        tmpXmlHttpObject = new XMLHttpRequest();
	
    } 
    else if (window.ActiveXObject) 
    { 
        // IE would use this method ...
        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    return tmpXmlHttpObject;
}

//call the above function to create the XMLHttpRequest object
var http = createRequestObject();

function makeGetRequest(callUri, elementIdValue) 
{
	  //make a connection to the server ... specifying that you intend to make a GET request 
    //to the server. Specifiy the page name and the URL parameters to send
    //La llamada es síncrona (se realizará una cada vez)    
    http.open('GET', callUri, false);
				
		//assign a handler for the response
    //http.onreadystatechange = processResponse;
	
		//Asignamos el valor al elementId
		elementId = elementIdValue;
	
    //actually send the request to the server
    http.send(null);
    
		//se pone aqui para que sea asincrona
    processResponse();
}

function processResponse() 
{
		//alert("Entrando a processResponse...");
    //check if the response has been received from the server
    if(http.readyState == STATE_COMPLETE)
    {
		
			if(http.status == RESPONSE_OK)
			{
				//read and assign the response from the server
		        var response = http.responseText;
				
		        //do additional parsing of the response, if needed
	
		        //in this case simply assign the response to the contents of the <div> on the page. 
		        document.getElementById(elementId).innerHTML = response;
			}
			//ERROR
			else
			{
				
				//If the server returned an error message like a 404 error, that message would be shown within the div tag!!. 
		        //So it may be worth doing some basic error before setting the contents of the <div>
			}
    }
}