/****************************************************************************
 *		Class Ajax
 *		Par Mikael LEVY (mikael.levy@free.fr),  août 07
 * 		A partir d'une librairie de Rubik

*	@ param url  (String): parametre obligatoire, url du fichier generant dynamiquement la reponse 
*	@ param method  (String) : get ou post
*	@ param parameters (String) : parametres envoyes (format : param1=value1&param2=value2&...)
*	@ param  async (String) : true ou false
*	@ param functionToExecute (string : nom de la fonction a executer si succes

******************************************************************************/


var ajax = 	function() {
	this.request.apply(this, arguments);
}


	/*******************************************************************/
	/**                                                Constructeur                                                        **/
	/*******************************************************************/
ajax.prototype = {
	
	request: function (url,method,parameters,async,functionToExecute) {	
		this.getObject();
			
		this.url = url;
		this.method = (method) ? method : 'post';
		this.parameters = (parameters) ? parameters : null;
		this.async = (async) ? async : true;
		this.functionToExecute = (functionToExecute) ? functionToExecute : null;
								
		this.setReadyProcess();
		this.processRequest();
	}
	,

	/*******************************************************************/
	/**                                        Initialisation de l'objet Ajax                                     **/ 
	/*******************************************************************/
	getObject: function() {
		// FF / OPERA / NETSCAPE
		if(window.XMLHttpRequest) this.Ajax_object = new XMLHttpRequest();
		// Internet Explorer
		else if (window.ActiveXObject) { 
			try {
				this.Ajax_object = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					this.Ajax_object = new ActiveXObject("Microsoft.XMLHTTP");
				} 
				catch (e1) {
					this.Ajax_object = null;
				}
			}
		}
		else  {
		    alert("AJAX impossible sur votre navigateur");
		}
	},

	/*******************************************************************/
	/* *                                       Initialisation de l'objet Ajax                                    * */ 
	/*******************************************************************/
	setReadyProcess: function() 
	{
		this.Ajax_object.onreadystatechange  = function() 
		{
			if (this.Ajax_object && this.Ajax_object.readyState == 4 ) 
			{
				this.responseText = this.Ajax_object.responseText;
				if ( this.Ajax_object.status != 200 ) alert('Erreur \n Status : '+this.Ajax_object.status + ' \n StatusText :  ' + this.Ajax_object.statusText);
				else 
				{
					if (this.functionToExecute != null) this.functionToExecute(this.responseText)
				}
			}
		}.bind(this);
	},
	
	/*******************************************************************/
	/* *                                       Envoie de la requète Ajax  	                                   **/ 
	/*******************************************************************/
	processRequest: function() 
	{
		this.Ajax_object.open(this.method,
			this.method == 'post' ? this.url : this.url + '?' + this.parameters,
			this.async);
		if ( this.method == 'post' )
			this.Ajax_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		this.Ajax_object.send(this.method == 'post' ? this.parameters : null);
	}
}

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
}



