DEBUG = false;


/*-------------------------------------------------
	replaces document.getElementByID()
/------------------------------------------------*/

function $(element_name)
{
	return document.getElementById(element_name);
}




/*-------------------------------------------------
	produces an associative array using hash-like
	syntax
/------------------------------------------------*/

function hash_params()
{
	param_string = "";

	for( var i=0; i<arguments.length; i+=2 )
	{
		if( typeof(arguments[i + 1]) != 'undefined')
		{
			param_string += arguments[i] + '=' + arguments[i + 1] + "&";
		}
	}

	return param_string.substring(0,param_string.length-1);
}




/*-------------------------------------------------
	strips leading and trailing whitespace
	from a string
/------------------------------------------------*/

function strip(source_string)
{
	  source_string = source_string.replace( /^\s+/g, "" );// strip leading
	  source_string = source_string.replace( /\s+$/g, "" );// strip trailing

	  return source_string;
}




/*-------------------------------------------------
	add the browser cache workaround string to
	a url.
/------------------------------------------------*/

function cache_bypass_fix(url)
{
	date_string = new Date().getTime().toString();

	if( url.indexOf("&") == -1 && url.indexOf("?") == -1 )
		url += "?browser_cache_workaround=" + date_string;
	else
		url += "&browser_cache_workaround=" + date_string;

	return url;
}




/*-------------------------------------------------
	create a XMLHttpRequestObject in a cross 
	browser friendly way.
/------------------------------------------------*/

function create_request_object()
{
	this.req = null;

	if( window.XMLHttpRequest ) // normal browser
	{
		this.req = new XMLHttpRequest();
	}
	else if( window.ActiveXObject ) // microsoft browser
	{
		try
		{
			this.req = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch(e)
		{
			try
			{
				this.req = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch(E)
			{
			}
		}
	}

	return this.req;
}




/*-------------------------------------------------
	call a server side script and return a value
	via a callback function passed to this
	function.
/------------------------------------------------*/

function ajax_request(url, ol_args)
{
	if(typeof(ol_args) == 'undefined')
	{
		ol_args = {};
	}

	var me = this;
	this.url = url;
	this.method = ol_args.method || "GET";
	this.parameters = ol_args.parameters || "";
	this.on_success = ol_args.on_success || function(){};
	this.on_error = ol_args.on_error || function(){};
	this.timeout = ol_args.timeout || -1;
	this.on_timeout = ol_args.on_timeout || function(){};
	this.updating = false;

	this.abort = function()
	{
		if( me.updating )
		{
			me.updating = false;
			clearTimeout(me.timeout_id);
			me.req.abort()
			me.req = null;
		}
	}

	this.update = function()
	{
		if( me.updating ) { return false; }

		me.req = create_request_object();

		if( me.req == null )
		{
			return false;
		}
		else
		{
			me.req.onreadystatechange=function()
			{
				if( me.req.readyState==4 )
				{
					me.updating = false;
					clearTimeout(me.timeout_id);

					if( DEBUG )
					{
						alert("status: " + me.req.status + "\nresponse: " + me.req.responseText);
					}

					if( me.req.status == 200 )
					{
						me.on_success(strip(me.req.responseText));
						me.req = null;
					}
					else
					{
						me.on_error(strip(me.req.responseText));
						me.req = null;
					}
				}
			}

			if( me.timeout > 0 )
			{
				me.timeout_id = setTimeout(function() { me.on_timeout(''); me.abort(); }, me.timeout);
			}

			me.updating = true;

			if( "POST PUT DELETE".indexOf(me.method.toUpperCase()) != -1 )
			{
				if( "PUT DELETE".indexOf(me.method.toUpperCase()) != -1)
				{
					me.parameters += "&_method=" + me.method.toUpperCase();
				}

				me.parameters = cache_bypass_fix(me.parameters);

				if( DEBUG )
				{
					alert("posting to: " + me.url + "\nparameters: " + me.parameters);
				}

				me.req.open("POST", me.url, true);
				me.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				me.req.send(me.parameters);
			}
			else
			{
				if( me.parameters.length > 0)
				{
					me.url += "?" + me.parameters
				}

				me.url = cache_bypass_fix(me.url);

				if( DEBUG )
				{
					alert("getting from: " + me.url);
				}

				me.req.open("GET", me.url, true);
				me.req.send(null);
			}
		}

		return true;
	}

	this.update();
}




/*-------------------------------------------------
	update the inner HTML of an element with the
	response from an ajax_request
/------------------------------------------------*/

function ajax_updater(element, url)
{
	new ajax_request(url,
	{
		on_success:function (response)
		{
			element.innerHTML = strip(response);
		}
	});
}




/*-------------------------------------------------
	periodically update the inner HTML of an
	element with the response from an ajax_request
/------------------------------------------------*/

function ajax_periodic_updater(element, url, period)
{
	setInterval( function() { new ajax_request(url,
	{
		on_success:function (response)
		{
			element.innerHTML = strip(response);
		}
	}); }, period );
}

