/* Important -- Ce script nécessite les librairies lib_core.js et lib_dhtml.js */

function AjaxComponent(rU)
{
	this._uri = null;
	var rq = new RequestParam();
	rq.setXmlCompat(false);
	this._requestParam = rq;

	this._result = null;

	this._handler = null;
	this._handlerParam = new NamedList();

	if(testTypeOfObject(rU,'string') && (rU != ''))
	{
		this._uri = rU;
	}

	this.setDataHandler = function(dH)
	{
		if(testTypeOfObject(dH,'function'))
		{
			this._handler = dH;
		}
	}

	this.addHandlerParam = function(pN,pV)
	{
		if(testTypeOfObject(pN,'string'))
		{
			this._handlerParam.addObject(pN,pV);
		}
	}

	this.addRequestParam = function(pN,pV)
	{
		this._requestParam.addParam(pN,pV);
	}

	this.getAjaxContent = function()
	{
		if((this._uri != null) && (this._handler != null))
		{
			var xhr = null;

			if(window.XMLHttpRequest)
			{
				xhr = new XMLHttpRequest();
			}
			else if(window.ActiveXObject)
			{
				try
				{
					xhr = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e)
				{
					try
					{
						xhr = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e)
					{
						// impossible de créer composant
					}
				}
			}
			else
			{
				// pas d'ajax !!
			}

			if(xhr != null)
			{
				var layObj = this._layer;
				var dH = this._handler;
				var paramData = this._handlerParam;

				if(this._uri != null)
				{
					this._uri += this._requestParam.createRequestString();
				}

				xhr.open('GET',this._uri,true);

				xhr.onreadystatechange = function()
				{
					if((xhr.readyState == 4) && (xhr.status == 200))
					{
						var rtO;
						var cT = xhr.getResponseHeader('Content-Type');
//alert(cT+'\nxml : '+xhr.responseXML+'\ntext : '+xhr.responseText)
						if((cT.indexOf('text/xml') != -1) || (cT.indexOf('application/xml') != -1))
						{
							rtO = xhr.responseXML;
						}
						else
						{
							rtO = xhr.responseText;
						}

						paramData.addObject('return-data',rtO);

						dH.call(this,paramData);
					}
				}

				xhr.send(null);
			}
		}
	}
}

