/***********************************************************************
*              Author: Klaus Hentschel [ All rights reserved ].                        				   *
************************************************************************
* Copyright (C) by Klaus Hentschel, http//javarea.de
*
* www: http://javaera.de.de
*
* Dieses Script ist Bestandteil des JavBB 2.0.0
*
* - AjaxRequest.class.js
*
* - AjaxRequest() 						- init class
* - openGetRequest() 					- get request senden
* - openPostRequest() 				- post request senden 
* - openXMLHttpRequest() 			- anlegen des XMLHttpRequest 
* - handleResponse() 					- handle response 
* - indicators() 						- blendet eine Ladestandsanzeige ein/aus
***********************************************************************/


function AjaxRequest() 
{
  this.xmlHttpRequest = null;
  var topicID = 0;
	
  /**
    * openGetRequest()
	* @param: <string> url
	* @param: <int> ID
	* @param: <func> callbackFunction
	* @return: <boolean> true/false
     **/
  this.openGetRequest = function(url, id, callbackFunction) 
	{
   	if (this.openXMLHttpRequest(id)) 
		{
			this.callbackFunction = callbackFunction;
			this.xmlHttpRequest.open('GET', url, true);
			this.xmlHttpRequest.send(null);
			return true;
		}
		return false;
	}

  /**
    * openPostRequest()
	* @param: <string> url
	* @param: <string> data
	* @param: <int> ID
	* @param: <func> callbackFunction
	* @return: <boolean> true/false
     **/
	this.openPostRequest = function(url, data, id, callbackFunction) 
	{		  
		//alert('url: '+ url + '\ndata: ' + data +'\nid: ' + id +'\ncallback: ' + callbackFunction);
		if (this.openXMLHttpRequest(id)) 
		{			
			this.callbackFunction = callbackFunction;			
			this.xmlHttpRequest.open('POST', url, true);
			this.xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			this.xmlHttpRequest.send(data);			
			return true;
		}
		return false;
	}

  // oeffnen des xmlhttprequest Objekts
  this.openXMLHttpRequest = function(ID) 
	{		  
    topicID = ID;    
		if (this.xmlHttpRequest) 
		{
			if (this.xmlHttpRequest.readyState != 0 && this.xmlHttpRequest.readyState != 4) 
				return false;

			this.xmlHttpRequest.abort();
		}
		else
			this.xmlHttpRequest = false;

		//Anlegen eines neuen  xmlhttprequest Objekts        
		try 
		{
			// Internet Explorer
			if (window.ActiveXObject) 
			{
				for (var i = 5; i; i--) 
				{
					try 
					{
						// laden einer neuen Version msxml dll version 						
						//if (i == 2) 
						//if (i == 4) 
								this.xmlHttpRequest = new ActiveXObject( "Microsoft.XMLHTTP" );    
						//else // letzte msxml dll verwenden										
								//this.xmlHttpRequest = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );

						break;
					} 
					catch (excNotLoadable) {                        
						this.xmlHttpRequest = false;
					}
				}
			}
			else if (window.XMLHttpRequest) 
			{
				// Mozilla, Opera und Safari
				this.xmlHttpRequest = new XMLHttpRequest();
				if (this.xmlHttpRequest.overrideMimeType) 
					this.xmlHttpRequest.overrideMimeType('text/xml');	
			}
		} 
		catch (excNotLoadable) {
			// laden des xmlhttp Objekts fehlgeschlagen
			this.xmlHttpRequest = false;
		}
   
    // event listener
		if (this.xmlHttpRequest)
		{
			this.xmlHttpRequest.onreadystatechange = this.handleResponse;
			return this.xmlHttpRequest;
		}
		else 
			return false;
	}

	this.handleResponse = function() 
	{
		// status 0 UNINITIALIZED open() init
		// status 1 LOADING send() Warteschlange
		// status 2 LOADED send() header und status
		// status 3 INTERACTIVE Downloading, responseText holen 
		// status 4 COMPLETED alle Aktionen beendet
		if (ajaxRequest)
		{      
			switch(ajaxRequest.xmlHttpRequest.readyState) 
			{             
				// uninitialized
				case 0:
				// loading
				case 1:					
				  if (topicID > 0)
            indicators(true, topicID);
			
				// loaded
				case 2:
				// interactive
				case 3:
				break;
				// complete
				case 4:            
					// check http status          
					if (ajaxRequest.xmlHttpRequest.status == 200) 
					{            
            if (topicID > 0)
              setTimeout('indicators(false, '+topicID+');',300);              
            else
							indicators(false, topicID);
            
						if (ajaxRequest.callbackFunction)
						{
							// call callback function              
							if (ajaxRequest.xmlHttpRequest.responseText)
								ajaxRequest.response = ajaxRequest.xmlHttpRequest.responseText;
							else if (ajaxRequest.xmlHttpRequest.responseXML)
								ajaxRequest.response = ajaxRequest.xmlHttpRequest.responseXML;
                              
							eval(ajaxRequest.callbackFunction);
						}
						else if (ajaxRequest.xmlHttpRequest.responseText != '')
						{	// debugmodus
							//alert(ajaxRequest.xmlHttpRequest.responseText);
						}
					} 
					else 
					{
						alert("ajaxRequest Fehler:\n Http status = " + ajaxRequest.xmlHttpRequest.status+"\n" + ajaxRequest.xmlHttpRequest.statusText);
					}					
			}
		}
  }
  
  /**
     * Author: Klaus Hentschel <javarea.de>
     * Description: blendet eine Ladestandsanzeige ein/aus
     * @param: <Boolean> turn
	* @param: <int>topicID
     **/
	indicators = function(turn, topicID)
  {      
		var indicator = document.getElementById('notification_'+topicID);
    var indicator2 = document.getElementById('displ_'+topicID);
    
		if (turn)
			indicator.innerHTML='<img src="./images/indicator.gif" alt="indicator.gif" /> Lade ..... ';

		if (indicator != null)
    {		       
      indicator.style.display = turn ? "block" : "none";
      indicator2.style.display = turn ? "none" : "block";
    }
  }   
}
   
var ajaxRequest = new AjaxRequest();
