// JavaScript Document Request = function(oListener, metodo){ this.pedido = new crearXHR(); this.reportar = true; this.respuestaXML = null; this.respuestaHTML = null; this.archivo = null; this.valores = null; this.listener = oListener; // this.onError = null; this.onRetry = null; this.retrys = 0; this.curRetry = 0; if(!metodo) this.metodo = 'POST'; else this.metodo = metodo; } Request.prototype.pedir = function(a, v, m, r){ //inicializamos if(!!a) this.archivo = a; if(!!v) this.valores = v; if(!!m) this.metodo = m; if(!r) this.curRetry = 0; // this.respuestaXML = null; this.respuestaHTML = null; // this.cancelar(this.pedido); // this.pedido.onreadystatechange = this.procesar.closure(this); // this.pedido.open(this.metodo, this.archivo, true); // if(this.valores){ this.valores = this.valores.puntualChars(); this.pedido.send(this.valores); } else this.pedido.send(); } Request.prototype.procesar = function(){ var termino = false; if(this.pedido && this.pedido.readyState == 4){ var stat = this.pedido.status; if((stat >= 200 && stat < 300) || stat == 304 || stat == 1223){ if(this.pedido.responseXML) this.respuestaXML = this.pedido.responseXML.documentElement; this.respuestaHTML = this.pedido.responseText; this.cancelar(); if(this.listener && this.listener.onRequestLoad) this.listener.onRequestLoad(); else if(typeof(this.listener)=='function'){ this.listener(); } } else if(this.curRetry < this.retrys){ this.curRetry++; this.pedir(false, false, false, true); if(typeof(this.onRetry) == 'function'){ this.onRetry(); } } else{ if(this.reportar){ ERROR.reportar("Error en clase Request."+ "\nESTADO: "+this.pedido.status+" "+this.pedido.statusText+ "\nARCHIVO: "+this.archivo+ "\nMETODO: "+this.metodo); } if(this.onError && (typeof(this.onError) == 'function' || typeof(this.onError) == 'object')){ this.onError(); } this.cancelar(); } } } Request.prototype.cancelar = function(){ cancelarPedido(this.pedido); } // String.prototype.puntualChars = function(){ var chrs = new Array({'chr':'€', 'ent':'€'}), i, str = this.toString(); for(i = 0; i < chrs.length; i++){ str = str.replace(chrs[i]['chr'], chrs[i]['ent']); } return str; } String.prototype.unPuntualChars = function(){ var chrs = new Array({'chr':'€', 'ent':'€'}), i, str = this.toString(); for(i = 0; i < chrs.length; i++){ str = str.replace(chrs[i]['ent'], chrs[i]['chr']); } return str; } /* FUNCION DE CREACION DE XMLHttpRequest */ crearXHR = function(){ var r = null if (window.XMLHttpRequest) r = new XMLHttpRequest() else if(window.ActiveXObject) { var msp = new Array('Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP') for(var i = 0; i < msp.length; i++){ try { r = new ActiveXObject(msp[i]) } catch (e){} } } return r } /* FUNCION DE DETENCION DE XMLHttpRequest */ cancelarPedido = function(reqXHR){ if(reqXHR!=null){ reqXHR.onreadystatechange=new Function()//una funcion vacia... reqXHR.abort() } } // ReportarError = function(f, m){ this.archivo = (!!f)? f:'xmlHttpRequest/reportarError.php'; this.metodo = (!!m)? m:'POST'; this.req = new Request(); this.req.reportar = false; this.alerta = true; this.reportar = function(msj){ var msg = "Ha ocurrido un error.\nLos administradores del sistema ya han sido notificados del mismo."+ "\nSi el error continua pongase en contacto con los mismos."; alert(msj); this.req.pedir(this.archivo, msj, this.metodo); if(this.alerta){ if(LayError){ LayLoader.showed = Blocker.showed = 1; LayLoader.hide(); Blocker.hide(); LayError.message = msg; LayError.onAcept = function(){ LayError.hide(); Blocker.hide(); }; LayError.onCancel = function(){ LayLoader.hide(); Blocker.hide(); }; Blocker.show(); LayError.show(); } else{ alert(msg); } } return false; }; } ERROR = new ReportarError(); // JavaScript Document function form(nombre, archivo){ var error = $('error' + nombre); var loader = $('loader' + nombre); var enviando = false; var errores = 0; var aCampos = new Array(); var aValidar = new Array(); var aLimpiar = new Array(); var req = new Request(); var elemento = false; // this.onSuccess = null; this.onError = null; this.onSend = null; // req.listener = function(){ var d = req.respuestaXML; block(false); if(!!loader){ loader.style.display = 'none'; } if(!d){ alert(req.respuestaHTML); } else if(d.getAttribute('exito') == 'si'){ if(!this.onSuccess){ block(true); Exito.onAccept = onAccept; Exito.show(d.firstChild.data); } else{ this.onSuccess(d); } } else{ if(!this.onError){ block(true); Error.onAccept = onAccept; Error.show(d.firstChild.data); } else{ this.onError(d); } } }.closure(this); var onAccept = function(){ clean(); block(false); }.closure(this); this.addField = function(campo, nombre, expresion, clear){ var o = {'c':campo, 'n':nombre}; // aCampos.push(o); if(expresion){ campo.expresion = expresion; campo.inputError = inputError; campo.onblur = funcVal.closure(campo); // aValidar.push(aCampos.length - 1); } if(clear){ aLimpiar.push(aCampos.length - 1); } } var clean = function(){ var i = null; // for(i = 0; i < aLimpiar.length; i++){ aCampos[aLimpiar[i]]['c'].value = ''; } for(i = 0; i < aCampos.length; i++){ inputError(aCampos[i]['c'], false); } errores = 0; showError(); } this.enter = function(event){ if(event.keyCode == 13){ this.send(); } }.closure(this); this.send = function(event){ var v = '', i = null; if(!!event){ StopEvent(event); } if(!!this.onSend){ this.onSend(); } // if(enviando){ return false; } else if(validate()){ return false; } // block(true); if(!!loader){ loader.style.display = 'block'; } // for(i = 0; i < aCampos.length; i++){ v += aCampos[i]['n'] + SEP_IGUAL + trim(aCampos[i]['c'].value) + SEP_AND; } // req.pedir(DIR_ROOT + 'requests/' + archivo + '.php', v); }.closure(this); var inputError = function(c, b){ errores += (b)? 1:0; c.className = (b)? 'inputError':''; }.closure(this); var showError = function(){ error.style.display = (errores > 0)? 'block':'none'; } var funcVal = function(){ this.value = trim(this.value); if(this.value.search(this.expresion) == 0){ this.inputError(this, false); } else{ this.inputError(this, true); } } var validate = function(){ var i = null; // errores = 0; for(i = 0; i < aValidar.length; i++){ aCampos[aValidar[i]]['c'].onblur(); } showError(); return (errores > 0); } var block = function(b){ var i = null; // enviando = b; for(i = 0; i < aValidar.length; i++){ aCampos[i]['c'].disabled = b; } } }