


// AJAX CONTAINER MIT ALLEN AJAX CALLS

function TAjaxContainer() {
	this.allElements = new Array();	
	this.addContainer = function(ajContainer) {
		this.allElements.push(ajContainer);
	}
	
	this.abortAll = function() {
		for(var i=0; i<this.allElements.length; i++) {
			this.allElements[i].abort();
		}
	}
}

var ajCont = new TAjaxContainer();

function TAjax(element, page) {
	// parameter & defaults
	this.timeOut = false;
	this.timeOutDelay = 5000; // in miliseconds
	this.page = page;
	this.showLoading = false;
	this.loadingMessage = "";
	this.allowCaching = false;
	
	if(page) {
		this.defaultLoading = '<div class="ajLoadingImage"><img src="/'+ page.getFilePath() +'/lade_m.gif" /></div>';
		this.loadingHTML = this.defaultLoading;
	} else {
		this.loadingHTML = "";
	}
	this.type = "GET";
	this.element = document.getElementById(element);
	this.req = null;
	this.async = true;
	this.hasRequest = false;
	

	// functions
	// Request type \u00E4ndern
	this.setRequestType = function(type)
		{
		 if(type.toLowerCase() == "post") {
		  this.type = "POST";
		 } else {
		  this.type = "GET";
		 }
 		}
	
	// Request-Objekt holen
	
	this.abort = function() {
	 if( this.req && this.hasRequest) {
	  this.hasRequest = false;
	  try {
	  	req.abort();
	  } catch (ms){
	  	
	  }
	 }
	}
	
	this.setCaching = function(doCache) {
	
		if(!doCache) {
			this.allowCaching = false;
		} else {
			this.allowCaching = true;
		}
		
	} 
	this.getRequest = function() 
	{
		var req;
		try{
			 req = new XMLHttpRequest();
			}
	        catch (ms){
				try{
					req = new ActiveXObject("Msxml2.XMLHTTP");
					} 
				catch (nonms){
					try{
						req = new ActiveXObject("Microsoft.XMLHTTP");
						} 
						catch (failed){
							req = null;
						}
					}  
			}
		
		// registriere ajax Klasse
		var thisElement = this;	
		//req.printer = "DIES IST DER PRINTER";
		//req.printer = thisElement;
		
		// feedback methode
		if( req ) req.onreadystatechange = function() {
			switch(req.readyState) {
			case 4:
				if(thisElement.timeOut) {
				thisElement.setContent("<div class=\"ajError\"><h3>Fehler:</h3>Das angegebene Ziel konnte nicht in angemessener Zeit geladen werden.<br />Bitte versuchen sie es zu einem sp&uuml;teren Zeitpunkt erneut.</div>");
				 break; // do nothing
				}
				
				if(req.status==200) {
				//alert(req.responseText);
					this.hasRequest = false;
					thisElement.setContent(req.responseText);
				} else if(req.status == 404) {
					thisElement.setContent("<div class=\"ajError\"><h3>Fehler:</h3>Das angegebene Ziel konnte leider nicht geladen werden</div>");
				} else if(req.status == 0) {
					thisElement.setContent("<div class=\"ajError\">Die Anfrage wurde abgebrochen.</div>");
				} else {
					thisElement.setContent("<div class=\"ajError\"><h3>Fehler (" + req.status + "):</h3>Es ist ein allgemeiner Fehler aufgetreten.</div>");
				}
				break;
	         default:
					// nothing to do
				break; 
			}
		}
		
		this.req = req;
		
	}
	
	// Element mit HTML f\u00FCllen
	this.setContent = function(html)
		{
		if(this.element) {
			if(this.timerID) clearTimeout(this.timerID);
			this.element.innerHTML = html;
			}
		}

	
	// option true o. false
	this.setLoading = function(loading, message)
		{
		if(loading) {
			this.showLoading = true;
			if(message) {
				this.loadingHTML = this.defaultLoading + '<div class="ajLoadingText">' + message + '</div>';
		 		}
		} else {
			this.showLoading = false;
		}

		}
	// HTML f\u00FCr Ladeanimation / text
	this.setLoadingHTML = function(html) 
		{
		this.loadingHTML = html;
		}


	this.showTimeOut = function() 
		{
		alert( this );
		this.setContent("<div>Timout: Daten konnten leider nicht geladen werden</div>");
		}
		
	this.open = function(targetUrl)
		{
		// DEAKTIVIERUNG DER AJAX-Funktionalit\u00E4t
		// return;
		// END
		this.hasRequest = true;
		if(! this.element) return;
		if(this.req == null) {
				this.setContent("Ihr Browser unterst\u00FCtzt das nicht...");
			} else {
				var random = 0;
				if(! this.allowCaching) {
					random = Math.round(Math.random()*10000);
				}
				if(targetUrl.indexOf("?") > 0) {
					targetUrl += "&r=" + random;
				} else {
					targetUrl += "?r=" + random;
				}
				this.req.open(this.type, targetUrl, this.async);
				this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		    	this.req.send(null);
		    	if(this.showLoading) {
		    		this.setContent(this.loadingHTML);
		    	}
		    	var ajaxObject = this;
		    	this.timerID = window.setTimeout('showTimeOut', this.timeOutDelay, this);
		    }
		}
		// time-out
	// init
	this.getRequest();
}

function showTimeOut( aj ) {
	alert( aj );
	aj.showTimeOut();
}

