/**
* @author:  Victor Caballero
* @since:   16-05-2008
* @version: 1.0
*/
function TTajax()
{
  //-- private
  var servidor = null;
  
  //-- public
  this.msg	  = "<img src='/images/comun/ico_loading.gif' alt='cargando' title='cargando' height='19'>";
  this.server = "server_file.php";
  this.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(metodo,destino,variables,aux){
	var error = this.error;
  	var servidor = getObjeto();
	switch(metodo){
		case 'get': 	servidor.open("GET",this.server+"?"+queryString(destino,variables,aux), true);	
						break;
		case 'post':
		case 'xml':		servidor.open('POST',this.server,true); 
						servidor.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
						break;	
	}
   	document.getElementById(destino).innerHTML=this.msg;
   	servidor.onreadystatechange = function(){
  		if (servidor.readyState == 4){
			if(servidor.status == 200){
				switch(metodo){
					case 'get':	
					case 'post':	document.getElementById(destino).innerHTML = servidor.responseText;	
									break;
					case 'xml':
									var oDocumento = servidor.responseXML;
									if(!pintaXML(oDocumento)) document.getElementById(destino).innerHTML = error;
									break;
				}
			}else document.getElementById(destino).innerHTML = error;
  		}
	};
	var cad = (metodo=='get') ? null : queryString(destino,variables,aux);
   	servidor.send(cad); 
   	return;	
  }
  
}