
/************************************
 *                                  *
 *  AJAX JS Class                   *
 *  Part of the con|comm Framework  *
 *                                  *
 *  © 2007 Timo Besenreuther        *
 *         www.concomm.de           *
 *                                  *
 ************************************
 
 
 This class handles AJAX calls using the
 con|comm contenido AJAX API.
 
*/


function ConcommAjax(file) {
	
	// initialization of the xml http request object
	var xmlHttp = null;
	
	// basic url for request
	var basicUrl  = 'front_content.php';
	
	// file to be requested
	this.file = file;
	
	// first request var is needed to catch requests while object is busy
	var firstRequest = true;
	this.disableBusyCatch = false;
	
	
	/**
	 * reset all variables
	 * defaults are set here
	 */
	
	this.reset = function() {
		if (xmlHttp == null || xmlHttp.readyState == 4 || firstRequest || this.disableBusyCatch) {
			if (xmlHttp == null || (this.disableBusyCatch && (xmlHttp.readyState != 4 && !firstRequest)) || navigator.appName.indexOf('Explorer') != -1) {
				getNewXmlHttp();
				firstRequest = true;
			}
			
			// additional parameters for the url
			this.addUrl = new Array();
			
			// response is stored here
			this.response = '';
			
			// debug mode?
			this.debug = false;
		}
	}
	
	this.reset();
	
	
	/**
	 * new request
	 */
	 
	this.request = function(callback) {
		if (xmlHttp.readyState != 4 && !firstRequest && !this.disableBusyCatch) {
			// running request
			alert('concomm ajax object busy');
		} else {
			// error, if xml http request object is not supported by browser
			if (xmlHttp == null) {
				alert('Ihr Browser unterstützt die Programmiersprache AJAX nicht. Bitte verwenden Sie einen anderen.');
				return;
			}
			
			// error, if no file selecte
			if (!this.file) {
				alert('concomm ajax object error: this.file is not set');
				return;
			}
			
			// build request url
			url  = basicUrl+'?ajax='+this.file;
			var param;
			for (param in this.addUrl) {
				url += '&'+param+'='+this.addUrl[param];
			}
			url += '&sid='+Math.random();
			
			// request
			xmlHttp.onreadystatechange = function() {
				if (xmlHttp.readyState == 4) {
					if (this.debug) {
						alert(xmlHttp.responseText);
					}
					if (typeof(callback) == 'function') {
						// get the XML and convert to array / object
						var xml  = (new DOMParser()).parseFromString(xmlHttp.responseText, 'text/xml');
						var root = xml.getElementsByTagName('root')[0]
						if (typeof(root) != 'undefined') {
							var obj = concommXmlNodeToObject(root);
						} else {
							// xml error
							if (xmlHttp.responseText != '' && typeof(xml.childNodes.item(0).childNodes.item(0).data) != 'undefined' && xml.childNodes.item(0).childNodes.item(0).data != '') {
								alert('RESPONSE:\n'+xmlHttp.responseText+'\n\nXML ERROR:\n'+xml.childNodes.item(0).childNodes.item(0).data);
							}
							var obj = false;
						}
						
						callback(obj);
					} else {
						alert('concomm ajax object error: callback is not a function');
					}
				}
			};
			xmlHttp.open('GET', url, true);
			xmlHttp.send(null);
			firstRequest = false;
		}
	}
	
	
	/**
	 * get xml http request object
	 */
	
	function getNewXmlHttp() {
		try {
			// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			// Internet Explorer
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	}
	
}


/**
 * converts an xml node to an object / array
 */

function concommXmlNodeToObject(node) {
	var obj = {};
	for (var i = 0; i < node.childNodes.length; i++) {
		var child = node.childNodes.item(i);
		if (child.childNodes.length > 1) {
			obj[child.tagName] = concommXmlNodeToObject(child);
		} else if (child.childNodes.length == 1) {
			obj[child.tagName] = child.firstChild.data;
		}
	}
	return obj;
}


/**
 * DOMParser for IE and Safari
 */

if (typeof(DOMParser) == "undefined") {
	DOMParser = function () {}

	DOMParser.prototype.parseFromString = function (str, contentType) {
		if (typeof ActiveXObject != "undefined") {
			var d = new ActiveXObject("Msxml2.DOMDocument");
			d.loadXML(str);
			return d;
		} else if (typeof XMLHttpRequest != "undefined") {
			var req = new XMLHttpRequest;
			req.open("GET", "data:" + (contentType || "application/xml") + ";charset=utf-8," + encodeURIComponent(str), false);
			if (req.overrideMimeType) {
				req.overrideMimeType(contentType);
			}
			req.send(null);
			return req.responseXML;
		}
	}
}





/**
 * OLD CODE
 *
 * delete this, after adjusting everything
 */

// Ajax
function ajax(url, callbackfunction) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Ihr Browser unterstützt die Programmiersprache AJAX nicht. Bitte verwenden Sie einen anderen.");
		return;
	} 
	url += "&sid="+Math.random();
	xmlHttp.onreadystatechange = callbackfunction;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

// XmlHttpObject erzeugen
function GetXmlHttpObject() {
	var xmlHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}