/**
*
*
* class Ajax
*
* @author		Bruno Rovito
* @copyright 	2007-06-05
*
* @requirements	JSON.js
* sample usage:
	function handleAjaxResponse(pIdInnerHtml, pJsonObject){
		if (pJsonObject == null){ // there was a problem with the json object (see Ajax.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 Ajax(){
	
	/**
	* 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. If it is not needed/wanted the param should be set to null
	* @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 string, that will handle the contents of the                                      ajax object's responseText. The param is optional. In some cases, an output function is                                      not needed/wanted (i.e. if this.RETURN_TYPE[pReturntype] = "javascript" ), and so the                                      param should be set to null
	*
	*
	**/
	this.getData = function(pIdInnerHtml/*string*/, pUrlToCall/*string*/, pReturnType/*int*/,pOutputFunction /*string*/){
		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) && (pOutputFunction==null) ){
								document.getElementById(pIdInnerHtml).innerHTML = oAjax.responseText;
							}else if ( (pIdInnerHtml==null) && (pOutputFunction!=null) ){
								// be careful with quotation marks within oAjax.responseText !
								eval(pOutputFunction + "(\"" + oAjax.responseText + "\")");
							}else if ( (pIdInnerHtml!=null) && (pOutputFunction!=null) ){
								// be careful with quotation marks within oAjax.responseText !
								eval(pOutputFunction + "('" + pIdInnerHtml + "',\"" + oAjax.responseText+ "\")");
							}else{
								return false;
							}
						}else if (oThis.RETURN_TYPE[pReturnType]=="javascript"){ 
							if ( (pIdInnerHtml!=null) && (pOutputFunction==null) ) {
								document.getElementById(pIdInnerHtml).innerHTML = "";
								eval(oAjax.responseText); 
							}else if ( (pIdInnerHtml==null) && (pOutputFunction!=null) ){
								// not available
							}else if ( (pIdInnerHtml!=null) && (pOutputFunction!=null) ){
								// not available
							}else{
								eval(oAjax.responseText); 
							}
						}else if (oThis.RETURN_TYPE[pReturnType]=="json"){
							var _new = oAjax.responseText.replace(/[*\n\r]*/g,"");
							//var _new2 = _new.replace(/[*"]*/g,"'");
							//alert(_new)
							//var _new = oAjax.responseText;
							if ( (pIdInnerHtml!=null) && (pOutputFunction==null) ){
								var data = JSON.parse(_new);
								document.getElementById(pIdInnerHtml).innerHTML = data;
							}else if ( (pIdInnerHtml==null) && (pOutputFunction!=null) ) {
								if (!JSON.parse(_new)){ // there is a  json syntax error
									eval(pOutputFunction + "(null)");
								}else{ // we have a json object !
									eval(pOutputFunction + "(" + _new + ")");
								}
							}else if ( (pIdInnerHtml!=null) && (pOutputFunction!=null) ){
								if (!JSON.parse(_new)){ // there is a  json syntax error
									eval(pOutputFunction + "('"+pIdInnerHtml+"',null)");
								}else{ // we have a json object !
									eval(pOutputFunction + "('" + pIdInnerHtml + "'," + _new +")");
								}
							}else{
								return false;
							}
						}
					 }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()
	
	
	
	
	/**
	* LEGACY  
	*
	* 20-11-2007
	*
	*
	* functions that were once in /lib/v1/js/AJAX.js
	* it is preferable to NOT use these functions anymore
	*
	*/
		this.getValue = function(strObjForReturn, strUrl, data) {
			if (data == undefined){
				data = null;
			}
			this.connect(strObjForReturn, strUrl, data);
		};
		this.connect = function(strObjForReturn, strUrl, data) {
			var objAjax;
			objAjax = this.getTransport();
			objAjax.open("GET", strUrl, true); 
			objAjax.onreadystatechange = function() {
				if (objAjax.readyState == 4) {
					if (strObjForReturn != null) {
						document.getElementById(strObjForReturn).innerHTML = objAjax.responseText;
					}
				}
			};
			objAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			objAjax.send(data);
		};
		this.getTransport = function() {
			var e, ee, eee;
			try {
				return new ActiveXObject('Msxml2.XMLHTTP');
			} catch(e) {
				try {
					return new ActiveXObject('Microsoft.XMLHTTP');
				} catch(ee) {
					try {
						return new XMLHttpRequest();
					} catch(eee) {
						return false;
					}
				}
			}
		};
	/**
	*  / LEGACY 
	*/
	
	
	
}; // Ajax()


