/**
 *	This page has all the necessary to use AJAX method easily
 *	
 *	First of all, the XmlHttpRequestObject Object is used to call server and retrieve result by callback function.
 *
 *	The function XmlHttpRequestCall() is the method which must be used in the client page
 *		It is used to make a XmlHttpRequestObject and refresh a HTML part in the page
 *	
 *	The function xmlHasFirstNodeNamed() is usefull for the callBackFunction, it tests if the Xml tag in parameter exists or if the Xml is not the one expected. 
 *
 *	How to use all these methods :
 *		
 *		client.html
 *			<html><body>
 *			<script language="javascript">
 *			//-- refreshFoo() is use to read the response from the server
 *			function refreshFoo(response,divId){
 *				var xml = response.responseXML;
 *				if( ! xmlHasFirstNodeNamed(xml,"FOO") ){	return;	}	// check if the Xml tag <FOO> is the first Node
 *			
 *				var FOO = xml.getElementsByTagName("FOO");
 *				....
 *				or 
 *				document.getElementById(divId).innerHTML = response.responseText;
 *			}
 *			//-- Call server and add the callBackFunction() to refresh with the timer
 *			XmlHttpRequestCall("server.php?id=12",5000,refreshFoo,"foo");
 *			</script>
 *
 *			<div id="foo" ></div>
 *
 *			</body></html>
 *		
 *
 *
 */




/**
 *	Object XmlHttpRequestObject is used to send Request to server by Javascript.
 *		call(url,function) is used to call the server and call function(response) to get and display data from server (XML or simple HTML)
 *	TODO: add an example
 */
function XmlHttpRequestObject()
{
	//var r, initialize the attempted request object, if r is not initialized it is global so it wont works with multiple call xml request !!! (3h de recherche grrr!)
	var r;	
	
	// branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			r = new XMLHttpRequest();
        } catch(e) {
			r = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	r = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		r = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		r = false;
        	}
		}
    }
	if (!r){
		return null;
	}
	this.call = function(serverUrl, callBackFunction, divId)
	{
    	if (!r){
    		return false;
    	}
       	r.open("GET", serverUrl, true);

		r.onreadystatechange = function(){
			//alert(r.readyState+" && "+r.status );
        	//if (r.readyState == 4 && r.status == 200)
			if (r.readyState == 4 )
        	{
	       		callBackFunction(r, divId);
        	}
        };
      	r.send(null);
    	return true;
  	};
 	return this;
}

/**
 *	Load and set refresh time to call server
 */
function XmlHttpRequestCall( serverUrl, timeOut, callBackMethod, divId){
	//--- Chargement initial
	var Request = new XmlHttpRequestObject();
	Request.call(serverUrl, callBackMethod, divId);// cette call ira qppeler les methods de processor
	//--- Rappel de l'object après dépassement du timer
	if(timeOut > 0){		
		setTimeout("XmlHttpRequestCall(\""+serverUrl+"\",\""+timeOut+"\","+callBackMethod+")",timeOut);
	}
}

/**
 *	Check if the XML is the one expected by testing if the first tag is right
 */
function xmlHasFirstNodeNamed(xml,tagName){
	if( xml && xml.hasChildNodes() ){
		if( xml.documentElement.nodeName == tagName ){	
			return true;
		}
		
	}
	return false;
}

/**
 *	Returns response from server
 */	
function getXmlHttpServerResponse(response){
	return response.responseText;	
}

