
function xhr() 
{
	/**
	 * @access public
	 * @var Object
	 */
	this.argList = new Object();
	
	for(var i=0; i < arguments.length; i++)
	{
		setArgument(this, arguments[i]);
	}
	
	/**
	 * @access private
	 * @param referenz ref 
	 * @param Object arg 
	 */
	function setArgument(ref, arg)
	{
		if(ref.argList == null)
			ref.argList = new Object();
			
		for(ele in arg)
		{
			ref.argList[ele] = arg[ele];
		}
	}
	
	/**
	 * @access private
	 * @retrun Object 
	 */
	function createXHR()
	{
		var request = { 
			req : null, 
			init : function() {
				try 
				{
					this.req = new XMLHttpRequest();
				}
				catch (trymicrosoft)
				{
					try
					{
						this.req = new ActiveXObject("Msxml2.XMLHTTP");
					}
					catch (othermicrosoft) 
					{
						try 
						{
							this.req = new ActiveXObject("Microsoft.XMLHTTP");
						} 
						catch (failed) 
						{
							this.req = null;
						}
					}
				}
				return this;
			},
			/**
			 * @access public
			 * @param string header 
			 */
			setDefaultReqHeader : function(header)
			{
				this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
				this.req.setRequestHeader("Connection", "close");
				if( typeof header == "string")
				{
					var headerArr = header.split("&");
					for(headerData in headerArr)
					{
						if(headerArr[headerData] != "")
							this.req.setRequestHeader(headerArr[headerData].split("=")[0], headerArr[headerData].split("=")[1]);
					}
				}
			}
		};

		return request.init();
	}
	
	/**
	 * @access public
	 * @return Object 
	 */
	this.getXHR = function()
	{
		return createXHR(); 
	};
	
}

xhr.prototype = {
	getTemp : "",
	actionTemp : null,
	errors : 0,
	/**
	 * @access public
	 * @param string get 
	 * @param function action 
	 */
	newReq : function (get, action)
	{
		var reqObj = this.getXHR();

		if(reqObj.req != null)
		{
			var tempRef = this;
			reqObj.req.onreadystatechange = function() { tempRef.handle(reqObj.req, action); }; 
			getTemp = get;
			actionTemp = action;
			if(this.argList["post"])
			{
				reqObj.req.open("POST", this.buildGet(get), true);
				reqObj.req.setRequestHeader("Content-length", this.argList["post"].length);
				reqObj.setDefaultReqHeader(this.argList["header"]);
				reqObj.req.send(this.argList["post"]);
			}
			else
			{
				reqObj.req.open("GET", this.buildGet(get), true);
				reqObj.setDefaultReqHeader(this.argList["header"]);
				reqObj.req.send(null);
			}
		}
		else
		{
			alert("Es trat ein Problem auf: " + get + ".xhr");
		}
	},
	/**
	 * @access public
	 * @param string get
	 * @return string sid
	 */
	buildGet : function(get)
	{
		
		if(get == "")
		{
			var path = window.location.pathname.split("/");
			get = path[path.length-1] + get;
		}
		
		if(this.argList["SID"])
		{
			if(get.indexOf("?") > -1)
			{
				get+= "&" + this.searchSID(this.argList["SID"]);
			}
			else
			{
				get+= "?" + this.searchSID(this.argList["SID"])
			}
		}

		
		return get;
	},
	/**
	 * @access public
	 * @return string sid
	 */
	searchSID : function(SID)
	{
		var data = window.location.search.substr(1).split("&");
		
		for(var i in data)
		{
			var dataArr = data[i].split("=");
			if(dataArr[0] == SID)
			{
				return SID + "=" + dataArr[1];
			}
		}
	},
	
	/**
	 * @access public
	 * @param Object req 
	 * @param function action 
	 */
	handle : function(res, action)
	{
		if (res.readyState == 4) 
		{ 
			if(this.argList["res"] && this.argList["res"] == "obj")
			{
				var erg = "";
				if(res.responseText != "")
				{
					erg = eval("(" + res.responseText + ")")
					action(erg);
				}
				else
				{
					if(!this.argList["errors"])
						this.argList["errors"] = 1;
					
					this.argList["errors"]++;
					
					if(this.argList["errors"] < 5)
					setTimeout(function()
					{
						new xhr(this.argList).newReq(getTemp, actionTemp);
					}, 1000);
					
				}
				
				return false;
			}
			
			action(res.responseText);
			return false;
		}
	}
	
};