/**
* Ajax Communication class
* Source: http://www.sitepoint.com/article/build-your-own-ajax-web-apps/2/
**/
function Ajax() {
	this.req = null;
	this.url = null;
	this.method = 'GET';
	this.async = true;
	this.status = null;
	this.statusText = '';
	this.postData = null;
	this.readyState = null;
	this.responseText = null;
	this.responseXML = null;
	this.handleResp = null;
	this.responseFormat = 'xml'; // 'text', 'xml', or 'object'
	this.submitinprogress = false;
	this.mimeType = null;
	this.postDataArray = [];

	this.init = function() {
	 if (!this.req) {
	   try {
	     // Try to create object for Firefox, Safari, IE7, etc.
	     this.req = new XMLHttpRequest();
	   }
	   catch (e_a) {
	     try {
	       // Try to create object for later versions of IE.
	       this.req = new ActiveXObject('MSXML2.XMLHTTP');
	     }
	     catch (e_b) {
	       try {
	         // Try to create object for early versions of IE.
	         this.req = new ActiveXObject('Microsoft.XMLHTTP');
	       }
	       catch (e_c) {
	         // Could not create an XMLHttpRequest object.
	         return false;
	       }
	     }
	   }
	 }
	 return this.req;
	};
	
	this.doReq = function() {
	 if (!this.init()) {
	   alert('Could not create XMLHttpRequest object.');
	   return;
	 }

	 this.submitinprogress = true;
	
	 var req = this.req;
	 req.open(this.method, this.url, this.async);
	 //enable the next rule will result in no POST send for MSXML2.XMLHTTP
	 //req.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");

	 if (this.mimeType) {
	  try {
	    req.overrideMimeType(this.mimeType);
	  }
	  catch (e) {
	    // couldn't override MIME type  --  IE6 or Opera?
	  }
	 }
	 var self = this; // Fix loss-of-scope in inner function
		this.req.onreadystatechange = function() {
		 var resp = null;
		 if (self.req.readyState == 4) {
		   this.submitinprogress = false;

		   switch (self.responseFormat) {
		     case 'text':
		       resp = self.req.responseText;
		       break;
		     case 'xml':
		       resp = self.req.responseXML;
		       break;
		     case 'object':
		       resp = req;
		       break;
		   }
		   if (self.req.status >= 200 && self.req.status <= 299) {
		     self.handleResp(resp);
		   }
		   else {
		     self.handleErr(resp);
		   }
		 }
		};

	 //Set some important headers (remco)
	 if(this.method == "POST"){
		this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.req.setRequestHeader("Content-length", this.postData.length);
		this.req.setRequestHeader("Connection", "close");
		this.req.send(this.postData);

	 } else {
		this.req.send(null);
	 }

	};




	this.setMimeType = function(mimeType) {
	 this.mimeType = mimeType;
	};


	this.setHandlerErr = function(funcRef) {
	 this.handleErr = funcRef;
	};

	this.setHandlerBoth = function(funcRef) {
 		this.handleResp = funcRef;
 		this.handleErr = funcRef;
	};

	this.handleErr = function() {
 		var errorWin;
		/** TODO: On several error statusTextmessages a popup **/
		//alert("Ajax Error");
		/*
		alert('An error occurred, but the error message cannot be '
		+ 'displayed. This is probably because of your browser\'s '
		+ 'pop-up blocker.\n'
		+ 'Please allow pop-ups from this web site if you want to '
		+ 'see the full error messages.\n'
		+ '\n'
		+ 'Status Code: ' + this.req.status + '\n'
		+ 'Status Description: ' + this.req.statusText);
		}
		*/
	};

	this.abort = function() {
		if (this.req) {
			this.req.onreadystatechange = function() { };
			this.req.abort();
			this.req = null;
		}
	};


	this.doGet = function(url, hand, format) {
		this.url = url;
		this.method = 'GET';
		this.handleResp = hand;
		this.responseFormat = format || 'xml';
		this.doReq();
	};
	
	this.doPost = function(url, postdata, hand, format) {
		this.url = url;
		this.method = 'POST';
		this.postData = postdata;
		this.handleResp = hand;
		this.responseFormat = format || 'xml';
		this.doReq();
	};

	this.doSPost = function(url, hand, format){
		//build the postdatastring
		var postdata = "";
		var keyVar;
		var pdarray = this.postDataArray;
		for ( keyVar in pdarray ){
			if(pdarray.hasOwnProperty(keyVar)) {
				if(postdata.length>0){
					postdata=postdata+"&";
				}
				postdata = postdata+keyVar+"="+encodeURIComponent(escape(this.postDataArray[keyVar]));
			}
		}

		this.doPost(url, postdata, hand, format);
	};

	this.appendPostData = function(key, val){
		this.postDataArray[key] = val;
	};

}



/** 
* Ajax Support for some older modules
* @Depricated
*/

function GetXmlHttpObject(){
	var xmlHttp=null;
	try {
		xmlHttp=new XMLHttpRequest();
	} catch (e_a) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e_b) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}