﻿// AJAX-Funktionen für KRW Online-Automation V1.0 zur xmlHTTT-Kommunikation mit einem PHP-Webserver
//-------------------------------------------------------------------------------------------------------------------------------------------
// Start der Kommunikation durch externen Aufruf der Funktion 'sendRequest'
// Bei erfolgreicher Antwort wird die externe Funktion 'processData'  aufgerufen
// Bei fehlerhafter Kommunikation wird die externe Funktion 'processError' aufgerufen
//--------------------------------------------------------------------------------------------------------------------------------------------
// V1.0  -- 6.6.2006 -- getestet und ok


  // global xmlhttprequest object
	var xmlHttp = false;
	
	// constants
	var REQUEST_GET		= 0;
	var REQEST_POST		= 2;
	var REQUEST_HEAD		= 1;
	var REQUEST_XML		= 3;
	
	// Diese Funktion wird von 'sendRequest' zwecks Initialisierung der xml-HTTP-Verbindung aufgerufen
	function getXMLRequester( )
		// erzeugt ein neues xmlhttprequest-Objekt und gibt entweder dieses Objekt oder 'false' zurück
	{
		var xmlHttp = false;
		var strHTTPerror="";
		
		// try to create a new instance of the xmlhttprequest object		
		try
		{
			// Internet Explorer
			if( window.ActiveXObject )
			{
				for( var i = 5; i; i-- )
				{
					try
					{
						// loading of a newer version of msxml dll (msxml3 - msxml5) failed
						// use fallback solution
						// old style msxml version independent, deprecated
						if( i == 2 )
						{
							xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" ); 	
						}
						// try to use the latest msxml dll
						else
						{
							
							xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
						}
						break;
					}
					catch( excNotLoadable )
					{						
						xmlHttp = false; 
					}
				}
			}
			// Mozilla, Opera und Safari
			else if( window.XMLHttpRequest )
			{
				xmlHttp = new XMLHttpRequest();
			}
		}
		// loading of xmlhttp object failed
		catch( excNotLoadable )
		{
			xmlHttp = false;
		}
		return xmlHttp ;
	}
	
	//Diese Funktion wird extern aufgerufen, wenn eine xml-HTTP-Anfrage erfolgen soll
	function sendRequest( strSource, intID, intType,  strData )
	// sendet eine HTTP-Anfrage an den Server und setzt einen Zeiger für das Ereignis 'onreadystatechange', also bei Wechsel des Ready-Status, auf die Funktion 'processResponse'.
	// param strSource : 	DatenQuelle; dies ist in der Regel eine PHP-Datei, die auf die Anfrage antworten kann
	// param intType : 	Anfragetyp, mögliche Werte sind: REQUEST_GET, REQUEST_POST, REQUEST_XML, REQUEST_HEAD default REQUEST_GET
	// param intID :		ID der Anfrage; zur Unterscheidung verschiedener Antworten auf verschiedene Anfragen
	// param strData :		Anfrage-String, der an die Datenquelle übergeben wird. optional
	{
		if( !strData )
			strData = '';
	
		// default type (0 = GET, 1 = xml, 2 = POST )
		if( isNaN( intType ) )
			intType = 0; // GET
	
		// previous request not finished yet, abort it before sending a new request
		if( xmlHttp && xmlHttp.readyState )
		{
			xmlHttp.abort( );
			xmlHttp = false;
		}
			
		// create a new instance of xmlhttprequest object
		// if it fails, return
		if( !xmlHttp )
		{
			xmlHttp = getXMLRequester( );
			if( !xmlHttp )
				return;
		}
		
		// parse query string
		if( intType != 1 && ( strData && strData.substr( 0, 1 ) == '&' || strData.substr( 0, 1 ) == '?' ) )
			strData = strData.substring( 1, strData.length );

		// data to send using POST
		var dataReturn = strData ? strData : strSource;
		
		switch( intType )
		{
			case 1:	// xml
				strData = "xml=" + strData;
				break;
			case 2: // POST
				// open the connection 
				xmlHttp.open( "POST", strSource, true );
				xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
				xmlHttp.setRequestHeader( 'Content-length', strData.length );
				break;
			case 3: // HEAD
				// open the connection 
				xmlHttp.open( "HEAD", strSource, true );
				strData = null;
				break;
			default: // GET
				// open the connection 
				var strDataFile = strSource + (strData ? '?' + strData : '' );
				xmlHttp.open( "GET", strDataFile, true );
				strData = null;
		}
		
		// set onload data event-handler
		xmlHttp.onreadystatechange = new Function( "", "processResponse(" + intID + ")" ); 
		// send request to server
		xmlHttp.send( strData );	// param = POST data
		
		return dataReturn;
	}
	
	//Diese Funktion wird bei erfolgter Anfrage vom Ereignis 'onreadystatechange', also bei Wechsel des Ready-Status, aufgerufen.
	function processResponse( intID )
	// behandelt die Statuswechsel der xml-HTTP-Verbindung und 
	//		- ruft nach vollständigem Empfang die extern zu programmierende Funktion 'processData' auf
	//		- ruft im Fehlerfall die extern zu programmierende Funktion 'processError' auf
	// param intID:		ID der Antwort; identisch mit der ID der Anfrage 
	{
		// status 0 UNINITIALIZED open() has not been called yet.
		// status 1 LOADING send() has not been called yet.
		// status 2 LOADED send() has been called, headers and status are available.
		// status 3 INTERACTIVE Downloading, responseText holds the partial data.
		// status 4 COMPLETED Finished with all operations.
				//alert(xmlHttp.readyState + " " + xmlHttp.status);
		switch( xmlHttp.readyState )
		{
			// uninitialized
			case 0:
				break;
			// loading
			case 1:
				break;
			// loaded
			case 2:
				break;
			// interactive
			case 3:
				break;
			// complete
			case 4:	
				// check http status
				if( xmlHttp.status == 200 )	// success
				{
					processData( xmlHttp.responseText, intID );
				}
				// loading not successfull, e.g. page not available
				else
				{
					if( window.handleAJAXError )
						handleAJAXError( xmlHttp, intID );
					else
						strHTTPerror = ( xmlHttp.status + "_" + xmlHttp.statusText ) ;
						processError(strHTTPerror);
				}
		}
	}
	

// end AJAX	







