var fullyLoaded=false;
var imageDir = "othello/";


function canplay() {
 fullyLoaded=true;
 ogame.style.visibility="visible";
 pwl.style.visibility="hidden";
 pwl.style.display="none";
 }

function showStatus(msg)
{
/////	document.Form.Status.value = msg;	
}

function quitGame() {
	return (confirm("Do you really want to quit playing?"));
}

function delay (time) {
	startdate = new Date();
	start = startdate.getTime();
	nowdate = new Date();
	while ((nowdate.getTime() - start) / 10 < time) {
		 nowdate = new Date();
	}
}

GAME_DONE = false;

navvendor = navigator.appName.substring(0,8).toUpperCase();
navversion = parseInt(navigator.appVersion.substring(0,1));
goodBrowser = "N";

if (goodBrowser == "N") {

goodBrowser = "Y";

black = new Image(37,37); black.src = imageDir+"black.gif";
white = new Image(37,37); white.src = imageDir+"white.gif";
blank = new Image(37,37); blank.src = imageDir+"blank.gif";
ghostb = new Image(37,37); ghostb.src = imageDir+"ghostb.gif";
ghostw = new Image(37,37); ghostw.src = imageDir+"ghostw.gif";
}

function makearray (dim) {
	this.length = dim;
	for (var i=0; i < dim; i++) {
			this[i] = '';
	}
	return this
}

grid = new makearray(10);		//10x10 matrix of squares (including offboard grids)
for (var i=0; i < grid.length; i++) {
		grid [i] = new makearray(10);
}

function restart() {
	GAME_DONE = false;
	yourtiles = 2;
	mytiles = 2;
	initBoard();
}

function settile(where,which) {
	if (goodBrowser == "Y") {
		document.images[where].src = which;
	}
}

hexn = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function toChar(numb) {
	return hexn.substring(numb,numb + 1);
}

function toInt(code) {
	return hexn.indexOf(code);
}

black_count = 0;        
white_count = 0;

BLACK = 1;          // declare state of each square
WHITE = 2;
EMPTY = 0;
OFFBOARD = -1;

function initBoard()                // initialize screen
{
	for (var i=0; i<10; i++)      // initialize off-board squares
	   {
	      grid [i][0] = OFFBOARD;
	      grid [i][9] = OFFBOARD;
	      grid [0][i] = OFFBOARD;
	      grid [9][i] = OFFBOARD;
	   }

	for (var i=1; i<9; i++)       // initialize game board to be empty
	   for (var j=1; j<9; j++)
	      grid [i][j] = EMPTY;

	grid [4][4] = WHITE;          // except for initial set up
	grid [5][4] = BLACK;
	grid [4][5] = BLACK;
	grid [5][5] = WHITE;

	paintBoard();
}
 
function doMouseOver (where) {
if (!fullyLoaded) {
 return;
 }
	if (goodBrowser == "Y" && GAME_DONE == false) {
		var x, y;
		x = toInt(where.substring(1,2));
		y = toInt(where.substring(2,3));

		if (legalMove (x, y, BLACK, WHITE, false)) {
			settile (where, ghostb.src);
		}
	}
}

function doMouseOut (where) {
if (!fullyLoaded) {
 return;
 }
	if (goodBrowser == "Y" && GAME_DONE == false) {
		var x, y;
		x = toInt(where.substring(1,2));
		y = toInt(where.substring(2,3));

		if (grid[x][y] == BLACK)
			settile (where, black.src);
		else if (grid[x][y] == WHITE)
			settile (where, white.src);
		else
			settile (where, blank.src);
	}
}

// BLACK clicked on a square - update screen

function doClick(where) {
if (!fullyLoaded) {
 alert("Please wait until the page is fully loaded or else you'll experience problems");
 location.reload();
 return true;
 }

	var x, y;
	x = toInt(where.substring(1,2));
	y = toInt(where.substring(2,3));

	if (legalMove(x,y,BLACK,WHITE,true)) {

// test
		showStatus(where);
// test

		grid [x][y] = BLACK;             // set that square to black
		paintBoard();

		whiteResponds(); //scp moved from below.
	}
	else showStatus(where+" - illegal move.");

	return true;
} 


   // computer responds with a move
function whiteResponds()
	{
	var found;   	                 // true if a legal square is found
	var i, j;				// indices for loops
  
	found=false;
	if (legalMove(1,1,WHITE,BLACK,true))      // first check corners
	   {
	   grid [1][1]=WHITE;
	   found=true;
	   }
	if ( (!found) && (legalMove(8,8,WHITE,BLACK,true)) )
	   {
	   grid [8][8]=WHITE;
	   found=true;
	   }
	if ( (!found) && (legalMove(1,8,WHITE,BLACK,true)) )
	   {
	   grid [1][8]=WHITE;
	   found=true;
	   }
	if ( (!found) && (legalMove(8,1,WHITE,BLACK,true)) )
	   {
	   grid [8][1]=WHITE;
	   found=true;
	   }

	i=3;				// check center squares
	while ((!found) && (i < 7))
	{
	    j=3;
	      while ( (!found) && (j < 7))
	    {
	    if	(legalMove(i,j,WHITE,BLACK,true)) 
	         {
	         grid [i][j]=WHITE;
	         found=true;
	         }
	       j++;
	     }
	     i++;
	}

	i=3;
	while ((!found) && (i < 7))    // then check edges except for those
	   {                           // surrounding a corner
	   if (legalMove(1,i,WHITE,BLACK,true))
	      {
	      grid [1][i]=WHITE;
	      found=true;
	      }
	   if ( (!found) && (legalMove(8,i,WHITE,BLACK,true)))
	        {
	      grid [8][i]=WHITE;
	      found=true;
	      }
	   if ( (!found) && (legalMove(i,1,WHITE,BLACK,true)))
	      {
	      grid [i][1]=WHITE;
	      found=true;
	      }
	   if ( (!found) && (legalMove(i,8,WHITE,BLACK,true)))
	      {
	      grid [i][8]=WHITE;
	      found=true;
	      }
	    i++;
	   }
	
	i=3;             
	while ((!found) && (i < 7))		// next check inner edges
	   {
	   if (legalMove(2,i,WHITE,BLACK,true))
	      {
	      grid [2][i]=WHITE;
	      found=true;
	      }
	   if ( (!found) && (legalMove(7,i,WHITE,BLACK,true)))
	      {
	      grid [7][i]=WHITE;
	      found=true;
	      }
	   if ( (!found) && (legalMove(i,2,WHITE,BLACK,true)))
	      {
	      grid [i][2]=WHITE;
	      found=true;
	      }
	   if ( (!found) && (legalMove(i,7,WHITE,BLACK,true)))
	      {
	      grid [i][7]=WHITE;
	      found=true;
	      }
	    i++;
	   }
   
	 i=1;		// finally squares surrounding a corner
	 while ((!found) && (i < 9))
	 {
	   j=1;
	   while ((!found) && (j < 9))
	   {	
	      if (legalMove(i,j,WHITE,BLACK,true)) 
	         {
	         found=true;
	         grid [i][j]=WHITE;
	         }
	       j++;
	   }
	   i++;
	}
	paintBoard(); // check whether game has ended also.

	if (GAME_DONE == false && !found) {
		alert("White cannot move. Black's turn."); //scp
		return; //scp
	}

	var black_done;               // true if black cannot move anywhere

	black_done=true;                 // check if black can move anywhere
	for (var i=1; i<9; i++)
		for (var j=1; j<9; j++) {
			if (legalMove(i,j,BLACK,WHITE,false) )
				black_done=false;
		}	
	if (GAME_DONE == false && black_done) { // black cannot move - white finishes up
		alert("Black cannot move. White will move."); //scp
		whiteResponds(); // scp recursive call
	}
}

// decide if the move is legal

function legalMove (r, c, color, othercolor, flip) {
	var i,j;                                 // position on board
	var legal = false;                	// true if legal move
	var stepcount;                  	// counts the stepping across the board

	if (grid [r][c] == EMPTY) {                 // square clicked must be empty
		for (var xdir=-1; xdir < 2; xdir++) {
			for (var ydir=-1; ydir < 2; ydir++) {
				stepcount = 0;

/**** NN3.0 does not support this?
				do
				{
					stepcount++;
					i = r + stepcount*xdir; // so many steps along x-axis
					j = c + stepcount*ydir; // so many steps along y-axis
				}
				while ( (i > 0) && (i < 9) && (j > 0) && (j < 9) &&
						  (grid [i][j] == othercolor));
****/
				{
					stepcount++;
					i = r + stepcount*xdir; // so many steps along x-axis
					j = c + stepcount*ydir; // so many steps along y-axis
				}
				while ( (i > 0) && (i < 9) && (j > 0) && (j < 9) &&
						  (grid [i][j] == othercolor))
				{
					stepcount++;
					i = r + stepcount*xdir; // so many steps along x-axis
					j = c + stepcount*ydir; // so many steps along y-axis
				}



				if (( i > 0) && (i < 9) && (j > 0) && (j < 9) &&
					(stepcount > 1) && 
					// You must move more than one step for legal move
					(grid [i][j] == color) ) {
					legal = true;
					if (flip)
						for (var k = 1; k < stepcount; k++)
							grid [r+xdir*k][c+ydir*k] = color;
				}
			}
		}
	}  
	if ( legal==true) return true;
	else return false;
}

// paint the screen with the right configuartion

function paintBoard()
   {
	black_count=0;                        // initialize counts to 0
	white_count=0;                        

	if (GAME_DONE) return;

	for (var i=1; i<9; i++) {              // draw the whole board
		for (var j=1; j<9; j++) {
			if (grid [i][j] == BLACK) {      // draw BLACK discs
				settile ("C"+toChar(i)+toChar(j), black.src); 
				black_count++;
			}
			else if (grid [i][j] == WHITE) {      // draw WHITE discs
				settile("C"+toChar(i)+toChar(j), white.src);
				white_count++;
			}
			else settile("C"+toChar(i)+toChar(j), blank.src);
		}
	}
	GAME_DONE = true;
	for (i=1; i<9; i++)
		for (j=1; j<9; j++)
			if ((legalMove(i,j,BLACK,WHITE,false)) ||
				(legalMove(i,j,WHITE,BLACK,false)))
				GAME_DONE=false;

	if (GAME_DONE == true) {
		if (white_count>black_count)
			alert("White won with "+white_count+" discs over Black's " + black_count);
		else if (black_count>white_count)
			alert("Black won with "+black_count+" discs over White's " + white_count);
		else alert("Tied game");
	}
	else {     
		if (white_count > black_count)
			showStatus("White is winning with "+white_count+" discs");
		else if (black_count > white_count)
			showStatus("Black is winning with "+black_count+" discs");
		else showStatus("Currently tied");
	}
}
 

if (goodBrowser == "Y") {

	document.writeln("<div align=center>");
	document.writeln("<table align=center border=0 cellpadding=0 cellspacing=0>");

	for (var x = 1; x < 9; x++) {
		document.writeln("<TR>");
		for (var y = 1; y < 9; y++) {
			document.write("<TD HEIGHT=37 WIDTH=37>");

			document.write('<A HREF="javascript://" onMouseOver="doMouseOver(\'C' + toChar(x) + toChar(y) + '\')" onMouseOut="doMouseOut(\'C' + toChar(x) + toChar(y) + '\')" onClick="doClick(\'C' + toChar(x) + toChar(y) + '\')" >');

			document.write('<IMG SRC="'+imageDir+'blank.gif" WIDTH=37 HEIGHT=37 BORDER=0 NAME="C' + toChar(x) + toChar(y) + '"></A>');
			document.writeln("</TD>");
		}
		document.writeln("</TR>");
	}
	document.writeln("</TABLE>");

// SCP
	document.writeln("<hr width=0 size=0>");
	document.writeln("<TABLE align=center border=2 cellpadding=2 cellspacing=2 bgcolor=#999999>");
	document.writeln("<tr><td align=center>");
	document.writeln("<a href='javascript://' onclick='restart()'>&nbsp;<font color=black>new game</font>&nbsp;</a>");
	document.writeln("</TD></TR>");
	document.writeln("</TABLE>");

// SCP Remove after testing!
/////	document.writeln("<form name=Form><input name=Status type=text size=30 value='Black's turn'></form>");
//scp Remove after testing!

	document.writeln("</div>");
// scp

	initBoard();
}
