// 	------------------------------------------------------
// 	VSys CMS
// 	@author Sebastian Martens
// 	@copyright Sebastian Martens
// 	@mail sebastian@sebastian-martens.de 
// 	@version 1.0
//	@SVN
//	@$LastChangedDate: 2007-12-12 15:35:45 +0100 (Mi, 12 Dez 2007) $
//	@$Rev: 4 $
//	@$Author: dinnerout $
//
	//$base = "./";
	//include_once($base."system/system.php");
// 	------------------------------------------------------

var AJAXEngine = {
	_afterLoadFunction:null, /** function to be called after successfull loading */ 
	_errorFunction:null, /** function to be calles after failed loading */
	_domId:null, /** DOM id of the element where to place the response */
	_req:null, /** variable for the ajax XML HTTP request */ 
	
	/**
	 * the request constructor
	 * calls the xmlhttp request with optional parameters and functions
	 * the response handling will be done in another another function with is _ajaxHandler by default
	 * 
	 */
	request: function(params){
		this._requestPath = params.url!=null?params.url:null;
		this._addparam = (params.param!=null)?params.param:null;
		this._requestCommand = params.command!=null?params.command:"";
		this._requestMethod = params.requestMethod!=null?params.requestMethod:"GET";
		_domId = params.domId!=null?params.domId:null,
		this._handleFunction = params.handleFunction!=null?params.handleFunction:this._ajaxHandler;
		_afterLoadFunction = params.afterLoadFunction!=null?params.afterLoadFunction:null;
		_errorFunction = params.errorFunction!=null?params.errorFunction:null;
		
		if(this._requestPath==null) return false;
		this._requestPath += "?command=" + this._requestCommand;
		this._requestPath += (this._addparam!=null)?"&param="+this._addparam:"";
				
		_req = (window.XMLHttpRequest)
				? new XMLHttpRequest()
				: ((window.ActiveXObject)? new ActiveXObject("Microsoft.XMLHTTP"):false);
					
		_req.open(this._requestMethod,this._requestPath,true);
		_req.onreadystatechange = this._ajaxHandler;
		_req.send(null);		
	},
	
	/**
	 * the deafault response handler
	 * replaces the content of the DOM id by the response text
	 * 
	 */
	_ajaxHandler: function(){
		var _domNode = _domId!=null?document.getElementById(_domId):document.getElementsByTagName('BODY')[0];
		if(_req.readyState==4){
			if(_req.status==200){
				_domNode.innerHTML = ""; // garbage collection
				_domNode.innerHTML = _req.responseText;
				if(_afterLoadFunction!=null){ _afterLoadFunction(); }
			}else{
				_domNode.innerHTML = ""; // garbage collection
				_domNode.innerHTML = "An Error occured. AJAX- Server not reachable.";
				if(_errorFunction!=null){ _errorFunction(); }
			}
		}
	}	
}
