/*	*	ajax.js	v1.0
	*	XML AJAX Wrapper
	*
	*	Copyright (c) 2006, Sebastian Gieseler info@my-byte.de
	*	All rights reserverd.
*/

var AJAX_debugVars = new Array();
function Ajax () {
	this.url = "";
	this.callbackFunc = "";

	this.postVars = new Array();
	this.respData = new Array();
	
	this.http = null;
	this.debug = false;
	this.debugPath = BASE_URL+"jslib/";
	
	var objectHolder = new Array();
	
	this.init = function() {
		this.getHTTPObject();

	}
	
	this.setUrl = function (url) {
		this.url = url;
	}
	
	this.setPost = function (key, value) {
		this.postVars[key] = value;
	}
	
	this.setCallback = function (callbackfunc) {
		this.callbackFunc = callbackfunc;
	}
	
	this.send = function (mode) {
		// clear data
		this.clear();

		// loading status
		this.showLoading();
			
		// send http object
		switch (mode)  {
			case 'POST':
				this.http.open("POST", this.url, true);
				this.http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				 
				break;
			case 'GET':
			default:
				this.http.open("GET", this.url, true);
				break;
		}
			
		// handle response
		var httplocal = this.http;
			
		// object holder
		var index = objectHolder.length;
		objectHolder[index] = this;
			
		httplocal.onreadystatechange = function () {
			if (httplocal.readyState == 4) {
				objectHolder[objectHolder.length-1].handleHttpResponse();
			}
		};
			
		// clear buffer
		httplocal.send(this.buildParams());
			
		return true;
	}

	this.utf8_encode = function(instr) {
		if(instr.replace){
			instr = instr.replace(/\r\n/g,"\n");
			var retstr = "";
			for(var n=0; n<instr.length; n++){
				var c=instr.charCodeAt(n);
				if (c<128)
					retstr += String.fromCharCode(c);
				else if((c>127) && (c<2048)) {
					retstr += String.fromCharCode((c>>6)|192);
					retstr += String.fromCharCode((c&63)|128);
				}
				else {
					retstr += String.fromCharCode((c>>12)|224);
					retstr += String.fromCharCode(((c>>6)&63)|128);
					retstr += String.fromCharCode((c&63)|128);
				}
			}
			return retstr;
		}
	}
	
	this.handleHttpResponse = function () {
		
		// loading status
		this.hideLoading();
		
		result = this.http.responseText;
		
		if (!result || result == "") {
			if (this.debug) {
				AJAX_debugVars = new Array;
				AJAX_debugVars[0] = 1;
				AJAX_debugVars[1] = "Es wurden keine Daten ausgegeben.";
				var fen = window.open(this.debugPath+"debug.html", "ajaxdebug", "height=600, width=600");
				fen.focus();
			}
			return false;
		}
		
		// using extended xml parser class
		var parser = new XMLParser;
		var doc = null;
		
		try {
			parser.parse(result);
			doc = parser.doc;
		}
		catch (e) {
		  // handle exception
			AJAX_debugVars = new Array;
			AJAX_debugVars[0] = 1;
			AJAX_debugVars[1] = e.message+"<br><br>"+result;
			var fen = window.open(this.debugPath+"debug.html", "ajaxdebug", "height=600, width=600");
			fen.focus();
		}
		
		if (doc != null) {
			// get the root element
			var root = doc.getDocumentElement();
			
			if((typeof(root) == "object") && root.childNodes.length>0){
				for (var x=0;x<root.childNodes.length;x++){
					var child = root.childNodes[x] ? root.childNodes[x] : false;
					if(child){
						var childName = child.nodeName ? child.nodeName : false;
						if((typeof(child) == "object") && !child.firstChild.nodeValue){
							this.respData[childName] = this.getRecursive(child, childName);
						}
						else{
							this.respData[childName] = child.firstChild.nodeValue;
						}
					}
				}
				// recall callback
				eval(this.callbackFunc);
				this.http = null;
			}
			else{
				AJAX_debugVars = new Array;
				AJAX_debugVars[0] = 0;
				AJAX_debugVars[1] = "cant fetch XML-Object";
				var fen = window.open(this.debugPath+"debug.html", "ajaxdebug", "height=600, width=600");
				fen.focus();
			}

			if (this.debug) {
				AJAX_debugVars = new Array;
				AJAX_debugVars[0] = 0;
				AJAX_debugVars[1] = "Das XML-Dokument wurde ohne Fehler durchlaufen";
				AJAX_debugVars[2] = this.respData;
				var fen = window.open(this.debugPath+"debug.html", "ajaxdebug", "height=600, width=600");
				fen.focus();
			}
			
			return true;
		}
	}
	
	this.getRecursive = function(child, key) {
		try {

			if((typeof(child) == "object") && !child.firstChild.nodeValue){
				var retArray = new Array();
				if(child.childNodes){
					for (var x = 0; x < child.childNodes.length; x++) {
						var recChild = child.childNodes[x] ? child.childNodes[x] : false;
						if(recChild){
							var childName = recChild.nodeName ? recChild.nodeName : false;
							if((typeof(recChild) == "object") && recChild.firstChild && !recChild.firstChild.nodeValue){
								retArray[childName] = this.getRecursive(recChild, childName);
							}
							// eval action
							else if(key == 'jsEval'){
								eval(recChild.firstChild.nodeValue);
							}
							// normal value		
							else{
								retArray[childName] = recChild.firstChild ? recChild.firstChild.nodeValue : null;
							}
						}
					}
				}
				return retArray;
			}
			else{
				return child.firstChild ? child.firstChild.nodeValue : null;
			}
		}
		catch (e) {
		  // handle exception
			AJAX_debugVars = new Array;
			AJAX_debugVars[1] = e.message+"<br><br>"+result;
			var fen = window.open(this.debugPath+"debug.html", "ajaxdebug", "height=600, width=600");
			fen.focus();
		}

	}

	this.get = function (index) {
		try {
			if(this.respData && this.respData[index]){
				return this.respData[index];
			}
			else{
				return false;
			}
		}
		catch (e) {
		  // handle exception
			AJAX_debugVars = new Array;
			AJAX_debugVars[1] = "Error on building respData Array";
			var fen = window.open(this.debugPath+"debug.html", "ajaxdebug", "height=600, width=600");
			fen.focus();
		}

	}

	this.buildParams = function () {
		var retString = "";
		for (var key in this.postVars) {
			retString += (retString != "" ? "&" : "");
			retString += key + "=" + escape(this.utf8_encode(this.postVars[key]));
		}
		return retString;
	}
	
	this.getHTTPObject = function () {
	  var xmlhttp;
	  /*@cc_on
	  @if (@_jscript_version >= 5)
	    try {
	      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	      try {
	        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	      } catch (E) {
	        xmlhttp = false;
	      }
	    }
	  @else
	  xmlhttp = false;
	  @end @*/
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
			try {
				xmlhttp = new XMLHttpRequest();
			} catch (e) {
				xmlhttp = false;
			}
		}
		
		this.http = xmlhttp;
		return true;
	}
	
	this.clear = function () {
		this.respData = new Array();
		this.outVars = new Array();
		this.outValues = new Array();
	}
	
	this.showLoading = function () {
		var ladeStatus = document.createElement("div");
		ladeStatus.setAttribute("id", "ladeStatus");
		ladeStatus.innerHTML = "loading...";
		ladeStatus.style.backgroundColor= "red";
		ladeStatus.style.font= "bold 10pt verdana, tahoma, arial, sans-serif";
		ladeStatus.style.color= "white";
		ladeStatus.style.padding= "6px";
		ladeStatus.style.position= "absolute";
		ladeStatus.style.top= "6px";
		ladeStatus.style.right= "6px";
		document.getElementById('CONTENT').appendChild(ladeStatus);
	}
	
	this.hideLoading = function () {	
		document.getElementById('CONTENT').removeChild(document.getElementById("ladeStatus"));
	}
}

function handleResponse (obj) {
	obj.handleHttpResponse();
}
