////////////////////////////////////////////////////////////////////
// uniAjax framework 1.0 beta by Andreas Kalsch, Trier /////////////
// Last change: 04.07.2006 /////////////////////////////////////////
// Tutorial and API: http://aka-fotos.de/scripts?web/ajax/uniajax //
////////////////////////////////////////////////////////////////////
// Published under General Public License: /////////////////////////
// http://www.fsf.org/licensing/licenses/gpl.html //////////////////
////////////////////////////////////////////////////////////////////
// More script: http://aka-fotos.de/scripts ////////////////////////
////////////////////////////////////////////////////////////////////

var uniAjaxConnection = function() {
	var con = null;

    if (window.XMLHttpRequest) {
    	con = new XMLHttpRequest();
        if (con.overrideMimeType)
        	con.overrideMimeType('text/xml');
    }
    else if (window.ActiveXObject) {
    	try {
        	con = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) {
        	try {
            	con = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){}
        }
    }

    return con;
}

uniAjax = function(type, useCache) {
	this.type = 'string';
	if (type == 'xml' || type == 'json')
		this.type = type;
		
	this.useCache = false;
	if (typeof useCache != 'undefined')
		this.useCache = useCache;
}

if (uniAjaxConnection()) {    
	uniAjax.prototype.request = function(con) {
		var method = 'GET';
		var url = con;
		var params = '';
		var type = this.type;
		var func = null;
		
		var callFunc = false;
		
		if (typeof con == 'object') {
			if (typeof con.url == 'string')
				url = con.url;
				
			if (typeof con.params == 'object') {
				params = new Array();
				for (var key in con.params)
					params = params.concat(key+'='+encodeURIComponent(con.params[key]));
				params = params.join('&');
			}
			
			if (typeof con.type == 'string') {
				type = con.type.toLowerCase();
				if (type != 'string' &&
					type != 'xml' &&
					type != 'json')
					type = 'string';
			}
	
			if (typeof con.func == 'function')
				func = con.func;
				
			callFunc = true;
		}
		
		if (params != '')
			method = 'POST';
	
		if (typeof url == 'string') {
			var con = uniAjax.connection();
		
		    if (con) {
			    con.open(method, this._uniqueUrl(url), true);
			    
			    if (method == 'POST') {
			    	con.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					con.send(params);
				}
			    
			    if (callFunc) { 
				    var args = arguments;
		
				    con.onreadystatechange = function() {
			            if (con.readyState == 4) {
			            	var response;
			            	if (type == 'string')
			            		response = con.responseText;
			            	else if (type == 'xml')
			                	response = con.responseXML;
			                else if (type == 'json') {
			                	try {
			                		eval('response = '+con.responseText);
								}
								catch (e) {
									response = null;
								}
							}
							
							//alert(con.responseText);
							        	
			                if (func != null) {
				                if (args.length > 1) {
				                	var code = 'func(response';
				                	for (var i = 1; i < args.length; i++)
				                		code += ', args['+i+']';
				                	code += ');';
				                	eval(code);
								}
				                else
				                	func(response);
				            }
			            }
			        }
				}
			        
			    if (method == 'GET')
			    	con.send(null);
		    }
		}
	}
	
	uniAjax.connection = uniAjaxConnection;
	
	uniAjax._blocked = false;
}
else {
	// create helper iframe
	if (!document.getElementById('uniAjaxFrame'))
		document.write('<iframe src="" width="0" height="0" name="uniAjaxFrame" id="uniAjaxFrame" marginheight="0" marginwidth="0" frameborder="0" style="border-width:0px;margin:0px;padding:0px;"></iframe>');
	
	// create helper form (for method 'post')
	if (!document.getElementById('uniAjaxForm'))
		document.write('<form name="uniAjaxForm" id="uniAjaxForm" action="" method="post" target="uniAjaxFrame" style="margin:0px;padding:0px;"></form>');
	          
	uniAjax.prototype.request = function(con) {
		var method = 'GET';
		var url = con;
		var params = '';
		var type = this.type;
		var func = null;
		var unique = this._uniqueUrl;
		
		var callFunc = false;
		
		if (typeof con == 'object') {
			if (typeof con.url == 'string')
				url = con.url;
				
			if (typeof con.params == 'object') {
				method = 'POST';
			}
			
			if (typeof con.type == 'string') {
				type = con.type.toLowerCase();
				if (type != 'string' &&
					type != 'xml' &&
					type != 'json')
					type = 'string';
			}
	
			if (typeof con.func == 'function')
				func = con.func;
				
			callFunc = true;
		}
	
		if (typeof url == 'string') {
			var args = arguments;
		
			var waitForIframe = function() {			
				if (!uniAjax._blocked)
				{
					uniAjax._blocked = true;
				
					var response = '~';
					try {
						document.getElementById('uniAjaxFrame').contentWindow.document.body.innerHTML = '~'; // besseres Zeichen
					} 
					catch(e) {
						try {
							document.frames.uniAjaxFrame.document.body.innerHTML = '~';
						}
						catch(e) {}
					}
				
					if (method == 'GET')
						document.getElementById('uniAjaxFrame').src = unique(url);
				    else if (method == 'POST') {
				    	for (var key in con.params) {
							var input = document.createElement('input');
							input.type = 'hidden';
							input.name = key;
							input.value = con.params[key];
							document.getElementById('uniAjaxForm').appendChild(input);
						}
				    	document.getElementById('uniAjaxForm').action = unique(url);
				    	document.getElementById('uniAjaxForm').submit();
					}
				    
				    if (callFunc) { 
					    var counter = 0;
		
						var getResponse = function() {
							counter++;
							
							try {
								response = document.getElementById('uniAjaxFrame').contentWindow.document.body.innerHTML;
							} 
							catch(e) {
								try {
									response = document.frames.uniAjaxFrame.document.body.innerHTML;
								}
								catch(e) {}
							}
							
							if ((response == '~' || response == '<P>&nbsp;</P>' || response == '') && counter < 3000) {
								window.setTimeout(getResponse, 10);
							}
							else {								
								if (method == 'POST')
									uniAjax._clearInputs();
									
								uniAjax._blocked = false;
							
								if (navigator.userAgent.toLowerCase().indexOf('opera') > -1)
				                	response = response.replace(/&quot;/g, '"');
				                	
								if (type == 'json') {
				                   	try {
				                		eval('response = '+response+';');
									}
									catch (e) {
										response = null; 
									}
								}
								else if (type == 'xml') {
									response = response;
								}
								
								if (func != null) {
					                if (args.length > 1) {
					                	var code = 'func(response';
					                	for (var i = 1; i < args.length; i++)
					                		code += ', args['+i+']';
					                	code += ');';
					                	eval(code);
									}
					                else
					                	func(response);
					            }
							}
						};
						
						getResponse();
					}
				}
				else
					window.setTimeout(waitForIframe, 10);
			}	
				
			waitForIframe();
		}
	}
	
	uniAjax._clearInputs = function() {
		if (document.getElementById('uniAjaxForm')) {
			while (document.getElementById('uniAjaxForm').hasChildNodes())
				document.getElementById('uniAjaxForm').removeChild(document.getElementById('uniAjaxForm').firstChild);
		}
	}
	
	uniAjax.connection = null;
}

uniAjax.prototype._uniqueUrl = function(url) {
	if (!this.useCache) {
		var res = url.search(/.*?\?/);
		if (res > -1 && res != null)
			url += '&';
		else 
			url += '?';
		url += (new Date()).getTime();
	}
	return url;
}

uniAjax.functions = {
	'innerHTML': function(response, id) {
		 document.getElementById(id).innerHTML = response;
	}
};
