// Joseph Colton
// CIS 571 - Artificial Intelligence
// Final Project - Nine Men's Morris

// Global Variables
var AJAX_UNINITIALIZED = 0;
var AJAX_LOADING       = 1;
var AJAX_LOADED        = 2;
var AJAX_INTERACTIVE   = 3;
var AJAX_COMPLETE      = 4;
var EMPTY = 0;
var TEAM1 = 1;
var TEAM2 = 2;
var boardPlaces = new Array();
var outTeam1 = 9;
var outTeam2 = 9;
var deadTeam1 = 0;
var deadTeam2 = 0;
var team1Title = "Blue";
var team2Title = "Pink";
var gameState = "none";
var moveFromLoc = "none";

/* Misc Functions */
function Element(ElementID) {
    return document.getElementById(ElementID);
}

/* Game Function Definitions */
function startGame() {
    initGame();
    gameState = "team1move";
    nextMove();
}

function getAIUrl(aiType) {
    if (aiType == "random") return "random.cgi";
    // Default to the random agent
    return 'none';
}

function Exists(ElementID) {
    var ref = Element(ElementID);
    if (ref == null) {
	return false;
    }
    return true;
}

function requestDataExec(requestURL, targetID, postVars, execScript) {
    if (typeof(postVars) == 'undefined') {
	postVars = '';
    }

    // Start by creating a request
    var request;
    if (window.ActiveXObject) {
	// IE
	request = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
	// Mozilla, Firefix, Safari, etc.
	request = new XMLHttpRequest();
	request.overrideMimeType('text/xml');
    }

    // Once we get the request, we want to process it
    request.onreadystatechange = function() {
	if (request.readyState == AJAX_COMPLETE) {
	    var data = request.responseText;
	    // Put the output in a nice place if it exists
	    if (Exists(targetID)) {
		Element(targetID).innerHTML = data;
	    }
	    // Execute code if any was passed in
	    if (execScript) {
		setTimeout(execScript, 500);
	    }
	}
    }

    // Actually make the request
    request.open('POST', requestURL, true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-length", postVars.length);
    request.setRequestHeader("Connection", "close");
    request.send(postVars);
}

function nextMove() {
    // Update the status line
    Element('statusMove').innerHTML = "Calculating";
    if (gameState == "team1move") Element('statusMove').innerHTML = team1Title + " move";
    if (gameState == "team2move") Element('statusMove').innerHTML = team2Title + " move";
    if (gameState == "team1moveto") Element('statusMove').innerHTML = team1Title + " move to";
    if (gameState == "team2moveto") Element('statusMove').innerHTML = team2Title + " move to";
    if (gameState == "team1kill") Element('statusMove').innerHTML = team1Title + " kill";
    if (gameState == "team2kill") Element('statusMove').innerHTML = team2Title + " kill";

    // See if someone won
    if (deadCount(TEAM1) == 7) {
	gameState == "gameover";
	Element('statusMove').innerHTML = team2Title + " Won!";
	return;
    }
    if (deadCount(TEAM2) == 7) {
	gameState == "gameover";
	Element('statusMove').innerHTML = team1Title + " Won!";
	return;
    }

    // Start players move
    player = getPlayer();
    agentType = Element('configTeam'+player).value;
    // If player is human, we wait for mouse input.
    // If player is ai, we send a AJAX request
    if (agentType != 'human') {
	// We do not need to fire off AI on moves, since they
	// Have already decided what to do.
	if (gameState == "team1moveto") return;
	if (gameState == "team2moveto") return;
	requestAIMove(agentType);
    }
}

function requestAIMove(aiType) {
    // Get the agent AI url
    agentUrl = getAIUrl(aiType);
    // Check to see if it is valid
    if (agentUrl == 'none') {
	alert("Invalid AI agent: "+aiType);
	return;
    }
    // Get the board configuration
    board = getBoard();
    // Build AJAX function arguments
    postVars = "board="+board;
    execScript = "doAIMove();";
    targetID = "ajaxReply";
    // Fire off AJAX request
    requestDataExec(agentUrl, targetID, postVars, execScript);
}

function getBoard() {
    var boardString = "";
    // Get the player number and move
    if (gameState == "team1move") boardString = '1m ';
    if (gameState == "team2move") boardString = '2m ';
    if (gameState == "team1kill") boardString = '1k ';
    if (gameState == "team2kill") boardString = '2k ';

    // Get the 24 positions
    boardString = boardString + boardPlaces['a1'];
    boardString = boardString + boardPlaces['b2'];
    boardString = boardString + boardPlaces['c3'];
    boardString = boardString + boardPlaces['d1'];
    boardString = boardString + boardPlaces['d2'];
    boardString = boardString + boardPlaces['d3'];
    boardString = boardString + boardPlaces['g1'];
    boardString = boardString + boardPlaces['f2'];
    boardString = boardString + boardPlaces['e3'];
    boardString = boardString + boardPlaces['a4'];
    boardString = boardString + boardPlaces['b4'];
    boardString = boardString + boardPlaces['c4'];
    boardString = boardString + boardPlaces['e4'];
    boardString = boardString + boardPlaces['f4'];
    boardString = boardString + boardPlaces['g4'];
    boardString = boardString + boardPlaces['c5'];
    boardString = boardString + boardPlaces['b6'];
    boardString = boardString + boardPlaces['a7'];
    boardString = boardString + boardPlaces['d5'];
    boardString = boardString + boardPlaces['d6'];
    boardString = boardString + boardPlaces['d7'];
    boardString = boardString + boardPlaces['e5'];
    boardString = boardString + boardPlaces['f6'];
    boardString = boardString + boardPlaces['g7'];

    // Get the unplaced pieces
    boardString = boardString + " " + outCount(TEAM1);
    boardString = boardString + " " + outCount(TEAM2);

    return boardString;
}

function makesMill(loc) {
    var team = boardPlaces[loc];

    // North-West Last
    if (loc == 'a1') {
	if (boardPlaces['d1'] == team && boardPlaces['g1'] == team) return true;
	if (boardPlaces['a4'] == team && boardPlaces['a7'] == team) return true;
    }
    if (loc == 'b2') {
	if (boardPlaces['d2'] == team && boardPlaces['f2'] == team) return true;
	if (boardPlaces['b4'] == team && boardPlaces['b6'] == team) return true;
    }
    if (loc == 'c3') {
	if (boardPlaces['d3'] == team && boardPlaces['e3'] == team) return true;
	if (boardPlaces['c4'] == team && boardPlaces['c5'] == team) return true;
    }
    // North Last
    if (loc == 'd1') {
	if (boardPlaces['a1'] == team && boardPlaces['g1'] == team) return true;
	if (boardPlaces['d2'] == team && boardPlaces['d3'] == team) return true;
    }
    if (loc == 'd2') {
	if (boardPlaces['b2'] == team && boardPlaces['f2'] == team) return true;
	if (boardPlaces['d1'] == team && boardPlaces['d3'] == team) return true;
    }
    if (loc == 'd3') {
	if (boardPlaces['c3'] == team && boardPlaces['e3'] == team) return true;
	if (boardPlaces['d1'] == team && boardPlaces['d2'] == team) return true;
    }
    // North-East Last
    if (loc == 'g1') {
	if (boardPlaces['a1'] == team && boardPlaces['d1'] == team) return true;
	if (boardPlaces['g4'] == team && boardPlaces['g7'] == team) return true;
    }
    if (loc == 'f2') {
	if (boardPlaces['b2'] == team && boardPlaces['d2'] == team) return true;
	if (boardPlaces['f4'] == team && boardPlaces['f6'] == team) return true;
    }
    if (loc == 'e3') {
	if (boardPlaces['c3'] == team && boardPlaces['d3'] == team) return true;
	if (boardPlaces['e4'] == team && boardPlaces['e5'] == team) return true;
    }
    // West Last
    if (loc == 'a4') {
	if (boardPlaces['a1'] == team && boardPlaces['a7'] == team) return true;
	if (boardPlaces['b4'] == team && boardPlaces['c4'] == team) return true;
    }
    if (loc == 'b4') {
	if (boardPlaces['b2'] == team && boardPlaces['b6'] == team) return true;
	if (boardPlaces['a4'] == team && boardPlaces['c4'] == team) return true;
    }
    if (loc == 'c4') {
	if (boardPlaces['c3'] == team && boardPlaces['c5'] == team) return true;
	if (boardPlaces['a4'] == team && boardPlaces['b4'] == team) return true;
    }
    // East Last
    if (loc == 'e4') {
	if (boardPlaces['e3'] == team && boardPlaces['e5'] == team) return true;
	if (boardPlaces['f4'] == team && boardPlaces['g4'] == team) return true;
    }
    if (loc == 'f4') {
	if (boardPlaces['f2'] == team && boardPlaces['f6'] == team) return true;
	if (boardPlaces['e4'] == team && boardPlaces['g4'] == team) return true;
    }
    if (loc == 'g4') {
	if (boardPlaces['g1'] == team && boardPlaces['g7'] == team) return true;
	if (boardPlaces['e4'] == team && boardPlaces['f4'] == team) return true;
    }
    // South-West Last
    if (loc == 'c5') {
	if (boardPlaces['c3'] == team && boardPlaces['c4'] == team) return true;
	if (boardPlaces['d5'] == team && boardPlaces['e5'] == team) return true;
    }
    if (loc == 'b6') {
	if (boardPlaces['b2'] == team && boardPlaces['b4'] == team) return true;
	if (boardPlaces['d6'] == team && boardPlaces['f6'] == team) return true;
    }
    if (loc == 'a7') {
	if (boardPlaces['a1'] == team && boardPlaces['a4'] == team) return true;
	if (boardPlaces['d7'] == team && boardPlaces['g7'] == team) return true;
    }
    // South Last
    if (loc == 'd5') {
	if (boardPlaces['c5'] == team && boardPlaces['e5'] == team) return true;
	if (boardPlaces['d6'] == team && boardPlaces['d7'] == team) return true;
    }
    if (loc == 'd6') {
	if (boardPlaces['b6'] == team && boardPlaces['f6'] == team) return true;
	if (boardPlaces['d5'] == team && boardPlaces['d7'] == team) return true;
    }
    if (loc == 'd7') {
	if (boardPlaces['a7'] == team && boardPlaces['g7'] == team) return true;
	if (boardPlaces['d5'] == team && boardPlaces['d6'] == team) return true;
    }
    // South-East Last
    if (loc == 'e5') {
	if (boardPlaces['c5'] == team && boardPlaces['d5'] == team) return true;
	if (boardPlaces['e3'] == team && boardPlaces['e4'] == team) return true;
    }
    if (loc == 'f6') {
	if (boardPlaces['b6'] == team && boardPlaces['d6'] == team) return true;
	if (boardPlaces['f2'] == team && boardPlaces['f4'] == team) return true;
    }
    if (loc == 'g7') {
	if (boardPlaces['a7'] == team && boardPlaces['d7'] == team) return true;
	if (boardPlaces['g1'] == team && boardPlaces['g4'] == team) return true;
    }

    return false;
}

function initGame() {
    // Initialize board positions
    boardPlaces['a1'] = EMPTY;
    boardPlaces['b2'] = EMPTY;
    boardPlaces['c3'] = EMPTY;
    boardPlaces['d1'] = EMPTY;
    boardPlaces['d2'] = EMPTY;
    boardPlaces['d3'] = EMPTY;
    boardPlaces['g1'] = EMPTY;
    boardPlaces['f2'] = EMPTY;
    boardPlaces['e3'] = EMPTY;
    boardPlaces['a4'] = EMPTY;
    boardPlaces['b4'] = EMPTY;
    boardPlaces['c4'] = EMPTY;
    boardPlaces['e4'] = EMPTY;
    boardPlaces['f4'] = EMPTY;
    boardPlaces['g4'] = EMPTY;
    boardPlaces['c5'] = EMPTY;
    boardPlaces['b6'] = EMPTY;
    boardPlaces['a7'] = EMPTY;
    boardPlaces['d5'] = EMPTY;
    boardPlaces['d6'] = EMPTY;
    boardPlaces['d7'] = EMPTY;
    boardPlaces['e5'] = EMPTY;
    boardPlaces['f6'] = EMPTY;
    boardPlaces['g7'] = EMPTY;

    // Reset Piece locations
    Element('piece11').className = 'pieceOut';
    Element('piece12').className = 'pieceOut';
    Element('piece13').className = 'pieceOut';
    Element('piece14').className = 'pieceOut';
    Element('piece15').className = 'pieceOut';
    Element('piece16').className = 'pieceOut';
    Element('piece17').className = 'pieceOut';
    Element('piece18').className = 'pieceOut';
    Element('piece19').className = 'pieceOut';

    Element('piece21').className = 'pieceOut';
    Element('piece22').className = 'pieceOut';
    Element('piece23').className = 'pieceOut';
    Element('piece24').className = 'pieceOut';
    Element('piece25').className = 'pieceOut';
    Element('piece26').className = 'pieceOut';
    Element('piece27').className = 'pieceOut';
    Element('piece28').className = 'pieceOut';
    Element('piece29').className = 'pieceOut';

    // Clean piece states
    outTeam1 = outCount(TEAM1);
    outTeam2 = outCount(TEAM2);
    deadTeam1 = deadCount(TEAM1);
    deadTeam2 = deadCount(TEAM2);
}

function getNextOut(team) {
    var i;
    for (i = 1; i <= 9; i++) {
	var id = "piece" + team + i;
	if (Element(id).className == 'pieceOut') {
	    // Found an available piece
	    return id;
	}
    }
    return "";
}

function outCount(team) {
    var count = 0;
    if (team == TEAM1) {
	if (Element('piece11').className == 'pieceOut') count++;
	if (Element('piece12').className == 'pieceOut') count++;
	if (Element('piece13').className == 'pieceOut') count++;
	if (Element('piece14').className == 'pieceOut') count++;
	if (Element('piece15').className == 'pieceOut') count++;
	if (Element('piece16').className == 'pieceOut') count++;
	if (Element('piece17').className == 'pieceOut') count++;
	if (Element('piece18').className == 'pieceOut') count++;
	if (Element('piece19').className == 'pieceOut') count++;
    }
    if (team == TEAM2) {
	if (Element('piece21').className == 'pieceOut') count++;
	if (Element('piece22').className == 'pieceOut') count++;
	if (Element('piece23').className == 'pieceOut') count++;
	if (Element('piece24').className == 'pieceOut') count++;
	if (Element('piece25').className == 'pieceOut') count++;
	if (Element('piece26').className == 'pieceOut') count++;
	if (Element('piece27').className == 'pieceOut') count++;
	if (Element('piece28').className == 'pieceOut') count++;
	if (Element('piece29').className == 'pieceOut') count++;
    }
    return count;
}

function deadCount(team) {
    var count = 0;
    if (team == TEAM1) {
	if (Element('piece11').className == 'pieceDead') count++;
	if (Element('piece12').className == 'pieceDead') count++;
	if (Element('piece13').className == 'pieceDead') count++;
	if (Element('piece14').className == 'pieceDead') count++;
	if (Element('piece15').className == 'pieceDead') count++;
	if (Element('piece16').className == 'pieceDead') count++;
	if (Element('piece17').className == 'pieceDead') count++;
	if (Element('piece18').className == 'pieceDead') count++;
	if (Element('piece19').className == 'pieceDead') count++;
    }
    if (team == TEAM2) {
	if (Element('piece21').className == 'pieceDead') count++;
	if (Element('piece22').className == 'pieceDead') count++;
	if (Element('piece23').className == 'pieceDead') count++;
	if (Element('piece24').className == 'pieceDead') count++;
	if (Element('piece25').className == 'pieceDead') count++;
	if (Element('piece26').className == 'pieceDead') count++;
	if (Element('piece27').className == 'pieceDead') count++;
	if (Element('piece28').className == 'pieceDead') count++;
	if (Element('piece29').className == 'pieceDead') count++;
    }
    return count;
}

function getPlayer() {
    if (gameState == 'team1move') return TEAM1;
    if (gameState == 'team2move') return TEAM2;
    if (gameState == 'team1moveto') return TEAM1;
    if (gameState == 'team2moveto') return TEAM2;
    if (gameState == 'team1kill') return TEAM1;
    if (gameState == 'team2kill') return TEAM2;
    return EMPTY;
}

function otherPlayer(team) {
    if (team == TEAM1) return TEAM2;
    if (team == TEAM2) return TEAM1;
    return EMPTY;
}

function doAIMove() {
    var ajaxReply = Element('ajaxReply').innerHTML;
    eval(ajaxReply);

    // Determine the move type
    if (AIMove[0] == 'place') {
	doMove(AIMove[1]);
    }
    if (AIMove[0] == 'move') {
	doMove(AIMove[1]);
	doMove(AIMove[2]);
    }
    if (AIMove[0] == 'kill') {
	doMove(AIMove[1]);
    }
    if (AIMove[0] == 'pass') {
	doPass();
    }
}

function doPass() {
    var player = getPlayer();
    var other = otherPlayer(player);

    // We will assume sanity checking has happened already.
    gameState = 'team'+other+'move';
    nextMove();
}

function doMove(loc) {
    var player = getPlayer();
    var playerOut = outCount(player);

    // alert("Clicked ("+loc+")");

    if (gameState == "team1move" || gameState == "team2move") {
	// Make sure the currect player is making a move
	if (boardPlaces[loc] == EMPTY) {
	    // Trying to place a piece
	    if (playerOut > 0) {
		// Get the piece to place	
		piece = getNextOut(player);
		if (piece == '') {
		    alert("Strange, a piece is missing!");
		    return;
		}
		// Place the piece
		Element(piece).className = loc;
		boardPlaces[loc] = player;
		// See if a mill was formed
		if (makesMill(loc)) {
		    // Mill was formed
		    gameState = "team" + player + "kill";
		} else {
		    // No Mill Formed
		    gameState = "team" + otherPlayer(player) + "move";
		}
		nextMove();
	    } else {
		alert("Player is out of spare pieces!");
		return;
	    }
	} else {
	    // Trying to move a piece
	    if (boardPlaces[loc] == player) {
		// Trying to player's piece
		if (outCount(player) <= 0) {
		    moveFromLoc = loc;
		    gameState = "team" + player + "moveto";
		    nextMove();
		} else {
		    alert("You cannot move pieces until all have been placed!");
		}
		return;
	    } else {
		// Trying to move enemy piece
		alert("You can only move your own piece!");
		return;
	    }
	}
    }

    if (gameState == "team1kill" || gameState == "team2kill") {
	if (boardPlaces[loc] == otherPlayer(player)) {
	    var piece = getLocationPiece(loc);
	    if (piece == 'none') {
		alert('Missing Piece!');
		return;
	    }
	    // Clean the board location
	    Element(piece).className = 'pieceDead';
	    boardPlaces[loc] = EMPTY;
	    // Make it the other teams turn
	    gameState = "team" + otherPlayer(player) + "move";
	    nextMove();
	    return;
	}
    }

    if (gameState == "team1moveto" || gameState == "team2moveto") {
	if (boardPlaces[loc] == EMPTY) {
	    // The move to place is clear
	    if (isNeighbor(loc, moveFromLoc, player)) {
		var piece = getLocationPiece(moveFromLoc);
		if (piece == 'none') {
		    alert('Missing Piece!');
		    return;
		}
		// Set new location
		Element(piece).className = loc;
		boardPlaces[loc] = boardPlaces[moveFromLoc];
		// Clear the old spot
		boardPlaces[moveFromLoc] = EMPTY;
		// Check for mills
		if (makesMill(loc)) {
		    // Mill was formed
		    gameState = "team" + player + "kill";
		} else {
		    // No Mill Formed
		    gameState = "team" + otherPlayer(player) + "move";
		}
		nextMove();
		return;
	    } else {
		// Trying to move to a place that is out of range.
		alert("That is not a valid position");
	    }
	}
    }

}

function isNeighbor(loc1, loc2, player) {
    // The flying rule allows players to fly at any
    // time when they have exactly 3 pieces left.
    if (deadCount(player) == 6 && outCount(player) == 0) return true;

    // North-West Places
    if (loc1 == 'a1') {
	if (loc2 == 'a4' || loc2 == 'd1') return true;
    }
    if (loc1 == 'b2') {
	if (loc2 == 'b4' || loc2 == 'd2') return true;
    }
    if (loc1 == 'c3') {
	if (loc2 == 'c4' || loc2 == 'd3') return true;
    }
    // North Places
    if (loc1 == 'd1') {
	if (loc2 == 'a1' || loc2 == 'g1' || loc2 == 'd2') return true;
    }
    if (loc1 == 'd2') {
	if (loc2 == 'b2' || loc2 == 'f2') return true;
	if (loc2 == 'd1' || loc2 == 'd3') return true;
    }
    if (loc1 == 'd3') {
	if (loc2 == 'c3' || loc2 == 'e3' || loc2 == 'd2') return true;
    }
    // North-East Places
    if (loc1 == 'g1') {
	if (loc2 == 'd1' || loc2 == 'g4') return true;
    }
    if (loc1 == 'f2') {
	if (loc2 == 'd2' || loc2 == 'f4') return true;
    }
    if (loc1 == 'e3') {
	if (loc2 == 'd3' || loc2 == 'e4') return true;
    }
    // West Places
    if (loc1 == 'a4') {
	if (loc2 == 'a1' || loc2 == 'a7' || loc2 == 'b4') return true;
    }
    if (loc1 == 'b4') {
	if (loc2 == 'a4' || loc2 == 'c4') return true;
	if (loc2 == 'b2' || loc2 == 'b6') return true;
    }
    if (loc1 == 'c4') {
	if (loc2 == 'c3' || loc2 == 'c5' || loc2 == 'b4') return true;
    }
    // East Places
    if (loc1 == 'e4') {
	if (loc2 == 'e3' || loc2 == 'e5' || loc2 == 'f4') return true;
    }
    if (loc1 == 'f4') {
	if (loc2 == 'e4' || loc2 == 'g4') return true;
	if (loc2 == 'f2' || loc2 == 'f6') return true;
    }
    if (loc1 == 'g4') {
	if (loc2 == 'g1' || loc2 == 'g7' || loc2 == 'f4') return true;
    }
    // South-West Places
    if (loc1 == 'c5') {
	if (loc2 == 'c4' || loc2 == 'd5') return true;
    }
    if (loc1 == 'b6') {
	if (loc2 == 'b4' || loc2 == 'd6') return true;
    }
    if (loc1 == 'a7') {
	if (loc2 == 'a4' || loc2 == 'd7') return true;
    }
    // South Places
    if (loc1 == 'd5') {
	if (loc2 == 'c5' || loc2 == 'e5' || loc2 == 'd6') return true;
    }
    if (loc1 == 'd6') {
	if (loc2 == 'b6' || loc2 == 'f6') return true;
	if (loc2 == 'd5' || loc2 == 'd7') return true;
    }
    if (loc1 == 'd7') {
	if (loc2 == 'a7' || loc2 == 'g7' || loc2 == 'd6') return true;
    }
    // South-East Places
    if (loc1 == 'e5') {
	if (loc2 == 'd5' || loc2 == 'e4') return true;
    }
    if (loc1 == 'f6') {
	if (loc2 == 'd6' || loc2 == 'f4') return true;
    }
    if (loc1 == 'g7') {
	if (loc2 == 'd7' || loc2 == 'g4') return true;
    }

    return false;
}

function getLocationPiece(loc) {
    if (Element('piece11').className == loc) return 'piece11';
    if (Element('piece12').className == loc) return 'piece12';
    if (Element('piece13').className == loc) return 'piece13';
    if (Element('piece14').className == loc) return 'piece14';
    if (Element('piece15').className == loc) return 'piece15';
    if (Element('piece16').className == loc) return 'piece16';
    if (Element('piece17').className == loc) return 'piece17';
    if (Element('piece18').className == loc) return 'piece18';
    if (Element('piece19').className == loc) return 'piece19';
    if (Element('piece21').className == loc) return 'piece21';
    if (Element('piece22').className == loc) return 'piece22';
    if (Element('piece23').className == loc) return 'piece23';
    if (Element('piece24').className == loc) return 'piece24';
    if (Element('piece25').className == loc) return 'piece25';
    if (Element('piece26').className == loc) return 'piece26';
    if (Element('piece27').className == loc) return 'piece27';
    if (Element('piece28').className == loc) return 'piece28';
    if (Element('piece29').className == loc) return 'piece29';
    return 'none';
}

function boardClick(event) {
    // Check to see if moving is allowed
    if (getPlayer() == TEAM1) {
	if (Element('configTeam1').value != 'human') {
	    alert("This is not a human's move!");
	    return;
	}
    }
    if (getPlayer() == TEAM2) {
	if (Element('configTeam2').value != 'human') {
	    alert("This is not a human's move!");
	    return;
	}
    }

    // Figure out where the move was
    x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("board").offsetLeft;
    y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("board").offsetTop;

    // Get the location of the move
    loc = calculateLocation(x, y);
    if (isValid(loc)) {
	doMove(loc);
    } else {
	alert("Invalid Location");
    }
}

function pieceClick(piece) {
    loc = Element(piece).className;
    if (loc == 'pieceOut') {
	alert("Pick a place on the board to place the piece.");
	return;
    }
    if (loc == 'pieceDead') {
	alert("How did you click a dead piece?");
	return;
    }
    doMove(loc);
}

function calculateLocation(x, y) {
    /* Calculate the Column */
    colFloat = x / 40;
    col = 'x';
    if (colFloat > 0.25 && colFloat < 0.75) col = 'a';
    if (colFloat > 1.25 && colFloat < 1.75) col = 'b';
    if (colFloat > 2.25 && colFloat < 2.75) col = 'c';
    if (colFloat > 3.25 && colFloat < 3.75) col = 'd';
    if (colFloat > 4.25 && colFloat < 4.75) col = 'e';
    if (colFloat > 5.25 && colFloat < 5.75) col = 'f';
    if (colFloat > 6.25 && colFloat < 6.75) col = 'g';

    /* Calculate the Row */
    rowFloat = y / 40;
    row = 0;
    if (rowFloat > 0.25 && rowFloat < 0.75) row = 1;
    if (rowFloat > 1.25 && rowFloat < 1.75) row = 2;
    if (rowFloat > 2.25 && rowFloat < 2.75) row = 3;
    if (rowFloat > 3.25 && rowFloat < 3.75) row = 4;
    if (rowFloat > 4.25 && rowFloat < 4.75) row = 5;
    if (rowFloat > 5.25 && rowFloat < 5.75) row = 6;
    if (rowFloat > 6.25 && rowFloat < 6.75) row = 7;    

    loc = col+row;
    return loc
}

function isValid(loc) {
    if (loc == 'a1') return true;
    if (loc == 'b2') return true;
    if (loc == 'c3') return true;
    if (loc == 'd1') return true;
    if (loc == 'd2') return true;
    if (loc == 'd3') return true;
    if (loc == 'g1') return true;
    if (loc == 'f2') return true;
    if (loc == 'e3') return true;
    if (loc == 'a4') return true;
    if (loc == 'b4') return true;
    if (loc == 'c4') return true;
    if (loc == 'e4') return true;
    if (loc == 'f4') return true;
    if (loc == 'g4') return true;
    if (loc == 'c5') return true;
    if (loc == 'b6') return true;
    if (loc == 'a7') return true;
    if (loc == 'd5') return true;
    if (loc == 'd6') return true;
    if (loc == 'd7') return true;
    if (loc == 'e5') return true;
    if (loc == 'f6') return true;
    if (loc == 'g7') return true;
    return false;
}