/*
 * ajax.js, Version 1.0
 * XML AJAX Wrapper
 *
 * Revision history:
 *    1.0, 01 Nov 2005 : Erste Version
 *
 * Copyright (C) 2005 Robert Bernhard (bloddynewbiue@gmail.com)
 *
 * Diese Bibliothek ist nicht frei!
 * Ohne Erlaubnis des Autors sind Verteilung, Modifizierung und Einsatz
 * untersagt.
 */

var _AjaxdebugVars_ = new Array();
function Ajax () {
	this.url = ""; // The server-side script
	this.callbackFunc = "" // callback function
	this.upgradeInProgress = false; 
	
	this.inVars = new Array(); // in Variables
	this.inValues = new Array(); // in Values
	this.inLength = 0;
	
	this.outVars = new Array(); // out Variables
	this.outValues = new Array(); // out Values
	
	this.http = null;
	this.multiline = false;
	this.debug = false;
	this.debugPath = "";
	
	var glob_objectHolder = new Array();
	
	this.init = function() {
		this.getHTTPObject();
	}
	
	this.setUrl = function (url) {
		this.url = url;
	}
	
	this.setSendVar = function (vari, val) {
		var found = false;
		
		// proof if existent
		for (var x = 0; x <  this.outVars.length; x++) {
			if (this.outVars[x] === vari) {
				found = true;
				break;
			}
		}
		
		// found or not
		if (!found) {
			this.outVars[this.outVars.length] = vari;
			this.outValues[this.outValues.length] = val;
		} else {
			this.outVars[x] = vari;
			this.outValues[x] = val;
		}
	}
	
	this.setCallback = function (callbackfunc) {
		this.callbackFunc = callbackfunc;
	}
	
	this.send = function (mode) {
		if (!this.upgradeInProgress) {
			
			// append loading status
			this.showLoading();
			
			// set upgrade in progress true
			this.upgradeInProgress = true;
			
			// send http object
			switch (mode)  {
				//post
				case 'POST':
					this.http.open("POST", this.url, true);
					this.http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					break;
				
				// get
				case 'GET':
				default:
					this.http.open("GET", this.url, true);
					break;
			}
			
			// if send -> handle response by triggering xml return
			var httplocal = this.http;
			
			// create object holder
			var index = glob_objectHolder.length;
			glob_objectHolder[index] = this;
			
			httplocal.onreadystatechange = function () {
				//alert(httplocal.myData);
				if (httplocal.readyState == 4) {
					glob_objectHolder[glob_objectHolder.length-1].handleHttpResponse();
				}
			};
			
			// clear buffer
			httplocal.send(this.buildParams());
			
			return true;
			
		}
	}
	
	this.handleHttpResponse = function () {
		
		// hide loading
		this.hideLoading();
		
		result = this.http.responseText;
		//alert(result);
		
		if (!result || result == "") {
		
			if (this.debug) {
				_AjaxdebugVars_ = new Array;
				_AjaxdebugVars_[0] = 1;
				_AjaxdebugVars_[1] = "Es wurden keine Daten ausgegeben.";
				var fen = window.open(this.debugPath+"debug.html", "bla", "height=600, width=600");
			}
			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
			_AjaxdebugVars_ = new Array;
			_AjaxdebugVars_[0] = 1;
			_AjaxdebugVars_[1] = e.message+"<br><br>"+result;
			var fen = window.open(this.debugPath+"debug.html", "bla", "height=600, width=600");
		}
		
		if (doc != null) {
			// get the root element
			var root = doc.getDocumentElement();
			// singleline
			if (!this.multiline) {
				for (var x = 0; x < root.childNodes.length; x++) {
					this.inVars[this.inVars.length] = root.childNodes[x].nodeName;
					this.inValues[this.inValues.length] = (root.childNodes[x].firstChild ? root.childNodes[x].firstChild.nodeValue : "");
				}
			}
			// multiline
			else {
				for (var x = 0; x < root.childNodes.length; x++) {
					this.inVars[x] = new Array;
					this.inValues[x] = new Array;
					for (var y = 0; y < root.childNodes[x].childNodes.length; y++) {
						this.inVars[x][y] = root.childNodes[x].childNodes[y].nodeName;
						this.inValues[x][y] = (root.childNodes[x].childNodes[y].firstChild ? root.childNodes[x].childNodes[y].firstChild.nodeValue : "");
					}
				}
			}
			this.inLength = this.inVars.length;
			
			// unset upgrade in progress
			this.upgradeInProgress = false;
			
			// recall callback
			eval(this.callbackFunc);
			
			this.http = null;
		
			if (this.debug) {
				_AjaxdebugVars_ = new Array;
				_AjaxdebugVars_[0] = 0;
				_AjaxdebugVars_[1] = "Das XML-Dokument wurde ohne Fehler durchlaufen";
				_AjaxdebugVars_[2] = this.inVars;
				_AjaxdebugVars_[3] = this.inValues;
				_AjaxdebugVars_[4] = this.multiline;
				var fen = window.open(this.debugPath+"debug.html", "bla", "height=600, width=600");
				fen.focus();
			}
			
			return true;
		}
	}
	
	this.buildParams = function () {
		var tmp = "";
		for (var x = 0; x < this.outVars.length; x++) {
			tmp += (tmp != "" ? "&" : "");
			tmp += this.outVars[x] + "=" + escape(this.outValues[x]);
		}
		
		return tmp;
	}
	
	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;
	}
	
	// for singleLine
	this.getInValueOFVar = function(vari) {
		var found = false;
		
		for (var x = 0; x < this.inVars.length; x++) {
			if (this.inVars[x] === vari) {
				found = true;
				break;
			}
		}
		
		if (!found) {
			return false;
		}

		return this.inValues[x];
	}
	
	// for multiLine
	this.getMultiline = function(index, vari) {
		var found = false;
		
		for (var x = 0; x < this.inVars[index].length; x++) {
			if (this.inVars[index][x] === vari) {
				found = true;
				break;
			}
		}
		
		if (!found) {
			return false;
		}
		
		return (this.inValues[index][x] ? this.inValues[index][x] : "");
	}
	
	this.clear = function () {
		this.inVars = new Array();
		this.inValues = new Array();
		this.outVars = new Array();
		this.outValues = new Array();
		this.inLength = 0;
	}
	
	
	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= "250px";
		ladeStatus.style.right= "6px";
		document.getElementById('CONTENT').appendChild(ladeStatus);
	}
	
	this.hideLoading = function () {	
		document.getElementById('CONTENT').removeChild(document.getElementById("ladeStatus"));
	}
}

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


