/**********************************************************************************************
Wrapper para AJAX
@.svn dweb/trunk/src/Javascript/AjaxVt
**********************************************************************************************/
function AjaxVt(c,b,a){this._VERSION="1.1";this._handlers=new Object();this._debuglevel=a;this._fback=(typeof c=="function")?c:null;this._dlg=(typeof b=="function")?b:null;this._locales=new Object();a=3}AjaxVt.prototype.setLocale=function(a,b){this._locales[a]=((typeof(b)!="undefined"&&b!="")?b:a)};AjaxVt.prototype._getLocale=function(a){return((typeof(this._locales[a])!="undefined")?this._locales[a]:a)};AjaxVt.prototype.getLocales=function(){return this._locales};AjaxVt.prototype.setLocales=function(a){this._locales=a};AjaxVt.prototype.getAjaxObj=function(){if(window.XMLHttpRequest){return new XMLHttpRequest()}else{if(window.ActiveXObject){return new ActiveXObject("Msxml2.XMLHTTP")}else{}alert(this._getLocale("Su navegador no soporta AJAX")+".")}return null};AjaxVt.prototype.addRequestHandler=function(d,b,c,a){this._handlers[d]={url:b,handler:c,type:a===undefined?"xml":a}};AjaxVt.prototype.setURL=function(b,a){if(typeof(this._handlers[b])=="undefined"){throw ("AjaxVT: origen no definido ("+b+")")}else{this._handlers[b].url=a}};AjaxVt.prototype.send=function(c,f,a,b,d,e){this._send({source:c,method:f,responseType:a,urlparams:d,data:b})};AjaxVt.prototype.requestData=function(c,a,d,b,e){this._send({source:c,method:"GET",responseType:a,urlparams:d,charset:e,data:b})};AjaxVt.prototype.sendData=function(c,a,b,d,e){this._send({source:c,method:"POST",responseType:a,urlparams:d,data:b,charset:e})};AjaxVt.prototype._send=function(args){var source="",method="GET",responseType="xml",urlparams=null,data=null,charset="utf-8",a,b,val;for(a in args){if(args[a]!==undefined){if(typeof(args[a])=="string"){eval(a+"='"+args[a]+"'")}else{if(typeof(args[a])=="object"){eval(a+"={}");for(b in args[a]){val=args[a][b];eval(a+'["'+b+'"]=val')}}else{eval(a+"="+args[a])}}}}if(this._debuglevel>=2){this.log("send().source="+source)}try{var url;var xhttp=this.getAjaxObj();if(typeof(responseType)!="undefined"){this._handlers[source].type=responseType}if(xhttp){if(this._fback!=null){this._fback.call(this,this._getLocale("Solicitando datos")+"...",{event:"onSendInit",source:source,data:data,params:urlparams})}if(typeof this._handlers[source]=="undefined"||this._handlers[source]==null){if(this._dlg!=null){this._dlg.call(this,this._getLocale("Handler no definido para")+" "+source)}return}url=this._handlers[source].url;if(urlparams&&urlparams!=""){url+="?"+urlparams}xhttp.open(method,url,true);var _wrapper={ajaxvt:this,xhttp:xhttp,source:source,data:data,params:urlparams};var _stateHandler=function(){var a=this.ajaxvt;if(a._fback!=null){a._fback.call(a,a._getLocale("Cargando")+"...",{event:"onSendState",source:this.source,data:this.data,params:this.params,state:this.xhttp.readyState})}switch(this.xhttp.readyState){case 0:break;case 1:break;case 2:break;case 3:break;case 4:if(a._fback!=null){a._fback.call(this,a._getLocale("Procesando datos")+"...",{event:"onDataProcess",source:this.source,data:this.data,params:this.params})}if(this.xhttp.status==200){if(a._debuglevel>=3){a.log("AjaxStateHandler().source="+this.source)}var _response=a.checkTrace(this.xhttp,this.source);if(!_response){if(a._dlg!=null){a._dlg.call(this,"Error",a._getLocale("Objeto XML Nulo"),a._getLocale("Aceptar"))}}else{a._clean(this.xhttp);delete this.xttp;if((a._handlers[this.source].handler!=undefined||a._handlers[this.source].handler!="undefined")&&a._handlers[this.source].handler){if(typeof(a._handlers[this.source].handler)=="function"){a._handlers[this.source].handler.call(this,_response,this.source,this.data,this.params)}else{eval(a._handlers[this.source].handler+"(_response, this.source, this.data, this.params )")}}else{if(a._fback!=null){a._fback.call(this,null,{event:"onSendComplete",source:this.source,data:this.data,params:this.params})}}}}else{if(a._dlg!=null){a._dlg.call(this,"Error",a._getLocale("Se produjo un error")+" "+this.xhttp.status+":<br>"+this.xhttp.statusText,a._getLocale("Aceptar"))}a._clean(this.xhttp);delete this.xttp}break}};xhttp.onreadystatechange=this._bind(_stateHandler,_wrapper);xhttp.setRequestHeader("If-Modified-Since","Sat, 1 Jan 1970 00:00:00 GMT");if(method=="POST"){xhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");if(typeof data=="object"){var dtmp="";for(var a in data){if(dtmp!=""){dtmp+="&"}if(charset.toLowerCase()=="utf-8"){dtmp+=a+"="+encodeURIComponent(data[a])}else{dtmp+=a+"="+escape(data[a])}}xhttp.send((data?(dtmp):null))}else{xhttp.send((data?(data):null))}}else{xhttp.send(null)}}}catch(e){if(this._dlg!=null){this._dlg.call(this,"Error",this._getLocale("Se produjo un error")+":<br>"+e.message,this._getLocale("Aceptar"))}}};AjaxVt.prototype._clean=function(b){try{if(this._req&&document.all){if(b.onreadystatechange){b.onreadystatechange=this._emptyfunc}if(b.ontimeout){b.ontimeout=this._emptyfunc}}}catch(a){}delete b};AjaxVt.prototype._emptyFunc=function(){};AjaxVt.prototype.checkTrace=function(h,d){var g=h.responseText.indexOf('<div name="ax_sfn"');if(g==-1&&h.responseXML!=null&&h.responseXML.firstChild!=null){if(this._handlers[d].type=="xml"){return h.responseXML}else{return h.responseText}}else{var a=(g==-1)?h.responseText:h.responseText.substring(0,g);if(this._handlers[d].type=="xml"){try{if(window.ActiveXObject){var c=new ActiveXObject("Microsoft.XMLDOM");c.async="false";var b=c.loadXML(a);if(!b){if(this._dlg!=null){this._dlg.call(this,"Error",c.parseError+" "+c.parseError.reason+".<br>Line "+c.parseError.line+"<br>"+c.parseError.srcText,this._getLocale("Aceptar"))}return null}return c}else{if(window.DOMParser){var i=new DOMParser();var c=i.parseFromString(a,"text/xml");return c}else{if(this._dlg!=null){this._dlg.call(this,"Error",this._getLocale("Parser XML no encontrado"))}return h.responseXML}}}catch(f){if(this._dlg!=null){this._dlg.call(this,"Error",this._getLocale("Parser XML no encontrado"))}return h.responseXML}}else{return a}}};AjaxVt.prototype._bind=function(b,a){return function(c){return b.apply(a||this,arguments)}};AjaxVt.prototype.log=function(a){try{if(typeof(console)!="undefined"){console.log("[AjaxVt] "+a)}}catch(b){}};
