//---------------------------------------------------------
// AJAX-Egine from Praxiswissen Ajax by Denny Carl
//
// Version 1.0 
// Lars Fabian Paape
//---------------------------------------------------------
function Ajax()
{
	// declaration and initialization of properties
	this.url="";
	this.params="";
	this.method="GET";
	this.onSuccess=null;
	this.onError=function(msg)
	{
		alert(msg);
	}
}


Ajax.prototype.doRequest=function()
{
	//check for existing url
	if (!this.url)
	{
		this.onError("Trying to initiate a connection without a url!");
		return false;
	}
	if (!this.method)
	{
	  this.method="GET";
	}
	else
	{
		this.method=this.method.toUpperCase();
	}
	
	// get XMLHttpRequest in a browser independ manner
	
	var xmlHttpRequest=getXMLHttpRequest();
	if (!xmlHttpRequest)
	{
		this.onError("Ajax connection not possible!");
		return false;
	}
	
	// helper variable for readyStateHandler
	var _this=this;
	
	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;

	}
	
	// private method for handling ready state
	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 during data transmission!");
			}
		
		}
	}
}




// Ajax connection not possible
function getXMLHttpRequest()
{
  if (window.XMLHttpRequest)
  {
  	//Firefox, Opera, Safari, ...
  	return new XMLHttpRequest();
  }
  else
  {
  	if(window.AxtiveXObject)
  	{
  		try
  		{
  			//IE
  			return new ActiveXObject("Msxml2.XMLHTTP");
  		}
  		catch(e)
  		{
  			//IE old
  			try
  			{
  				return new ActiveXObject("Microsoft.XMLHTTP");
  			}
  			catch(e)
  			{
  			    // Ajax connection not possible
  				return null;
  			}
  		}
  	}
  }
  // Ajax connection not possible
  return null;
}