/*
Copyright (c) 2007, PMP Concept
version: 1
*/

/*****************************************************************************
 *
 * 								Classe pmpAjax
 *
 *	Fonction : 	gestion de la communication client/server via AJAX
 *
 *  Paramètres
 *  	- url		: (string) fichier du script appelé
 *		- mode		: (string) method d'envoi POST ou GET
 *		- autosend  : (boolean) envoi automatiquement la requete au serveur 
 					  lors de l'ajout / modification des données
 *
*****************************************************************************/

PMP.util.pmpAjax = function(url, mode, autosend) 
{
	this.xhr = false;
	this.url = PMP.common.isString(url) ? url : new String() ;
	this.data = new Array();
	this.autoReset = false;
	this.autoSend = PMP.common.isBoolean(autosend) ? autosend : false;
	this.sendMethod = PMP.common.isString(mode) && (mode.toUpperCase() == "POST" || mode.toUpperCase() == "GET") ? mode : "POST";

	if (window.XMLHttpRequest) 
	{
		try 
		{ 
			this.xhr = new window.XMLHttpRequest(); 
		}
		catch (e) { return false; }
	} 
	else if (window.ActiveXObject)
	{
		var msXML = new Array(	"Msxml2.XMLHTTP.5.0",
								"Msxml2.XMLHTTP.4.0",
								"Msxml2.XMLHTTP.3.0",
								"Msxml2.XMLHTTP",
								"Microsoft.XMLHTTP");
		var nbMsXML = msXML.length;
		for (var i = 0; i < nbMsXML; i++) 
		{
			try 
			{
				this.xhr = new ActiveXObject(msXML[i]);
				break;
			}
			catch (e) { }
		}
		if (!this.xhr) { return false; }
	}

	this.xhr.onreadystatechange = (function () 
	{
		switch (this.xhr.readyState) {
			case 0 : this.uninitialized(this.xhr);
				break;

			case 1 : this.loading(this.xhr);
				break;

			case 2 : this.loaded(this.xhr);
				break;

			case 3 : this.interactive(this.xhr);
				break;

			case 4: this.complete(this.xhr);
				break;
		}
	}).bind(this);
}



PMP.util.pmpAjax.prototype.uninitialized	= new Function;
PMP.util.pmpAjax.prototype.loading		= new Function;
PMP.util.pmpAjax.prototype.loaded		= new Function;
PMP.util.pmpAjax.prototype.interactive	= new Function;
PMP.util.pmpAjax.prototype.complete		= new Function;

PMP.util.pmpAjax.prototype.autoResetOff = function ()	{ this.autoReset = false; }
PMP.util.pmpAjax.prototype.autoResetOn  = function ()	{ this.autoReset = true;  }
PMP.util.pmpAjax.prototype.autoSendOff  = function ()	{ this.autoSend  = false; }
PMP.util.pmpAjax.prototype.autoSendOn   = function ()	{ this.autoSend  = true;  }

PMP.util.pmpAjax.prototype.setSendMethod = function (method) 
{
	method = method.toUpperCase();
	switch(method)
	{
		case "GET": this.sendMethod = "GET";
		break;

		case "POST": this.sendMethod = "POST";
		break;

		default: this.sendMethod = "POST";
	}
}

PMP.util.pmpAjax.prototype.switchSendMethod = function() 
{
	if (this.sendMethod == "POST") 
		this.sendMethod = "GET";
	else 
		this.sendMethod = "POST";
}

PMP.util.pmpAjax.prototype.appendData = function (field, value)
{
	// vérification de l'existence de la variable
	for (var i = 0; i < this.data.length; i++)
	{
		if (this.data[i]["field"] == field) 
		{
			this.data[i]["value"] = value.toString();
			return (this.autoSend) ? this.send() : true;
		}
	}
	// si la variable n'a pas été trouvé, on l'insère à la fin
	this.data.push(new Array());
	this.data[this.data.length - 1]["field"] = field;
	this.data[this.data.length - 1]["value"] = value.toString();
	return (this.autoSend) ? this.send() : true;
}

PMP.util.pmpAjax.prototype.resetData = function () 
{
	delete this.data;
	this.data = new Array();
	return true;
}

PMP.util.pmpAjax.prototype.prepareData = function () 
{
	var prepared = new String();
	for (var i = 0; i < this.data.length; i++) 
	{
		prepared += (i == 0 ? "" : "&") + encodeURIComponent(this.data[i]["field"]);
		prepared += "=" + encodeURIComponent(this.data[i]["value"]);
	}
	return prepared;
}

PMP.util.pmpAjax.prototype.setField = function (field, newField)
{
	for (var i = 0; i < this.data.length; i++)
	{
		if (this.data[i]["field"] == field) 
		{
			this.data[i]["field"] = newField;
			return (this.autoSend) ? this.send() : true;
		}
	}
	return false;
}

PMP.util.pmpAjax.prototype.deleteField = function (field) 
{
	var newData = new Array();
	var newIndex = 0;
	var deleted = false;
	for (var i = 0; i < this.data.length; i++) 
	{
		if (this.data[i]["field"] != field) 
		{
			newData.push(new Array());
			newData[newIndex]["field"] = this.data[i]["field"];
			newData[newIndex]["value"] = this.data[i]["value"];
			newIndex ++;
		} 
		else 
		{ 
			deleted = true;
		}
	}
	this.data = newData;
	return (deleted) ? ((this.autoSend) ? this.send() : true) : false;
}

PMP.util.pmpAjax.prototype.setValue = function (field, value) 
{
	for (var i = 0; i < this.data.length; i++) 
	{
		if (this.data[i]["field"] == field) 
		{
			this.data[i]["value"] = value;
			return (this.autoSend) ? this.send() : true;
		}
	}
	return false;
}

PMP.util.pmpAjax.prototype.deleteValue = function (field)
{
	for (var i = 0; i < this.data.length; i++)
	{
		if (this.data[i]["field"] == field) 
		{
			this.data[i]["value"] = new String;
			return (this.autoSend) ? this.send() : true;
		}
	}
	return false;
}

PMP.util.pmpAjax.prototype.send = function (url) 
{
	if(!PMP.common.isString(url))
		 var url = this.url;
	
	if( !PMP.common.isString(url) || url.length == 0 )
		return false;

	var preparedData = this.prepareData();

	switch(this.sendMethod)
	{
		case "GET":
			//try 
			//{
				if (preparedData.length > 0) 
					url = url + "?" + preparedData;
				this.xhr.open("GET", url, false);
				this.xhr.send(null);
			//} 
			//catch (e) { return false; }
			break;

		case "POST":
			//try 
			//{
				this.xhr.open("POST", url, false);
				this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.xhr.send(preparedData);
			//} 
			//catch (e) { return false; }
			break;
	}


	if(this.xhr.readyState == 4) 
	{
		if(this.xhr.status == 200)
			return(this.xhr);
	}
	
	if(this.autoReset) 
		this.resetData();

	return false;
}


