/**
 * Ajax JavaScript framework
 * Loosely based off of the Prototype JavaScript framework
 *    Prototype is (c) 2005 Sam Stephenson <sam@conio.net>
 *    Prototype is freely distributable under the terms of an MIT-style license.
 *    For details, see the Prototype web site: http://prototype.conio.net/
 */

SynAjax_Request_Events =
  ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];


function getTransport() 
{
	try	
	{
		return new ActiveXObject('Msxml2.XMLHTTP')
	}
	catch (exception) 
	{
		try 
		{
			return new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch (exception)
		{
			try
			{
				return new XMLHttpRequest()
			}
			catch (exception)
			{
				return false;
			}
		}
	}
}

SynAjax = function()
{
	var self = new Object();

	self.transport = getTransport();
	
	self.request = function(url, options) 
	{
		this.doRequest(null, url, options);
	}

	self.update = function(element_id, url, options)
	{
		this.doRequest(element_id, url, options);
	}

	self.doRequest = function(element_id, url, options)
	{
		this.url        = url;
		this.options    = options;
		this.updater    = element_id;
		this.parameters = this.options.parameters || '';

		try 
		{
			if (this.options.method == 'get')
			{
				this.url += (this.url.match(/\?/) ? '&' : '?') + this.parameters;
			}

			var loader = this;
			this.transport.onreadystatechange = function()
			{
				loader.onStateChange.call(loader);
			}
			this.transport.open(this.options.method, this.url, true);
			this.setRequestHeaders();

			var body = this.options.postBody ? this.options.postBody : this.parameters;
			this.transport.send(this.options.method == 'post' ? body : null);

		}
		catch (exception) 
		{ }
	}

	self.setRequestHeaders = function() 
	{
		var requestHeaders =
			['X-Requested-With', 'XMLHttpRequest'];

		if (this.options.method == 'post') 
		{
			requestHeaders.push('Content-type',	'application/x-www-form-urlencoded');

			/**
			 * Force "Connection: close" for Mozilla browsers to work around
			 * a bug where XMLHttpReqeuest sends an incorrect Content-length
			 * header. See Mozilla Bugzilla #246651.
			 */
			if (this.transport.overrideMimeType)
				requestHeaders.push('Connection', 'close');
		}

		if (this.options.requestHeaders)
		{
			requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);
		}

		for (var i = 0; i < requestHeaders.length; i += 2)
		{
			this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
		}
	}

	self.onStateChange = function()
	{
		if (!this.transport)
		{
			if (this.transport == 'undefined')
			{
				return;
			}
		}

		var event = SynAjax_Request_Events[this.transport.readyState];

		if (this.transport.readyState != 1)
		{
			if (event == 'Complete')
			{
				if (this.updater != null)
				{
					var element = document.getElementById(this.updater);
					try 
					{
						element.innerHTML = this.transport.responseText;
					}
					catch (exception)
					{ }
				}

				try
				{
					this.options.onComplete(this.transport);
				}
				catch (exception)
				{ }

				if ((this.header('Content-type') || '').match(/^text\/javascript/i))
				{
					this.evalResponse();
				}
			}

			// Avoid memory leak in MSIE: clean up the oncomplete event handler 
			if (event == 'Complete')
			{
				this.transport.onreadystatechange = function(){};
			}
		}
	}

	self.header = function(name)
	{
		try
		{
			return this.transport.getResponseHeader(name);
		}
		catch (exception)
		{
			return null;
		}
	}

	self.evalResponse = function()
	{
		try
		{
			return eval(this.transport.responseText);
		}
		catch (exception)
		{
			return null;
		}
	}

	return self;
}

