/*******************************************************************
 *
 *		AJAX-Engine (universell)
 *		last Modified 2008-09-04
 *		Version: 1.1
 *
 *		changelog:
 *		- ver?nderte Dynamisierung, es kann jetzt der Response gleich in
 *		   ein Element mit vergebener ID gespeichert werden
 *		- bevorzugt werden hierf?r DIV-Elemente
 *		- es m?ssen jetzt die URL, die METHODE, das ANZEIGEELEMENT 
 *		   und der IMAGEPFAD mit angegeben werden
 *
 *******************************************************************/

/*******************************************
 *		AJAX - Klasse
 ********************************************/

 
 
 
function Ajax( sInUrl, sInMethod, oInViewElement, sInPreloader ) 
{
	//Eigenschaften deklarieren und initialisieren
	this.sUrl = sInUrl;
	this.sParams = " ";
	this.sMethod = sInMethod;
	this.onSuccess = null;
	this.oResponse = new Object();
	this.oResponse.text = " ";
	this.oResponse.xml = " ";
	this.oResponse.state = 0;
	this.oViewElement = typeof( oInViewElement ) == "object" ? oInViewElement: document.getElementById( oInViewElement );
	this.nLoadingTime = 50;
	this.sPreloader = sInPreloader;
	this.withJSON = false;
	this.oJSON = new Object();
	this.oJSON.text = "";
	this.oJSON.object = null;
}

/********************************************
 * SETTER - Funktionen
 ********************************************/
Ajax.prototype.setUrl = function( sInUrl )
{	this.sUrl = sInUrl;	}
Ajax.prototype.setParams = function( sInParams )
{	this.sParams = sInParams;	}
Ajax.prototype.setMethod = function( sInMethod )
{	this.sMethod = sInMethod;	}
Ajax.prototype.setViewElement = function( sElementName )
{	this.oViewElement = document.getElementById( sElementName );	}
Ajax.prototype.setLoadingTime = function( nInLoadingTime )
{	this.nLoadingTime = nInLoadingTime;	}
Ajax.prototype.setPreloader = function( sInPreloader )
{	this.sPreloader = sInPreloader;	}
Ajax.prototype.setJSON = function( sJSONtext )
{	
	this.oJSON.text = sJSONtext; 
	if( typeof(JSON) != 'undefined' )
		this.oJSON.object = JSON.parse( sJSONtext );
}

/********************************************
 * GETTER - Funktionen
 ********************************************/
Ajax.prototype.getUrl = function( )
{	return this.sUrl;	}
Ajax.prototype.getParams = function( )
{	return this.sParams;	}
Ajax.prototype.getMethod = function( )
{	return this.sMethod;	}
Ajax.prototype.getResponseText = function( )
{	return this.oResponse.text;	}
Ajax.prototype.getResponseXML = function( )
{	return this.oResponse.xml;	}
Ajax.prototype.getResponseState = function( )
{	return this.oResponse.state;	}
Ajax.prototype.getViewElement = function( )
{	return this.oViewElement;	}
Ajax.prototype.getLoadingTime = function( )
{	return this.nLoadingTime;	}
Ajax.prototype.getPreloader = function( )
{	return this.sPreloader;	}
Ajax.prototype.getJSON = function( sType )
{	return this.oJSON[sType];	}

/********************************************
 * JSON-SET - Funktionen
 ********************************************/
Ajax.prototype.withJSON = function( )
{	this.withJSON = true;	}
Ajax.prototype.noJSON = function( )
{	this.withJSON = false;	}
 
/********************************************
 * Funtktion doRequest()
 * Return: _Object_
 ********************************************/
Ajax.prototype.doRequest = function() 
{
	//?eberpruefen der Angaben
	if ( !this.sUrl ) 
	{
		this.onError( "Es wurde kein URL angegeben. Der Request wird abgebrochen." );
		return false;
	}
	if ( !this.sMethod ) 
	{	this.sMethod="GET";	} 
	else 
	{	this.sMethod = this.sMethod.toUpperCase();	}
	
	//Zugriff auf Klasse f?r readyStateHandler erm?glichen
	var o_this = this;
	
	//XMLHttpRequest-Objekt erstellen
	var oXmlHttpRequest = getXMLHttpRequest();
	if ( !oXmlHttpRequest ) 
	{
		this.onError("Es konnte kein XMLHttpRequest-Objekt erstellt werden.");
		return false;
	}
	
	// Kopie von sich selber anlegen
	var cp_Self = this;
	//Fallunterscheidung nach ?bertragungsmethode
	switch ( this.sMethod ) 
	{
		case "GET": 
			oXmlHttpRequest.open( this.sMethod, this.sUrl + "?" + this.sParams, true);
			oXmlHttpRequest.onreadystatechange = function() { readyStateHandler( cp_Self ) }; // ?bergabe des Klones
			oXmlHttpRequest.send(null);
			break;
		case "POST": 
			oXmlHttpRequest.open( this.sMethod, this.sUrl, true);
			oXmlHttpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
			oXmlHttpRequest.setRequestHeader( 'Content-length', this.sParams.length );
			oXmlHttpRequest.setRequestHeader( 'Connection', 'close' );
			oXmlHttpRequest.onreadystatechange = function() { readyStateHandler( cp_Self ) };
			oXmlHttpRequest.send( this.sParams );
			break;
	}
	
	//Private Methode zur Verarbeitung der erhaltenen Daten
	function readyStateHandler( oObject ) 
	{		
		if ( oXmlHttpRequest.readyState < 4 )
		{
			return false;
		}
		if ( oXmlHttpRequest.readyState == 4 ) 
		{
			// @Debug
			//alert( '[ AJAX::oXmlHttpRequest.responseText ] : ' + oXmlHttpRequest.responseText + '\[ AJAX::oXmlHttpRequest.responseXML ] : ' + oXmlHttpRequest.responseXML );
			if ( o_this.onSuccess )
			{
				o_this.onSuccess( oXmlHttpRequest.responseText, oXmlHttpRequest.responseXML );
				// @Debug
				//alert( '[AJAX::doRequest]\nonSuccess:\n' + o_this.onSuccess );
			}
			oObject.oResponse.text = oXmlHttpRequest.responseText;
			oObject.oResponse.xml = oXmlHttpRequest.responseXML;
			oObject.oResponse.state = 4;
			var sOut = '';
			for( sKey in oObject.oResponse )
				sOut += '[ ' + sKey + ' ] : ' + oObject.oResponse[sKey];
		} 
		else 
		{
			if ( o_this.onError ) 
				o_this.onError( "[" + oXmlHttpRequest.sStatus + " " + oXmlHttpRequest.sStatusText + "] Es trat ein Fehler bei der Datenbertragung auf." );
		}
	}
}
 // l?d die Daten in das HTML-Objekt
Ajax.prototype.loadData = function( idName)
{
	// l?d das Dokument ?ber die Ajax-Klasse
	this.doRequest();
	// eigenes Objekt klonen, damit der Zugriff auf die Klassenfunktionen von Sicht der Handler aus gew?hrleistet ist
	var sCloneName = "Ajax";
	while( typeof( document[sCloneName] ) != "undefined" )
	{	sCloneName = "Ajax" + Math.random();	}
	// ein Interval starten, das die Daten l?dt
	this.getViewElement( ).innerHTML = '<div style="position:relative; width:100%; margin-top:0px; text-align:center; vertical-align:middle"><img src="../../Kopievont-mobile/js/'+this.getPreloader()+'" /><br /><b>...loading...</b>'
	
	window[idName] = this;
	
	//pT.interval = window.setInterval( "Ajaxload("+pT+")", this.nLoadingTime );
	window[idName].interval = window.setInterval( "Ajaxload('"+idName+"')", this.nLoadingTime );
}
// Daten laden
function Ajaxload ( idName )
{
	// Speichert den Ajax-Klon in einer eigene Objekt-Instanz
	var oObject = window[idName];
	
	//alert(typeof(index)+" - "+typeof(window)+" - "+oObject.getUrl());
	//oObject.getViewElement( ).innerHTML = typeof(index);
	//oObject.getViewElement( ).innerHTML = oObject.getResponseState();
	//alert( oObject.getViewElement() );
	// wird ausgef?hrt wenn alle Daten  geladen sind
	if( oObject.getResponseState() == 4 )
	{
		clearInterval( oObject.interval );
		// setzen des geladenen Content in das daf?r vorgesehene Objekt
		oObject.getViewElement( ).innerHTML = oObject.getResponseText();
	}
	else
	{
		// (Preloader)
	}
}

// Error-Handler
Ajax.prototype.onError = function( sErrorMessage, nRow )
{	alert( "[AJAX Error] - Zeile "+nRow+"\n\n"+sErrorMessage );	}
/*************************************************
 * Gibt browserunabh?ngig ein XMLHttpRequest-Objekt zur?ck
 *************************************************/
function getXMLHttpRequest()
{
	if ( window.XMLHttpRequest ) 
	{
		//XMLHttpRequest f?r Firefox, Opera, Safari, ...
		return new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) 
	{
		try 
		{
			//XMLHTTP (neu) f?r Internet Explorer
			return new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch( sError ) 
		{
			try 
			{
				//XMLHTTP (alt) f?r Internet Explorer
				return new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch ( sError ) 
			{
				return null;
			}
		}	
	}
	return false;
}
