/*
************************************************
**  Augustin de Preville <augustin AT fidesio DOT com>,
**	http://www.fidesio.com
**
**  Tout droits reserves - Copie interdite du code
**	Pour Fidesio  <info AT fidesio DOT com>
**	
**  All rights reserved - Copy forbiden
**  Copyright Fidesio
************************************************
*/

function ajax(sFile)
{

	this.create = function(sFile)
	{
		if (sFile) this.sFile = sFile; else this.sFile = '';
		this.sMethod = 'POST';
		this.sUrl = '';
		this.sReturn = '';

		this.onFinish = function() { };

		this.isEncodeURI = true;

		this.oXmlhttp = false;
		
		try { this.oXmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e)
		{
			try { this.oXmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (err)
			{
				this.oXmlhttp = null;
			}
		}

		if(!this.oXmlhttp && typeof XMLHttpRequest != "undefined")
			this.oXmlhttp = new XMLHttpRequest();
	
		if (!this.oXmlhttp)
			this.isOk = false;
		else
			this.isOk = true;
	};

/*
*****************************************************
**
**
*****************************************************
*/

	this.setVar = function(name, value)
	{
		
		if (this.sUrl.length < 3)
			this.sUrl = name + "=" + value;
		else
			this.sUrl += "&" + name + "=" + value;
	};

/*
*****************************************************
**
**
*****************************************************
*/

	this.encVar = function(name, value)
	{
		return encodeURIComponent(name) + "=" + encodeURIComponent(value);
	};

/*
*****************************************************
**
**
*****************************************************
*/

	this.encodeURL	= function(sUrl)
	{
		if (!sUrl) return '';
	
		var	aVar = sUrl.split('&');
		
		for (i = 0; i < aVar.length; i++)
		{
			var	aUrl = aVar[i].split('=');

			if (aUrl[0].indexOf('amp;') != -1)
				aUrl[0] = aUrl[0].substring(4);
				
			aVar[i] = this.encVar(aUrl[0],aUrl[1]);
		}
		return aVar.join('&');
	};

/*
*****************************************************
**
**
*****************************************************
*/		
	
	this.run = function(sUrlString)
	{
		if(!this.isOk) return;

		this.aStatus = new Array(2);

		if (sUrlString)
		{ 
			if (this.sUrl.length)
				this.sUrl = this.sURL + "&" + sUrlString;
			else
				this.sUrl = sUrlString; 
		}

		if (this.isEncodeURI)
		{
			this.sUrl = this.encodeURL(this.sUrl);
			this.setVar("rand", new Date().getTime());
		}

/*
******************************************************************************************
*/
/*
******************************************************************************************
*/

		
		if (this.oXmlhttp)
		{
			var self = this;

			if (this.sMethod == 'GET')
				this.oXmlhttp.open(this.sMethod,  this.sFile + "?" + this.sUrl, true);
			else
				this.oXmlhttp.open(this.sMethod, this.sFile, true);

			if (this.sMethod == 'POST')
			{
  				try { this.oXmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded') }
				catch (e) {}
			}

			this.oXmlhttp.send(this.sUrl);
			this.oXmlhttp.onreadystatechange = function()
			{
				switch (self.oXmlhttp.readyState)
				{
					case 1:	case 2:	case 3:	break;
					case 4:
							self.sReturn = self.oXmlhttp.responseText;
							self.sReturnXML = self.oXmlhttp.responseXML;
							self.aStatus[0] = self.oXmlhttp.status;
							self.aStatus[1] = self.oXmlhttp.statusText;
							self.onFinish();
							self.sUrl = '';
					break;
				}
			};
		}
	};

	this.create(sFile);
}

function Send_Form(sFile, Id_bloc)
{
	var Form = new ajax(sFile);
	Form.setVar('nom', document.getElementById('nom').value);
	Form.setVar('tel', document.getElementById('tel').value);
	Form.setVar('email', document.getElementById('mail').value);
	var result = document.getElementById(Id_bloc);
	Form.onFinish = function () { result.innerHTML = this.sReturn; };
	Form.run();
}


function Set_Conteneur(sFile,Id_du_block, selObj)
{
	var oAjax = new ajax(sFile);
	oAjax.setVar('id', selObj.options[selObj.selectedIndex].value);
	var e_sousContaineur = document.getElementById(Id_du_block);
	e_sousContaineur.innerHTML = "Chargement ...";
	oAjax.onFinish = function () { e_sousContaineur.innerHTML = this.sReturn; };
	oAjax.run();
}