var Ajax=function(){this.dbgmode=false;this.dbginfo=new String;this.xmlhttp=null;this.qid=0;this.quene=new Array();this.busy=false;this.form=null;this.returnVal=false;ajax=this;};Ajax.prototype.init=function(){this.debug('Creating XMLHTTP object...',true);this.xmlhttp=this.createXMLHTTP();if(this.xmlhttp==null){this.debug('failed!');this.returnVal=true;}else this.debug('done!');};Ajax.prototype.prepareForm=function(form){this.debug('Parsing form...');var preparedString=new String();for(var i=0;i<form.elements.length;i++){element=form.elements[i];name=element.getAttribute('name');value=this.urlEncode(element.value);if(name!=null && name!='' && name!='null'){if(element.tagName=='INPUT'){if(!element.disabled){this.debug('Adding new element: ',true);this.debug(element.tagName+',',true);type=element.getAttribute('type');if(type!='file'){this.debug(type+',',true);this.debug(name+'='+value,true); if(type=='checkbox'||type=='radio'){if(element.checked==true||element.checked=="checked") preparedString+=name+'='+value+'&';}else preparedString+=name+'='+value+'&';this.debug('...done!');}}}else if(element.tagName=='SELECT' && element.multiple){if(!element.disabled){this.debug('Adding new element: ',true);this.debug(element.tagName+',',true);this.debug(element.name+',{',true);options=element.getElementsByTagName('option');for(var j=0;j<options.length;j ++) if(options[j].selected){this.debug(options[j].value+' ',true);if(j>0) this.debug(',',true);preparedString+=name+'='+options[j].value+'&';}this.debug('}...done!');}}else if(!element.disabled){this.debug('Adding new element: ',true);this.debug(element.tagName+',',true);this.debug(name+'=',true);this.debug(value,true);preparedString+=name+'='+value+'&';this.debug('...done!');}}}this.form=preparedString.replace(/(\&)$/,'');this.debug('Final form query string: '+this.form);return this.form;};Ajax.prototype.error=function(){if(this.dbgmode)alert(this.dbginfo);else alert('Fatal error occured in Ajax!');};Ajax.prototype.createXMLHTTP=function(){try{return new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP');}catch(e){try{return new XMLHttpRequest();}catch(e){return null;}}}};Ajax.prototype.sendQuery=function(qid){this.init();running=this.quene[qid];this.busy=true;this.windowOnClose=window.onclose;this.windowOnUnload=window.onunload;window.onclose=function(){return confirm('Request in progress.\nAre you sure you want to close window?');};window.onunload=function(){return confirm('Request in progress.\nAre you sure you want to close window?');};this.debug('Sending request...',true);this.xmlhttp.onreadystatechange=this.onStateChange;if(this.quene[qid]['method']=='GET'){this.xmlhttp.open('GET',this.appendQStr(this.quene[qid]['uri'],this.quene[qid]['data']),true);this.xmlhttp.setRequestHeader('X-Requested-With','XMLHttpRequest');this.xmlhttp.send(null);}else{this.xmlhttp.open('POST',this.quene[qid]['uri'],true);this.xmlhttp.setRequestHeader('X-Requested-With','XMLHttpRequest');this.xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');this.xmlhttp.setRequestHeader('Content-length',this.quene[qid]['data'].length);this.xmlhttp.setRequestHeader('Connection','close');this.xmlhttp.send(this.quene[qid]['data']);}this.debug('done!');};Ajax.prototype.onStateChange=function(){if(ajax.readyState()==4){window.onclose=ajax.windowOnClose;window.onunload=ajax.windowOnUnload;ajax.busyStateOff();ajax.done();ajax.xmlhttp.responseXML==null||ajax.xmlhttp.responseXML.firstChild==null ?(ajax.quene[ajax.qid]['callback'](ajax.xmlhttp.responseText)):(ajax.quene[ajax.qid]['callback'](ajax.xmlhttp.responseXML));}};Ajax.prototype.busyStateOff=function(){this.busy=false;};Ajax.prototype.done=function(){this.quene[this.qid]['done']=true;};Ajax.prototype.readyState=function(){return this.xmlhttp.readyState;};Ajax.prototype.newRequest=function(uri,data,callback,method){this.debug('Adding new request to quene...');if(callback==null||typeof callback!='function'){this.debug('fail!');this.debug('Invalid Callback method!');this.error();return this;}uri=this.urlEncode(uri);this.debug('URI: '+uri);data=this.urlEncode(data);this.debug('Query data: '+data);method=(method=='POST') ? 'POST': 'GET';this.debug('Method: '+method);qid=this.quene.length;this.quene[qid]=new Array();this.quene[qid]['uri']=uri;this.quene[qid]['data']=data;this.quene[qid]['callback']=callback;this.quene[qid]['method']=method;this.quene[qid]['done']=false;this.rotateQuene();return this.returnVal;};Ajax.prototype.urlEncode=function(decoded){return encodeURI(decoded).replace(/\+/g,'%2B');};Ajax.prototype.appendQStr=function(uri,qstr){if(uri.search(/(\?)$/)!=-1) return uri+qstr;else if(uri.search(/\?/)!=-1) return uri+'&'+qstr;else return uri+'?'+qstr;};Ajax.prototype.rotateQuene=function(){if(this.busy){this.debug('Currently running query #'+qid+'. Waiting 30 sec.');window.setTimeout(function(){ajax.rotateQuene()},3000);}else{this.debug('Starting next query!');for(i=0;i<this.quene.length;i++) if(this.quene[i]['done']==false){this.qid=i;this.debug('QID: '+i);this.sendQuery(i);window.setTimeout(function(){ajax.rotateQuene()},3000);return true;}else continue;this.debug('No more queries!');this.debugSummary();return true;}};Ajax.prototype.debug=function(msg,nonewline){if(this.dbgmode) this.dbginfo+=msg +((nonewline==true) ? '': '');};Ajax.prototype.debugSummary=function(){if(this.dbgmode){div=document.createElement('div');div.style.width='500px';div.style.position='absolute';div.style.left='50%';div.style.top='30px';div.style.marginLeft='-250px';div.style.zIndex='100';div.style.backgroundColor='#fff';div.style.border='1px solid #f00';div.style.padding='10px';a=document.createElement('a');a.onclick=function(){document.body.removeChild(this.parentNode);};a.innerHTML='X';a.style.display='inline-block';a.style.width='20px';a.style.textAlign='center';a.style.cursor='pointer';a.style.border='1px solid #f00';a.style.backgroundColor='#fff';a.style.position='absolute';a.style.top='-15px';a.style.right='10px';a.style.zIndex='101';div.appendChild(a);document.body.appendChild(div);p=document.createElement('p');p.style.overflow='scroll';p.style.maxHeight='450px';p.innerHTML=this.dbginfo;div.appendChild(p);}}