/*======================================================================*\
|| #################################################################### ||
|| # ÜberNet Javascript Libary                                        # ||
|| # ---------------------------------------------------------------- # ||
|| # All Javascript in this file is ©2000-2007 ÜberNet.               # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # --------------- THIS FILE IS NOT FREE SOFTWARE ----------------- # ||
|| # http://www.ubernet.biz  |  http://www.ubernet.biz/license.unc    # ||
|| #################################################################### ||
\*======================================================================*/

var ajaxObject = Class.create();

ajaxObject.prototype = {
	initialize: function( url, variables, method )
	{
		this.uri = url || "ajax.unc";
		this.vars = variables || "";

		method = method || 'POST';
		this.post = (method.toUpperCase() == 'GET') ? false : true;
	},
	
	getVariables: function()
	{
		return this.vars;
	},
	
	addVariable: function(variable)
	{
		this.vars += "&" + variable
	},
	
	setVariable: function(name, value)
	{
		this.addVariable(escape(name) + '=' + escape(value));	
	},
	
	deleteVariables: function()
	{
		this.vars = "";
	},
	
	getURI: function()
	{
			return this.uri;
	},
	
	setURI: function(url)
	{
		this.uri = url;
	},
	
	getMethod: function()
	{
		return ((this.post) ? 'POST' : 'GET');	
	},
	
	setMethod: function(method)
	{
		this.post = (method.toUpperCase() == 'POST') ? true : false;
	},
	
	willPost: function()
	{
		return this.post;
	},
	
	setPost: function(shouldPost)
	{
		this.post = shouldPost || false;
	},
	
	getAjaxObject: function()
	{
		return Try (
			function() { return new XMLHttpRequest(); },
			function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
			function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
		) || false;
	},
	
	connect: function( return_function )
	{
		this.object = this.getAjaxObject();
		
		if (!this.object)
		{
			alert('Unable to create XML HTTP Request Object');	
			return false;
		}
		
		this.object.open( ((this.post) ? "POST" : "GET"), this.uri, true);
		
		var ptr = this.object;
		this.object.onreadystatechange = function()
			{
				if (ptr.readyState != 4)
					return;
				
				if (typeof return_function == 'string')
				{
					eval( return_function + "(" + this.object.responseText + ")");	
				} else {
					return_function(ptr.responseText);
				}
				
				delete ptr;
			}
			
		if (this.post)
		{
			this.object.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
			this.object.setRequestHeader('Connection', 'Close');
		}
		
		this.object.setRequestHeader('X_REQUESTED_WITH', 'XMLHTTPRequest');
		this.object.setRequestHeader('X_POWERED_BY', 'UberNet-JS-Libary');
		
		this.object.send( this.vars );
		
		return true;
	},
	
	writeToLayer: function(layerID, HTML)
	{
		//Get the layer
		layer = $(layerID);
		
		//Update the inner HTML
		if (layer != null)
		{
			layer.innerHTML = HTML;
		}
	},
	
	updateLayer: function(layerID, loadingHTML)
	{
		loadingHTML = loadingHTML || null;
		
		if (loadingHTML != null)
			this.writeToLayer(layerID, loadingHTML);
		
		var ptr = this;
		
		this.connect ( function(responseText)
							{
								ptr.writeToLayer(layerID, responseText);
							}
						);
	}
}