var netp = new Object();

netp.READY_STATE_UNINITIALIZED=0;
netp.READY_STATE_LOADING=1;
netp.READY_STATE_LOADED=2;
netp.READY_STATE_INTERACTIVE=3;
netp.READY_STATE_COMPLETE=4;

// constructor
netp.CargadorContenidos = function(url, metodo, parametros, contentType, funcion, funcionError) 
{
	this.url=url;
	this.req=null;
	this.contentType = contentType;
	this.metodo = metodo;
	this.parametros = parametros;
	this.onload=(funcion) ? funcion : this.showResponse;
	this.onerror = (funcionError) ? funcionError : this.defaultError;
	this.cargarContenidoXML();
}

netp.CargadorContenidos.prototype= {
	// Envia la peticion HTTP  y realiza la llamada a la funcion que procesa
	// la respuesta
	cargarContenidoXML:function()
	{
		if(window.XMLHttpRequest) {
			this.req = new XMLHttpRequest();
		}
		else if(window.ActiveXObject) {
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if(this.req) {
				var loader = this;
				this.req.onreadystatechange = function() {
					loader.onReadyState();
				}
				this.req.open(this.metodo, this.url, true);
				if(this.contentType) {
					this.req.setRequestHeader("Content-Type", this.contentType);
				}
				this.req.send(this.parametros);
		} else 
		{ 
			this.onerror.call(this);
		}
	},
	// gestiona la respuesta del servidor
	// Se invoca al recibir una respuesta del servidor
	onReadyState:function() 
	{
		var req = this.req;
		var ready = req.readyState;
		if(ready == net.READY_STATE_COMPLETE) 
		{
			var httpStatus = req.status;
			if(httpStatus == 200 || httpStatus == 0) 
			{
				this.onload.call(this);
			} 
			else 
			{
				this.onerror.call(this);
			}			
		}
	},
	// Se invoca si no se ha definido la funcion "funcionError"
	defaultError:function() 
	{	
		alert("Se ha producido un error al obtener los datos"
			+ "\n\nreadyState:" + this.req.readyState
			+ "\nstatus:" + this.req.status
			+ "\nheaders:" + this.req.getAllResponseHeaders());
	},
	// Muestra la respuesta del servidor
	showResponse:function () {
		alert(this.req.responseText);
	}
}
