/* 
20081109 - Gestion des communications HTTPrequest
 */

function ajaxRequest(strUrl, casReponse)
{
	try
	{
		this.strUrl = strUrl; //URl de la page cible
		this.casReponse = casReponse; //Fonction de retour pour le traitement
		
		this.version = "3.0"; //Version du programme
		this.dataPost = ""; //Option passé pour un POST ou sinon 404 en texte pour un HEAD
		this.typeReponse = 1; //0 = reponse XML , 1 = reponse TEXT

		this.sendAjax = function ajaxRequest_sendAjax()
				{
					var STATUS_404 = "404";
					
					//On re-init les variables
					var strUrl = this.strUrl;
					var casReponse = this.casReponse;
					var version = this.version;
					var strDataPost = this.dataPost;
					var strTypeReponse = this.typeReponse;
					
					var strSOAPAction = this.SOAPAction;
					
					var blOk_xmlhttp = false;
					var xmlHttpObj_Reponse = false;
					
					xmlHttpObj_Reponse = getXmlHttp();
					
					try{ if (xmlHttpObj_Reponse == false) blOk_xmlhttp = false; else blOk_xmlhttp = true; }
					catch(err){ blOk_xmlhttp = true; }
					
					if (blOk_xmlhttp)
					{
						
						if (strDataPost == "")
						{
							//Navigateur OK on lance l'open
							xmlHttpObj_Reponse.open("GET", strUrl, true);
						}
						else
						{
							if (strDataPost == STATUS_404)
							{
								xmlHttpObj_Reponse.open("HEAD", strUrl, true);
							}
							else
							{
								//Navigateur OK on lance l'open
								xmlHttpObj_Reponse.open("POST", strUrl, true);
								xmlHttpObj_Reponse.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
								xmlHttpObj_Reponse.send(strDataPost);
							}
						}
						
						xmlHttpObj_Reponse.onreadystatechange = function()
						{
							if (xmlHttpObj_Reponse.readyState == 4)
							{
								if ( (xmlHttpObj_Reponse.status == 200) || (xmlHttpObj_Reponse.status == 0) )
								{
									
									var strReponse = "null";
									try
									{
										if (strDataPost != STATUS_404)
										{
											if (strTypeReponse == 0)
											{
												strReponse = xmlHttpObj_Reponse.responseXML;
											}
											else if (strTypeReponse == 1)
											{
												strReponse = xmlHttpObj_Reponse.responseText;
											}
										}
										else
										{
											strReponse = "ok";
										}
									}
									catch(oErr)
									{
										strReponse = "Erreur - sendAjax = " + oErr;
									}
									
									
									echangeurReponse(casReponse, strReponse);
								}
								else if ( (xmlHttpObj_Reponse.status == 404) || (xmlHttpObj_Reponse.status == 2) )
								{
									//Il na pas trouvé la page
									strReponse = STATUS_404;
									echangeurReponse(casReponse, strReponse);
								}
								else
								{
									alert( xmlHttpObj_Reponse.status + "\n" + strUrl + "\n" + xmlHttpObj_Reponse.responseText );
									//Erreur d'un autre type
									strReponse = "Erreur - sendAjax Status is " + xmlHttpObj_Reponse.status;
									echangeurReponse(casReponse, strReponse);
								}
							}
						}
						if ( (strDataPost == "") || (strDataPost == STATUS_404) )  { xmlHttpObj_Reponse.send(null); }
					}
					else
					{
						strReponse = "Erreur - sendAjax = " + strMessageNavigateurOut;
						echangeurReponse(casReponse, strReponse);
					}
				}//fin sendAjax		
	}
	catch(err)
	{
		alert("Err - ajaxRequest = " + err);	
	}
}//fin ajaxRequest


function echangeurReponse(casReponse_, strReponse_)
{
	try
	{
		var strReponse = strReponse_;
		var casReponse = casReponse_;
		var strEval = casReponse + "(strReponse);";
		eval(strEval);
	}
	catch(err)
	{
		alert("Err - echangeurReponse = " + err);	
	}
}//fin echangeurReponse()

//Fonction qui recupere l'objet http ou renvoi false si le navigateur ne comprend pas
function getXmlHttp()
{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	{
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (E)
			{
				xmlhttp = false;
			}
		}
	}
	@else
	{
		xmlhttp = false;
	}
	@end @*/
	
	if (!xmlhttp && typeof XMLHttpRequest != "undefined")
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch (err)
		{
			xmlhttp = false;
		}
	}
	return xmlhttp;
}//fin getXmlHttp


/* 
20090101 - Rajout d'un convertisseur Mootools XML to XSL
 */
 
var TransformXMLtoXSL = new Class({
    
    initialize: function(strFnName)
    {
        this.strFnName = strFnName; //Important, le nom de l'Object créé pour pouvoir faire les binding
        
        this.strXMLfile = "";
        this.strXSLfile = "";
        
        this.oXmlReponse = null;
				this.oXslReponse = null;
        
        this.strHtmlOutput = "";
        
        this.fnPeriodicalListener;
    },
    		
		sendRequest: function(strXMLfile, strXSLfile, strHtmlOutput)
		{
        try
				{
	        this.strXMLfile = strXMLfile;
        	this.strXSLfile = strXSLfile;
        	
        	this.oXmlReponse = null;
					this.oXslReponse = null;
        	
        	this.strHtmlOutput = strHtmlOutput;
        	
        	this.fnPeriodicalListener = this.variableListener.periodical(800, this);
        	        	
	        var oDajax = new ajaxRequest(this.strXMLfile, this.strFnName+".outputXmlListener");
						oDajax.typeReponse = 0;
						oDajax.sendAjax();
					
					var oDajax = new ajaxRequest(this.strXSLfile, this.strFnName+".outputXslListener");
						oDajax.typeReponse = 0;
						oDajax.sendAjax();
				}
				catch(oErr)
				{
					alert(oErr);
				}
    },
    
    variableListener: function()
    {
    	try
			{
				if( (this.oXmlReponse != null) && (this.oXslReponse != null) )
				{
					var strHtmlTmp = this.convertXMLtoXSL(this.oXmlReponse, this.oXslReponse);
					this.setHtml(strHtmlTmp);
					
					this.fnPeriodicalListener = $clear(this.fnPeriodicalListener);
				}
			}
			catch(oErr)
			{
				
			}
    },
    
    outputXmlListener: function(strReponse)
    {
			try
			{
				this.oXmlReponse = strReponse;
			}
			catch(oErr)
			{
			
			}
    },
    
    outputXslListener: function(strReponse)
    {
    	try
			{
				this.oXslReponse = strReponse;
			}
			catch(oErr)
			{
			
			}
    },
    
    setHtml: function(strHtml)
		{
			try
			{
					$(this.strHtmlOutput).innerHTML = strHtml;
			}
			catch(oErr)
			{
				
			}	
		},
    
    convertXMLtoXSL: function(oXmlTmp, oXslTmp)
		{
			try
			{
				var oXml = oXmlTmp;
				var oXsl = oXslTmp;
				var myEldiv = new Element("div");
				var strHtmlOutput = "";
				
				var isXSLTProcessor = true; try{ new XSLTProcessor() }catch(oErr){ isXSLTProcessor = false; }
				
				if( isXSLTProcessor )
				{
					var xsltProcessor = new XSLTProcessor();
					xsltProcessor.importStylesheet(oXsl);
					var strHtml = xsltProcessor.transformToFragment(oXml, document);
					myEldiv.appendChild(strHtml);
					strHtmlOutput = myEldiv.innerHTML;
					myEldiv.destroy();
					
					return strHtmlOutput;
				}
				else
				{
					var strHtmlOutput = oXml.transformNode(oXsl);
					return strHtmlOutput;
				}
			}
			catch(oErr)
			{
				return "<p>Erreur : " + oErr + "</p>";
			}
		}//fin convertXMLtoXSL		        
});


