function calcage(secs, num1, num2) {
	  s = ((Math.floor(secs/num1))%num2).toString();
	  //if (LeadingZero && s.length < 2)
	//  if (s.length < 2)
	//	s = "0" + s;
	  return "<b>" + s + "</b>";
}

function CountBack(secs) {
	  if (secs < 0) {
		//document.getElementById("cntdwn").innerHTML = "DONE!";
		document.location = document.location;
		return;
	  }
	CountBackCommon(secs);
	setTimeout("CountBack(" + (secs-1) + ")", 1000);
}
function CountBackPassive(secs) {
	  if (secs < 0) {
		document.getElementById("cntdwn").innerHTML = "Ended";
		return;
	  }
	CountBackCommon(secs);
	setTimeout("CountBackPassive(" + (secs-1) + ")", 1000);
}
function CountBackUpdateStatus(secs) {
	  if (secs < 0) {
		update_game();
		return;
	  }
	CountBackCommon(secs);
	setTimeout("CountBackUpdateStatus(" + (secs-1) + ")", 1000);
}
function CountBackCommon(secs){
	  DisplayFormat = "%%M%% Minutes, %%S%% Seconds ";
	  DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
	  DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
	  DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
	  DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
	  document.getElementById("cntdwn").innerHTML = DisplayStr;
	  

}	
	
var xmlHttp;
var xmlStatus;
var xmlHttpScore;
var xmlHttpChat;
var xmlHttpPrevGame;
var xmlHttpWordAction;
var xmlHttpAdminStatus;


function updateNotifications(){
	var url="boggle_action.php?actionType=showSideNotifications";
	xmlStatus=GetXmlHttpObject(stateChangedUpdateNotifications);
	xmlStatus.open("GET", url , true);
	xmlStatus.send(null);
}
function stateChangedUpdateNotifications() { 
	if (xmlStatus.readyState==4 || xmlStatus.readyState=="complete")
		document.getElementById("notificationsDiv").innerHTML=xmlStatus.responseText 
} 

function reportWord(str){
	if (str.length > 0)
	{ 
		var url="boggle_action.php?actionType=reportWord&word=" + str
		xmlHttp=GetXmlHttpObject(stateChangedReport)
		xmlHttp.open("GET", url , true)
		xmlHttp.send(null)
	} 
}
function stateChangedReport() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("reportStatus").innerHTML=xmlHttp.responseText 
	} 
} 

function showLastGame(){	
	var url="boggle_action.php?actionType=showLastGameDiv";
	xmlHttpPrevGame=GetXmlHttpObject(stateChangedLastGame)
	xmlHttpPrevGame.open("GET", url , true)
	xmlHttpPrevGame.send(null)
	
}
function stateChangedLastGame() { 
	if (xmlHttpPrevGame.readyState==4 || xmlHttpPrevGame.readyState=="complete")
	{ 
		document.getElementById("bogglePrevGameDiv").innerHTML=xmlHttpPrevGame.responseText 
	} 
} 

function queryWord(str){
	if (str.length > 0)
	{ 
		var url="boggle_action.php?actionType=queryDictionary&word=" + str
		xmlHttp=GetXmlHttpObject(stateChangedQuery)
		xmlHttp.open("GET", url , true)
		xmlHttp.send(null)
	} 
}
function stateChangedQuery() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("reportStatus").innerHTML=xmlHttp.responseText 
	} 
} 

function adminQuery(url){		
	xmlHttpAdminStatus=GetXmlHttpObject(stateChangedAdminQuery)
	xmlHttpAdminStatus.open("GET", url , true)
	xmlHttpAdminStatus.send(null)
}
function stateChangedAdminQuery() { 
	if (xmlHttpAdminStatus.readyState==4 || xmlHttpAdminStatus.readyState=="complete")	{ 
		document.getElementById("adminStatusDiv").innerHTML=xmlHttpAdminStatus.responseText 
	} 
} 


function addPlayerWords(str)
{ 
	if (str.length > 0)
	{ 
		var url="boggle_action.php?actionType=displayPlayerWords&wordToAdd=" + str
		xmlHttpWordAction=GetXmlHttpObject(stateChanged)
		xmlHttpWordAction.open("GET", url , true)
		xmlHttpWordAction.send(null)
	} 
} 

function addChatText(str, displayType) { 
	var url="boggle_action.php?actionType=displayChat&chatShowOption=" + displayType;
	if (str.length > 0)
	{ 
		str = escape(str);
		url="boggle_action.php?actionType=displayChat&chatShowOption=" + displayType + "&chatTextToAdd=" + str;
	}
	xmlHttpChat=GetXmlHttpObject(stateChangedChat)
	xmlHttpChat.open("GET", url , true)
	xmlHttpChat.send(null)	 
}
function stateChangedChat() { 
	if (xmlHttpChat.readyState==4 || xmlHttpChat.readyState=="complete")
	{ 
		document.getElementById("chatDiv").innerHTML=xmlHttpChat.responseText 
	} 
} 

function showPrevPlayerWords(){ 
	var url="boggle_action.php?actionType=displayPrevPlayerWords"
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
} 

function updateBoardWords() { 
	var url="boggle_action.php?actionType=updateGameDictFoundWords"
	xmlHttp=GetXmlHttpObject(stateChangedGameStatus)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
	
} 
function stateChangedGameStatus() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if(xmlHttp.responseText != "") document.getElementById("gameMessagesDiv").innerHTML=xmlHttp.responseText;
		
	} 
} 
function stateChanged() 
{ 
	if (xmlHttpWordAction.readyState==4 || xmlHttpWordAction.readyState=="complete")
	{ 
		document.getElementById("playerWordsDiv").innerHTML= xmlHttpWordAction.responseText;
	} 
} 
function showPlayerScores()
{ 
	var url="boggle_action.php?actionType=displayPlayerScores"
	xmlHttpScore=GetXmlHttpObject(stateChangedScores)
	xmlHttpScore.open("GET", url , true)
	xmlHttpScore.send(null)
}
function showPrevPlayerScores()
{ 
	var url="boggle_action.php?actionType=displayPrevPlayerScores"
	xmlHttpScore=GetXmlHttpObject(stateChangedScores)
	xmlHttpScore.open("GET", url , true)
	xmlHttpScore.send(null)
}
function stateChangedScores() 
{ 
	if (xmlHttpScore.readyState==4 || xmlHttpScore.readyState=="complete")
	{ 
		document.getElementById("playerListDiv").innerHTML=xmlHttpScore.responseText 
	} 
} 

function showGameInfo()
{ 
	var url="boggle_action.php?actionType=displayGameTimerInfo"
	xmlHttpScore=GetXmlHttpObject(stateChangedGameInfo)
	xmlHttpScore.open("GET", url , true)
	xmlHttpScore.send(null)
}
function stateChangedGameInfo() 
{ 
	if (xmlHttpScore.readyState==4 || xmlHttpScore.readyState=="complete")
	{ 
		document.getElementById("gameTimerInfo").innerHTML=xmlHttpScore.responseText 
	} 
} 



function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		//alert("This example doesn't work in Opera") 
		return 
	}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP"
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} 
		catch(e)
		{ 
			//alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
} 