﻿	var ListeXML= new Array();
	var timerXML=0;
	var flagXML=false;
	var controlXML=0;		
	var toto=0;
	var RETOURHTML=""
	// vide tous les elements en attente dans la queue
	function KillQueue()
	{
		while (ListeXML.length>0)
		{ListeXML.shift()}
		
	}
	
	function XXdebug(txt)
	{
		var DIV=document.getElementById("javadebug")
		if (DIV) {DIV.innerHTML+=txt+"<br>"}
	}
		
	// ajoute un element a la queue de traitement
	function QueueXML(URL,CallBackFunction)
	{
		
		controlXML++;
		XXdebug(controlXML+" Queue "+URL)
		Ubound=ListeXML.length;
		ListeXML[Ubound] = new Array();
		ListeXML[Ubound]["url"] = URL;
		ListeXML[Ubound]["CallBackFunction"] = CallBackFunction;
		EmptyQueue();
	}

	// lecture de la queue et execution si pas occupe
	function EmptyQueue()
	{
		if (flagXML==true)
		{
			if (ListeXML.length>0)
			{
			XXdebug("SetTimeOut LISTE="+ListeXML.length)
			timerXML=setTimeout("EmptyQueue()",300);
			}
		}
		else if (ListeXML.length>0)
		{
			controlXML--;
			flagXML=true;
			//Ubound=ListeXML.length-1;
			Ubound=0
			try
			{
			XXdebug(controlXML+" Empty "+ListeXML[Ubound]["url"])
			StartXML(ListeXML[Ubound]["url"],ListeXML[Ubound]["CallBackFunction"])
			}
			catch(err)
 			{
 			XXdebug(controlXML+" ERREUR ")
 			flagXML=false;
 			}
			//ListeXML.pop()
			ListeXML.shift()
			if (ListeXML.length>0)EmptyQueue()
		}
	}
	
	
	// appel de l'URL
	function StartXML(URL,CallBackFunction)
	{
		flagXML=true;
		var url=utf8(URL);
		if (window.XMLHttpRequest)
		{
			xmlhttp=new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if (xmlhttp!=null)
		{
			xmlhttp.onreadystatechange=function() {EndXML(CallBackFunction,url)};
			xmlhttp.open("GET",url,true);
			xmlhttp.send(null);
		}
		else
		{
			alert("Ton navigateur ne supporte pas la fonction XMLHTTP.");
		}
	}

	// retour de l'appel de l'URL et appelle de la fonction callback
	function EndXML(CallBackFunction,url)
	{
		if (xmlhttp.readyState==4)
		{
			flagXML=false;
			if (xmlhttp.status==200)
			{
				retour=xmlhttp.responseText
				var reg=new RegExp("[']+", "g");
				retour=retour.replace(reg,"\'")
				RETOURHTML=retour
				//CallBackFunction=CallBackFunction.replace('<XMLHTTP>',retour)
				CallBackFunction=CallBackFunction.replace('<XMLHTTP>','retour')
				try
				{
				eval(CallBackFunction);
				}
				catch(err)
				{
				alert("Erreur ENDXML :"+err.description+String.fromCharCode(13)+"Fonction "+CallBackFunction+String.fromCharCode(13)+"l'URL appelée va être copiée dans le presse-papier")
				window.clipboardData.setData('Text', url);
				}
			}
			else
			{
				alert("Probleme appel XML "+xmlhttp.status+" URL="+url );
			}
		}
	}

function repl(txt,cherche,remplace)
{
var reg=new RegExp(cherche, "g");
return txt.replace(reg,remplace);
}

function utf8(txt)
{
txt=repl(txt,"à","%C3%A0")
txt=repl(txt,"â","%C3%A2")
txt=repl(txt,"ä","%C3%A4")
txt=repl(txt,"é","%C3%A9")
txt=repl(txt,"è","%C3%A8")
txt=repl(txt,"ê","%C3%AA")
txt=repl(txt,"ë","%C3%AB")
txt=repl(txt,"î","%C3%AE")
txt=repl(txt,"ï","%C3%AF")
txt=repl(txt,"ô","%C3%B4")
txt=repl(txt,"ö","%C3%B6")
txt=repl(txt,"û","%C3%BB")
txt=repl(txt,"ü","%C3%BC")
txt=repl(txt,"ç","%C3%A7")
return txt
}
