// b3rtXMLHttp 1.3
// Copyright 2007, H. poort
// This software may not be used without a valid license

function b3rtXMLHttp(){this.options={'method':'get','url':'','asynchronous':true,'encoding':'UTF-8','queryPairs':{},'requestHeaders':{},'requestBody':null};this.xmlHttp=null;this.callbackFunction=null;this.requestHandled=false};b3rtXMLHttp.prototype.setMethod=function(method){if((method.toLowerCase()=='get')||(method.toLowerCase()=='post'))this.options.method=method.toLowerCase()};b3rtXMLHttp.prototype.setURL=function(url){this.options.url=url};b3rtXMLHttp.prototype.setAsynchronous=function(asynchronous){this.options.asynchronous=asynchronous};b3rtXMLHttp.prototype.setEncoding=function(encoding){this.options.encoding=encoding};b3rtXMLHttp.prototype.addQueryPair=function(queryKey,queryValue){this.options.queryPairs[queryKey]=queryValue};b3rtXMLHttp.prototype.clearQueryPairs=function(){this.options.queryPairs={}};b3rtXMLHttp.prototype.addRequestHeader=function(headerKey,headerValue){this.options.requestHeaders[headerKey]=headerValue};b3rtXMLHttp.prototype.clearRequestHeaders=function(){this.options.requestHeaders={}};b3rtXMLHttp.prototype.setRequestBody=function(requestBody){this.options.requestBody=requestBody};b3rtXMLHttp.prototype.clearRequestBody=function(){this.options.requestBody=null};b3rtXMLHttp.prototype.execute=function(callbackFunction){if(!this.setupXMLHttp())return false;this.callbackFunction=((typeof(callbackFunction)=='function')?callbackFunction:null);var methodIsGet=(this.options.method=='get');var queryString=this.buildQueryString();if(methodIsGet&&(queryString.length>0)){if(this.options.url.indexOf('?')==-1)queryString='?'+queryString;else queryString='&'+queryString}try{this.xmlHttp.open(this.options.method.toUpperCase(),this.options.url+(methodIsGet?queryString:''),this.options.asynchronous);this.requestHandled=false;var refThis=this;this.xmlHttp.onreadystatechange=function(){if((typeof(refThis)=='undefined')||!refThis)return;if(refThis.xmlHttp.readyState==4){refThis.onXMLHttpCompleted();refThis=null}};this.xmlHttp.setRequestHeader('X-Requested-With','b3rtXMLHttp');this.xmlHttp.setRequestHeader('Accept','text/javascript, text/html, application/xml, text/xml, */*');if(methodIsGet);else this.xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded'+(this.encoding?'; charset='+this.encoding:''));for(var i in this.options.requestHeaders)this.xmlHttp.setRequestHeader(i,this.options.requestHeaders[i]);if(this.options.requestBody)this.xmlHttp.send(this.options.requestBody);else this.xmlHttp.send((methodIsGet?null:queryString));if(!this.options.asynchronous)this.onXMLHttpCompleted()}catch(ex){if(this.xmlHttp){this.xmlHttp.onreadystatechange=function(){};this.xmlHttp=null}this.callbackFunction=null;return false}return true};b3rtXMLHttp.prototype.setupXMLHttp=function(){if(this.xmlHttp!=null){this.xmlHttp.onreadystatechange=function(){};this.xmlHttp=null}if(typeof(window.XMLHttpRequest)!='undefined'){this.xmlHttp=new window.XMLHttpRequest()}else if(window.ActiveXObject){var msXmlHttpIdentifiers=['MSXML2.XMLHTTP','Microsoft.XMLHTTP'];for(var i=0;i<msXmlHttpIdentifiers.length;i++){try{this.xmlHttp=new ActiveXObject(msXmlHttpIdentifiers[i]);break}catch(ex){this.xmlHttp=null}}}return(this.xmlHttp!=null)};b3rtXMLHttp.prototype.buildQueryString=function(){var queryArray=[];for(var i in this.options.queryPairs){if(typeof(i)!='string')continue;queryArray[queryArray.length]=window.encodeURIComponent(i)+'='+window.encodeURIComponent(this.options.queryPairs[i])}return queryArray.join('&')};b3rtXMLHttp.prototype.onXMLHttpCompleted=function(){if(this.requestHandled)return;this.requestHandled=true;this.xmlHttp.onreadystatechange=function(){};if(this.callbackFunction){this.callbackFunction(this.xmlHttp.responseText,this.xmlHttp.responseXML,this.xmlHttp.getAllResponseHeaders(),this.xmlHttp.status);this.callbackFunction=null}this.xmlHttp=null};