	/*
explications ici : http://www.javascriptfr.com/codes/CLASSE-AJAX-COMPLETE_42970.aspx
Classe pour AJAX
	*/

// Synthaxe : ErreurObj ([String Message]) Objet
// Objet pour la gestion d'erreur
function ErreurObj(txt)
{
	// Variable //
	this.txt = txt;
	this.caller = getCaller(ErreurObj.caller);
	
	// Fonction //
	this.toString = function () {
	return "Erreur : " + this.txt + " Fonction : " + this.caller;
	}
}

// Synthaxe : isset ([Variable]) Boolean
// Même chose que en PHP
function isset(toTest)
{
	return (typeof toTest != "undefined");
}

// Synthaxe : Array.inArray ([String ChercheQuoi]) Boolean
// Chercher pour une chaine particuliere dans un tableau
function inArray(text)
{
	for (a=0;a<this.length;a++)
	{
		if(this[a] == text)
		{
			return true;
		}
	}
}
Array.prototype.inArray = inArray;

//  Synthaxe : getCaller ([String fonction.caller]) String
// Retourne le nom de la fonction appartir de l'attribut caller //
function getCaller(rawCaller)
{
	rawCaller = rawCaller.toString();
	if (rawCaller == null)
	return "";
	
	return rawCaller.substring(rawCaller.indexOf("function ") + 9,rawCaller.indexOf("(")).replace(" ","");
}
function retInputVal(inp)
{
        if(typeof(inp)=='undefined')
                        return '';

                if(!isNaN(inp.selectedIndex))// && !isNan(inp[inp.selectedIndex].value))
		{
                        val=inp[inp.selectedIndex].value;                       // select
		}
        else    if(inp.type=="radio")        
		{
                        if(inp.checked) val=inp.value;
			else		val='';
		}
        else    if(inp.type=="checkbox")        
                        val=(inp.checked?1:0);                  // ckeckbox
        else            
			val=inp.value;                          // input

        return val;
}

// Synthaxe : Ajax () Objet
// Objet centrale de la classe
function Ajax()
{
	// Variable //
	this.asyn = true;
	this.data = "";
	this.url = "";
	this.method = "POST";
	this.returnFormat = "txt";
	this.obj;
	this.init();
}

// Synthaxe : httprequest () Objet XMLHttpRequest
// Crée l'objet XMLHttpRequest //
Ajax.prototype.init = function()
{
    this.obj = null;
    if (window.XMLHttpRequest)
        this.obj = new XMLHttpRequest();
    else if (window.ActiveXObject)  // if IE
    {
        var ieversions = ['Msxml2.XMLHTTP','Microsoft.XMLHTTP','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0'];

        for(var i=0; !this.obj && i<ieversions.length; i++)
        {
		    try
		    {
		        this.obj = new ActiveXObject(ieversions[i]);
		    }
		    catch(e)  { }
        }
    }
}

// Lorsque la requête ne réussit pas
Ajax.prototype.onFailure = function (errorCode)
{
	// ...
}

// Lorsque la requête réussit
Ajax.prototype.onComplete = function (response)
{
	// ...
}

// Synthaxe : setParamFromForm ([HTML Form || String Name || Int Index])
// Ajoute les paramètres et les données d'un formulaire à celui de la requête
Ajax.prototype.setParamFromForm = function (obj)
{
	if (!isNaN(obj))
	obj = document.forms[obj];
	
	if (typeof obj == "string")
		eval("obj = document."+obj);
	
	if (!isset(obj))
	{
		return ErreurObj("Donnée Invalide");
	}
	
	this.method = (isset(obj.method) && (["GET","POST"].inArray(obj.method.toUpperCase()))) ? obj.method.toUpperCase() : this.method;
	this.url = obj.action;
	
	for (i=0;i<obj.elements.length;i++)
	{
		if (!isset(obj.elements[i].type) // DADA
		|| ["file","button","reset","submit"].inArray(obj.elements[i].type.toLowerCase()))
			continue;
		
		val=retInputVal(obj.elements[i]);
        	if(obj.elements[i].type=="radio" && val=='')
			continue;

		if (this.data != null)
			this.data += "&";

		this.data += obj.elements[i].name + "=" + escape(val);
	}
}

// Synthaxe : setParam ([Array data])
// Ajoute les paramètres à la requête à partir d'un tableau
Ajax.prototype.setParam = function (arr)
{
	if (typeof arr != "object" && !isset(arr))
	{
		return ErreurObj("Donnée Invalide");
	}
	
	for(k in arr)
	{
		switch (k)
		{
			case "url" : this.url = arr[k]; break;
			case "method" : this.method = (["GET","POST"].inArray(arr[k].toUpperCase())) ? arr[k].toUpperCase() : this.method; break;
			case "data" :
			if (typeof arr[k] == "string")
			{
				if (this.data != "")
				this.data += "&";
				this.data += arr[k];
			}
			else
			{
				if (typeof arr[k] != "object")
				break;
				
				for (j in arr[k])
				{
					if (this.data != "")
					this.data += "&";
					this.data += j + "=" + escape(arr[k][j]);
				}
				
			}
			break;
			case "asynchronus" : this.asyn = arr[k]; break;
			case "onComplete" : this.onComplete = arr[k];break;
			case "onFailure" : this.onFailure = arr[k];break;
			case "returnFormat" : this.returnFormat = arr[k];break;
		}
	}
}

// Synthaxe : Function execRequest () //
// Exécute la requête, ainsi que le callback
Ajax.prototype.execute = function ()
{
	this.obj.open(this.method,this.url,this.asyn);
	
	if (this.method == "POST")
	this.obj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	if (this.asyn)
	{
		_tempAJAX_Reference_ = this; // Crée une copie de l'objet AJAX courant pour pouvoir le récupérer après //
		this.obj.onreadystatechange = function () {
			if (_tempAJAX_Reference_.obj.readyState == 4 && _tempAJAX_Reference_.obj.status == 200)
			{
				if (_tempAJAX_Reference_.returnFormat != "txt")
				response = _tempAJAX_Reference_.obj.responseXML;
				else
				response = _tempAJAX_Reference_.obj.responseText;
				
				if (typeof _tempAJAX_Reference_.onComplete == "string")
				eval (_tempAJAX_Reference_.onComplete);
				else
				_tempAJAX_Reference_.onComplete(response);
			}
			else if (_tempAJAX_Reference_.obj.readyState == 4)
			{
				errorCode = _tempAJAX_Reference_.obj.status;
				
				if (typeof _tempAJAX_Reference_.onFailure == "string")
				eval(_tempAJAX_Reference_.onFailure);
				else
				_tempAJAX_Reference_.onFailure(errorCode);
			}
		}
		this.obj.send(this.data);
	}
	else
	{
		this.obj.send(this.data);
		
		if (this.obj.status == "200")
		{
			if (this.returnFormat != "txt")
			response = this.obj.responseXML;
			else
			response = this.obj.responseText;
			
			if (typeof this.onComplete == "string")
			eval (this.onComplete);
			else
			this.onComplete(response);
		}
		else
		{
			errorCode = this.obj.status;
			if (typeof this.onFailure == "string")
			eval (this.onFailure);
			else
			this.onFailure(errorCode);
		}
	}
	
}

