/**
* Project:     LNSRMT: mini aplicación AJAX
* File:        lnsrmt.js
*
* @link http://www.labint.com/
* @copyright 2006-2007 Labint Network Solutions.
* @author Carlos Reburdo <carlos@labint.com>
* @package LNSclass
* @version 1.0.0
*/

/*
Funciones remotas
*/
function LRMT_inicializa() {
var xx=null;
 try {
  xx = new XMLHttpRequest();
 } catch (e) {
  xx=null;
  var msxmlhttp = new Array('Msxml2.XMLHTTP.5.0',
                      'Msxml2.XMLHTTP.4.0',
                      'Msxml2.XMLHTTP.3.0',
                      'Msxml2.XMLHTTP',
                      'Microsoft.XMLHTTP');
  var success = false;
  for (var i=0; i<msxmlhttp.length && !success;i++) {
   try {
    xx = new ActiveXObject(msxmlhttp[i]);
    success = true;
   } catch (e) { xx=null; }
  }
 }
 return xx;
}

function LNSR_ejecutar(okfun,errorfun,jsfun,lay,metodo,uri,funcion, args) {
 var i, n;
 var post_data;
 var retorno=true;
 LRMT_ResultadoError='';
 LRMT_ResultadoHttp='';
 LRMT_ResultadoJS='';
 LNSR_layer=lay;
 LNSR_funcionok=okfun;
 LNSR_funcionjs=jsfun;
 LNSR_funcionerror=errorfun;
 if (typeof(metodo) == "undefined" || metodo== "") 	metodo = "GET";
 if (metodo == "GET") {
  if (uri.indexOf("?") == -1) uri += "?fn=" + escape(funcion);
  else uri += "&fn=" + escape(funcion);
  uri += "&fnrnd=" + new Date().getTime();
  for (i = 0; i < args.length-1; i+=2) uri += "&" + args[i] + "=" + escape(args[i+1]);
  post_data = null;
 } else if (metodo == "POST") {
  post_data = "fn=" + escape(funcion);
  post_data += "&fnrnd=" + new Date().getTime();
  for (i = 0; i < args.length-1; i+=2) {
		post_data += "&" + args[i] + "=" + escape(args[i+1]);
	}
 } else {
  alert(LRMTmsg[1]+ ": " + LRMT_tipopeticion);
  LNSR_funcionerror();
  return false;
 }
 if (LRMT_conector == null) {
  alert(LRMTmsg[2]+'....');
  LNSR_funcionerror();
  return false;
 } else {
	bComplete = false;
  LRMT_conector.open(metodo, uri, true);
  if (metodo == "POST") {
   LRMT_conector.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
   LRMT_conector.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
   LRMT_conector.setRequestHeader("Content-length", post_data.length);
	 LRMT_conector.setRequestHeader("Connection", "close");
  }
  LRMT_conector.onreadystatechange = function(){
   if (LRMT_conector.readyState == 4 && !bComplete) {
     bComplete = true
     LNSR_ejecutar2(LRMT_conector)
   }
  };
  LRMT_conector.send(post_data);
  return retorno;
 }
}

function LNSR_ejecutar2(a) {
		var oFunc;
		var reto = false;
		try {
			switch(a.status) {
				case 200:
					LRMT_ResultadoHttp=LRMT_conector.responseText
					var i=LRMT_limpiaresultado();
					switch(i) {
						case 0:
							alert(LRMT_ResultadoError);
							break;
						case 1:
							oFunc = this[LNSR_funcionok];
							oFunc();
							reto = true;
							break;
						case 2:
							alert(LRMT_ResultadoError);
							break;
						case 3:
							oFunc = this[LNSR_funcionjs];
							oFunc();
							reto = true;
							break;
						case 4:
							document.getElementById(LNSR_layer).innerHTML=LRMT_ResultadoHttp;
							oFunc = this[LNSR_funcionok];
							oFunc();
							reto = true;
							break;
					}
					break;
				case 400:
					alert(LRMTmsg[3]+': '+a.status+' '+LRMTmsg[4]);
					break;
				case 401:
					alert(LRMTmsg[3]+': '+a.status+' '+LRMTmsg[5]);
					break;
				case 403:
					alert(LRMTmsg[3]+': '+a.status+' '+LRMTmsg[6]);
					break;
				case 404:
					alert(LRMTmsg[3]+': '+a.status+' '+LRMTmsg[7]);
					break;
				default:
					alert(LRMTmsg[3]+': '+a.status+' '+LRMTmsg[8]);
					break;
			}
		} catch (e) {
  		alert(e);
  		alert(LRMTmsg[9])
		}
	if (!reto) {
		oFunc = this[LNSR_funcionerror];
		oFunc();
	}
	return reto;
}

function LRMT_limpiaresultado() {
 var z=LRMT_ResultadoHttp;
 var ini="";
 var fin="";
 var datos="";
 var largo=LRMT_cmd[0].length;
 var r=0;
 if (z.length>largo) {
  ini=z.substring(0, largo);
  fin=z.substring(z.length-largo);
  datos=z.substring(largo,z.length-largo);
 } else {
  LRMT_ResultadoError=LRMTmsg[0]+'\n'+LRMT_ResultadoHttp;
 }
 if ((ini==LRMT_cmd[0]) && (fin==LRMT_cmd[1])) {
  LRMT_ResultadoHttp= datos;
  r=1;
 } else {
  if ((ini==LRMT_cmd[2]) && (fin==LRMT_cmd[3])) {
   LRMT_ResultadoError= datos;
   r=2;
  } else {
   if ((ini==LRMT_cmd[4]) && (fin==LRMT_cmd[5])) {
    LRMT_ResultadoJS= datos;
    r=3;
   } else {
    if ((ini==LRMT_cmd[6]) && (fin==LRMT_cmd[7])) {
     LRMT_ResultadoHttp= datos;
     r=4;
    } else {
     LRMT_ResultadoError=LRMTmsg[0]+'\n'+LRMT_ResultadoHttp;
    }
   }
  }
 }
 return r;
}

var LRMT_ejecutor = '/rmt.php';
var LRMT_conector = LRMT_inicializa();
var LRMT_ResultadoHttp="";
var LRMT_ResultadoError="";
var LRMT_ResultadoJS="";
var LNSR_layer='';
var LNSR_funcion='';
var LNSR_funcionok='';
var LNSR_funcionerror='';
var LNSR_funcionjs='';
var LRMT_cmd = new Array('#LNSIRMTDaToS#','#LNSFRMTDaToS#',
'#LNSIRMTErroR#','#LNSFRMTErroR#',
'#LNSIRMTDaTJS#','#LNSFRMTDaTJS#',
'#LNSIRMTDaTDI#','#LNSFRMTDaTDI#');


function LNSajax(EFunctionOk, EFunctionError, EFunctionJs, Layer, url, postMethod) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(funcion, args) {
    if (that.updating) { return false; }
    that.AJAX = null;
    var reto=-1;
    var errormsg='';
    var ejec=0;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();            
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      alert(LRMTmsg[2]+'....');
      return false;                            
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {
	        var error=false;
		      reto=0;
	        try {
		        switch(that.AJAX.status) {
			        case 200:
			        	reto=1;
			        	break;
							case 400:
								txtResultadoError=LRMTmsg[3]+': '+that.AJAX.status+' '+LRMTmsg[4];
								break;
							case 401:
								txtResultadoError=LRMTmsg[3]+': '+that.AJAX.status+' '+LRMTmsg[5];
								break;
							case 403:
								txtResultadoError=LRMTmsg[3]+': '+that.AJAX.status+' '+LRMTmsg[6];
								break;
							case 404:
								txtResultadoError=LRMTmsg[3]+': '+that.AJAX.status+' '+LRMTmsg[7];
								break;
							default:
								txtResultadoError=LRMTmsg[3]+': '+that.AJAX.status+' '+LRMTmsg[8];
								break;
		        }
		      } catch (e) {
			      errormsg=e+'\n'+LRMTmsg[9];
		      }
		      if (reto==1) {
			      var i=that.limpiaresultado(that.AJAX.responseText);
			      switch(i) {
				      case 0:
				      case 2:
				      	reto=0;
				      	break;
				      case 1:
				      	reto=1;
				      	break;
				      case 3:
				      	reto=2;
				      	break;
				      case 4:
				      	reto=3;
				      	break;
			      }
		      }
          that.updating=false;                
          //that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;
          switch(reto) {
	          case 0:
	          	that.resultadoError(txtResultadoError);
	          	break;
	          case 1:
	          	that.resultadoOk();
	          	break;
	          case 2:
	          	that.resultadoJs(txtResultadoJS);
	          	break;
	          case 3:
	          	document.getElementById(ElementId).innerHTML=txtResultadoHttp;
	          	that.resultadoOk();
	          	break;
          }
        }                                                      
      }
      
      that.updating = new Date();                              
      var uri=urlCall;
      if (uri.indexOf("?") == -1) uri += "?fnrnd=";
			else uri += "&fnrnd=";
			uri += that.updating.getTime();
		  var passData = "fn=" + escape(funcion);
		  for (i = 0; i < args.length-1; i+=2) {
				passData += "&" + args[i] + "=" + escape(args[i+1]);
			}
      if (/post/i.test(that.Metodo)) {
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        that.AJAX.open("GET", uri+'&'+passData, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
	this.limpiaresultado= function(a) {
		var z=a;
		var ini="";
		var fin="";
		var datos="";
		var largo=cmd[0].length;
		var r=0;
		if (z.length>largo) {
			ini=z.substring(0, largo);
			fin=z.substring(z.length-largo);
			datos=z.substring(largo,z.length-largo);
		} else {
			txtResultadoError=LRMTmsg[0]+'\n'+a;
		}
		if ((ini==cmd[0]) && (fin==cmd[1])) {
			txtResultadoHttp= datos;
			r=1;
		} else {
		if ((ini==cmd[2]) && (fin==cmd[3])) {
		 txtResultadoError= datos;
		 r=2;
		} else {
		 if ((ini==cmd[4]) && (fin==cmd[5])) {
		  txtResultadoJS= datos;
		  r=3;
		 } else {
		  if ((ini==cmd[6]) && (fin==cmd[7])) {
		   txtResultadoHttp= datos;
		   r=4;
		  } else {
		   txtResultadoError=LRMTmsg[0]+'\n'+a;
		  }
		 }
		}
		}
		return r;
	}
  //this.callback = callbackFunction || function () { };
  this.resultadoJs= EFunctionJs || function () { };
  this.resultadoOk= EFunctionOk || function () { };
  this.resultadoError= EFunctionError || function () { };
	var ElementId= Layer;
  var urlCall = url;
  var Metodo = (typeof(metodo) == "undefined" || metodo== "") ?	"GET" : postMethod;
	var cmd = new Array('#LNSIRMTDaToS#','#LNSFRMTDaToS#',
											'#LNSIRMTErroR#','#LNSFRMTErroR#',
											'#LNSIRMTDaTJS#','#LNSFRMTDaTJS#',
											'#LNSIRMTDaTDI#','#LNSFRMTDaTDI#');
	var txtResultadoHttp='';
	var txtResultadoError='';
	var txtResultadoJS='';
}

/*
Fin Funciones Remotas
*/
