/**
 * AjAX-Class
 * Diese Klasse stellt eine einfache AJAX-Klasse bereit, die fuer die 
 * Verwendung von AJAX-Requests benutzt wird.
 * 
 * Die Klasse wird zwischen <head> und </head> im HTML-Kopf zunaechst
 * eingefuegt:
 * <script type="text/javascript" src="Ls/ajax.class.js"></script>
 * 
 * Danach kann die AJAX-Klasse direkt verwendet werden.
 * 
 * @author 		Stefan Jacomeit <stefan@lamp-systems.net>
 * @version		2.1.1
 * @copyright 	LAMP systems Ltd.| Stefan Jacomeit <stefan@lamp-systems.net>
 */

/**
 * AJAX-Klasse bilden
 */
function Ajax()
{
	this.url 		= '';
	this.params 	= '';
	this.method 	= 'GET';
	this.onSuccess 	= null;
	this.onError	= function(msg) {
		alert(msg);
	}
}
/**
 * AJAX Prototyp Funktion
 */
Ajax.prototype.doRequest=function()
{
	if (!this.url) {
		this.onError("Es wurde keine URL angegeben. Der Request wird abgebrochen!");
		return false;
	}
	if (!this.method) this.method = 'GET';
	else this.method = this.method.toUpperCase();
	var xmlHttpRequest=getXMLHttpRequest();
	if (!xmlHttpRequest) {
		this.onError("Es konnte kein Request-Objekt erstellt werden.");
		return false;
	}
	var _this = this;
	switch(this.method) {
	case "GET": 	xmlHttpRequest.open(this.method, this.url+"?"+this.params, true);
					xmlHttpRequest.onreadystatechange = readyStateHandler;
					xmlHttpRequest.send(null);
					break;
	case "POST":	xmlHttpRequest.open(this.method, this.url, true);
					xmlHttpRequest.onreadystatechange = readyStateHandler;
					xmlHttpRequest.setRequestHandler('Content-Type', 'application/x-www-form-urlencode');
					xmlHttpRequest.send(this.params);
					break;
	}
	function readyStateHandler()
	{
		if (xmlHttpRequest.readyState < 4) return false;
		if (xmlHttpRequest.status == 200 || xmlHttpRequest.status == 304) {
			if (_this.onSuccess) _this.onSuccess(xmlHttpRequest.responseText, xmlHttpRequest.responseXML);
		} else {
			if (_this.onError) _this.onError("["+xmlHttpRequest.status+" "+xmlHttpRequest.statusText+"] Es trat ein Fehler bei der Datenübertragung auf.");
		}
	}
}
/**
 * Gibt Browserunabhaengig ein XMLHttpRequest-Objekt zurueck.
 */
function getXMLHttpRequest()
{
	if (window.XMLHttpRequest) return new XMLHttpRequest();
	else 
		if (window.ActiveXObject) {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch(E) {
					return null;
				}
			}
		}
	return null;
}
