

/* -------------------------------- Funciones Ajax  ----------------------------------------------------------------
 ------------------------------------------------------------------------------------------------------------------ 
*/


function AjaxManager(){
	var enProceso = false; 		    // lo usamos para ver si hay un proceso activo
	var http = getHTTPObject();     // Créo el objeto XMLHttpRequest
	
	this.action				= '';		//url a ejecutar. Si es necesario, incluir sus parametros
	this.method				= 'GET';	
	this.requestString		= '';		//string con los parametros : "a=90&user=juan&x=algo", util para method='POST'
	this.isSinchronic		= false;
	this.callBackFunction	= null;
	this.errorFunction      = null;
	this.forceValidXML		= false;	//la respuesta deber ser un xml ó no			 
					 
	this.execCallBack = function(){
			if(this.action==''){
				alert('Debe especificar una URL');
				enProceso = false;
				return;
			}
			if(this.callBackFunction==''){
				alert('Debe especificar una funcion de callback');
				enProceso = false;
				return;
			}
			
			if (!enProceso && http) {
										
				http.open(this.method, this.action, this.isSinchronic);
				enProceso	= true;
				http.send(null); //Para metodo Post: http.send('a=15&b=25&c=33');
				
				try{
				    http.onreadystatechange = this.HttpStateChange();
				}
				catch(e){}
				
				
			}
			
	}				 
	
	
	
	
	this.HttpStateChange = function() {
	    //alert(http.readyState);
	    if (http.readyState == 4)
	    {
		    if (http.status == 200) { 
			    if (http.responseText.indexOf('invalid') == -1){
    												
				    if(this.forceValidXML)
					    this.callBackFunction(http.responseXML); 
				    else
					    this.callBackFunction(http.responseText);
    																		
				    enProceso = false;
			    }
		    }
		    else //hubo un error durante la ejecucion
		    {
		       if(this.errorFunction!=null) this.errorFunction(http.responseText,http.status);
		    }
	    }

    	
    }

	
	
	
	this.showProgress = function(oContainer,imgPath){
			oContainer.innerHTML = "<img src='" + imgPath + "' border='0' /> Processing";  
		}
	
	
	function getHTTPObject() {
		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 (e) { xmlhttp = false; }
		}
		
		return xmlhttp;
		
	}
	

}




/* --------------------------------------------------------------------------------------------------------------

	Ejemplo de uso: desde donde se quiera usar.
				var oAjax = new AjaxManager();
				oAjax.action			= url;		//url completa con parametros
				oAjax.method			= 'GET';	//por ahora no lo probe con Post
				oAjax.isSinchronic		= false; 	//asincronico ó no	
				oAjax.callBackFunction	= cargarComboDesdeXml;	//nombre de la funcion de callback
				oAjax.execCallBack();				//ejecuta funcion de callback con la respuesta
	
---------------------------------------------------------------------------------------------------------------- */	



