// Loading subpages
function ajx_OpenURL(Target, URL) 
{
 	if (window.ActiveXObject) 
	{
		link = new ActiveXObject("Microsoft.XMLHTTP");
 	} 
	else if (window.XMLHttpRequest) 
	{
		link = new XMLHttpRequest();
 	}

 	if (link == undefined) return false;

 	link.onreadystatechange = function() { _ajx_SetupResponse(Target, URL); };
 	link.open("GET", URL, true);
 	link.send(null);
}

function _ajx_SetupResponse(Target, URL) 
{
 	if (link.readyState!=4) return;
	
	if (link.status!=200)
	{
		document.getElementById(Target).innerHTML = "<p>Can't access "+URL+" with error "+link.status+"</p>";
		return;
	}
	
	document.getElementById(Target).innerHTML = link.responseText;
	onAjxLoaded(Target, URL);
}

// Handling history
var _ajx_ExpectedHash;

function _ajx_HandleHistory()
{
  if ( window.location.hash != _ajx_ExpectedHash )
  {
    _ajx_ExpectedHash = window.location.hash;
    onHistoryChange( _ajx_ExpectedHash );
  }
  return true;
}

function ajx_PollHistory() 
{
  onHistoryChange(window.location.hash);
  window.setInterval("_ajx_HandleHistory()", 1000);
  return true;
}

function ajx_SetHistory(page)
{
	window.location.hash = page;
}

// Loading scripts
function ajx_LoadScript(url, callback) 
{
	var script = document.createElement('script');
	script.type = 'text/javascript';

	if (callback)
	{
		script.onload 						= 
		script.onreadystatechange = 
			function() 
			{
				if (		script.readyState 
						&& 	script.readyState != 'loaded' 
						&& 	script.readyState != 'complete'
					 )
					return;
				
				script.onreadystatechange = 
				script.onload 						= null;
				callback();
			};
	}
	
	script.src = url;
	document.getElementsByTagName('head')[0].appendChild(script);
}
