/* ======================================================
= AJAX
=======================================================*/

var __AJAX_DEBOGUE_  = false;


var tbAjaxLoad = new Array();
var xhr_object = null;
var xhr_idcourrant = 0;
var bAjaxModeTransfertAsynchrone = false;
var bAjaxIndicateur = false;
var sAjaxMethode = 'get';

function MTGSI_JS_Ajax( bAttente )
{
	tbAjaxLoad = new Array();
	xhr_object = null;
	xhr_idcourrant = 0;
	if(bAttente != 'undefined') bAjaxIndicateur = bAttente;
	else bAjaxIndicateur = false;
}//fin fonction

function MTGSI_JS_Ajax_FormulaireEncode( sObjetId )
{	
	return sObjetId+"=" + encodeURI( document.getElementById(sObjetId).value )
}//fin fonction

//--eval du code js associ?
function MTGSI_JS_Ajax_InterpreteJs(id)
{
	if(id!='')
	{
		var mesScripts = document.getElementById(id).getElementsByTagName("script");
		for (var i=0; i<mesScripts.length; i++) {
			
			s = document.createElement('script');	
					s.setAttribute('type', 'text/javascript');		
					s.text = mesScripts[i].innerHTML;
					h = document.getElementsByTagName('head')[0];
					h.appendChild(s);	
		}	
	}
}

function MTGSI_JS_Ajax_AjouterFormulaire(tbParameter)
{
	var data='';
	for (i=0;i<tbParameter.elements.length;i++) 
	{ 
		var sVal=(tbParameter.elements[i].tagName.toLowerCase()=="select")?MTGSI_JS_Ajax_AjouterFormulaireCombo(tbParameter.elements[i]):tbParameter.elements[i].value
		data+=escape(tbParameter.elements[i].name)+"="+escape(sVal)+"&";
	}
	return data.substr(0, data.length-1);
}

function MTGSI_JS_Ajax_AjouterFormulaireCombo( oCombo )
{
	var sVal="";
	for (var i=0; true; i++) 
	{
		if (oCombo.options[i]) 
		{
			if (oCombo.options[i].selected) 
			{
				sVal += oCombo.options[i].sVal + ",";
			}
		} 
		else return sVal.substr(0, sVal.length-1); 
	}
}



function MTGSI_JS_Ajax_Ajouter( sUrl, sDestination, tbParameter )
{
	if( __AJAX_DEBOGUE_ ) 
	{
		window.open( sUrl );
	}
	
	nIndice = tbAjaxLoad.length;
	tbAjaxLoad[ nIndice ] = new Array();
	tbAjaxLoad[ nIndice ][0] = sUrl;
	tbAjaxLoad[ nIndice ][1] = sDestination;
	if( tbParameter != '')
	{
		if( typeof(tbParameter) == 'object' )
		{
			tbParameter = MTGSI_JS_Ajax_AjouterFormulaire(tbParameter);				
		}
	}
	tbAjaxLoad[ nIndice ][2] = tbParameter;

}//fin fonction

function MTGSI_JS_Ajax_SendPostRequest( nId )
{
	if( nId < tbAjaxLoad.length )
	{
		if( bAjaxIndicateur)
		{
			jsPopupInfo(true);
			jsPopupInfoAfficher();
		}
		
		MTGSI_JS_Ajax_Init();

		// Pour tous mes objets en attente
		xhr_url = tbAjaxLoad[nId][0];
		xhr_destination = tbAjaxLoad[nId][1];
		if(( xhr_destination != '' ) && (typeof(document.getElementById( xhr_destination )) != 'undefined' ))
		{
			document.getElementById( xhr_destination ).innerHTML = 'Chargement en cours'; 
		}
		
		// On ouvre la requete vers la page d?sir?e
		bAjaxModeTransfertAsynchrone = true;
		// Si des paramétre alors POST
		if( tbAjaxLoad[nId][2] != '' ) xhr_object.open( 'POST', xhr_url, bAjaxModeTransfertAsynchrone );					
		// else GET
		else xhr_object.open( 'GET', xhr_url, bAjaxModeTransfertAsynchrone );
		// On lui donne le pointeur de fonction de retour
		xhr_idcourrant = nId;
		xhr_object.onreadystatechange = MTGSI_JS_Ajax_CallBackRequest;
		xhr_object.setRequestHeader("Content-type",   "application/x-www-form-urlencoded");
		xhr_object.setRequestHeader("Content-length", tbAjaxLoad[nId][2].length);

		// On envoi la requete
		xhr_object.send( tbAjaxLoad[nId][2] );	
		if ( xhr_object.readyState == 4 ) 
		{
			MTGSI_JS_Ajax_CallBackRequest();
		}		
	}
}//fin fonction

function MTGSI_JS_Ajax_SendGetRequest( nId ) 
{
	if( nId < tbAjaxLoad.length )
	{
		MTGSI_JS_Ajax_Init();
		
		// Pour tous mes objets en attente
		xhr_url = tbAjaxLoad[nId][0];
		
		
		// On ouvre la requete vers la page d?sir?e
		xhr_object.open( 'GET', xhr_url, false );
		// On lui donne le pointeur de fonction de retour
		xhr_idcourrant = nId;
		xhr_object.onreadystatechange = MTGSI_JS_Ajax_CallBackRequest;
		//xhr_object.setRequestHeader ("Content-type",   "application/x-www-form-urlencoded");
		
		// On envoi la requete
		xhr_object.send( null );
		
	}
}//fin fonction

function MTGSI_JS_Ajax_Init()
{
	if ( window.XMLHttpRequest )
	{
		// Objet XmlHttpRequest pour les moteurs GECKO
		xhr_object = new XMLHttpRequest();
	}
	else if ( window.ActiveXObject )
	{
		// Objet XmlHttpRequest pour Internet Explorer
		xhr_object = new ActiveXObject( 'Microsoft.XMLHTTP' );
	}
	else
	{
		// Navigateur non-compatible
		alert( 'Votre navigateur ne supporte pas les objets XMLHTTPRequest...' );
		return;
	}
}//fin fonction

function MTGSI_JS_Ajax_CallBackRequest() 
{
	//parent.document.getElementById('oDebogue').value += xhr_object.readyState;
	// Sur le retour de la requete, on teste son status
	if ( xhr_object.readyState == 4 ) 
	{				
		var js='';
		var html = '';
		
		// --- on recup?re le html		
		oXmlReponse = xhr_object.responseXML.getElementsByTagName("html");
		if( oXmlReponse.length == 1 ) 
		{
			if( oXmlReponse.item(0).firstChild != null )
			{
				html = oXmlReponse.item(0).firstChild.data;
			}			
		}		
				
		// --- on recup?re le xml
		oXmlReponse = xhr_object.responseXML.getElementsByTagName("xml");
		if( oXmlReponse.length == 1 ) 
		{
				oTemp = oXmlReponse[0];
				for( i=0;i<oTemp.childNodes.length;i++)
				{

					sNomObjet = oTemp.childNodes[i].nodeName;
					sValObjet = oTemp.childNodes[i].firstChild.nodeValue;
														
					if( document.getElementById( sNomObjet ))
					{						
						
						oObjetForm = document.getElementById( sNomObjet );

						// Traiter en fonction du type
						if( oObjetForm.type == 'select-one' )
						{														
							if(sValObjet.substr(0,6)=="Array(")
							{
								var toto = eval(sValObjet);
								var nbElem = document.getElementById( sNomObjet ).options.length; 
								 
								//-- vide combo ou liste
								for(var iter=(nbElem-1) ;iter>=0; iter--)
								{
									
									//document.getElementById( sNomObjet ).options.remove(iter);
									document.getElementById( sNomObjet ).options[iter]=null;
								}
								
								//-- vide liste dest
								if(sNomObjet.substr(0,7)=='SOURCE_')
								{
									var nom = sNomObjet.substr(7,sNomObjet.length);
									nom =  'SELECTION_'+nom;
									var nbElem = document.getElementById( nom ).options.length;
									for(var iter=(nbElem-1) ;iter>=0; iter--)
									{
										document.getElementById( nom ).options[iter]=null;
									}
								}
								
								//--- si combo normale ajouter "faite selection"
								var z=0;
								
								document.getElementById( sNomObjet).options[ 0 ] = new Option( '[[Faite une selection]]', '', true, true );
								z=1;
								
								//-- rempli la combo
								var selectId = 0;
								for(var k=0;k<toto.length;k++)
								{
									document.getElementById( sNomObjet).options[ z ] = new Option( toto[k][1], toto[k][0], true, true );
									if(toto[k][2]=='selected') selectId=k+1;
									z++;
								}
								document.getElementById( sNomObjet ).selectedIndex = selectId;
								
							}
							else
							{
								for(j=0;j<oObjetForm.options.length;j++)
								{
									
									if( oObjetForm.options[j].value == sValObjet )
									{
										oObjetForm.options[j].selected = true;
										break;
									}
								}	
							}	
						}
						else if(( oObjetForm.tagName == 'DIV') || ( oObjetForm.tagName == 'MARQUEE') || ( oObjetForm.tagName == 'TD')) 
						{
							
							oObjetForm.innerHTML = sValObjet;
							
						}
						else
						{							
							oObjetForm.value = sValObjet;
						}
						
						MTGSI_JS_Ajax_InterpreteJs(sNomObjet);
					}
				
				}
		
		}		
		// --- on recup?re le js
		oXmlReponse = xhr_object.responseXML.getElementsByTagName("js");
		
		if( oXmlReponse.length == 1 ) 
		{	
			
			if( oXmlReponse.item(0).firstChild != null )
			{
				
				js = oXmlReponse.item(0).firstChild.data;
			}
		}
		
		if( JS_COMPLEMENT_trimAll(js) != '' ) 
		{			
			s = document.createElement('script');	
				s.setAttribute('type', 'text/javascript');		
				s.text = js;
				h = document.getElementsByTagName('head')[0];
				h.appendChild(s);				
		}
		
		if( JS_COMPLEMENT_trimAll(html) != '' ) 
		{
			document.getElementById( tbAjaxLoad[ xhr_idcourrant ][1] ).innerHTML = '';
			document.getElementById( tbAjaxLoad[ xhr_idcourrant ][1] ).innerHTML = html+document.getElementById( tbAjaxLoad[ xhr_idcourrant ][1] ).innerHTML;
		}
	
		MTGSI_JS_Ajax_InterpreteJs(tbAjaxLoad[ xhr_idcourrant ][1]);
		
		// Appel SendRequest pour les autres chargements;
		MTGSI_JS_Ajax_SendGetRequest( xhr_idcourrant+1 );
		
		if( bAjaxIndicateur)
		{		
			jsPopupInfoEffacer();
		}	
	}
	else
	{
		/*if( document.getElementById( tbAjaxLoad[ xhr_idcourrant ][1] ) != 'undefined')

			document.getElementById( tbAjaxLoad[ xhr_idcourrant ][1] ).innerHTML = document.getElementById( tbAjaxLoad[ xhr_idcourrant ][1] ).innerHTML + '...';
		}*/

	}
	
}//fin fonction


function JS_COMPLEMENT_trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}//fin fonction

