/**
*
*
* class Ajax
*
* @author		Bruno Rovito
* @copyright 	2008
*																				   
	
	** @NOTE: 
		le nom de la fonction constructor ne peut pas être "Ajax" parceque ça cause une erreur avec la librarie MooTools (qui est utilisée par la console AV)

* sample usage:
	function handleAjaxResponse(pIdInnerHtml, pJsonObject){
		if (pJsonObject == null){ // there was a problem with the json object (see Ajax2008.js)
			document.getElementById(pIdInnerHtml).innerHTML = "Désolé, la requête ne peut être traitée."
		}else{
			document.getElementById(pIdInnerHtml).innerHTML = pJsonObject.msg;	
		}	
	}; // handleAjaxResponse()
	new Ajax().getData("divFeedback", "_InProg_Ajax_PageCalledTest.asp", 2, handleAjaxResponse);
*
**/

function Ajax2008(){
	
	/**
	* Vars
	*/
	this.RETURN_TYPE = new Array("text","javascript","json");
	
	/**
	* constructor() = creates the Ajax object
	*/
	this.constructor = function(){ 
		var tmp = "";
		var e, ee, eee;
		try {
			tmp =  new ActiveXObject('Msxml2.XMLHTTP');
		} catch(e) {
			try {
				tmp =  new ActiveXObject('Microsoft.XMLHTTP');
			} catch(ee) {
				try {
					tmp =  new XMLHttpRequest();
					if (tmp.overrideMimeType) {
                		tmp.overrideMimeType('text/xml'); 
            		}
				} catch(eee) {
					tmp =  false;
				}
			}
		}
		return tmp;
	};// this.constructor
	
	
	
	
	
	
	/**
	*
	* API
	*
	* getData() 	= returns responseText of ajax object according to what type of data we are expecting (see: 																			                      this.RETURN_TYPE)
	* 
	*
	* @param 		pIdInnerHtml 	= the id of the div(or span,or td, etc) that will hold the data returned by the ajax 			                                      object's responseText. The param should be set to null if not needed
	* @param		pUrlToCall		= the page called by the ajax/HttpRequest object
	* @param		pReturnType 	= one of the return types availbale in the array this.RETURN_TYPE (see above)
	* @param		pOutputFunction	= the name of a function, passed as a reference that will handle the contents of the                                      ajax object's responseText. The param is optional. The param should be set to null if not needed
	*
	*
	**/
	this.getData = function(pIdInnerHtml/*string*/, pUrlToCall/*string*/, pReturnType/*int*/,pOutputFunction /*référence*/){
		
		
		pOutputFunction = (typeof(pOutputFunction)=="string") ? eval(pOutputFunction) : pOutputFunction; // forcer la "référence"
		
		var _isASynch = ( (navigator.userAgent.indexOf("Macintosh")!=-1) && (navigator.userAgent.indexOf("Safari")!=-1) ) ? false : true;		
		var data = null;
		var oAjax = this.constructor();
		var oThis = this;
		var _q =  (pUrlToCall.indexOf("?")== -1) ? "?" : "&" ;
		var _sNoCacheUrl = pUrlToCall + _q + Math.random()+new Date();
		oAjax.open("GET", _sNoCacheUrl, _isASynch); 
		oAjax.onreadystatechange = function() {
			if (oAjax.readyState == 4) { // loading complete
				if (oAjax.status == 200) { // server response: 200  means "file found/all kosher"// only works when viewing via a server, use 0 when testing without
					 //alert("ok");
					 if (oThis.RETURN_TYPE[pReturnType]!=undefined && oThis.RETURN_TYPE[pReturnType]!=null) {
						
						if (oThis.RETURN_TYPE[pReturnType]=="text"){ 
							if ( pIdInnerHtml!=null ){
								
								document.getElementById(pIdInnerHtml).innerHTML = oAjax.responseText;
								
								/*// parse javascript
									NOTE: javascript to evaluate MUST return a string
								//*/ 
								var _aJsDivs = oThis.getElementsByClassName('javascript',"DIV",document.getElementById(pIdInnerHtml));
								for(var i=0;i<_aJsDivs.length;i++){
									_aJsDivs[i].innerHTML = eval(_aJsDivs[i].innerHTML);
								}
							}
							if ( pOutputFunction!=null ){
								pOutputFunction(oAjax.responseText);
							}
							
						}else if (oThis.RETURN_TYPE[pReturnType]=="javascript"){ 
						
							eval(oAjax.responseText); 
							if ( pOutputFunction!=null ){
								pOutputFunction(true);
							}
							
						}else if (oThis.RETURN_TYPE[pReturnType]=="json"){
							if ( pOutputFunction!=null ){
								pOutputFunction( JSON2005.parse(oAjax.responseText) ); 
							}
						}
					 }else{
						alert("Erreur: objet Ajax"); 
					 }
				}else{
					//alert("erreur");
					if (pIdInnerHtml!=null){
						document.getElementById(pIdInnerHtml).innerHTML  = "Erreur: Requête AJAX ne peut être traitée.";
					}
				}
			}else if (oAjax.readyState == 1){ // loading
				 //alert("Téléchargement en cours...");
				 if (pIdInnerHtml!=null){
					document.getElementById(pIdInnerHtml).innerHTML  = "T&eacute;l&eacute;chargement en cours...."; 
				 }
			}
		};
		oAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		oAjax.send(data);
	};// this.getData()
	
	
	
	
	
	
	/**
	*
	* API
	*
	* getElementsByClassName() 	
	* 
	*
	* @param 		className 	= name of class we want to find  (usually class="javascript")
	* @param		tag		= the HTML element (tag) that we want to search in (usually "DIV")
	* @param		elm 	= staring element within which we want to search for @param tag
	*
	*
	**/
	this.getElementsByClassName = function (className, tag, elm){ 
		var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
		var tag = tag || "*";
		var elm = elm || document;
		var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
		var returnElements = [];
		var current;
		var length = elements.length;
		for(var i=0; i<length; i++){
			current = elements[i];
			if(testClass.test(current.className)){
				returnElements.push(current);
			}
		}
		return returnElements;
	};//this.getElementsByClassName
	
	
	
	
	
	
}; // Ajax()


