/**
 * @author Israel "Judas" Díaz 2007
**/

AjaxObject = function(URL, format, method){
	//declaración de variables
	var root = this;
	var ajaxRef = false;
	this.response = "";
	this.urlRef = URL;
	this.params = "";
	this.method = ((method != "" && method != undefined && method != null) && (method.toUpperCase() == "GET" || method.toUpperCase() == "POST")) ? method : "GET";
	this.format = ((format != "" && format != undefined && format != null) && (format.toLowerCase() == "text" || format.toLowerCase() == "xml")) ? format : "text";
	
	//comprobamos el tipo de navegador para definir el tipo de objecto Ajax
	if (window.XMLHttpRequest){
		ajaxRef = new XMLHttpRequest();
	} else if (window.ActiveXObject){
		try {
			ajaxRef = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
			try{
				ajaxRef = new ActiveXObject("Microsoft.XMLHTTP")
			} catch (e){}
		   }
	} else {
		return false;
	}

	
	//método que envía y recibe los datos
	this.sendAndLoad = function (){
		 //abrimos la conexión
		 ajaxRef.open(root.method, root.urlRef);
		 //verificamos los diferentes estados de recepción de datos
		 ajaxRef.onreadystatechange = function() {
			switch(ajaxRef.readyState){
				// 0 El objeto no se ha inicializado
				// 1 Cargando información
				// 2 Información cargada
				// 3 Información disponible
				// 4 Objecto e Información inicializados
 				case 0:
				case 1:  
				case 2:  
  					loadStatus(ajaxRef.readyState);
  					break;
				case 3:  
  					//loadStatus(ajaxRef.readyState);
  					break;
				case 4: 
					loadCompleted();
					break;
				default:
					break;
			}
		};
		ajaxRef.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
		root.onLoadInit();
	    ajaxRef.send(root.params);
	}
	
	//-----------------------------//
	function loadCompleted(){
		//escribimos el mensaje de respuesta...
		root.response = (root.format == "text") ? ajaxRef.responseText : ajaxRef.responseXML;
		//ejecutamos el método onLoad
		root.onLoad();
	};
	function loadStatus(statusNum){
		root.onLoadProgress(statusNum);
	};
	
	this.onLoadInit = function(){};
	this.onLoad = function(){};
	this.onLoadProgress = function(statusNum){};
	
}
