/**
@author :  Victor Santana
@Version : 1.0 16-05-2008 1.0
*/
function TTajax2()
{
  //-- private
  var servidor = null;
  
  //-- public
  this.msg	  = "<img src='../../images/ajax_loading.gif' alt='Cargando...' title='Cargando'>";
  this.server = "../../server_s.php";
  this.error  = "-ERROR!-";
  
  
  //-- private method
  var getObjeto = function(){
  	try {  xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP");   }
  	catch(e) {
		try  {	xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");  }
	  	catch(E) {
			xmlhttp = false;
	   	}
  	}
  	if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
		xmlhttp = new XMLHttpRequest();
  	}
 	return xmlhttp;
  }
  
  var queryString = function(destino,variables,aux)
  {
	var query = "destino="+destino;
	if(variables){
		for(i=0 ; i<variables.length; i++){
			query += "&"+variables[i]+"="+encodeURIComponent(document.getElementById(variables[i]).value); 
		}	
	}
	return query+aux;
  }
  
  var pintaXML = function(oDocumento)
  {
  	if(!oDocumento) return false;
  	var nodos = oDocumento.getElementsByTagName("nodo");
	for(var i=0; i<nodos.length; i++) {
  		var nodo = nodos[i];
  		switch(nodo.getElementsByTagName("accion")[0].firstChild.nodeValue){
  			case "script":
  				var script = document.createElement('script');
				script.setAttribute('type','text/javascript');
				script.text = nodo.getElementsByTagName("content")[0].firstChild.nodeValue;
				document.getElementsByTagName('head').item(0).appendChild(script);
  			break;
  			default:
  				var capa = nodo.getElementsByTagName("id")[0].firstChild.nodeValue;
  				document.getElementById(capa).innerHTML = nodo.getElementsByTagName("content")[0].firstChild.nodeValue;
  			break;
  		}
	}
  	return true;
  }
  
  //-- public method
  this.Carga = function(destino,variables,aux){
	var error = this.error;
  	var servidor = getObjeto();
	servidor.open("GET",this.server+"?"+queryString(destino,variables,aux), true);
   	document.getElementById(destino).innerHTML=this.msg;
   	servidor.onreadystatechange = function(){
  		if (servidor.readyState == 4){
			if(servidor.status == 200) document.getElementById(destino).innerHTML = servidor.responseText;
			else document.getElementById(destino).innerHTML = error;
  		}
	};
   	servidor.send(null); 
   	return;	
  }
  
  this.cargaPOST = function(destino,variables,aux){
  	var error = this.error;
  	var servidor = getObjeto();	 
  	
   	servidor.open('POST',this.server,true); 
	servidor.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   	document.getElementById(destino).innerHTML=this.msg;
   	servidor.onreadystatechange = function(){
	  	if (servidor.readyState == 4){
			if(servidor.status == 200) document.getElementById(destino).innerHTML = servidor.responseText;
			else document.getElementById(destino).innerHTML = error;
		}  	
 	};
	servidor.send(queryString(destino,variables,aux));
	return;
  }
  
  this.cargaXML = function(destino,variables,aux){
  	var error = this.error; 
  	var servidor = getObjeto();	 
  	servidor.open('POST',this.server,true);
	servidor.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   	document.getElementById(destino).innerHTML=this.msg;
   	servidor.onreadystatechange = function(){
	  	if (servidor.readyState == 4){
			if(servidor.status == 200){
				var oDocumento = servidor.responseXML;
				if(!pintaXML(oDocumento)) document.getElementById(destino).innerHTML = error;
			}else document.getElementById(destino).innerHTML = error;
		}  	
 	};
	servidor.send(queryString(destino,variables,aux));
	return;
  }
	  
}
