/**
 * Ajax.js
 * 
 * Description :
 * 		Classe facilitant l'envoi de requete XmlHttprequest.
 * 
 * Historique :
 * 	7 juil. 06 : cr?ation
 */


function Ajax(url)
{
	
	// --------------------
	// -- Initialisation --
	// --------------------

	// -- Fermeture lexicale
	var _this = this;
	
	// -- Propri?t?s priv?e de l'objet
	this.xhr = null;
	
	// -- Propri?t?s public de l'objet
	this.url = url;
	this.asynchrone = true;
	this.attributes = new Array();

	// Id des attribut a envoyer, le send remplie attributes en se basant dessus (XUL)
	this.idOfAttributes = new Array();	
	
	// -- Initialisation de l'objet

	if (window.XMLHttpRequest) 
	{ 
		// Mozilla, Safari,...
		this.xhr = new XMLHttpRequest();
		if (this.xhr.overrideMimeType)  	this.xhr.overrideMimeType('text/xml');
        } 
        else if (window.ActiveXObject) 
        { 
        	// IE
		try {this.xhr = new ActiveXObject("Msxml2.XMLHTTP");} 
		catch (e) {
			try {this.xhr = new ActiveXObject("Microsoft.XMLHTTP");} 
			catch (e) {}
		}
        }
	else { // XMLHttpRequest non support? par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   this.xhr = false; 
	}

        
}



	// Handler Vides destin? a ?tre redefinie a l'exterieur
	Ajax.prototype.onOpen = function () {}
	Ajax.prototype.onSend =  function () {}
	Ajax.prototype.onReceive =  function () {}
	Ajax.prototype.onSuccess = function () {}
	Ajax.prototype.onFail = function () {}



	// -----------------
	// --  Accesseurs --
	// -----------------
	Ajax.prototype.statusText = function () {
		return this.xhr.statusText;
	}
		
	Ajax.prototype.status = function () {
		return this.xhr.status;
	}
	
	Ajax.prototype.readyState = function() {
		return this.xhr.readyState;
	}
	
	Ajax.prototype.responseText = function ()	{
		return this.xhr.responseText;
	}
		
	Ajax.prototype.responseXML = function () {
		return this.xhr.responseXML;
	}

	
	// ------------------------------
	// -- D?claration des methodes --
	// ------------------------------
	
	Ajax.prototype.send = function (methode)	{

		// La methode par defaut est GET
		if (methode=="") 		methode="POST";

		if (!methode)
			methode = "GET";



		// On rajoute les attributs depuis le tableau id
		for(var i in this.idOfAttributes)
			this.attributes[this.idOfAttributes[i]] = document.getElementById(this.idOfAttributes[i]).value;
		
		// On convertie les attribute en une chaine
		var liste_args = new Array();
		for (var i in this.attributes)
			liste_args.push(i+"="+this.attributes[i]);
		var args = liste_args.join('&');
	
	

		
		// Handler
		_this = this;
		this.xhr.onreadystatechange = function () {
			switch(_this.xhr.readyState)
			{
				case 0 : //Non initialis?.
					// On fait rien
				break;
				case 1: // Ouverture (open() vient de s'ex?cuter).
					_this.onOpen();
				break;
				case 2: // Envoy? (send() vient de s'ex?cuter).
					_this.onSend();
				break;
				case 3: // En cours (des donn?es sont en train d'arriver).
					_this.onReceive();
				break;
				case 4: // Pr?t (toutes les donn?es sont charg?es).
				{
					if(_this.xhr.status == 200 ) // Reponse OK du serveur
						_this.onSuccess();
					else
						_this.onFail();
				}
				break;
			}
		}
	
	
	
		// Ouverture de la connexion et envoi en fonction de la methode
		if (methode == "POST")
		{
			this.xhr.open(methode, this.url, true)
			this.xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			this.xhr.send(args);
		}
		else
		{
			this.xhr.open("GET", this.url+"?"+args, true)
			this.xhr.send(null);
		}
	}






// D?pr?ci?

function getHttpObject()
{
	http_request = false;

	if (window.XMLHttpRequest) 
	{ 
		// Mozilla, Safari,...
		http_request = new XMLHttpRequest();

		if (http_request.overrideMimeType) 
		{
                	http_request.overrideMimeType('text/xml');
		}

        } 
        else if (window.ActiveXObject) 
        { 
        	// IE
		try {
                	http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
        }


	return (http_request);
}




