// JavaScript Ataxx.
// Copyright (C) 2007 Thunor <thunorsif@hotmail.com>
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

// --- Global constants. ---
// --- Can't use 'const' here as IE doesn't like it so must use 'var' :s ---

var axleveleasy = 2;
var axlevelnormal = 3;
var axlevelhard = 4;
var axcounterblank = 0;
var axcounteryou = 1;
var axcountercpu = 2;
var axcounterpillar = 3;
var axturnyou = 1;
var axturncpu = 2;
var axpillarconfigurationmax = 8;
var axpieceselection = 0;
var axpieceplacement = 1;
var axgameboardwidth = 7;
var axgameboardheight = 7;
var axturncputimeout = 1000;
var axwinnerfoundtimeout = 300;
// --- Global variables. ---
var axanimation = false;
var axgameover = false;
var axgamestarted = false;
var axcurrentlevel = axlevelnormal;
var axscoreyou = 0;
var axscorecpu = 0;
var axmaxposscore = 0;
var axturnwhos = axturnyou;
var axpillarconfiguration = 0;
var axpieceselectorplace = axpieceselection;
var axpieceselectionx = 0;
var axpieceselectiony = 0;
var axwinnerfoundmsgstate = 0;
var axyouwins = 0;
var axcpuwins = 0;
var axturncputimeoutid = 0;
var axwinnerfoundtimeoutid = 0;

// --- Create 2 dimensional array for the game board. ---
var axcount = 0;
var axboardarray = new Array(7);
for (axcount = 0; axcount < 7; axcount++) {
	axboardarray[axcount] = new Array(7);	// 7*7 = 49 board squares = [0][0] to [6][6].
}

// --- Load all images used within document. ---
counteryou = new Image(); counteryou.src = "images/counteryou.gif";
counteryouno = new Image(); counteryouno.src = "images/counteryouno.gif";
counteryouyes = new Image(); counteryouyes.src = "images/counteryouyes.gif";
counteryounew = new Image(); counteryounew.src = "images/counteryounew.gif";
counteryoudie = new Image(); counteryoudie.src = "images/counteryoudie.gif";
counteryoukill = new Image(); counteryoukill.src = "images/counteryoukill.gif";

countercpu = new Image(); countercpu.src = "images/countercpu.gif";
countercpuno = new Image(); countercpuno.src = "images/countercpuno.gif";
countercpuyes = new Image(); countercpuyes.src = "images/countercpuyes.gif";
countercpunew = new Image(); countercpunew.src = "images/countercpunew.gif";
countercpudie = new Image(); countercpudie.src = "images/countercpudie.gif";
countercpukill = new Image(); countercpukill.src = "images/countercpukill.gif";

pillar = new Image(); pillar.src = "images/pillar.gif";

number0 = new Image(); number0.src = "images/0.gif";
number1 = new Image(); number1.src = "images/1.gif";
number2 = new Image(); number2.src = "images/2.gif";
number3 = new Image(); number3.src = "images/3.gif";
number4 = new Image(); number4.src = "images/4.gif";
number5 = new Image(); number5.src = "images/5.gif";
number6 = new Image(); number6.src = "images/6.gif";
number7 = new Image(); number7.src = "images/7.gif";
number8 = new Image(); number8.src = "images/8.gif";
number9 = new Image(); number9.src = "images/9.gif";

easy1sel = new Image(); easy1sel.src = "images/easy1sel.jpg";
easy2sel = new Image(); easy2sel.src = "images/easy2sel.jpg";
normal1sel = new Image(); normal1sel.src = "images/normal1sel.jpg";
normal2sel = new Image(); normal2sel.src = "images/normal2sel.jpg";
normal3sel = new Image(); normal3sel.src = "images/normal3sel.jpg";
hard1sel = new Image(); hard1sel.src = "images/hard1sel.jpg";
hard2sel = new Image(); hard2sel.src = "images/hard2sel.jpg";

pointerleftoff = new Image(); pointerleftoff.src = "images/pointerleftoff.jpg";
pointerrightoff = new Image(); pointerrightoff.src = "images/pointerrightoff.jpg";

youwin1off = new Image(); youwin1off.src = "images/youwin1off.jpg";
youwin2off = new Image(); youwin2off.src = "images/youwin2off.jpg";
cpuwin1off = new Image(); cpuwin1off.src = "images/cpuwin1off.jpg";
cpuwin2off = new Image(); cpuwin2off.src = "images/cpuwin2off.jpg";

blank = new Image(); blank.src = "images/blankx1y1.gif";
pillarsel = new Image(); pillarsel.src = "images/pillarsel.gif";

animonsel = new Image(); animonsel.src = "images/animonsel.jpg";
animoffsel = new Image(); animoffsel.src = "images/animoffsel.jpg";

// ------------------------------------------------------------------
// Functions.
// ------------------------------------------------------------------

function axprocessboardclick(boardxy) {
	var colloop = 0; var rowloop = 0;
	var topx = 0; var topy = 0;
	var botx = 0; var boty = 0;
	var arrayindexx = 0; var arrayindexy = 0;
	var moveablepiece = false;
	
	// --- Game on? ---
	if (!axgameover) {
		// --- Filter out board clicks while the cpu is making a move. ---
		if (axturnwhos == axturnyou) {
			// --- Seperate passed coordinates into x,y. ---
			arrayindexx = (boardxy - (boardxy % 10)) / 10;
			arrayindexy = boardxy % 10;
			// --- Has the user clicked on a 'you' piece? ---
			if (axboardarray[arrayindexx][arrayindexy] == axcounteryou) {
				// --- Can the selected piece be moved? ---
				// --- Define a movement square around the selected piece. ---
				topx = arrayindexx - 2; if (topx < 0) topx = 0;
				topy = arrayindexy - 2; if (topy < 0) topy = 0;
				botx = arrayindexx + 2; if (botx > axgameboardwidth - 1) botx = axgameboardwidth - 1;
				boty = arrayindexy + 2; if (boty > axgameboardheight - 1) boty = axgameboardheight - 1;
				// --- Search for any possible valid move. ---
				moveablepiece = false;
				for (rowloop = topy; rowloop <= boty; rowloop++) {
					for (colloop = topx; colloop <= botx; colloop++) {
						if (axboardarray[colloop][rowloop] == axcounterblank) moveablepiece = true;
					}
				}
				// --- Display the validity of the move. ---
				if (moveablepiece) {
					document.images["board" + boardxy].src = counteryouyes.src;
				} else {
					document.images["board" + boardxy].src = counteryouno.src;
				}
				// --- Has a piece already been selected? ---
				if (axpieceselectorplace == axpieceplacement) {
					if (!(axpieceselectionx == arrayindexx && axpieceselectiony == arrayindexy)) {
						// --- Restore any previously selected piece. ---
						document.images["board" + axpieceselectionx + axpieceselectiony].src = counteryou.src;
					}
				}
				// --- Store piece location and record selection as complete. ---
				axpieceselectionx = arrayindexx;
				axpieceselectiony = arrayindexy;
				axpieceselectorplace = axpieceplacement;
			} else {
				// --- Piece Selection or Placement mode? ---
				if (axpieceselectorplace == axpieceplacement) {
					// --- Has the user clicked on a blank square? ---
					if (axboardarray[arrayindexx][arrayindexy] == axcounterblank) {
						// --- Is the blank square within 2 moves away? ---
						if (Math.abs(axpieceselectionx - arrayindexx) <= 2 && Math.abs(axpieceselectiony - arrayindexy) <= 2) {
							// --- Make the move. ---
							axmakeamove(axpieceselectionx, axpieceselectiony, arrayindexx, arrayindexy, "you");
							// --- Has the board been filled or any player been eliminated? ---
							axdisplayscores();	
							if (axfindawinner()) return;	// Exit here as game over man.
							// --- Check for Cpu not being able to move before passing control back. ---
							if (axcheckplayercanmove("cpu")) {
								// --- Pass control to Cpu. ---
								axturnwhos = axturncpu;
								axdisplaywhosturn("cpu");
								// --- We use the timeout method else it's too fast to watch. ---
								axturncputimeoutid = setTimeout("axprocesscomputersturn()", axturncputimeout);
							} else {
								// --- You can take another turn - Cpu can't move. ---
								axpieceselectorplace = axpieceselection;
							}
						}
					}
				}
			}
		}
	}
}

function axprocesscomputersturn() {
	var colloop = 0; var rowloop = 0;
	var colloop2 = 0; var rowloop2 = 0;
	var colloop3 = 0; var rowloop3 = 0;
	var topx = 0; var topy = 0;
	var botx = 0; var boty = 0;
	var topx2 = 0; var topy2 = 0;
	var botx2 = 0; var boty2 = 0;

	// --- Create array (list) to hold all possible cpu moves. ---
	var movearray = new Array(axgameboardwidth * axgameboardheight * 24);	// Impossible to exceed ;)
	for (colloop = 0; colloop < axgameboardwidth * axgameboardheight * 24; colloop++) {
		movearray[colloop] = new Array(3);
		movearray[colloop][0] = null;	// [x][0] = move value.
		movearray[colloop][1] = 0;		// [x][1] = sourcexy.
		movearray[colloop][2] = 0;		// [x][2] = destxy.
	}
	var posmovecount = 0;	// This is an index into the movearray.
	var movevalue = 0;		// Value assigned to a particular move.
	var sourcex = 0; var sourcey = 0;
	var destx = 0; var desty = 0;
	var count = 0;
	var swaparray = new Array(3);
	var swapindex = 0;
		
	// --- Evaluate all possible moves. ---
	for (rowloop = 0; rowloop < axgameboardheight; rowloop++) {
		for (colloop = 0; colloop < axgameboardwidth; colloop++) {
			if (axboardarray[colloop][rowloop] == axcountercpu) {
				// --- Define a movement square around the selected piece. ---
				topx = colloop - 2; if (topx < 0) topx = 0;
				topy = rowloop - 2; if (topy < 0) topy = 0;
				botx = colloop + 2; if (botx > axgameboardwidth - 1) botx = axgameboardwidth - 1;
				boty = rowloop + 2; if (boty > axgameboardheight - 1) boty = axgameboardheight - 1;
				// --- Search for any possible valid move (empty square). ---
				for (rowloop2 = topy; rowloop2 <= boty; rowloop2++) {
					for (colloop2 = topx; colloop2 <= botx; colloop2++) {
						if (axboardarray[colloop2][rowloop2] == axcounterblank) {
							// --- Record the move (source and destination). ---
							movearray[posmovecount][1] = String(colloop) + String(rowloop);		// SourceXY.
							movearray[posmovecount][2] = String(colloop2) + String(rowloop2);	// DestXY.
							// --- Calculate the move's worth (initialise it).
							if (Math.abs(colloop - colloop2) == 2 || Math.abs(rowloop - rowloop2) == 2) {
								movevalue = 0;	// If the move is 2 places then it's worth 0.
							} else {
								movevalue = 1;	// If the move is 1 place then it's worth 1.
							}
							// --- Do some HARD setting specific stuff here. ---
							if (Math.abs(colloop - colloop2) == 2 || Math.abs(rowloop - rowloop2) == 2) {
								if (axcurrentlevel == axlevelhard) {
									// --- Identify all bordering cpu pieces around source. ---
									topx2 = colloop - 1; if (topx2 < 0) topx2 = 0;
									topy2 = rowloop - 1; if (topy2 < 0) topy2 = 0;
									botx2 = colloop + 1; if (botx2 > axgameboardwidth - 1) botx2 = axgameboardwidth - 1;
									boty2 = rowloop + 1; if (boty2 > axgameboardheight - 1) boty2 = axgameboardheight - 1;
									// --- -1 for each bordering cpu piece. ---
									for (rowloop3 = topy2; rowloop3 <= boty2; rowloop3++) {
										for (colloop3 = topx2; colloop3 <= botx2; colloop3++) {
											if (axboardarray[colloop3][rowloop3] == axcountercpu) {
												// --- Don't count the piece we are moving. ---
												if (!(colloop == colloop3 && rowloop == rowloop3)) movevalue--;
											}
										}
									}
								}
							}
							// --- Identify all bordering opponent pieces around destination. ---
							topx2 = colloop2 - 1; if (topx2 < 0) topx2 = 0;
							topy2 = rowloop2 - 1; if (topy2 < 0) topy2 = 0;
							botx2 = colloop2 + 1; if (botx2 > axgameboardwidth - 1) botx2 = axgameboardwidth - 1;
							boty2 = rowloop2 + 1; if (boty2 > axgameboardheight - 1) boty2 = axgameboardheight - 1;
							// --- +1 for each bordering opponent piece. ---
							for (rowloop3 = topy2; rowloop3 <= boty2; rowloop3++) {
								for (colloop3 = topx2; colloop3 <= botx2; colloop3++) {
									if (axboardarray[colloop3][rowloop3] == axcounteryou) movevalue++;
								}
							}
							movearray[posmovecount][0] = movevalue;
							posmovecount++;	// Point to next available array element.
						}
					}
				}
			}
		}
	}
	// --- Randomise the list so that the computer isn't predictable. ---
	for (count = 0; count < posmovecount; count++) {
		swapindex = Math.round(Math.random() * (posmovecount - 1));
		swaparray[0] = movearray[count][0];
		swaparray[1] = movearray[count][1];
		swaparray[2] = movearray[count][2];
		movearray[count][0] = movearray[swapindex][0];
		movearray[count][1] = movearray[swapindex][1];
		movearray[count][2] = movearray[swapindex][2];
		movearray[swapindex][0] = swaparray[0];
		movearray[swapindex][1] = swaparray[1];
		movearray[swapindex][2] = swaparray[2];
	}	
	// --- 'Computer AI' starts here :) ---
	// --- Find best move available and take it. ---
	movevalue = -255;
	for (count = 0; count < posmovecount; count++) {
		if (axcurrentlevel != axleveleasy) {
			if (movearray[count][0] > movevalue) {
				movevalue = movearray[count][0];
				sourcex = (movearray[count][1] - (movearray[count][1] % 10)) / 10;
				sourcey = movearray[count][1] % 10;
				destx = (movearray[count][2] - (movearray[count][2] % 10)) / 10;
				desty = movearray[count][2] % 10;
			}
		}
		// --- Do some EASY setting specific stuff here. ---
		if (axcurrentlevel == axleveleasy) {
			// --- EASY is basically NORMAL there is a 50% chance it will choose
			// a better move if it finds one. ---
			if (movearray[count][0] > movevalue) {
				if ((count == 0) || (Math.round(Math.random()) == 1 )) {
					movevalue = movearray[count][0];
					sourcex = (movearray[count][1] - (movearray[count][1] % 10)) / 10;
					sourcey = movearray[count][1] % 10;
					destx = (movearray[count][2] - (movearray[count][2] % 10)) / 10;
					desty = movearray[count][2] % 10;
				}
			}
		}
	}
	// --- Make the move. ---
	axmakeamove(sourcex, sourcey, destx, desty, "cpu");
	// --- Has the board been filled or any player been eliminated? ---
	axdisplayscores();	
	if (axfindawinner()) return;	// Exit here as game over man.
	// --- Check for You not being able to move before passing control back. ---
	if (axcheckplayercanmove("you")) {
		// --- Pass control to You. ---
		axturnwhos = axturnyou;
		axdisplaywhosturn("you");
		axpieceselectorplace = axpieceselection;
	} else {
		// --- Cpu can take another turn - You can't move. ---
		// --- We use the timeout method else it's too fast to watch. ---
		axturncputimeoutid = setTimeout("axprocesscomputersturn()", axturncputimeout);
	}
}

function axmakeamove(sourcex, sourcey, destx, desty, who) {
	var colloop = 0; var rowloop = 0;
	var topx = 0; var topy = 0;
	var botx = 0; var boty = 0;
	var opponent = "you";
	
	if (who == "you") opponent = "cpu";	// Invert if necessary.
	axboardarray[destx][desty] = eval("axcounter" + who);
	axgamestarted = true;	// Global variable. Game has truly started after 1st move.
	if (axanimation) {
		document.images["board" + destx + desty].src = eval("counter" + who + "new.src");
	} else {
		document.images["board" + destx + desty].src = eval("counter" + who + ".src");
	}
	// --- Is it a two place move? If so remove source counter. ---
	if (Math.abs(sourcex - destx) == 2 || Math.abs(sourcey - desty) == 2) {
		axboardarray[sourcex][sourcey] = axcounterblank;
		if (axanimation) {
			document.images["board" + sourcex + sourcey].src = eval("counter" + who + "die.src");
		} else {
			document.images["board" + sourcex + sourcey].src = blank.src;
		}
	} else {
		// --- Restore any previously selected piece. ---
		document.images["board" + sourcex + sourcey].src = eval("counter" + who + ".src");
	}
	// --- Identify all bordering pieces. ---
	topx = destx - 1; if (topx < 0) topx = 0;
	topy = desty - 1; if (topy < 0) topy = 0;
	botx = destx + 1; if (botx > axgameboardwidth - 1) botx = axgameboardwidth - 1;
	boty = desty + 1; if (boty > axgameboardheight - 1) boty = axgameboardheight - 1;
	// --- Aquire all bordering opponent pieces. ---
	for (rowloop = topy; rowloop <= boty; rowloop++) {
		for (colloop = topx; colloop <= botx; colloop++) {
			if (axboardarray[colloop][rowloop] == eval("axcounter" + opponent)) {
				axboardarray[colloop][rowloop] = eval("axcounter" + who);
				if (axanimation) {
					document.images["board" + colloop + rowloop].src = eval("counter" + who + "kill.src");
				} else {
					document.images["board" + colloop + rowloop].src = eval("counter" + who + ".src");
				}
			}
		}
	}
}

function axcheckplayercanmove(who) {
	var colloop = 0; var rowloop = 0;
	var colloop2 = 0; var rowloop2 = 0;
	var topx = 0; var topy = 0;
	var botx = 0; var boty = 0;
	var moveablepiece = false;

	for (rowloop = 0; rowloop < axgameboardheight; rowloop++) {
		for (colloop = 0; colloop < axgameboardwidth; colloop++) {
			if (axboardarray[colloop][rowloop] == eval("axcounter" + who)) {
				// --- Define a movement square around the selected piece. ---
				topx = colloop - 2; if (topx < 0) topx = 0;
				topy = rowloop - 2; if (topy < 0) topy = 0;
				botx = colloop + 2; if (botx > axgameboardwidth - 1) botx = axgameboardwidth - 1;
				boty = rowloop + 2; if (boty > axgameboardheight - 1) boty = axgameboardheight - 1;
				// --- Search for any possible valid move. ---
				for (rowloop2 = topy; rowloop2 <= boty; rowloop2++) {
					for (colloop2 = topx; colloop2 <= botx; colloop2++) {
						if (axboardarray[colloop2][rowloop2] == axcounterblank) moveablepiece = true;
					}
				}
			}
		}
	}
	return moveablepiece;
}

function axfindawinner() {
	if ((axscoreyou + axscorecpu == axmaxposscore) || (axscoreyou == 0 || axscorecpu == 0)) {
		axgameover = true;	// Global variable.
		// --- Draws are not possible because there's always an odd number of squares. ---
		if (axscoreyou > axscorecpu) {
			axyouwins++;
			axdisplaywins();
			axwinnerfound("you");
		} else {
			axcpuwins++;
			axdisplaywins();
			axwinnerfound("cpu");
		}
		// Save the stats to a cookie.			
		cookiemanager("write", "axyouwins", axyouwins, 366);
		cookiemanager("write", "axcpuwins", axcpuwins, 366);
		return true;
	} else {
		return false;
	}
}

function axwinnerfound(who) {
	if (axgameover) {
		if (axwinnerfoundmsgstate == 0) {
			axwinnerfoundmsgstate = 1;
			document.images[who + "win1"].src = blank.src;
			document.images[who + "win2"].src = blank.src;
		} else {
			axwinnerfoundmsgstate = 0;
			document.images[who + "win1"].src = eval(who + "win1off.src");
			document.images[who + "win2"].src = eval(who + "win2off.src");
		}
		// --- Flash "You win!" or "Cpu win!". ---
		axwinnerfoundtimeoutid = setTimeout("axwinnerfound('" + who + "')", axwinnerfoundtimeout);
	}
}

function axreset() {
	var colloop = 0;
	var rowloop = 0;

	// --- Clear any pending timed operations else unexpected things occur. ---
	clearTimeout(axturncputimeoutid);
	clearTimeout(axwinnerfoundtimeout);
	// --- Setup some default values. ---
	axgameover = false;
	axgamestarted = false;
	axturnwhos = axturnyou;
	axpieceselectorplace = axpieceselection;
	axwinnerfoundmsgstate = 0;
	// --- Initialise the game board array. ---
	for (rowloop = 0; rowloop < axgameboardheight; rowloop++) {
		for (colloop = 0; colloop < axgameboardwidth; colloop++) {
			axboardarray[colloop][rowloop] = axcounterblank;
		}
	}
	// --- Set the default counters. ---
	axboardarray[0][0] = axcounteryou;
	axboardarray[axgameboardwidth - 1][axgameboardheight - 1] = axcounteryou;
	axboardarray[0][axgameboardheight - 1] = axcountercpu;
	axboardarray[axgameboardwidth - 1][0] = axcountercpu;
	// --- Set the pillar configuration. ---
	switch (axpillarconfiguration) {
		case 0 :
			axboardarray[2][2] = axcounterpillar;
			axboardarray[4][2] = axcounterpillar;
			axboardarray[2][4] = axcounterpillar;
			axboardarray[4][4] = axcounterpillar;
			break;	
		case 1 :
			axboardarray[1][1] = axcounterpillar;
			axboardarray[5][1] = axcounterpillar;
			axboardarray[2][2] = axcounterpillar;
			axboardarray[4][2] = axcounterpillar;
			axboardarray[2][4] = axcounterpillar;
			axboardarray[4][4] = axcounterpillar;
			axboardarray[1][5] = axcounterpillar;
			axboardarray[5][5] = axcounterpillar;
			break;	
		case 2 :
			axboardarray[3][2] = axcounterpillar;
			axboardarray[2][3] = axcounterpillar;
			axboardarray[4][3] = axcounterpillar;
			axboardarray[3][4] = axcounterpillar;
			break;	
		case 3 :
			axboardarray[3][1] = axcounterpillar;
			axboardarray[3][2] = axcounterpillar;
			axboardarray[1][3] = axcounterpillar;
			axboardarray[2][3] = axcounterpillar;
			axboardarray[4][3] = axcounterpillar;
			axboardarray[5][3] = axcounterpillar;
			axboardarray[3][4] = axcounterpillar;
			axboardarray[3][5] = axcounterpillar;
			break;	
		case 4 :
			axboardarray[3][0] = axcounterpillar;
			axboardarray[0][3] = axcounterpillar;
			axboardarray[6][3] = axcounterpillar;
			axboardarray[3][6] = axcounterpillar;
			break;	
		case 5 :
			axboardarray[3][0] = axcounterpillar;
			axboardarray[3][1] = axcounterpillar;
			axboardarray[3][2] = axcounterpillar;
			axboardarray[0][3] = axcounterpillar;
			axboardarray[1][3] = axcounterpillar;
			axboardarray[2][3] = axcounterpillar;
			axboardarray[4][3] = axcounterpillar;
			axboardarray[5][3] = axcounterpillar;
			axboardarray[6][3] = axcounterpillar;
			axboardarray[3][4] = axcounterpillar;
			axboardarray[3][5] = axcounterpillar;
			axboardarray[3][6] = axcounterpillar;
			break;	
		case 6 :
			axboardarray[2][1] = axcounterpillar;
			axboardarray[4][1] = axcounterpillar;
			axboardarray[1][2] = axcounterpillar;
			axboardarray[5][2] = axcounterpillar;
			axboardarray[1][4] = axcounterpillar;
			axboardarray[5][4] = axcounterpillar;
			axboardarray[2][5] = axcounterpillar;
			axboardarray[4][5] = axcounterpillar;
			break;
		case 7 :
			axboardarray[2][0] = axcounterpillar;
			axboardarray[4][0] = axcounterpillar;
			axboardarray[0][2] = axcounterpillar;
			axboardarray[6][2] = axcounterpillar;
			axboardarray[0][4] = axcounterpillar;
			axboardarray[6][4] = axcounterpillar;
			axboardarray[2][6] = axcounterpillar;
			axboardarray[4][6] = axcounterpillar;
			break;
		default :
			break;	
	}
	// --- Set up score and buttons etc. ---
	axdisplaygameboard();
	axdisplayscores();	
	axdisplaywins();
	axsetupdifficultybuttons();
	axdisplaywhosturn("you");
	axsetanimation();

	// --- Reset pillar level indicators. ---
	for (colloop = 0; colloop <= axpillarconfigurationmax; colloop++) {
		document.images["pillar" + colloop].src = blank.src;
	}
	document.images["pillar" + axpillarconfiguration].src = pillarsel.src;
	// --- Blank Win pointers. ---
	document.images["youwin1"].src = youwin1off.src;
	document.images["youwin2"].src = youwin2off.src;
	document.images["cpuwin1"].src = cpuwin1off.src;
	document.images["cpuwin2"].src = cpuwin2off.src;
}

function axdisplaygameboard() {
	var colloop = 0;
	var rowloop = 0;

	// --- Draw the game board on screen. ---
	for (rowloop = 0; rowloop < axgameboardheight; rowloop++) {
		for (colloop = 0; colloop < axgameboardwidth; colloop++) {
			switch (axboardarray[colloop][rowloop]) {
				case axcounterblank :
					document.images["board" + colloop + rowloop].src = blank.src;
					break;			
				case axcounteryou :
					if (axanimation) {
						document.images["board" + colloop + rowloop].src = counteryounew.src;
					} else {
						document.images["board" + colloop + rowloop].src = counteryou.src;
					}
					break;			
				case axcountercpu :
					if (axanimation) {
						document.images["board" + colloop + rowloop].src = countercpunew.src;
					} else {
						document.images["board" + colloop + rowloop].src = countercpu.src;
					}
					break;			
				case axcounterpillar :
					document.images["board" + colloop + rowloop].src = pillar.src;
					break;			
			}
		}
	}
}

function axdisplaywhosturn(who) {
	if (who == "you") {
		document.images["turnyou"].src = blank.src;
		document.images["turncpu"].src = pointerrightoff.src;
	} else {
		document.images["turnyou"].src = pointerleftoff.src;
		document.images["turncpu"].src = blank.src;
	}
}

function axdisplayscores() {
	var colloop = 0;
	var rowloop = 0;
	var numberindex = 0;

	// --- Calculate and store the (global) scores. ---
	axscoreyou = 0;	// Global variable.
	axscorecpu = 0;	// Global variable.
	axmaxposscore = 0;	// Global variable.
	for (rowloop = 0; rowloop < axgameboardheight; rowloop++) {
		for (colloop = 0; colloop < axgameboardwidth; colloop++) {
			if (axboardarray[colloop][rowloop] != axcounterpillar) axmaxposscore++;
			if (axboardarray[colloop][rowloop] == axcounteryou) axscoreyou++;
			if (axboardarray[colloop][rowloop] == axcountercpu) axscorecpu++;
		}
	}
	// --- Display the scores. ---
	numberindex = (axscoreyou - (axscoreyou % 10)) / 10;
	document.images["scoreyou1"].src = eval("number" + numberindex + ".src");
	numberindex = axscoreyou % 10;
	document.images["scoreyou2"].src = eval("number" + numberindex + ".src");
	numberindex = (axscorecpu - (axscorecpu % 10)) / 10;
	document.images["scorecpu1"].src = eval("number" + numberindex + ".src");
	numberindex = axscorecpu % 10;
	document.images["scorecpu2"].src = eval("number" + numberindex + ".src");
}

function axdisplaywins() {
	var numberindex = 0;
	
	// --- Display the wins. ---
	numberindex = (axyouwins - (axyouwins % 10)) / 10;
	document.images["winsyou1"].src = eval("number" + numberindex + ".src");
	numberindex = axyouwins % 10;
	document.images["winsyou2"].src = eval("number" + numberindex + ".src");
	numberindex = (axcpuwins - (axcpuwins % 10)) / 10;
	document.images["winscpu1"].src = eval("number" + numberindex + ".src");
	numberindex = axcpuwins % 10;
	document.images["winscpu2"].src = eval("number" + numberindex + ".src");
}

function axsetupdifficultybuttons() {
	switch (axcurrentlevel) {
		case axleveleasy :
			document.images["easy1"].src = easy1sel.src;
			document.images["easy2"].src = easy2sel.src;
			document.images["normal1"].src = blank.src;
			document.images["normal2"].src = blank.src;
			document.images["normal3"].src = blank.src;
			document.images["hard1"].src = blank.src;
			document.images["hard2"].src = blank.src;
			break;
		case axlevelnormal :
			document.images["normal1"].src = normal1sel.src;
			document.images["normal2"].src = normal2sel.src;
			document.images["normal3"].src = normal3sel.src;
			document.images["easy1"].src = blank.src;
			document.images["easy2"].src = blank.src;
			document.images["hard1"].src = blank.src;
			document.images["hard2"].src = blank.src;
			break;
		case axlevelhard :
			document.images["hard1"].src = hard1sel.src;
			document.images["hard2"].src = hard2sel.src;
			document.images["normal1"].src = blank.src;
			document.images["normal2"].src = blank.src;
			document.images["normal3"].src = blank.src;
			document.images["easy1"].src = blank.src;
			document.images["easy2"].src = blank.src;
			break;
	}	
}

function axincpillarconfiguration(level) {
	if (axcurrentlevel == level) {
		axpillarconfiguration++;
		if (axpillarconfiguration > axpillarconfigurationmax) {
			axpillarconfiguration = 0
		}
	} 
}

function axsetanimation() {
	if (axanimation) {
		document.images["animon"].src = animonsel.src;
		document.images["animoff"].src = blank.src;
	} else {
		document.images["animon"].src = blank.src;
		document.images["animoff"].src = animoffsel.src;
	}
}

function axresetcookie() {
	if (confirm("Are you sure you want to reset the cookie?")) {
		cookiemanager('write', 'axyouwins', 0, 0);
		cookiemanager('write', 'axcpuwins', 0, 0);
	}
}


