class jcapAjax{constructor(e,t,o,r,s,n=!1){this.method=e,this.url=t,this.parameters=o,this.destination=r,this.returnFormat=s,this.viewProgress=n,this.vfuncion,this.charURLvar,this.objeto=this.createAjaxObject(),this.runAjax=this.runAjax,this.objeto.onreadystatechange=this.updateAjax}createAjaxObject=()=>{try{var t=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){try{t=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){t=!1}}return t=t||"undefined"==typeof XMLHttpRequest?t:new XMLHttpRequest};runAjax=()=>{var r=this;return new Promise(function(t,o){var e;"GET"==r.method.toUpperCase()||""==r.method?(e=r.charURLvar||"?",r.url=r.url.substr(r.url.length-1,1)!=e?r.url+e+r.parameters:r.url+r.parameters,r.objeto.open("GET",r.url,!0),r.parameters=""):"POST"==r.method.toUpperCase()&&(r.parameters="object"==typeof r.parameters?r.parameters:r.generacampos(),r.objeto.open("POST",r.url,!0),r.objeto.setRequestHeader("Content-type","application/x-www-form-urlencoded")),"blob"==r.returnFormat.toLowerCase()&&(r.objeto.responseType="blob",r.objeto.onload=function(){var e;200==r.objeto.status&&(e=new Blob([r.objeto.response],{type:"application/pdf"}),e=window.URL.createObjectURL(e),document.getElementById(r.destination)?(document.getElementById(r.destination).src=e,console.log("self.destination :",r.destination,"  url: ",e),t({status:"ok",msg:"Archivo pdf en "+r.destination})):console.error("No existe el elemento :",r.destination))}),r.objeto.send(r.parameters),r.objeto.onreadystatechange=function(){if(4==r.objeto.readyState)if(200==r.objeto.status)try{"text"==r.returnFormat.toLowerCase()?t(r.objeto.responseText):"json"==r.returnFormat.toLowerCase()&&t(JSON.parse(r.objeto.responseText))}catch(e){console.info(r.objeto.responseText),o(e)}else 400==r.objeto.status?console.log("There was an error 400"):console.log("something else other than 200 was returned")},r.vfuncion})};generacampos=()=>{let t=[],o;return document.querySelectorAll(`#${this.parameters} input`).forEach(e=>{o=!0,"checkbox"!=e.type||e.checked||(o=!1),(o="radio"!=e.type||e.checked?o:!1)&&t.push(encodeURIComponent(e.name)+"="+encodeURIComponent(e.value))}),t.join("&")};tryParseJSON=e=>{try{var t=JSON.parse(e);if(t&&"object"==typeof t)return JSON.stringify(t)}catch(e){return console.log("Error en tryParseJSON :",e),!1}};updateAjax=()=>{if(this.viewProgress&&document.getElementById(this.viewProgress)){var t=["","Cargando...","Enviando...","Recibiendo...","Finalizado..."];let e=document.getElementById(this.viewProgress);e.style.display="",e.innerHTML=t[this.objeto.readyState],4==this.objeto.readyState&&setTimeout(()=>{e.style.display="none"},500)}if(4==this.objeto.readyState&&200==this.objeto.status){if("returns"==this.destination.toLowerCase()){if("text"==this.returnFormat.toLowerCase())return this.objeto.responseText;if("json"==this.returnFormat.toLowerCase())return tryParseJSON(this.objeto.responseText)}else if(document.getElementById(this.destination)){let e=document.getElementById(this.destination);"text"==this.returnFormat.toLowerCase()?e.innerHTML=this.objeto.responseText:"json"==this.returnFormat.toLowerCase()&&(e.innerHTML=tryParseJSON(this.objeto.responseText))}"function"==typeof vfuncion&&vfuncion()}}}