//class handles URL functionality for back button and bookmarking
function ajaxState(changedfunc)
{
	address=window.location.toString();
	if ((hashpos=address.indexOf('#')) > -1)
		address=address.substring(0,hashpos);

	this.baseAddr = address;
	this.curURL = null;
	this.check = false;
	this.changed = changedfunc;

	this.checkChange();
}
ajaxState.prototype.getHashArgs = function()
{
	address = window.location.toString();
	if ((hashpos=address.indexOf('#')) > -1)
		return address.substring(hashpos+1);
	else
		return "";
}
ajaxState.prototype.setHashArgs = function(newArgs)
{
	this.check = false;
	if (newArgs != "")
		newArgs = "#"+newArgs;
	this.curURL = this.baseAddr+newArgs;
	window.location.replace(this.curURL);
	this.check = true;
}
ajaxState.prototype.appendHashArgs = function(newArgs)
{
	this.check = false;
	if (newArgs != "")
	{
		if (this.curURL == this.baseAddr) 
			this.curURL = this.curURL+"#";
		else
			this.curURL = this.curURL+"&";
		this.curURL = this.curURL+newArgs;
	}
	window.location.replace(this.curURL);
	this.check = true;
}
ajaxState.prototype.checkChange = function()
{
	address = window.location.toString();
	if (this.check && (address.indexOf('#')+1 == address.length))
		window.location.replace(this.curURL);
	else if (this.check && (address != this.curURL))
	{
		this.curURL = address;
		eval(this.changed);
	}
	var self = this;
	setTimeout(function(){ self.checkChange(""); }, 500); 
}

/*Arguments:
callback: Name of the javascript callback function in the web page
url: Location of the php file which will generate the JSON data
phpfunc: The name of the function to be called in the php file
args: Arguments that will be passed to the url
*/
function makeRequest(callback, url, phpfunc, args)
{
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}


	if (phpfunc == null)
	{
		http_request.onreadystatechange = function() { eval(callback) };
		http_request.open('GET', url, true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send("");
	}
	else
	{
		http_request.onreadystatechange = function() { eval(callback) };
		http_request.open('POST', "ajax.php", true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send(args+"&funcname="+phpfunc+"&urlname="+url);

	}
}

var numLoading = 0;
function loading_show()
{
	if (numLoading < 1)
	{
		var loading = document.getElementById('loading');
		if (!loading)
		{
			loading = document.createElement('div');
			loading.id = 'loading';
			loading.innerHTML = '<font style="font-family:verdana; font-size:12px; color:white;">Loading...</' + 'font>';
			loading.style.position = 'absolute';
			loading.style.top = '4px';
			loading.style.right = '4px';
			loading.style.backgroundColor = 'red';
			loading.style.width = '65px';
			loading.style.padding = '2px';
			document.getElementsByTagName('body').item(0).appendChild(loading);
		}
		loading.style.display = 'block';
	}
	numLoading++;
}

function loading_hide()
{
	//numLoading--;
	numLoading = 0;
	if(numLoading < 1) {
		var loading = document.getElementById('loading');
		if (loading) {
			loading.style.display = 'none';
		}
	}
}
