
function gameBoard(){
	var gameBoard;
	arrWidth = Math.sqrt(posArray.length);
	arrSize = posArray.length;
	gameBoard = makeBoard();
	document.write(gameBoard);
}

function loadNetscape(){
	
	gameBoard();

}

function makeBoard(){
	var output;
	var pos = 0;
	
	output = "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% HEIGHT=100%>";
	output = output + "<TR>";
	output = output + "<TD ALIGN=CENTER VALIGN=CENTER> ";
	output = output + "<TABLE BORDER=1 BORDERCOLOR=black CELLSPACING=0 CELLPADDING=0>";
	for(var i=0; i<arrWidth; i++){//rows for the gameboard
		output = output + "<TR>";
		for(var j=0; j<arrWidth; j++){
			output = output + "<TD ALIGN=CENTER VALIGN=CENTER width=20>";
			output = output + "<img name=\"img" + pos + "\" src=\"images/blank.gif\" width=20 height=20 onclick='doMove(" + pos + ")'>";
			output = output + "</TD>";
			pos++;
		}
		output = output + "</TR>";
	}
	output = output + "</TD>";
	output = output + "</TR>";
	output = output + "</TABLE>";
	output = output + "<form name='myform'><div id='new'><input value='new game' type=button onclick='resetBoard()'></div>  <div id='teleport' style='VISIBILITY:hidden'><input value='teleport!' type=button onclick='teleport()'></div>  <div id='blast'  style='VISIBILITY:hidden'><input value='blast em!' type=button onclick='blast()'></div><div id='last'  style='VISIBILITY:hidden'><input value='last stand' type=button onclick='lastStand()'></div><br>";
	output = output + "<FONT FACE='Arial'>Score = <b id='score'>0</b> level = <b id='level'>0</b> blasts left = <b id='blasts'>0</b> High = <b id='hscore'>0</b></FONT></form>";
	return output;
}

function resetBoard(){
	putThingsInBoard();
	drawBoard();
}

function drawBoard(){
	var imgName;
	for(var i=0; i<arrSize; i++){
		imgName = "img" + i;
		if(posArray[i]=="&nbsp;")
			document.images[imgName].src = "images/blank.gif";
		if(posArray[i]=="D")
			document.images[imgName].src = "images/dalek.gif";
		if(posArray[i]=="M")
			document.images[imgName].src = "images/DocWho.gif";
		if(posArray[i]=="W")
			document.images[imgName].src = "images/leftup.gif";
		if(posArray[i]=="U")
			document.images[imgName].src = "images/up.gif";
		if(posArray[i]=="X")
			document.images[imgName].src = "images/rightup.gif";
		if(posArray[i]=="L")
			document.images[imgName].src = "images/left.gif";
		if(posArray[i]=="R")
			document.images[imgName].src = "images/right.gif";
		if(posArray[i]=="Y")
			document.images[imgName].src = "images/leftbot.gif";
		if(posArray[i]=="B")
			document.images[imgName].src = "images/bot.gif";
		if(posArray[i]=="Z")
			document.images[imgName].src = "images/rightbot.gif";
		if(posArray[i]=="Q")
			document.images[imgName].src = "images/mound.gif";
		if(posArray[i]=="*")
			document.images[imgName].src = "images/death.gif";
			
	}
	if(score>0){
		document.all.score.innerHTML=score;
		document.all.level.innerHTML=level;
		document.all.blasts.innerHTML=blasts;
		if(score>hscore)
			hscore=score;
		document.all.hscore.innerHTML=hscore;
		if(needInit){
			resetBoard();
		}
	}else{
		if(!needInit){
			document.all.score.innerHTML=score;
			document.all.level.innerHTML=level;
			document.all.blasts.innerHTML=blasts;
			if(score>hscore)
				hscore=score;
			document.all.hscore.innerHTML=hscore;
		}
	}

}

function putThingsInBoard(){

	for(var j=0; j<arrSize; j++){
		posArray[j] = "&nbsp;";
	}
	
	if(needInit) setup();
	for(var i=0; i<dalArray.length; i++){
		posArray[dalArray[i]]="D";
	}
	for(var j=0; j<mndArray.length; j++){
		posArray[mndArray[j]]="Q";
	}
	posArray[manPos]="M";
	if(manPos-arrWidth-1>-1 && manPos%arrWidth!=0){
		if(posArray[manPos-arrWidth-1]=="&nbsp;") posArray[manPos-arrWidth-1]="W";
	}
	if(manPos-arrWidth>-1){
		if(posArray[manPos-arrWidth]=="&nbsp;") posArray[manPos-arrWidth]="U";
	}
	if(manPos-arrWidth+1>-1  && manPos%arrWidth!=arrWidth-1){
		if(posArray[manPos-arrWidth+1]=="&nbsp;") posArray[manPos-arrWidth+1]="X";
	}
	if(manPos-1>-1 && manPos%arrWidth!=0){
		if(posArray[manPos-1]=="&nbsp;") posArray[manPos-1]="L";
	}
	if(manPos+1<(arrSize-1) && manPos%arrWidth!=arrWidth-1){
		if(posArray[manPos+1]=="&nbsp;")posArray[manPos+1]="R";
	}		
	if(manPos+arrWidth-1<(arrSize-1) && manPos%arrWidth!=0){
		if(posArray[manPos+arrWidth-1]=="&nbsp;")posArray[manPos+arrWidth-1]="Y";
	}
	if(manPos+arrWidth<(arrSize-1)){
		if(posArray[manPos+arrWidth]=="&nbsp;") posArray[manPos+arrWidth]="B";
	}		
	if(manPos+arrWidth+1<(arrSize-1) && manPos%arrWidth!=(arrWidth-1)){
		if(posArray[manPos+arrWidth+1]=="&nbsp;") posArray[manPos+arrWidth+1]="Z";
	}
	
	if(dalArray.length==0){
		needInit=true;

	}else{
		if(!needInit && (!arrayDoesntContain(dalArray, manPos) || !arrayDoesntContain(mndArray, manPos))){
			endGame();
		}
	}
}

function arrayDoesntContain( thisArray, thing){
	for(var i=0; i<thisArray.length; i++){
		if(thisArray[i]==thing) return false;
	}
	return true;
}

function setup(){
	var noDaleks;
	var tmpRand;
	var notPutIn;
	
	if(level==null||level==0) {
		level=1;
	}else{
		level++;
	}
	
	mndArray = new Array();
	
	noDaleks = level*addPerRound;
	
	dalArray = new Array(noDaleks);
	for(var i=0; i<noDaleks; i++){
		notPutIn = true;
		while (notPutIn){
			tmpRand = Math.floor(Math.random()*arrSize);
			if(arrayDoesntContain(dalArray, tmpRand)){
				dalArray[i] = tmpRand;
				notPutIn = false;
			}
		}
	}
	notPutIn=true;
	while (notPutIn){
		tmpRand = Math.floor(Math.random()*arrSize);
		if(arrayDoesntContain(dalArray, tmpRand)){
			manPos = tmpRand;
			notPutIn = false;
		}
	}
	blasts++;
	document.all["teleport"].style.visibility="visible";
	document.all["blast"].style.visibility="visible";
	//document.all["last"].style.visibility="visible";
	document.all["new"].style.visibility="hidden";
	needInit=false;
}

function doMove(posClicked){
	if(posClicked!=manPos)
		moveMan(posClicked);
	if(arrayDoesntContain(dalArray, manPos)){
		moveDaleks();
		resetBoard();
	}else{
		endGame();
	}
}

function teleport(){
	var tmpRand;
	var notPutIn;

	notPutIn=true;
	while (notPutIn){
		tmpRand = Math.floor(Math.random()*arrSize);
		if(arrayDoesntContain(dalArray, tmpRand) && arrayDoesntContain(mndArray, tmpRand)){
			manPos = tmpRand;
			notPutIn = false;
		}
	}
	moveDaleks();
	resetBoard();
}

function blast(){
	if(manPos-arrWidth-1>-1 && manPos%arrWidth!=0){
		if(!arrayDoesntContain(dalArray, manPos-arrWidth-1 )){
			dalArray = removedFromArray(dalArray, manPos-arrWidth-1);
			score++;
		}
	}
	if(manPos-arrWidth>-1){
		if(!arrayDoesntContain(dalArray, manPos-arrWidth )){
			dalArray = removedFromArray(dalArray, manPos-arrWidth);
			score++;
		}
	}
	if(manPos-arrWidth+1>-1  && manPos%arrWidth!=arrWidth-1){		
		if(!arrayDoesntContain(dalArray, manPos-arrWidth+1 )){
			dalArray = removedFromArray(dalArray, manPos-arrWidth+1);
			score++;
		}
	}
	if(manPos-1>-1 && manPos%arrWidth!=0){
		if(!arrayDoesntContain(dalArray, manPos-1 )){
			dalArray = removedFromArray(dalArray, manPos-1);
			score++;
		}
	}
	if(manPos+1<(arrSize-1) && manPos%arrWidth!=arrWidth-1){
		if(!arrayDoesntContain(dalArray, manPos+1 )){
			dalArray = removedFromArray(dalArray, manPos+1);
			score++;
		}
	}		
	if(manPos+arrWidth-1<(arrSize-1) && manPos%arrWidth!=0){
		if(!arrayDoesntContain(dalArray, manPos+arrWidth-1 )){
			dalArray = removedFromArray(dalArray, manPos+arrWidth-1);
			score++;
		}
	}
	if(manPos+arrWidth<(arrSize-1)){
		if(!arrayDoesntContain(dalArray, manPos+arrWidth )){
			dalArray = removedFromArray(dalArray, manPos+arrWidth);
			score++;
		}
	}		
	if(manPos+arrWidth+1<(arrSize-1) && manPos%arrWidth!=(arrWidth-1)){
		if(!arrayDoesntContain(dalArray, manPos+arrWidth+1 )){
			dalArray = removedFromArray(dalArray, manPos+arrWidth+1);
			score++;
		}
	}
	blasts--;
	if(blasts==0){
		document.all["blast"].style.visibility="hidden";
	}
	resetBoard();
}

function moveMan(posClicked){
	manPos = moveAToB(manPos, posClicked);
}

function moveDaleks(){
	var tmpArray = new Array();
	var tmpPos;
	
	for(var i=0; i<dalArray.length; i++){
		tmpPos = moveAToB(dalArray[i], manPos);
		
		if(arrayDoesntContain(tmpArray,tmpPos)){
			if(arrayDoesntContain(mndArray,tmpPos)){
				tmpArray[tmpArray.length] = tmpPos;
			}else{
				score++;
			}
		}else{
			mndArray[mndArray.length] = tmpPos;
			tmpArray = removedFromArray(tmpArray, tmpPos);
			score++;
			score++;
		}
	}
	dalArray = tmpArray;
}

function endGame(){
	posArray[manPos]="*";
	level=0;
	document.all.score.innerHTML=score;
	score=0;
	blasts=0;
	needInit=true;
	document.all["teleport"].style.visibility="hidden";
	document.all["blast"].style.visibility="hidden";
	//document.all["last"].style.visibility="hidden";
	document.all["new"].style.visibility="visible";
}

function removedFromArray(thisArray, thing){
	var tmpArray = new Array;
	var cnt=0;
	for(var i=0; i<thisArray.length; i++){
		if(thisArray[i]!=thing){
			tmpArray[cnt] = thisArray[i];
			cnt++;
		}
	}
	return tmpArray;
}

function moveAToB(A, B){
	var xA, yA, xB, yB;
	
	xA = A%arrWidth;
	yA = Math.floor(A/arrWidth);
	xB = B%arrWidth;
	yB = Math.floor(B/arrWidth);
	
	if(xA>xB){
		xA--;
	}else{
		if(xA<xB)
			xA++;
	}
	if(yA>yB){
		yA--;
	}else{
		if(yA<yB)
			yA++;
	}
	return (yA*arrWidth) + xA;
}

function pause(sec){
	var now, then, diff;
	then = new Date();
	now = new Date();
	diff = now.getTime() - then.getTime();
	while(sec*1000>diff){
		now = new Date();
		diff = now.getTime() - then.getTime();
	}
}

function lastStand(){
	var areDaleks = true;
	while(areDaleks){
		moveDaleks();
		if(dalArray.length==0){
			areDaleks=false;
		}else{
			resetBoard();
		}
	}

}