/*AjaxMachine*/
// author: ovolgmann@brainpool.de
var BACS = new Object();

function changeObject(fnc, obj)
{
	fnc.call(obj); // runs the method of the object being passed in
}


function createHttpRequestObject()
{
	var httpRequest;
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		/*if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
		}*/
	} 
	else if (window.ActiveXObject)
	{ // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {
				debug(e);
			}
		}
	}
	if (!httpRequest) {
		debug('Giving up. Cannot create an XMLHTTP instance.');
		return false;
	}
	return httpRequest;
}




function AjaxManager()
{
	this.items = new Array();
	this.queue = new Array();
	this.processCount = 0;
}
AjaxManager.prototype.toString = function(){
	return 'Registrierte Requests: ' + this.items.length + ' Requests in Queue: ' + this.queue.length;
}
AjaxManager.prototype.createAjaxObject = function(params)
{
	var res = new ajaxObject(params);
	this.items.push(res);
	return this.items.length-1;
}

AjaxManager.prototype.appendRequest = function(id)
{
	this.queue.push(id);
}

AjaxManager.prototype.handleReadyStateChangeRequest = function(id)
{  
	if (this.items[id].checkState())
	{
		// pr�fen ob object erfolgreich beendet
		if (this.items[id].retry == 0)
		{
			this.queue.shift();
			 
		}else{
			// falls nicht erfolgreich, so verschieben wir das element in der qeue nach hinten, damit 
			// zuerst andere requests verarbeitet werden koennen ...
			this.queue.push(this.queue.shift());
		}
		this.processCount--;
		this.processQueue();
	}
}

AjaxManager.prototype.processQueue = function()
{
	//debug(' ### processCount: ' + this.processCount);
	if (this.processCount == 0 && this.queue.length > 0)
	{
		//debug(' ### processing next');
		this.processCount++;
		var id = this.queue[0];
		//debug(' ### processingId: ' + id);
		var req = this.items[id].getRequest();
		req.onreadystatechange = function() { BACS.ajaxManager.handleReadyStateChangeRequest(id); };
		req.send(null);
	} 
}

BACS.ajaxManager = new AjaxManager();

function StartAjaxHTTP(par1, par2, par3,par4_Callback)
{
	ao = BACS.ajaxManager.createAjaxObject({0:par1, 1:par2, 2:par3, callback:par4_Callback});
	BACS.ajaxManager.appendRequest(ao);
	BACS.ajaxManager.processQueue();
}

function ajaxObject(params)
{ 
	this.url = params["0"];
	this.elementId = params["1"];
	
	this.loader = '<center><br /><br /><img src="img/loader.gif" border="0" align="center" valign="middle" /></center>';
	this.retry = 5;
	this.Req=undefined;
	this._callBack=params["callback"];
	
	//debug('URL: => ' + this.url + ', divId: => ' + this.elementId + ', Loader: => ' + this.loader);
}

ajaxObject.prototype.callBack = function()
{
	if(typeof(this._callBack)!="undefined")
	{	  
		this._callBack(this);
	}
}
ajaxObject.prototype.getRequest = function()
{
	this.Req = createHttpRequestObject();
	this.Req.open('GET', this.url, true);
	return this.Req;
}

ajaxObject.prototype.checkState = function()
{ 
	var data = '';
	var success = false; 
	//debug(this.Req + "\n" + this.elementId + "\n" + this.loader + "\n" + 'readyState: ' + this.Req.readyState+ "\n" + 'Status: ');
	if (this.Req.readyState == 4){
		success = true;
		if (this.Req.status == 200){
			data = this.Req.responseText;
			this.retry = 0; 
			this.updateHtmlElement(data); 
			this.callBack();
		}else {
			this.retry--;
			//debug('Request failed. Remaining tries: ' + this.retry);
		}
	} else {
		data = 'Status: ' + this.Req.readyState + this.loader;
		//debug('Lade: ' + this.url + 'Status: ' + this.Req.readyState);
	}

	//if (retry) setTimeout(this.executeRequest(), 1000);
	return success;
}

ajaxObject.prototype.updateHtmlElement = function(data)
{
	if (document.getElementById(this.elementId)){
		document.getElementById(this.elementId).innerHTML = data;
	}else{
		debug('Div nicht vorhanden: '+ this.elementId);
	}
}


