var net = new Object();

net.ERROR = false;


net.ContentLoader=function(){
}

net.ContentLoader.prototype={
	
	createXMLHttpRequest:function(){
		if(this.req != null) {
			this.req.abort; //Abbruch falls die Instanz des Objekt schon exitiert. 
			this.req = null;
		//	alert("XMLHttpRequest exists");
		}
		
		try {
			// IE mit neuer XML Lib
			this.req = new ActiveXObject("MSXML2.XMLHTTP");
		}
		catch (Error) {
			try {
				// IE mit alter XML Lib
				this.req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (Error) {
				// andere Browser
				this.req = new XMLHttpRequest();
			}
		}

	},

	openGET:function(url,onload,onerror){
		this.onload=onload;
		this.onerror=(onerror) ? onerror : this.defaultError;
		this.createXMLHttpRequest();
		//Weiterverarbeiten des Objektes
		if (this.req){	
			try {
				var loader=this;

				var now = new Date(); 
				url = url + '&time=' +  new Date().getTime(); // caching austricksen

				//alert("sendGET: "+url);
				this.req.open('GET', url, true);
				//caching deaktivieren
				this.req.setRequestHeader('If-Modified-Since', 'Sat, 29 Oct 1994 00:00:00 GMT'); 
				this.req.setRequestHeader('Expires', 'Sat, 29 Oct 1994 00:00:00 GMT'); 
				this.req.setRequestHeader('Last-Modified', 'Sun, 30 Oct 1994 00:00:00 GMT'); 
				this.req.setRequestHeader('Cache-Control', 'no-store'); 
				this.req.setRequestHeader('Cache-Control', 'must-revalidate'); 
				this.req.setRequestHeader('Cache-Control', 'proxy-revalidate'); 
				this.req.setRequestHeader('Cache-Control', 'max-age=0'); 
				this.req.setRequestHeader('Cache-Control', 'post-check=0'); 
				this.req.setRequestHeader('Cache-Control', 'pre-check=0'); 
				this.req.setRequestHeader('Pragma', 'no-cache'); 
				
				this.req.onreadystatechange=function(){
					//Aufruf der übergegebenen Methode zur 
					//Weiterverarbeitung des XML
					loader.onReadyGET.call(loader); 
				}
	
				this.req.send(null);
			}
			catch (err){
				this.onerror.call(this);
			}
		}
		else {
			alert("loadXMLDoc - kein req");
		}
	},
	openPOST:function(onload,onerror,xml, ressourceToOpen, optionalParams){
		this.onload=onload;
		this.onerror=(onerror) ? onerror : this.defaultError;
		this.createXMLHttpRequest();
	
		if (this.req){
			
			try {
				var loader=this;
				params = "";
				if (xml) {
					params += "xml="+encodeURIComponent(unescape(encodeURIComponent(xml)));
				}
				if (typeof optionalParams != 'undefined') {
					params += optionalParams
				}

				this.req.open('POST', ressourceToOpen, true);
				
				/*if (typeof ressourceToOpen != 'undefined') {
					this.req.open('POST', ressourceToOpen, true);
				}
				else {
					this.req.open('POST', "change.php", true);
				}*/				
				this.req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; Charset=utf-8');
				this.req.onreadystatechange=function(){
					//Aufruf der übergegebenen Methode zur 
					//Weiterverarbeitung des XML
					loader.onReadyPOST.call(loader); 
				}
				this.req.send(params);			
			}
			catch (err){
				alert("ERROR: readyState: " + this.req.readyState + "\n status: "+this.req.status);
			}
		} else {
			alert("this.req = null");
		}
	},
	/**
	 * Handhabt die Änderung einen Request-Objekt Status
	 */
	onReadyGET:function(){
		if (this.req) {
			if (this.req.readyState==4){//falls Anfrage erfolgreich beendet wurde
				if (this.req.status) {
					var httpStatus = this.req.status;
					//falls Anfrage Ergebnis liefert 
					if (httpStatus==200 || httpStatus==0){
						this.onload.call(this);//Weiterverarbietungsfkt. aufrufen
					}else{//Falls Anfrage falsche URL bekommen hat
						this.onerror.call(this);//Fehlerfunktion aufrufen
					}
				}
			}
		}
	},

	onReadyPOST:function(){
		var ready = this.req.readyState;
//		alert("onPost " + ready);
		if (ready==4){//falls Anfrage erfolgreich beendet wurde

			var httpStatus = this.req.status;
			//falls Anfrage Ergebnis liefert 
			if (httpStatus==200 || httpStatus==0){
			
				//unsuccessful
				if (this.req.responseText == "" || this.req.responseText == null) {
					//displayFeedBackMessage("WEB_MSG_SAVE_UNSUCCESSFULL",null,null,onload);
					alert("Post unsuccessfull");
				}
				//successful
				else {
					if (this.onload != null) {
						//if(this.req.responseXML)encodeURIinXML(this.req.responseXML);
						this.onload.call(this);//Weiterverarbietungsfkt. aufrufen
					}
					
				}
			}
			else{//Falls Anfrage falsche URL bekommen hat
				 alert('There was a problem with the request.' + this.req.responseText);
				//this.onerror.call(this);//Fehlerfunktion aufrufen
			}
		}
	},

	/**
	 * Funktion zur Behandlung eines Fehlers
	 * Gibt des Fehler im Overlay Container mit details aus.
	 */
	defaultError:function(){
		net.ERROR = true;
/*		var close = "<a href='javascript:closeInfoLayer();'>"
		"<img src='img/close_big.png' class='pic'/></a><br/>";
		document.getElementById("infolayer").style.visibility = "visible";
		document.getElementById("overnavi").style.visibility = "hidden";
		document.getElementById("buttons").style.visibility = "hidden";
		document.getElementById("infohead").innerHTML ="Error"+close;
		var err ="There is a connection problem!<br>"
		+"Please send the admin the following message:<br>" 
		+"<br><b>Error message</b>"
		+"<br>readyState:"+this.req.readyState
		+"<br>status: "+this.req.status
		+"<br>headers: "+this.req.getAllResponseHeaders()
		+"<br>onload function: "+this.onload
		+"<br>ID: "+this.id
		+"<br>isQueue: "+this.isQueue
		+"<br>URL: "+this.url;
		document.getElementById("layercont").innerHTML = err;
	*/			
	},
	
	cleanup:function() {
		if (this.req) {
			this.req.onreadystatechange = function(){}; 
			//this.req.onreadystatechange = null; //geht nicht in IE
			this.req = null;
		}
		this.onload=null;
		this.onerror=null;
	}
}
