/*
 * Makes a HTTP request (Ajax call) for the given url. Executes the specified fnc when content is returned from the call. 
 */
function loadDoc(url, fnc)
{
	var xmlhttp;
	
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange = createCallback(xmlhttpChange, xmlhttp, fnc);
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		if (xmlhttp)
		{
			xmlhttp.onreadystatechange = createCallback(xmlhttpChange, xmlhttp, fnc);
			xmlhttp.open("GET",url,true);
			xmlhttp.send();
		}
	}
}

// Callback used internally by loadDoc 
function xmlhttpChange(xmlhttp, fnc)
{
	if (xmlhttp.readyState==4)
	{
		if (xmlhttp.status==200)
		{
			fnc(xmlhttp.responseText);
	    }
		else
		{
			fnc();
		}
	}
}

/*
 * Returns a function that has an arbitrary number of arguments passed to it.
 * Usage: createCallback(myFunctionName, arg1, arg2);
 * Assuming the following exists: function myFunctionName(arg1, arg2){ do something }
 */
function createCallback(fnc)
{
	var args = [];
	for(var i=1; i < arguments.length; i++)
	{
		args.push(arguments[i]);
	}		
	return function()
	{
		for(var i=0; i < arguments.length; i++)
		{
			args.push(arguments[i]);
		}			
		return fnc.apply(undefined, args);
	}
}

/*
 * Render's the given xhtml inside of the document with given docid.
 */
function renderDocInnerHTML(docid, xhtml)
{
	var doc = document.getElementById(docid);
	if(doc != undefined)
	{
		if(xhtml != undefined && xhtml != "")
		{
			doc.innerHTML = xhtml;
		}
		else
		{
			doc.innerHTML = "<div style='padding:5px;'>Data currently unavailable.</div>";
		}
	}
}
