var tick;
var chessTime;
var gameTotal;
var chessId;

function updateTime(_chessTime, _gameTotal)
{
	chessTime=_chessTime;
	gameTotal=_gameTotal;
	doUpdateTime();
}

function doUpdateTime()
{
	// handle chess game update
	if(chessTime!=-1)
	{
		chessTime-=1;

		if(chessTime>0) document.getElementById("chessgametimer").innerHTML=getTimeString(chessTime);
		else
		{
			chessTime=-1;
			doCheckChessGameTimeout();
		}
	}

	for(i=0; i<gameTotal; i++)
	{
		gameTimes[i]-=1;
		divName="gameTime"+i;
		if(gameTimes[i]>-1)
		{
			if(gameTimes[i]>0)
			{
				setSmallTimeString(gameTimes[i], document.getElementById(divName));
			}
			else
			{
				document.getElementById(divName).value="0";
				document.getElementById(divName).style.color="FF0000";
				gameTimes[i]-=1;
			}
		}
		else if(document.getElementById(divName).value!="-")
			document.getElementById("gameTime"+i).value="-";
	}

	// recall update timer
	tick=setTimeout("doUpdateTime()", 1000);
}

function getTimeString(time)
{
	var d, h, m, s, data;
	d=Math.floor(time/24/60/60);
	h=addLeadingZero(Math.floor(time/60/60)%24);
	m=addLeadingZero(Math.floor(time/60)%60);
	s=addLeadingZero(time%60);
	data=d+"&nbsp;days&nbsp;"+h+":"+m+":"+s;
	if(d==0) return "<font color='FF0000'>"+data+"</font>";
	else return data;
}

function setSmallTimeString(time, obj)
{
	var d, h, m, s, data;
	d=Math.floor(time/24/60/60);
	h=addLeadingZero(Math.floor(time/60/60)%24);
	m=addLeadingZero(Math.floor(time/60)%60);
	s=addLeadingZero(time%60);
	data=d+"d:"+h+":"+m+":"+s;
	if(d==0) obj.style.color="FF0000";
	else obj.style.color="000000";
	obj.value=data;
}

function addLeadingZero(value)
{
	return value<=9?"0"+value:value;
}
	
function stopTime()
{
	clearTimeout(tick);
}

function validateMailingListEmail(form, text)
{
	if(!isEmail(form.email.value, text))
	{
		form.email.focus();
		return false;
	}
	return true;
}

function validateLogin(form)
{
	if(form._lun.value.toString() == "")
	{
		alert("Please fill in the username field");
		form._lun.focus();
		return false;
	}
	else if(form._lpw.value.toString() == "")
	{
		alert("Please enter your account password!");
		form._lpw.focus();
		return false;
	}
	else return true;
}

function viewChessGame(sqr, move)
{
	document.viewchessgameform.sqr.value=sqr;
	document.viewchessgameform.move.value=move;
	document.viewchessgameform.submit();
}

function invertchessboard()
{
	document.invertchessboardform.submit();
}

function doChessMove(a, b)
{
	document.dochessmoveform.a.value=a;
	document.dochessmoveform.b.value=b;
	document.dochessmoveform.submit();
}

function doChessMoveConfirm(a, b, piece, sqrA, sqrB)
{
	if(confirm("Move "+piece+" from "+sqrA+" to "+sqrB+"?"))
		doChessMove(a, b);
}

function doChessPromote(a, b, prom)
{
	document.dochesspromoteform.a.value=a;
	document.dochesspromoteform.b.value=b;
	document.dochesspromoteform.prom.value=prom;
	document.dochesspromoteform.submit();
}

function doChessOfferDraw()
{
	if(confirm("Are you sure you wish to offer a draw to your opponent?"))
		document.chessofferdraw.submit();
}

function doChessResignGame()
{
	if(confirm("Are you sure you wish to resign this game?"))
		document.chessresigngame.submit();
}

function doChessCancelGame()
{
	if(confirm("Are you sure you wish to cancel this game?\n\n1. A game is declared void if no more than 3 moves have been made\n2. Cancelling the game at this stage will therefore not effect your rating"))
		document.chessresigngame.submit();
}

function doCheckChessGameTimeout()
{
	document.docheckchessgametimeoutform.submit();
}

function changeBoardImage(value)
{
	document.board_image.src=value;
}

function changeFlagImage(value)
{
	document.flag_image.src=value;
}

function mooveWatcher()
{
	window.open('watcher/watcher.php', "cowplaymovewatcher", "width=320, height=100, status=no");
}

function helpOnload()
{
	help_init();
}

function helpCallback(index)
{
	parent.data.location="helpdata.php?index="+(index-1);
}

function cancelOpenChessGame(id)
{
	if(confirm("Are you sure you wish to cancel this open game offer?"))
		window.location='index.php?cmd=cancelopenchessgame&id='+id;
}

function joinChessGame(id)
{
	if(confirm("Are you sure you wish to play this game?"))
		window.location='index.php?cmd=joinchessgame&id='+id;
}

function alterChessTimeControl()
{
	boolFlag=gamechallengeform.time_type[0].checked;
	gamechallengeform.move_days.disabled=!boolFlag;
	gamechallengeform.move_minutes.disabled=boolFlag;
	gamechallengeform.move_seconds.disabled=boolFlag;
}

function alterChessColor()
{
	gamechallengeform.color.disabled=gamechallengeform.color_both.checked;
}

function alterChessCreateGameTimeControl()
{
	boolFlag=creategameform.time_type[0].checked;
	creategameform.move_days.disabled=!boolFlag;
	creategameform.move_minutes.disabled=boolFlag;
	creategameform.move_seconds.disabled=boolFlag;
}

function alterChessCreateGameColor()
{
	creategameform.color.disabled=creategameform.color_both.checked;
}

function help()
{
	openPopWin('help.php', 760, 570, ', status, resizable');
}

function whyDonate()
{
	openPopWin('donate.php', 320, 400, '');
}

function showImage(imageName)
{
	openPopWin(imageName, 660, 510, '');
}

function deleteMessage(id)
{
	if(confirm("Are you sure you wish to delete this message?"))
		window.location='index.php?cmd=deletemessage&id='+id;
}

function joinOpenTournament()
{
	if(confirm("Are you sure you want to join this tournament?"))
		joinopentournamentform.submit();
}

function leaveOpenTournament()
{
	if(confirm("Are you sure you want to leave this tournament?"))
		leaveopentournamentform.submit();
}

function removeTournamentPlayer(id, memberId)
{
	if(confirm("Are you sure you want to remove this player from the tournament?"))
		window.location="index.php?cmd=doremovetournamentplayer&id="+id+"&member_id="+memberId;
}

function startTournament()
{
	if(confirm("Are you sure you want to start this tournament?"))
		startopentournamentform.submit();
}

function cancelTournament()
{
	if(confirm("Are you sure you want to cancel this tournament?"))
		cancelopentournamentform.submit();
}