/**
  * Klasse: CAjaxRequest 
  *
  */
function AjaxRequest(url, requestHandler, method, sync, headers){
	this.ID             = 123;
	this.url            = url;
	this.requestHandler = requestHandler;
	this.method         = method || "POST";
	this.sync           = sync || true;
//	this.xmlHttp        = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP"):( (window.XMLHttpRequest) ? new XMLHttpRequest() : false );
	this.xmlHttp        = (window.XMLHttpRequest) 
	                      ?
	                      new XMLHttpRequest()
	                      :
	                      ((window.ActiveXObject)
	                       ?
	                       new ActiveXObject("Msxml2.XMLHTTP.3.0")
	                       :
	                       false
	                       );
	                      
	this.headers        = headers || false;

	// Code nur einmal ausführen
	//
	if( typeof AjaxRequest._initialized == "undefined" ){
	
		// XMLHttpRequest readyStates
		//
		AjaxRequest.prototype.STATE_UNINIT   = 0; 
								 // Der Request wurde noch nicht durch open() 
		                         // ausgelöst
		
		AjaxRequest.prototype.STATE_LOADING  = 1; 
								 // Der Request wurde gestartet aber noch nicht 
		                         // abgeschickt
		                         
		AjaxRequest.prototype.STATE_LOADED   = 2; 
								 // Request wurde mit send() ausgeführt und die Antwort 
		                         // ist noch nicht eingetroffen
		                         
		AjaxRequest.prototype.STATE_ACTIVE 	= 3; 
								 // Die Übertragung der Antwort vom Server läuft 
		                         // und Teile sind schon mittels responseText bzw.
		                         // responseXML verfügbar
		                         
		AjaxRequest.prototype.STATE_COMPLETE = 4; 
								 // Der Request wurde vollständig ausgeführt
		                         
		
		// HTTP Serverstaties
		//
		AjaxRequest.prototype.HTTP_STATE_OK = 200; 
								 // Die Anfrage des Clients war erfolgreich
						
						
						
	    /**
	      * Methode: addHeader(headerName, headerValue)
	      * Fügt einen neuen header für den Request hinzu
	      *
	      * @param headerName Name des headers
	      * @param headerValue Inhalt des headers
	      */
		AjaxRequest.prototype.addHeader = function(name, value){
			if(!this.headers){
				this.headers = new Array();
			}
			this.headers[this.headers.length] = name;
			this.headers[this.headers.length] = value;
		}
		/**
		  *  Setzt die übertragungsmethode
		  */
		AjaxRequest.prototype.setMethod = function(method){
			this.method = method;
		}  

		/**
		  *  Setzt die url
		  */
		AjaxRequest.prototype.setUrl = function(url){
			this.url = url;
		}  
		
	    /**
	      * Methode: setHeader(headerName, headerValue)
	      * Löscht den Heder und fügt das eine neue Element ein
	      *
	      * @param headerName Name des headers
	      * @param headerValue Inhalt des headers
	      */
		AjaxRequest.prototype.setHeader = function(name, value){
			if(this.headers){
				delete this.headers;
			}
			this.headers = new Array();			
			this.headers[this.headers.length] = name;
			this.headers[this.headers.length] = value;
		}
		
					           
	    /**
	      * Methode: setBody(body)
	      * Setzt das body-Element für den Request
	      *
	      * @param body String mit dem Body der Anfrage
	      */
	    AjaxRequest.prototype.setBody = function(body){
	    	this.body = body;
	    }
		
	    /**
	      * Methode: setRequestHandler(reqh)
	      *
	      * @param reqh Zeiger auf die Methode/Funktion für
	      *        den RequestHandler.
		  */	
		AjaxRequest.prototype.setRequestHandler = function(reqh){
			this.requestHandler = reqh;
		}
					           
	    /**
	      * Methode: doRequest
	 	  * Beschr.: Baut alle nötigen zutaten für einen Request zusammen  
		  *          und stellt den Request an den Server.
		  *           
		  */				           	
		AjaxRequest.prototype.startRequest = function(){
			try{
				this.xmlHttp.open(this.method, this.url, this.sync);
				this.xmlHttp.onreadystatechange = this.requestHandler;
				if(this.headers){
					for(var i=0; i<this.headers.length; i+=2){
						this.xmlHttp.setRequestHeader(this.headers[i], 
						                              this.headers[i+1]);			
					}
				}
				this.xmlHttp.send(this.body);
			}catch(e){
				strError  = "Method=" + this.method + "\n";
				strError += "URL   =" + this.url + "\n";
				strError += "Sync  =" + this.sync + "\n";
				strError += "state =" + this.xmlHttp.readyState + "\n";
				strError += "status=" + this.xmlHttp.status + "\n";
				strError += "body  =" + this.body + "\n";
				//alert(strError);
				this.xmlHttp = (window.XMLHttpRequest) 
	                      ?
	                      new XMLHttpRequest()
	                      :
	                      ((window.ActiveXObject)
	                       ?
	                       new ActiveXObject("Msxml2.XMLHTTP.3.0")
	                       :
	                       false
	                       );
				setTimeout("ajaxRequest.startRequest()", 500);
			}
		}
		/**
	      * Methode: startBinaryRequest
	 	  * Beschr.: Baut alle nötigen zutaten für einen Request zusammen  
		  *          und stellt den Request an den Server.
		  *           
		  */	
		AjaxRequest.prototype.startBinaryRequest = function(body){
			this.xmlHttp.open(this.method, this.url, this.sync);
			this.xmlHttp.onreadystatechange = this.requestHandler;
			if(this.headers){
				for(var i=0; i<this.headers.length; i+=2){
					this.xmlHttp.setRequestHeader(this.headers[i], 
					                              this.headers[i+1]);			
				}
			}
			this.xmlHttp.send(body);
		}		
	AjaxRequest._initialized = true;
	}// END if(typeof CAjaxRequest._initialized == "undefined")
}// END AjaxRequest

