var clientOutputID, clientLoadingID;
var ajaxLogID = 'ajaxLog';

function createAjaxPeriodicalUpdater(url, clientOutput){
	var myAjax = new Ajax.PeriodicalUpdater(
	    {success: clientOutput}, 
		url, 
		{method: 'post'}
	);
}

function createAjaxRequest(url, clientOutput, clientLoading, showLoadingState) {
	clientOutputID = clientOutput;
	clientLoadingID = clientLoading;
	LoadingState = showLoadingState;
	
	// Warten-Bild laden
	if(LoadingState)
	{
		$(clientOutputID).innerHTML = "Wird geladen";
	    Element.show( $(clientLoading) );    
    }
    
	var myAjax = new Ajax.Request(
		url,
		{ 
		    method: 'post',                       
			onSuccess:   output,     
			onFailure:   show_failure,
          	onException: show_exception
		}
	);
}

function createFormBasedAjaxRequest(form, url, clientOutput, clientLoading, showLoadingState) {
	clientOutputID = clientOutput;
	clientLoadingID = clientLoading;
	LoadingState = showLoadingState;
	
	// Warten-Bild laden
	if(LoadingState)
	{
		$(clientOutputID).innerHTML = "Wird geladen";
	    Element.show( $(clientLoading) );    
    }   
    
	var myAjax = new Ajax.Request(
		url,
		{ 
		    method: 'post',                 
			parameters: Form.serialize($(form)),        
			onSuccess:   output,     
			onFailure:   show_failure,
          	onException: show_exception
		}
	);
}

function output( requestObject ) {
	if ( requestObject.responseText.search(/error/) >= 0 )
    	log_error("Error in PHP: " + requestObject.responseText);
	else	
		$(clientOutputID).innerHTML = requestObject.responseText;
	if(LoadingState)
		Element.hide( $(clientLoadingID) );     
}


/* FEHLERBEHANDLUNG */

function show_failure( requestObject ) {
    log_error("Error from Server:" + requestObject.statusText);
}

function show_exception( requestObject, ex) {
 log_error("Keine AJAX-Anfrage an " + requestObject.url 
    + " möglich: " + ex);
}

function log_error ( t ) {
  $(ajaxLogID).innerHTML += t + "<br />";
}
