// fonction commune
function JCOMM_GetXmlHttpObject()
{ 
	this.xmlHttp=null;
	if (window.XMLHttpRequest)
	{
		this.xmlHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		this.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
}

// classe JCOMM
// fonctions
function JCOMM_charge(tabstr,bExec)
{
	this.attente_comm_str_tab[this.nb_attente_comm]=tabstr;
	this.attente_comm_exec[this.nb_attente_comm]=bExec;
	
	this.nb_attente_comm++;
	
	if(!this.comm_encours) this.comm_charge();
}

function JCOMM_comm_charge()
{
	if(this.nb_attente_comm==0) { this.comm_encours=false; return; }
	this.comm_encours=true;
	
	if(this.attente_comm_exec[0]==true)
	{ // on execute l'action
		eval(this.attente_comm_str_tab[0][0]);
		
		this.attente_comm_str_tab.shift();
		this.attente_comm_exec.shift();
		this.nb_attente_comm--;
		this.comm_charge();
		return;
	}

	this.GetXmlHttpObject();
	
	if (this.xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url;
	url=this.make_url(this.attente_comm_str_tab[0]);
	if(this.b_ajouter_random) url+="&sid="+Math.random();
//	eval("function jcomm_ready() { JCOMM_comm_stateChanged('"+this.nom+"'); };");
//	this.xmlHttp.onreadystatechange=jcomm_ready;
	
	eval("this.xmlHttp.onreadystatechange=function() { JCOMM_comm_stateChanged('"+this.nom+"'); };");

	this.xmlHttp.open("GET",url,true);
	this.xmlHttp.send(null);
}

function JCOMM_make_url(strtab)
{
	var url;
	
	url="traitement.php"+
			"?req="+strtab[0];

	return url;
}

function JCOMM_comm_stateChanged(nomobj)
{
	var obj=eval(nomobj);
	if (obj.xmlHttp.readyState==4 || obj.xmlHttp.readyState=="complete")
	{
		obj.accompli(obj.attente_comm_str_tab[0],obj.xmlHttp.responseText);
		obj.attente_comm_str_tab.shift();
		obj.attente_comm_exec.shift();
		obj.nb_attente_comm--;
		obj.comm_charge();
	} 
}

function JCOMM_accompli(params,resultat)
{
	alert("params="+params[0]+"\n"+"resultat="+resultat);
}

function JCOMM(nom)
{
	this.nom=nom;
	this.xmlHttp=null;
	this.attente_comm_str_tab=new Array();
	this.attente_comm_exec=new Array();
	this.nb_attente_comm=0;
	this.comm_encours=false;
	this.b_ajouter_random=true;
	
	this.GetXmlHttpObject=JCOMM_GetXmlHttpObject;
	this.charge=JCOMM_charge;
	this.comm_charge=JCOMM_comm_charge;
	this.make_url=JCOMM_make_url;
	this.accompli=JCOMM_accompli;
}

