/**
 * @author Jeff Stirn
 * based on the class ajax.js
 * from Denny Carl 
 * book Praxiswissen Ajax
 * 
 */
function SimpleAjax(){
	//start the member declaration
	this.url="";
	this.params="";
	this.method="GET";
	this.asynchronous=true;
	this.onSuccess=null;
	this.onError=function (msg) {
		alert(msg);
	}
}

SimpleAjax.prototype.doRequest=function() {
	//check if all the needed params have been set
	if (!this.url) {
		this.onError("The Url is missing. Request has been canceled.");
		return false;
	}

  	if (!this.method) {
    	this.method="GET";
  	} else {
    	this.method=this.method.toUpperCase();
  	}

  	//Grand access for readyStateHandler
  	var _this = this;
  
  	//Create XMLHttpRequest-Objekt
 	var xmlHttpRequest=getXMLHttpRequest();
  	if (!xmlHttpRequest) {
    	this.onError("The XMLHttpRequest-Object could not be created.");
    	return false;
  	}
  
  	//check if we are in asynchronous mode
  	if (this.asynchronous){
	  	//check used data transfer method
	  	switch (this.method) {
	   		case "GET": xmlHttpRequest.open(this.method, this.url+"?"+this.params, true);
	                	xmlHttpRequest.onreadystatechange = readyStateHandler;
	                	xmlHttpRequest.send(null);
	                	break;
	    	case "POST": xmlHttpRequest.open(this.method, this.url, true);
	                 	xmlHttpRequest.onreadystatechange = readyStateHandler;
	                 	xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	                 	xmlHttpRequest.send(this.params);
	                 	break;
		}
		
  	}else{
  		//we are in synchronous mode
  		
  		//check used data transfer method
	  	switch (this.method) {
	   		case "GET": xmlHttpRequest.open(this.method, this.url+"?"+this.params, false);
	                	xmlHttpRequest.send(null);
	                	break;
	    	case "POST": xmlHttpRequest.open(this.method, this.url, false);
	                 	xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	                 	xmlHttpRequest.send(this.params);
	                 	break;
		}
		if (xmlHttpRequest.status == 200 || xmlHttpRequest.status==304) {
			if (_this.onSuccess) {
				_this.onSuccess(xmlHttpRequest.responseText, xmlHttpRequest.responseXML);
			}
		} else {
			if (_this.onError) {
				_this.onError("["+xmlHttpRequest.status+" "+xmlHttpRequest.statusText+"] Error occured while receving the data.");
			}
		}
  	}

  	//Private method to treat the receved data when we are in asynchronous mode
	function readyStateHandler() {
		if (xmlHttpRequest.readyState < 4) {
			return false;
		}
		if (xmlHttpRequest.status == 200 || xmlHttpRequest.status==304) {
			if (_this.onSuccess) {
				_this.onSuccess(xmlHttpRequest.responseText, xmlHttpRequest.responseXML);
			}
		} else {
			if (_this.onError) {
				_this.onError("["+xmlHttpRequest.status+" "+xmlHttpRequest.statusText+"] Error occured while receving the data.");
			}
		}
	}
}

// This function returns an XML Http Request according to the used browser
function getXMLHttpRequest() {
	
	var _oHttpRequest = null;
	
	try{
		// Try to create request object. XMLHttpRequest is the request object used by 
		// Mozilla, Safari, Firefox, Opera and most non microsoft browsers
		_oHttpRequest = new XMLHttpRequest();
		if (_oHttpRequest.overrideMimeType) {
			_oHttpRequest.overrideMimeType('text/xml');
		}
		
	} catch (e) {
		
		try {
			// Try to create request object supported by most microsoft browsers
			_oHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			
			try {
				// However if the previous microsoft request object creation failed try this one
				_oHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				_oHttpRequest = null;
			}
		}
	}
	
	if (_oHttpRequest == null) {
		alert('The XMLHTTP object can not be created !');
	}
	return _oHttpRequest;
};
