/*
*	AJAX class
*/

function Ajax(url, params, method){

	this.url = url;
	this.params = params;
	this.method = method ? method : "POST";
	this.onError = function(msg){ alert(msg); }
	
	this.doRequest = function(onSuccess){
		
		if(!this.url){
			this.OnError("keine url !");
			return false;
		}
			
		if(!this.method){
			this.method = "GET";
		} else {
			this.method.toUpperCase();
		}
		
		var xmlHttpRequest = jsf_getXMLHttpRequest();
		if(!xmlHttpRequest){
			this.onError("Es konnte kein xmlHttpRequest 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.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				xmlHttpRequest.send(this.params);
				break;
		}
		
		function readyStateHandler(){
			if(xmlHttpRequest.readyState < 4){
				return false;
			}
			if( xmlHttpRequest.status == 200 || xmlHttpRequest.state == 304){
				if(onSuccess){
					onSuccess( xmlHttpRequest.responseText,xmlHttpRequest.responseXML);
				}
			} else {
				_this.onError("[" + xmlHttpRequest.status + xmlHttpRequest.statusText + "]" + " \nFehler bei der datenübertragung");
			}
		}
		
	}
	
	function jsf_getXMLHttpRequest(){
	
		if(window.XMLHttpRequest){
			return new XMLHttpRequest();
		} else {
			if(window.ActiveXObject){
				try {
					// neuer IE
					return new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e){
					try {
						// alter IE
						return new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e){
						return null;
					}
				}
			}
			
		}
		return null;
	}
	
}


