function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function popGame(gameId) {
	//1) If this game isn't already open, start a new game. Otherwise focus existing window.
	var ref = window.open('/game.php?id='+gameId+'#null','_popGame'+gameId);
	ref.focus();
	return false;
}
function refreshCurrentWindow()
{
	window.location.reload( true );
}
function switchGame(existingGameId,newGameId) {
	//1) Close the existing game window. New game window should already exist and be focused.
	closeGame(existingGameId);

	//2) Double check to make sure existing session is forfeited. Closing the previous window SHOULD do it...
	//   but the async call below ensures it then refreshes the current (new game) window to start a new session.
	ajaxUrlReload('/forfeit_game_session.php');
	return false;
}
function closeGame(gameId) {
	//named anchor prevents refresh of child window.
	var ref = window.open('/game.php?id='+gameId+'#null','_popGame'+gameId);

	if(!ref.closed&&true==ref.gIN_PROGRESS)
	{
		ref.close();
	}
	else
	{
		//1) Close window
		if(ref!=window)
		{
			ref.close();
		}
		//2) we lost track of the original window. Manually kill the open game session & close the new window we just created.
		//the alert is NECESSARY because we need to give IE time to close the damn reference above. Without this confirm, somehow
		//the ajax call below will occur before the open call at the top of this function does... :-/
		alert("Oops we cannot find your existing game session. This can happen if your existing session crashed, or is open on another computer. We will close your open game session and start you a new one now.");
		ajaxUrl('/forfeit_game_session.php');
	}
	return false;
}
function focusGame(gameId) {
	//named anchor prevents refresh of child window.
	var ref = window.open('/game.php?id='+gameId+'#null','_popGame'+gameId);

	if(!ref.closed&&true==ref.gIN_PROGRESS)
	{
		//1) Focus existing game
		ref.focus();
		//2) Close new game
		window.close();
	}
	else
	{
		//1) Close bogus window we just opened.
		if(ref!=window)
		{
			ref.close();
		}
		//We've lost track of the original window. Ask user what they want to do.

		//the confirm is NECESSARY because we need to give IE time to close the damn reference above. Without this confirm, somehow
		//the ajax call below will occur before the open call at the top of this function does... :-/
		var result = confirm("Oops we cannot find your existing game session. This can happen if your existing session crashed, or is open on another computer.\n\nDo you want to close your existing game session? Closing your existing game session will cause you to lose all progress but will allow you to play a new game.");
		if(result)
		{
			//2) Close existing game session
			ajaxUrlReload('/forfeit_game_session.php');
		}
	}
	return false;
}
function pop(url,width,height) {
	window.open(url,'_pop1','toolbar=no,menubar=no,location=no,height='+height+',width='+width+',scrollbars=yes').focus();
	return false;
}
function _blank(href) {
	window.open(href);
	return false;
}
function submit(form) {
	form = document.getElementById(form);
	form.submit();
}
function toggleMoreLess(elementID,parent) {
	var div = document.getElementById(elementID);
	div.style.display = ('none'==div.style.display) ? 'block' : 'none';
	parent.innerHTML = ('More �'==parent.innerHTML) ? 'Less �' : 'More �';
	return false;
}
function toggleDiv(elementID) {
	var div = document.getElementById(elementID);
	div.style.display = ('none'==div.style.display) ? 'block' : 'none';
	return false;
}
//item management functions
function removeItemFromList(cookieKey,id,name) {
	var path="/sys/bounceback.php5?removeItemFromList=true&cookieKey="+cookieKey+"&id="+id+"&name="+name;
	//alert(path);
	window.location=path;
	return false;
}
function copyToClipboard(s)
{
	if( window.clipboardData && clipboardData.setData )
	{
		clipboardData.setData("Text", s);
	}
	else
	{
		// You have to sign the code to enable this or allow the action in about:config by changing
		user_pref("signed.applets.codebase_principal_support", true);
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

		var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;

		// create a transferable
		var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;

		// specify the data we wish to handle. Plaintext in this case.
		trans.addDataFlavor('text/unicode');

		// To get the data from the transferable we need two new objects
		var str = new Object();
		var len = new Object();

		var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);

		var copytext=meintext;

		str.data=copytext;

		trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);

		var clipid=Components.interfaces.nsIClipboard;

		if (!clip) return false;

		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
}

//general functions
function ajaxUrl(url)
{
	var xmlHttp = null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.open("GET",url,false);//non async transfer
	xmlHttp.send(null);
}//general functions
function ajaxUrlReload(url)
{
	//alert("ajaxReload");
	var xmlHttp = null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			// Get the data from the server's response in xmlHttp.responseText;
			//1) For now, we'll always want to refresh the current window once the ajax call finishes.
			//could be a scope issue here...
			//alert("Ajax Done:"+xmlHttp.responseText);
			
			//alert("ready state 4");
			window.location.reload( true );
		}
	}
	xmlHttp.open("GET",url,true);//async transfer
	xmlHttp.send(null);
}