/**
 * @author: Anatolij Rau
 * @copyright: Anatolij Rau
 * @access: 2009-11-11
 * @version: 1.1.2
 **/

function ajaxClass() {
	this.evalScripts = true; // Javascript innerhalb des Requests ausführen
	this.responseType = 'replace'; // Was soll mit dem ergebniss passieren (replace | append )
	this.method = 'GET'; // Request methode (GET | POST)
	this.charset = 'UTF-8';

	this.header = new Object();
	this.header['Accept-Charset'] = this.charset;
	this.header['Content-Type'] = 'text/html';
	
	this.load = function(url, target) {
		if (typeof target == 'string' || typeof target == 'number' ) {
			var tmp = false;
			if (tmp = document.getElementById(target)) target = tmp;
		}
		
		var xmlhttp;
		if (window.XMLHttpRequest) {
		  // IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
	  }
		else {
			if (window.ActiveXObject) {
				// IE6, IE5
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			} else {
				alert("Browser does not support XMLHTTP!");
			}
		}
		
		xmlhttp.open(this.method, url, true);
		for (var i in this.header) xmlhttp.setRequestHeader(i, this.header[i]);
		
		xmlhttp.onreadystatechange=function() {
			if(xmlhttp.readyState==4) {
				if (xmlhttp.status == 404)
					alert('Target not found "'+url+'"!');
				else {
					if (typeof target == 'string') {
						switch (target) {
							case 'ibox':
								iBoxObj.display(xmlhttp.responseText);
								break;
							case 'alert':
								alert(xmlhttp.responseText);
								break;
							case 'nothing':
								break;
						}
					} 
					else if (typeof target == 'function') {
						target(xmlhttp.responseText);
					}
					else {
						switch(this.responseType) {
							case 'replace':
								target.innerHTML = xmlhttp.responseText;
								if (this.evalScripts) this.runRequestScripts(target);
								break;
							case 'append':
								target.innerHTML += xmlhttp.responseText;
								if (this.evalScripts) this.runRequestScripts(target);
								break;
						}
						target.innerHTML = xmlhttp.responseText ? xmlhttp.responseText : 'empty';
						if (this.evalScripts) this.runRequestScripts(target);
						
					}
				}
			}
		};
		
		xmlhttp.send(null);
	};
	
	this.runRequestScripts = function(container) {
		var end = container.childNodes.length;
		for (var i = 0; i < end; i++ ) {
			if (container.childNodes[i].tagName == 'SCRIPT') {
				eval(container.childNodes[i].innerHTML);
			}
		}
	};
}

var ajaxObj = new ajaxClass();