function $(v){return(document.getElementById(v))}function getEventSrc(e){return window.event?window.event.srcElement:e.target}function CancelEvent(e){if((typeof(e)=="undefined")&&(typeof(window.event)!="undefined")){e=window.event}if(typeof(window.event)!="undefined"){e.cancelBubble=true;}else{e.stopPropagation();}}function AjaxCreate(){try{return new XMLHttpRequest()}catch(e){}try{return new ActiveXObject("MSXML2.XMLHTTP")}catch(e){}try{return new ActiveXObject("Microsoft.XMLHTTP")}catch(e){}return false}function AjaxExecute(url,data,funct){var xhr=AjaxCreate();xhr.onreadystatechange=function(){if((xhr.readyState==4)&&(xhr.status==200)){funct(xhr.responseText)}};xhr.open("POST",url,true);xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");xhr.send(data)}function AjaxDummy(data){}function ltrim(s){return s.replace(/^\s*/,"")}function rtrim(s){return s.replace(/\s*$/,"")}function trim(s){return rtrim(ltrim(s))}function setFavorite(iURL,iTitle){if(navigator.appVersion.indexOf("Mac",0)>0){alert("This function is not available on Macintosh.\n\nUse instead the key combination \"Apple-D\".");return}if((navigator.appName.indexOf("Microsoft",0)>=0)&&(parseInt(navigator.appVersion)>=4)){window.external.AddFavorite(iURL,iTitle)}else{alert("This function is only available on Internet Explorer.\n\nFor Netscape, you can use instead the key combination \"Control-D\".");}}function showHelp(iShow){$("help").style.display=(iShow?"block":"none");return false}function __Query(iQueryId,iPath){window.location.href="http://"+window.location.hostname+iPath+"/"+$("__query_"+iQueryId).value;return false;}


var iPlayer	;
var iCells	;
var aCell	;

var iGamesWon	= 0;
var iGamesNull	= 0;
var iGamesLost	= 0;





function init()
	{
	setReplay  (false );
	setMessage ('none');

	for (i=0; i<3; i++){
	for (j=0; j<3; j++){ $("img"+i+j).src = "img/e.gif"; }}

	iPlayer = 1;
	iCells  = 0;
	aCell	= [[0,0,0],[0,0,0],[0,0,0]];
	}





function onLoad()
	{
	var i0 = new Image;  i0.src="img/x.gif" ;
	var i1 = new Image;  i1.src="img/o.gif" ;
	var i2 = new Image;  i2.src="img/x_.gif";
	var i3 = new Image;  i3.src="img/o_.gif";

	var i4 = new Image;  i4.src="img/win.gif";
	var i5 = new Image;  i5.src="img/lost.gif";
	var i6 = new Image;  i6.src="img/null.gif";
	var i7 = new Image;  i7.src="img/replay.gif";

	init();
	}





function thinkHarder()
	{
	if (iCells == 1)
		{
		var iRandom = Math.floor(2*Math.random());

		if (((aCell[0][0]==1)
		  || (aCell[0][2]==1)
		  || (aCell[2][0]==1)
		  || (aCell[2][2]==1))
		  ||((aCell[1][1]==0)
		  && (iRandom	 ==0)))	{ return (10*1			+ 1			);	}
		if (  aCell[1][0]==1)	{ return (10*2*iRandom	+ 2			);	}
		if (  aCell[0][1]==1)	{ return (10*2			+ 2*iRandom	);	}
		if (  aCell[1][2]==1)	{ return (10*2*iRandom	+ 0			);	}
		if (  aCell[2][1]==1)	{ return (10*0			+ 2*iRandom	);	}
								  return -1;
		}


	if (iCells == 3)
		{
		if (((aCell[2][2]==1)
		  && (aCell[0][0]==1))
		 || ((aCell[2][0]==1)
		  && (aCell[0][2]==1)))
			{
			switch (Math.floor(4*Math.random()))
				{
				case 0 :  return (10*0 + 1);
				case 1 :  return (10*1 + 2);
				case 2 :  return (10*2 + 1);
				case 3 :  return (10*1 + 0);
				}
			}

		if ( (aCell[0][0]==1)
		 && ((aCell[1][2]==1)
		  || (aCell[2][1]==1))
		 &&  (aCell[2][2]==0) )	{ return (10*2+2); }

		if ( (aCell[0][2]==1)
		 && ((aCell[1][0]==1)
		  || (aCell[2][1]==1))
		 && ( aCell[2][0]==0) )	{ return (10*2+0); }

		if ( (aCell[2][2]==1)
		 && ((aCell[1][0]==1)
		  || (aCell[0][1]==1))
		 && ( aCell[0][0]==0) )	{ return (10*0+0); }

		if (( aCell[2][0]==1)
		 && ((aCell[0][1]==1)
		  || (aCell[1][2]==1))
		 && ( aCell[0][2]==0) )	{ return (10*0+2); }
		}

	return -1;
	}





function thinkAndPlay()
	{
	for (a=2; a>0; a--)
		{
		if ((aCell[1][1]==0)  &&  ( ((aCell[0][0]==a) && (aCell[2][2]==a))
								  ||((aCell[2][0]==a) && (aCell[0][2]==a))
								  ||((aCell[1][0]==a) && (aCell[1][2]==a))
								  ||((aCell[0][1]==a) && (aCell[2][1]==a))))	{ playCell(1,1);  return; }
		if ((aCell[0][0]==0)  &&  ( ((aCell[0][1]==a) && (aCell[0][2]==a))
								  ||((aCell[1][1]==a) && (aCell[2][2]==a))
								  ||((aCell[1][0]==a) && (aCell[2][0]==a))))	{ playCell(0,0);  return; }
		if ((aCell[0][2]==0)  &&  ( ((aCell[0][0]==a) && (aCell[0][1]==a))
								  ||((aCell[2][0]==a) && (aCell[1][1]==a))
								  ||((aCell[1][2]==a) && (aCell[2][2]==a))))	{ playCell(0,2);  return; }
		if ((aCell[2][2]==0)  &&  ( ((aCell[2][0]==a) && (aCell[2][1]==a))
								  ||((aCell[0][0]==a) && (aCell[1][1]==a))
								  ||((aCell[1][2]==a) && (aCell[0][2]==a))))	{ playCell(2,2);  return; }
		if ((aCell[2][0]==0)  &&  ( ((aCell[0][0]==a) && (aCell[1][0]==a))
								  ||((aCell[0][2]==a) && (aCell[1][1]==a))
								  ||((aCell[2][1]==a) && (aCell[2][2]==a))))	{ playCell(2,0);  return; }
		if ((aCell[0][1]==0)  &&  ( ((aCell[0][2]==a) && (aCell[0][0]==a))
								  ||((aCell[2][1]==a) && (aCell[1][1]==a))))	{ playCell(0,1);  return; }
		if ((aCell[1][2]==0)  &&  ( ((aCell[0][2]==a) && (aCell[2][2]==a))
								  ||((aCell[1][0]==a) && (aCell[1][1]==a))))	{ playCell(1,2);  return; }
		if ((aCell[2][1]==0)  &&  ( ((aCell[2][0]==a) && (aCell[2][2]==a))
								  ||((aCell[0][1]==a) && (aCell[1][1]==a))))	{ playCell(2,1);  return; }
		if ((aCell[1][0]==0)  &&  ( ((aCell[0][0]==a) && (aCell[2][0]==a))
								  ||((aCell[1][1]==a) && (aCell[1][2]==a))))	{ playCell(1,0);  return; }
		}


	var c = $("expert").checked ? thinkHarder() : -1;

	if (c == -1)
		{
		do	{
			i = Math.floor(3 * Math.random());
			j = Math.floor(3 * Math.random());
			}
		while (aCell[i][j] != 0)
		}
	  else
	  	{
		i = Math.floor(c/10);
		j = c - 10*i;
		}

	playCell(i,j);
	}





function endGame(iWinner,iRow1,iCol1
						,iRow2,iCol2
						,iRow3,iCol3)
	{
	if (iWinner!=0)
		{
		$("img"+iRow1+iCol1).src = (iWinner==1)?"img/x_.gif":"img/o_.gif";
		$("img"+iRow2+iCol2).src = (iWinner==1)?"img/x_.gif":"img/o_.gif";
		$("img"+iRow3+iCol3).src = (iWinner==1)?"img/x_.gif":"img/o_.gif";
		}

	switch (iWinner)
		{
		case 1	: $("games_won" ).innerHTML = ++iGamesWon ;	  if (iGamesWon >1)  {$("txt_games_won" ).innerHTML = "parties gagnées";}	setMessage("win" );  break;
		case 2	: $("games_lost").innerHTML = ++iGamesLost;	  if (iGamesLost>1)  {$("txt_games_lost").innerHTML = "parties perdues";}	setMessage("lost");  break;
		default	: $("games_null").innerHTML = ++iGamesNull;   if (iGamesNull>1)  {$("txt_games_null").innerHTML = "parties nulles" ;}	setMessage("null");  break;
		}

	iGames = iGamesWon
			+iGamesNull
			+iGamesLost;	 

	$("games").innerHTML = iGames;  if (iGames>1)  {$("txt_games").innerHTML = "parties jouées";}

	setReplay (true);
	}





function checkGame()
	{
	for (b=1; b<3; b++){
	for (a=0; a<3; a++)
		{
		if ((aCell[a][0]==b)
		 && (aCell[a][1]==b)
		 && (aCell[a][2]==b))	{ endGame(b,a,0,a,1,a,2);	return true; }

		if ((aCell[0][a]==b)
		 && (aCell[1][a]==b)
		 && (aCell[2][a]==b))	{ endGame(b,0,a,1,a,2,a);	return true; }
		}

		if ((aCell[0][0]==b)
		  &&(aCell[1][1]==b)
		  &&(aCell[2][2]==b))	{ endGame(b,0,0,1,1,2,2);	return true; }

		if ((aCell[0][2]==b)
		  &&(aCell[1][1]==b)
		  &&(aCell[2][0]==b))	{ endGame(b,0,2,1,1,2,0);	return true; }
		}

	if (iCells == 9)			{ endGame(0,0,0,0,0,0,0);	return true; }

	return false;
	}





function playCell(iRow,iCol)
	{
	if (aCell[iRow][iCol] == 0)
		{
		aCell[iRow][iCol] = iPlayer;
		iCells++;

		$("img"+iRow+iCol).src = (iPlayer==1)?"img/x.gif":"img/o.gif";

		if (checkGame())
			{
			return;
			}

		iPlayer = 3-iPlayer;

		if (iPlayer == 2)
			{
			setTimeout ("thinkAndPlay()",750);
			}
		}
	}





function clickCell(iRow,iCol)
	{
	if ((iPlayer		   == 1)
	 && (aCell[iRow][iCol] == 0))
		{
		playCell(iRow,iCol);
		}
	}





function setMessage(iMessage)  { $("messages").innerHTML = "<img src='img/"+iMessage+".gif' alt='' />";						}
function setReplay (iReplay )  { $("replay"  ).innerHTML = iReplay?"<img src='img/replay.gif' alt='' onclick='init()' />"
																  :"<img src='img/none.gif'	  alt='' />";					}





