// Script:  ddobj v3.22 (DHTML Drag-and-Drop Objects Framework)
// Company: Interaxis
// Author:  dynamicreport.com
// Website: http://dynamicreport.com/ddobj.html
// License: This script is subject to the EULA included in the ddobj package

var ajax_containers = new Array (); // ajax containers
var ajax_preload_data = new Array (); // cached data
var cache_on = false; // enable or disable data caching
var loader_info = ""; // loader HTML (image/info. indication) - empty value disables

function ajax (path, method)
{
	// class attributes and constructor
	this.path = path;
	this.method = method;

	this.url = this.status = this.statusText = this.xmlhttp = null;

	this.onCompletion = function () { };

	// function aliases
	this.init = init;
	this.load = load;
	this.getHTTPObject = getHTTPObject;

	this.init ();

	// class methods

	function init ()
	{
		if (method != "GET" && method != "POST")
			method = "GET"; // ensure the method is valid

		if (!this.xmlhttp)
			this.xmlhttp = this.getHTTPObject ();
	}
	
	function getHTTPObject ()
	{
		if (typeof XMLHttpRequest != 'undefined')
			return new XMLHttpRequest ();
		try	{
			return new ActiveXObject ("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try	{
				return new ActiveXObject ("Microsoft.XMLHTTP");
			}
			catch (e) {
			}
		}
		return null;
	}

	function load (url)
	{
		if (this.xmlhttp)
		{
			this.url = (url ? url : "");
			if (this.xmlhttp)
			{
				var self = this;
				if (this.method == "GET") // get method (parameter passing)
					this.xmlhttp.open (this.method, this.path + (this.url ? "?" + this.url : ""), true);
				else // default method
					this.xmlhttp.open (this.method, this.path, true);
				if (this.method == "POST")
				{
  					try	{
						this.xmlhttp.setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');
					}
					catch (e) {
					}
				}

				this.xmlhttp.send (this.url);
				this.xmlhttp.onreadystatechange = function ()
				{ // all state changes are processed here
					// ready states: 0 = uninitialized, 1 = open, 2 = sent, 3 = receiving, 4 = loaded
					if (self.xmlhttp.readyState == 4) // loaded
					{
						self.response = self.xmlhttp.responseText;
						self.responseXML = self.xmlhttp.responseXML;
						self.status = self.xmlhttp.status;
						self.statusText = self.xmlhttp.statusText;
						self.onCompletion ();
						self.url = "";
					}
				};
			}
		}
	}
}

function ajax_output (id, path, method, url, cb_func, cb_params, dd_disable)
{ // return false for preloaded data, return true for new data server request
	if (!id) // id parameter not specified, terminate
		return false;
	var obj = document.getElementById (id);
	if (!obj) // document object does not exist
		return false;
	var unique = bset = 0;
	if (!cache_on) // timestamp for unique path parameter so as to avoid caching
		url += "&ddobjts=" + new Date ().getTime ();

	if (cache_on && ajax_preload_data[path])
	{ // associative array by path for locating cached data
		if (cb_func) // callback function call after layer content is written
		{
			if (typeof dd_obj != 'undefined' && !dd_disable)
			{ // write cached content to drag and drop object's content attribute
				if (dd_obj[cb_params])
					dd_obj[cb_params].content = ajax_containers[c_index].response; // ddobj object
				cb_func (cb_params); // now call the callback function with custom parameters
			}
			else
			{ // write cached content to object
				if (cb_func (cb_params)) // now call the callback function with custom parameters
					bset = 1;
				if (bset)
				{ // set the output if the callback returned true
					if (obj.nodeName == "INPUT") obj.value = ajax_preload_data[path]; // input control
					else obj.innerHTML = ajax_preload_data[path]; // layer element
				}
			}
		}
		else
		{ // no callback, write content to object
			if (obj.nodeName == "INPUT") obj.value = ajax_preload_data[path]; // input control
			else obj.innerHTML = ajax_preload_data[path]; // layer element
		}
		return false;
	}

	if (loader_info)
	{ // write default content (preloader indication) until content is retrieved remotely
		if (obj.nodeName == "INPUT") obj.value = loader_info; // input control
		else obj.innerHTML = loader_info; // layer element
	}

	var c_index = ajax_containers.length;
	ajax_containers[c_index] = new ajax (path, method);
	ajax_containers[c_index].onCompletion = function ()
	{
		if (cache_on) // write content to preloaded storage if caching is enabled
			ajax_preload_data[path] = ajax_containers[c_index].response;

		if (cb_func) // callback function call after object content is written
		{
			if (typeof dd_obj != 'undefined' && !dd_disable)
			{ // write content to drag and drop object's content attribute
				if (dd_obj[cb_params])
					dd_obj[cb_params].content = ajax_containers[c_index].response; // ddobj object
				cb_func (cb_params); // now call the callback function with custom parameters
			}
			else
			{ // write content to object
				if (cb_func (cb_params)) // now call the callback function with custom parameters
					bset = 1;
				if (bset)
				{ // set the output if the callback returned true
					if (obj.nodeName == "INPUT") obj.value = ajax_containers[c_index].response; // input control
					else obj.innerHTML = ajax_containers[c_index].response; // layer element
				}
			}
		}
		else
		{ // no callback, write content to object
			if (obj.nodeName == "INPUT") obj.value = ajax_containers[c_index].response; // input control
			else obj.innerHTML = ajax_containers[c_index].response; // layer element
		}
		ajax_containers[c_index] = null; // free resource - remove ajax instance from queue
	};
	ajax_containers[c_index].load (url ? url : "");
	return true;
} 

