/** * JsHttpRequest: JavaScript "AJAX" data loader * Minimized version: see debug directory for the complete one. * * @license LGPL * @author Dmitry Koterov, http://en.dklab.ru/lib/JsHttpRequest/ * @version 5.x $Id: script.js 91 2007-04-09 12:29:16Z wingedfox $ */ function JsHttpRequest(){var i=this;i.onreadystatechange=null;i.readyState=0;i.responseText=null;i.responseXML=null;i.status=200;i.statusText="OK";i.responseJS=null;i.caching=false;i.loader=null;i.session_name="PHPSESSID";i._ldObj=null;i._reqHeaders=[];i._openArgs=null;i._errors={inv_form_el:"Invalid FORM element detected: name=%, tag=%",must_be_single_el:"If used,
must be a single HTML element in the list.",js_invalid:"JavaScript code generated by backend is invalid!\n%",url_too_long:"Cannot use so long query with GET request (URL is larger than % bytes)",unk_loader:"Unknown loader: %",no_loaders:"No loaders registered at all, please check JsHttpRequest.LOADERS array",no_loader_matched:"Cannot find a loader which may process the request. Notices are:\n%",no_headers:"Method setRequestHeader() cannot work together with the % loader."};i.abort=function(){with(this){if(_ldObj&&_ldObj.abort){_ldObj.abort();}_cleanup();if(readyState==0){return}if(readyState==1&&!_ldObj){readyState=0;return}_changeReadyState(4,true);}};i.open=function(I,l,o,O,Q){with(this){try{if(document.location.search.match(new RegExp("[&?]"+session_name+"=([^&?]*)"))||document.cookie.match(new RegExp("(?:;|^)\\s*"+session_name+"=([^;]*)"))){l+=(l.indexOf("?")>=0?"&":"?")+session_name+"="+this.escape(RegExp.$1);}}catch(e){};_openArgs={method:(I||"").toUpperCase(),url:l,asyncFlag:o,username:O!=null?O:"",password:Q!=null?Q:""};_ldObj=null;_changeReadyState(1,true);return true}};i.send=function(I){if(!this.readyState){return}this._changeReadyState(1,true);this._ldObj=null;var l=[];var o=[];if(!this._hash2query(I,null,l,o)){return}var O=null;if(this.caching&&!o.length){O=this._openArgs.username+":"+this._openArgs.password+"@"+this._openArgs.url+"|"+l+"#"+this._openArgs.method;var Q=JsHttpRequest.CACHE[O];if(Q){this._dataReady(Q[0],Q[1]);return false}}var _=(this.loader||"").toLowerCase();if(_&&!JsHttpRequest.LOADERS[_]){return this._error("unk_loader",_);}var c=[];var C=JsHttpRequest.LOADERS;for(var e in C){var v=C[e].loader;if(!v){continue}if(_&&e!=_){continue}var V=new v(this);JsHttpRequest.extend(V,this._openArgs);JsHttpRequest.extend(V,{queryText:l.join("&"),queryElem:o,id:(new Date().getTime())+""+JsHttpRequest.COUNT++,hash:O,span:null});var x=V.load();if(!x){this._ldObj=V;JsHttpRequest.PENDING[V.id]=this;return true}if(!_){c[c.length]="- "+e.toUpperCase()+": "+this._l(x);}else{return this._error(x);}}return e?this._error("no_loader_matched",c.join("\n")):this._error("no_loaders");};i.getAllResponseHeaders=function(){with(this){return _ldObj&&_ldObj.getAllResponseHeaders?_ldObj.getAllResponseHeaders():[]}};i.getResponseHeader=function(I){with(this){return _ldObj&&_ldObj.getResponseHeader?_ldObj.getResponseHeader():[]}};i.setRequestHeader=function(I,l){with(this){_reqHeaders[_reqHeaders.length]=[I,l]}};i._dataReady=function(I,l){with(this){if(caching&&_ldObj){JsHttpRequest.CACHE[_ldObj.hash]=[I,l]}if(I!==null||l!==null){status=4;responseText=responseXML=I;responseJS=l}else{status=500;responseText=responseXML=responseJS=null}_changeReadyState(2);_changeReadyState(3);_changeReadyState(4);_cleanup();}};i._l=function(I){var l=0,o=0,O=this._errors[I[0]];while((o=O.indexOf("%",o))>=0){var Q=I[++l]+"";O=O.substring(0,o)+Q+O.substring(o+1,O.length);o+=1+Q.length}return O};i._error=function(I){I=this._l(typeof(I)=="string"?arguments:I);I="JsHttpRequest: "+I;if(!window.Error){throw I}else{if((new Error(1,"test")).description=="test"){throw new Error(1,I);}else{throw new Error(I);}}};i._hash2query=function(I,l,o,O){if(l==null){l=""}if((""+typeof(I)).toLowerCase()=="object"){var Q=false;if(I&&I.parentNode&&I.parentNode.appendChild&&I.tagName&&I.tagName.toUpperCase()=="FORM"){I={form:I}}for(var _ in I){var c=I[_];if(c instanceof Function){continue}var C=l?l+"["+this.escape(_)+"]":this.escape(_);var e=c&&c.parentNode&&c.parentNode.appendChild&&c.tagName;if(e){var v=c.tagName.toUpperCase();if(v=="FORM"){Q=true}else{if(v=="INPUT"||v=="TEXTAREA"||v=="SELECT"){}else{return this._error("inv_form_el",(c.name||""),c.tagName);}}O[O.length]={name:C,e:c}}else{if(c instanceof Object){this._hash2query(c,C,o,O);}else{if(c===null){continue}o[o.length]=C+"="+this.escape(""+c);}}if(Q&&O.length>1){return this._error("must_be_single_el");}}}else{o[o.length]=I}return true};i._cleanup=function(){var I=this._ldObj;if(!I){return}JsHttpRequest.PENDING[I.id]=false;var l=I.span;if(!l){return}I.span=null;var o=function(){l.parentNode.removeChild(l);};JsHttpRequest.setTimeout(o,50);};i._changeReadyState=function(I,l){with(this){if(l){status=statusText=responseJS=null;responseText=""}readyState=I;if(onreadystatechange){onreadystatechange();}}};i.escape=function(I){return escape(I).replace(new RegExp("\\+","g"),"%2B");}}JsHttpRequest.COUNT=0;JsHttpRequest.MAX_URL_LEN=2000;JsHttpRequest.CACHE={};JsHttpRequest.PENDING={};JsHttpRequest.LOADERS={};JsHttpRequest._dummy=function(){};JsHttpRequest.TIMEOUTS={s:window.setTimeout,c:window.clearTimeout};JsHttpRequest.setTimeout=function(i,I){window.JsHttpRequest_tmp=JsHttpRequest.TIMEOUTS.s;if(typeof(i)=="string"){l=window.JsHttpRequest_tmp(i,I);}else{var l=null;var o=function(){i();delete JsHttpRequest.TIMEOUTS[l]};l=window.JsHttpRequest_tmp(o,I);JsHttpRequest.TIMEOUTS[l]=o}window.JsHttpRequest_tmp=null;return l};JsHttpRequest.clearTimeout=function(i){window.JsHttpRequest_tmp=JsHttpRequest.TIMEOUTS.c;delete JsHttpRequest.TIMEOUTS[i];var I=window.JsHttpRequest_tmp(i);window.JsHttpRequest_tmp=null;return I};JsHttpRequest.query=function(i,I,l,o){var O=new this();O.caching=!o;O.onreadystatechange=function(){if(O.readyState==4){l(O.responseJS,O.responseText);}};var Q=null;if(i.match(/^((\w+)\.)?(GET|POST)\s+(.*)/i)){O.loader=RegExp.$2?RegExp.$2:null;Q=RegExp.$3;i=RegExp.$4}O.open(Q,i,true);O.send(I);};JsHttpRequest.dataReady=function(i){var I=this.PENDING[i.id];delete this.PENDING[i.id];if(I){I._dataReady(i.text,i.js);}else{if(I!==false){throw"dataReady(): unknown pending id: "+i.id}}};JsHttpRequest.extend=function(i,I){for(var l in I){i[l]=I[l]}};JsHttpRequest.LOADERS.xml={loader:function(i){JsHttpRequest.extend(i._errors,{xml_no:"Cannot use XMLHttpRequest or ActiveX loader: not supported",xml_no_diffdom:"Cannot use XMLHttpRequest to load data from different domain %",xml_no_headers:"Cannot use XMLHttpRequest loader or ActiveX loader, POST method: headers setting is not supported, needed to work with encodings correctly",xml_no_form_upl:"Cannot use XMLHttpRequest loader: direct form elements using and uploading are not implemented"});this.load=function(){if(this.queryElem.length){return["xml_no_form_upl"]}if(this.url.match(new RegExp("^([a-z]+)://([^\\/]+)(.*)","i"))){if(RegExp.$2.toLowerCase()==document.location.hostname.toLowerCase()){this.url=RegExp.$3}else{return["xml_no_diffdom",RegExp.$2]}}var I=null;if(window.XMLHttpRequest){try{I=new XMLHttpRequest();}catch(e){}}else{if(window.ActiveXObject){try{I=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){};if(!I){try{I=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}}}}if(!I){return["xml_no"]}var l=window.ActiveXObject||I.setRequestHeader;if(!this.method){this.method=l?"POST":"GET"}if(this.method=="GET"){if(this.queryText){this.url+=(this.url.indexOf("?")>=0?"&":"?")+this.queryText}this.queryText="";if(this.url.length>JsHttpRequest.MAX_URL_LEN){return["url_too_long",JsHttpRequest.MAX_URL_LEN]}}else{if(this.method=="POST"&&!l){return["xml_no_headers"]}}this.url+=(this.url.indexOf("?")>=0?"&":"?")+"JsHttpRequest="+(i.caching?"0":this.id)+"-xml";var o=this.id;I.onreadystatechange=function(){if(I.readyState!=4){return}I.onreadystatechange=JsHttpRequest._dummy;i.status=null;try{i.status=I.status;i.responseText=I.responseText}catch(e){};if(!i.status){return}try{eval("JsHttpRequest._tmp = function(id) { var d = "+i.responseText+"; d.id = id; JsHttpRequest.dataReady(d); }");}catch(e){return i._error("js_invalid",i.responseText);}JsHttpRequest._tmp(o);JsHttpRequest._tmp=null};I.open(this.method,this.url,true,this.username,this.password);if(l){for(var O=0;O=0?"&":"?")+this.queryText}this.url+=(this.url.indexOf("?")>=0?"&":"?")+"JsHttpRequest="+this.id+"-"+"script";this.queryText="";if(!this.method){this.method="GET"}if(this.method!=="GET"){return["script_only_get"]}if(this.queryElem.length){return["script_no_form"]}if(this.url.length>JsHttpRequest.MAX_URL_LEN){return["url_too_long",JsHttpRequest.MAX_URL_LEN]}if(i._reqHeaders.length){return["no_headers","SCRIPT"]}var I=this,l=document,o=null,O=l.body;if(!window.opera){this.span=o=l.createElement("SCRIPT");var Q=function(){o.language="JavaScript";if(o.setAttribute){o.setAttribute("src",I.url);}else{o.src=I.url}O.insertBefore(o,O.lastChild);}}else{this.span=o=l.createElement("SPAN");o.style.display="none";O.insertBefore(o,O.lastChild);o.innerHTML="Workaround for IE.";var Q=function(){o=o.getElementsByTagName("SCRIPT")[0];o.language="JavaScript";if(o.setAttribute){o.setAttribute("src",I.url);}else{o.src=I.url}}}JsHttpRequest.setTimeout(Q,10);return null}}};JsHttpRequest.LOADERS.form={loader:function(i){JsHttpRequest.extend(i._errors,{form_el_not_belong:"Element \"%\" does not belong to any form!",form_el_belong_diff:"Element \"%\" belongs to a different form. All elements must belong to the same form!",form_el_inv_enctype:"Attribute \"enctype\" of the form must be \"%\" (for IE), \"%\" given."});this.load=function(){var I=this;if(!I.method){I.method="POST"}I.url+=(I.url.indexOf("?")>=0?"&":"?")+"JsHttpRequest="+I.id+"-"+"form";if(i._reqHeaders.length){return["no_headers","FORM"]}if(I.method=="GET"){if(I.queryText){I.url+=(I.url.indexOf("?")>=0?"&":"?")+I.queryText}if(I.url.length>JsHttpRequest.MAX_URL_LEN){return["url_too_long",JsHttpRequest.MAX_URL_LEN]}var l=I.url.split("?",2);I.url=l[0];I.queryText=l[1]||""}var o=null;var O=false;if(I.queryElem.length){if(I.queryElem[0].e.tagName.toUpperCase()=="FORM"){o=I.queryElem[0].e;O=true;I.queryElem=[]}else{o=I.queryElem[0].e.form;for(var Q=0;Q")+"";if(!o){o=I.span.firstChild}e.body.insertBefore(V,e.body.lastChild);var x=function(_,z){var Z=[];var w=_;if(_.mergeAttributes){var w=e.createElement("form");w.mergeAttributes(_,false);}for(var Q=0;Q=0;Q--){var W=w[Q].split("=",2);var _=e.createElement("INPUT");_.type="hidden";_.name=unescape(W[0]);_.value=W[1]!=null?unescape(W[1]):"";o.appendChild(_);}for(var Q=0;Q= 0x80 && c <= 0x7ff) { d += String.fromCharCode(((c >> 6) & 0x1f) | 0xc0); d += String.fromCharCode((c & 0x3f) | 0x80); } else { d += String.fromCharCode((c >> 12) | 0xe0); d += String.fromCharCode(((c >> 6) & 0x3f) | 0x80); d += String.fromCharCode((c & 0x3f) | 0x80); } }; return d; };