﻿// JavaScript Document

var ajaxRequest = function(u, m, s){
	this.url		= u;
	this.method		= m || "GET";
	this.async		= s || true;
	this.body		= null;
	this.head		= false;

	var _this = this;

	//------------------------------------------------------------
	try{
		this.request = new XMLHttpRequest();
	}catch(e){
		try{
			this.request = new ActiceXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				this.request = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				this.request = false;
			}
		}
	}

	//------------------------------------------------------------
	this.doRequest = function(){
		if(!this.url){
			this.onError("keine URL gesetzt");
			return false;
		}
		if(!this.method){
			this.method = "GET";
		}else{
			this.method = this.method.toUpperCase();
		}
		if(!this.request){
			this.onError("kein Verbindungsobjekt gesetzt");
			return false;
		}

		this.request.open(this.method, this.url, this.async);

		if(this.method == "POST"){
			this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
		}
		if(this.head){
			for(var i=0; i<this.head.length; i+=2){
				this.request.setRequestHeader(this.head[i], this.head[i+1]);
			}
		}
		this.request.onreadystatechange = this.checkState;
		this.request.send(this.body);
	}

	//------------------------------------------------------------
	// nur zu testzwecken ... fuer die meisten funktionen umgangen
	this.onSuccess = function(txt){
		alert(txt);
	}

	//------------------------------------------------------------
	this.onError = function(msg){
		alert("Fehler: "+msg);
	}

	//------------------------------------------------------------
	// die funktion moeglicherweise ausgliedern
	this.checkState = function(){
		if(_this.request.readyState<4){
			document.getElementById("main_box").value="Daten werden geladen ...";
		}else{
			if(_this.request.status==200 || _this.request.status == 304){
				var xml = _this.request.responseXML ? _this.request.responseXML : 'keine XML-Daten';
				_this.onSuccess(_this.request.responseText,xml);
			}else{
			//	_this.onError("Fehler bei der Datenuebertragung");
			}
		}
	}
}
