/**
*	@fileoverview Simple example that shows how to encapsulate 
*	XMLHTTPRequestCalls
*
*	@author Mike Chambers (mesh@adobe.com)
*/



/**
*	Constructor for the class.
*
*	@param {String} dataURL The path to the data that the class
*	@param {String} name of the <DIV> HTML tag where te contents are going
*	will load (OPTIONAL)
*
*	@constructor
*/
function MessageLoader(dataURL, divContainerName, bIncludeAnimation, stringToEvaluate, bUseRender)
{
	
	this._render=render || null;
	
	if(dataURL != undefined)
	{
		this._dataURL = dataURL;
	}
	
	//String ToEvaluate
	if(stringToEvaluate != undefined)
	{
		this._stringToEvaluate = stringToEvaluate;
	}
	else
	{
		this._stringToEvaluate = null;
	}	
		
	//String ToEvaluate
	if(bUseRender != undefined)
	{
		this._bUseRender = bUseRender;
	}
	else
	{
		this._bUseRender = false;
	}	
	
	
			
	//Animation
	if(bIncludeAnimation != undefined)
	{
		this._bIncludeAnimation = bIncludeAnimation;
	}
	else
	{
		this._bIncludeAnimation = true;
	}
	
	//Siempre hay que pasarlo
	if(divContainerName != undefined)
	{	
		this._containerID = divContainerName;
	}
	
	
	//No Necesito crear el div dinamicamente
	//this._writeContainer();
}

//Value with the URL where to take te data
//MessageLoader.prototype._dataURL = "";

//var to hold an instance of the XMLHTTPRequest object
MessageLoader.prototype._request = undefined;

//ID for the html div we will create to display the data
//MessageLoader.prototype._containerID = "container";

//name of the css class for the HTML container
//Por ahora no lo necesito 
//MessageLoader.prototype._containerClass = "ml_container";

/**************** Public APIs **********************/


/*
*	Tells the class to load its data and render the results.
*/
MessageLoader.prototype.load = function()
{
	
	
	

	if(this._bIncludeAnimation)
	{
		//Agregado par que no se desesperen
		var content = document.getElementById(this._containerID);
		content.innerHTML = "<TABLE height=\"100%\" width=\"100%\"><TR><TD align=\"center\"><BR><BR><BR><img src=\"js/ajax/images/tiny_red.gif\" alt=\"loading...\"><BR><Font size=\"1\" face=\"Verdana\">loading</FONT></TD></TR></TABLE>";
	}
	
	//get a new XMLHTTPRequest and store it in an isntance var.
	this._request = this._getXMLHTTPRequest();
	
	//set the var so we can scope the callback
	var _this = this;
	
	//callback will be an anonymous function that calls back into our class
	//this allows the call back in which we handle the response (_onData())
	// to have the correct scope.
	this._request.onreadystatechange = function(){_this._onData()};
	this._request.open("GET", this._generateDataUrl(), true);	
	this._request.send(null);	
}

/***************Private Rendering APIs ********************/

//No Necisto esto por ahora
/*//writes the top level div for the class / widget
MessageLoader.prototype._writeContainer = function()
{
	//styles should be in external CSS
	document.write("<div id='"+this._containerID+"' class='"+this._containerClass+"'></div>");
}*/

//renders the entire widget
MessageLoader.prototype._render = function(title, xml, stringToEval, containerID)
{
		
		
			
		
			//Siempre escupe igual los datos
			if (this._render) {				
				this._render(title, xml, stringToEval, containerID);
			}	
				
			
		
		
}

/***************Private Data Loading Handlers*******************/

//return the URL from which the data will be loaded
MessageLoader.prototype._generateDataUrl = function()
{
	return this._dataURL;
}


//callback for when the data is loaded from the server
MessageLoader.prototype._onData = function()
{
	if(this._request.readyState == 4)
	{
		if(this._request.status == "200")
		{
			
			if(this._bUseRender)
			{
				this._render(this._request.responseText, this._request.responseXML, this._stringToEvaluate, this._containerID);
			}
			else
			{
				var content = document.getElementById(this._containerID);		
				//Modificado para que meta el HTML dentro del DIV
				//content.appendChild(document.createTextNode(title));
		
				content.innerHTML = this._request.responseText;		
			}
			
			//if the onDraw callback has been defined
			//call it to let the listener know
			//that we are done creating the list
			if(this.onDraw != undefined)
			{
				this.onDraw();
			}
		}
		else
		{	
			//check if an error callback handler has been defined
			if(this.onError != undefined)
			{
				//pass an object to the callback handler with info
				//about the error
				this.onError({status:this_request.status, 
						statusText:this._request.statusText});
			}
		}
		
		//clean up
		delete this._request;
	}
}

/***************Private Data Util Functions ********************/


//returns an XMLHTTPRequest instance (based on browser)
MessageLoader.prototype._getXMLHTTPRequest = function()
{
	var xmlHttp;
	try
	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHttp");
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
		catch(e2)
		{
		}
	}
	
	if(xmlHttp == undefined && (typeof XMLHttpRequest != 'undefined'))
	{
		xmlHttp = new XMLHttpRequest();
	}
	
	
	
	return xmlHttp;
}
