function showMoves(p,q,r) {
	if (cell[p][q][r].piece=="WhiteKing") {
	showKingMoves(p,q,r,"White");
	}
	if (cell[p][q][r].piece=="BlackKing") {
	showKingMoves(p,q,r,"Black");
	}
	if (cell[p][q][r].piece=="WhiteQueen") {
	showBishopMoves(p,q,r,"White",4);
	showRookMoves(p,q,r,"White",4);
	}
	if (cell[p][q][r].piece=="BlackQueen") {
	showBishopMoves(p,q,r,"Black",4);
	showRookMoves(p,q,r,"Black",4);
	}
	if (cell[p][q][r].piece=="WhiteKnight") {
	showKnightMoves(p,q,r,"White");
	}
	if (cell[p][q][r].piece=="BlackKnight") {
	showKnightMoves(p,q,r,"Black");
	}
	if (cell[p][q][r].piece=="WhiteUnicorn") {
	showUnicornMoves(p,q,r,"White");
	}
	if (cell[p][q][r].piece=="BlackUnicorn") {
	showUnicornMoves(p,q,r,"Black");
	}
	if (cell[p][q][r].piece=="WhiteRook") {
	showRookMoves(p,q,r,"White",4);
	}
	if (cell[p][q][r].piece=="BlackRook") {
	showRookMoves(p,q,r,"Black",4);
	}
	if (cell[p][q][r].piece=="WhiteBishop") {
	showBishopMoves(p,q,r,"White",4);
	}
	if (cell[p][q][r].piece=="BlackBishop") {
	showBishopMoves(p,q,r,"Black",4);
	}
	if (cell[p][q][r].piece=="WhitePawn") {
	showPawnMoves(p,q,r,"White",1);
	}
	if (cell[p][q][r].piece=="BlackPawn") {
	showPawnMoves(p,q,r,"Black",-1);
	}
	setTimeout("initCellPositions()",3000);
	}
	
function showPawnMoves(p,q,r,self,increment) {

	if ( // capture to the right
	q<=4 && ((r<=4 && increment==1) || (r>=2 && increment==-1))
	&& (cell[p][parseInt(q)+1][parseInt(r)+increment].piece.indexOf(self)==-1) // Pawn does not capture own piece
	&& (cell[p][parseInt(q)+1][parseInt(r)+increment].piece.indexOf("Empty")==-1) // Pawn does not capture own piece
	) {
//	setBackgroundColor(cell[p][parseInt(q)+1][parseInt(r)+increment],"#FF0000");
	u = p;
	v = parseInt(q)+1;
	w = parseInt(r)+increment;
	showLayer('red'+u+v+w);
	}

	if ( // capture to the left
	q>=2 && ((r<=4 && increment==1) || (r>=2 && increment==-1))
	&& (cell[p][parseInt(q)-1][parseInt(r)+increment].piece.indexOf(self)==-1) // Pawn does not capture own piece
	&& (cell[p][parseInt(q)-1][parseInt(r)+increment].piece.indexOf("Empty")==-1) // Pawn does not capture own piece
	) {
//	setBackgroundColor(cell[p][parseInt(q)-1][parseInt(r)+increment],"#FF0000");
	u = p;
	v = parseInt(q)-1;
	w = parseInt(r)+increment;
	showLayer('red'+u+v+w);
	}

	if ( // capture to the right & down
	q<=4 && ((p<=4 && increment==1) || (p>=2 && increment==-1))
	&& (cell[parseInt(p)+increment][parseInt(q)+1][r].piece.indexOf(self)==-1) // Pawn does not capture own piece
	&& (cell[parseInt(p)+increment][parseInt(q)+1][r].piece.indexOf("Empty")==-1) // Pawn does not capture own piece
	) {
//	setBackgroundColor(cell[parseInt(p)+increment][parseInt(q)+1][r],"#FF0000");
	u = parseInt(p)+increment;
	v = parseInt(q)+1;
	w = r;
	showLayer('red'+u+v+w);
	}

	if ( // capture to the left & down
	q>=2 && ((p<=4 && increment==1) || (p>=2 && increment==-1))
	&& (cell[parseInt(p)+increment][parseInt(q)-1][r].piece.indexOf(self)==-1) // Pawn does not capture own piece
	&& (cell[parseInt(p)+increment][parseInt(q)-1][r].piece.indexOf("Empty")==-1) // Pawn does not capture own piece
	) {
//	setBackgroundColor(cell[parseInt(p)+increment][parseInt(q)-1][r],"#FF0000");
	u = parseInt(p)+increment;
	v = parseInt(q)-1;
	w = r;
	showLayer('red'+u+v+w);
	}

	if ( // capture ahead & down
	((p<=4 && increment==1) || (p>=2 && increment==-1)) && ((r<=4 && increment==1) || (r>=2 && increment==-1))
	&& (cell[parseInt(p)+increment][q][parseInt(r)+increment].piece.indexOf(self)==-1) // Pawn does not capture own piece
	&& (cell[parseInt(p)+increment][q][parseInt(r)+increment].piece.indexOf("Empty")==-1) // Pawn does not capture own piece
	) {
//	setBackgroundColor(cell[parseInt(p)+increment][q][parseInt(r)+increment],"#FF0000");
	u = parseInt(p)+increment;
	v = q;
	w = parseInt(r)+increment;
	showLayer('red'+u+v+w);
	}

	if ( // move ahead
	((r<=4 && increment==1) || (r>=2 && increment==-1))
	&& (cell[p][q][parseInt(r)+increment].piece.indexOf("Empty")!=-1) // Pawn does not move onto occupied square
	) {
//	setBackgroundColor(cell[p][q][parseInt(r)+increment],"#FF0000");
	u = p;
	v = q;
	w = parseInt(r)+increment;
	showLayer('red'+u+v+w);
	}

	if ( // move down
	((p<=4 && increment==1) || (p>=2 && increment==-1))
	&& (cell[parseInt(p)+increment][q][r].piece.indexOf("Empty")!=-1) // Pawn does not move onto occupied square
	) {
//	setBackgroundColor(cell[parseInt(p)+increment][q][r],"#FF0000");
	u = parseInt(p)+increment;
	v = q;
	w = r;
	showLayer('red'+u+v+w);
	}

	}
	
function showUnicornMoves(p,q,r,self) {
	for (var h=1; h<=3; h++) {
	for (var i=1; i<=2; i++) {
	for (var j=1; j<=2; j++) {
	for (var k=1; k<=2; k++) {
	if (
	(parseInt(p)+(i+parseInt(i/2)-2)*(parseInt(h/3)+3-h))<=5 &&
	(parseInt(p)+(i+parseInt(i/2)-2)*(parseInt(h/3)+3-h))>0 &&
	(parseInt(q)+(j+parseInt(j/2)-2)*(h-(parseInt(h/3)*2)))<=5 &&
	(parseInt(q)+(j+parseInt(j/2)-2)*(h-(parseInt(h/3)*2)))>0 &&
	(parseInt(r)+(k+parseInt(k/2)-2)*(1+parseInt(h/3)))<=5 &&
	(parseInt(r)+(k+parseInt(k/2)-2)*(1+parseInt(h/3)))>0
	&& (cell[parseInt(p)+(i+parseInt(i/2)-2)*(parseInt(h/3)+3-h)][parseInt(q)+(j+parseInt(j/2)-2)*(h-(parseInt(h/3)*2))][parseInt(r)+(k+parseInt(k/2)-2)*(1+parseInt(h/3))].piece.indexOf(self)==-1) // Unicorn does not capture own piece
	) {
//	setBackgroundColor(cell[parseInt(p)+(i+parseInt(i/2)-2)*(parseInt(h/3)+3-h)][parseInt(q)+(j+parseInt(j/2)-2)*(h-(parseInt(h/3)*2))][parseInt(r)+(k+parseInt(k/2)-2)*(1+parseInt(h/3))],"#FF0000");
	u = parseInt(p)+(i+parseInt(i/2)-2)*(parseInt(h/3)+3-h);
	v = parseInt(q)+(j+parseInt(j/2)-2)*(h-(parseInt(h/3)*2));
	w = parseInt(r)+(k+parseInt(k/2)-2)*(1+parseInt(h/3));
	showLayer('red'+u+v+w);

	}
	}
	}
	}
	}
	}

function showKnightMoves(p,q,r,self) {
	for (var i=1; i<=6; i++) {
	for (var j=1; j<=4; j++) {
	var u = parseInt(p);
	var v = parseInt(q);
	var w = parseInt(r);
	if (i==1) u = parseInt(p)+2
	if (i==2) u = parseInt(p)-2
	if (i==3) v = parseInt(q)+2
	if (i==4) v = parseInt(q)-2
	if (i==5) w = parseInt(r)+2
	if (i==6) w = parseInt(r)-2
	
	if (j==1 && u!=p) v = parseInt(q)+1
	if (j==2 && u!=p) v = parseInt(q)-1
	if (j==3 && u!=p) w = parseInt(r)+1
	if (j==4 && u!=p) w = parseInt(r)-1

	if (j==1 && v!=q && u==p) u = parseInt(p)+1
	if (j==2 && v!=q && u==p) u = parseInt(p)-1
	if (j==3 && v!=q && u==p) w = parseInt(r)+1
	if (j==4 && v!=q && u==p) w = parseInt(r)-1
	
	if (j==1 && w!=r && u==p && v==q) u = parseInt(p)+1
	if (j==2 && w!=r && u==p && v==q) u = parseInt(p)-1
	if (j==3 && w!=r && u==p && v==q) v = parseInt(q)+1
	if (j==4 && w!=r && u==p && v==q) v = parseInt(q)-1
	
	if (
		(u<=5 && u>0 && v<=5 && v>0 && w<=5 && w>0) // Knight remains on board
		&& (cell[u][v][w].piece.indexOf(self)==-1) // Knight does not capture own piece
	) {
		showLayer('red'+u+v+w);
		}
	}}}

function showBishop2Moves(p,q,r,self,total) {
	for (var h=1; h<=total; h++) {
	for (var i=1; i<=6; i++) {
	for (var j=1; j<=4; j++) {
	var u = parseInt(p);
	var v = parseInt(q);
	var w = parseInt(r);
	if (i==1) u = parseInt(p)+h
	if (i==2) u = parseInt(p)-h
	if (i==3) v = parseInt(q)+h
	if (i==4) v = parseInt(q)-h
	if (i==5) w = parseInt(r)+h
	if (i==6) w = parseInt(r)-h
	
	if (j==1 && u!=p) v = parseInt(q)+h
	if (j==2 && u!=p) v = parseInt(q)-h
	if (j==3 && u!=p) w = parseInt(r)+h
	if (j==4 && u!=p) w = parseInt(r)-h

	if (j==1 && v!=q && u==p) u = parseInt(p)+h
	if (j==2 && v!=q && u==p) u = parseInt(p)-h
	if (j==3 && v!=q && u==p) w = parseInt(r)+h
	if (j==4 && v!=q && u==p) w = parseInt(r)-h
	
	if (j==1 && w!=r && u==p && v==q) u = parseInt(p)+h
	if (j==2 && w!=r && u==p && v==q) u = parseInt(p)-h
	if (j==3 && w!=r && u==p && v==q) v = parseInt(q)+h
	if (j==4 && w!=r && u==p && v==q) v = parseInt(q)-h
	
	if (
	(u<=5 && u>0 && v<=5 && v>0 && w<=5 && w>0)
	&& (cell[u][v][w].piece.indexOf(self)==-1) // Bishop does not capture own piece
	) showLayer('red'+u+v+w);
	}}}}

function showRookMoves(p,q,r,self,total) {
	rook1=true;rook2=true;rook3=true;rook4=true;rook5=true;rook6=true;
	for (var h=1; h<=total; h++) {
	for (var i=1; i<=6; i++) { // six directions of movement
	if (eval("rook"+i)) {
	var u = parseInt(p);
	var v = parseInt(q);
	var w = parseInt(r);
	if (i==1) u = parseInt(p)+h
	if (i==2) u = parseInt(p)-h
	if (i==3) v = parseInt(q)+h
	if (i==4) v = parseInt(q)-h
	if (i==5) w = parseInt(r)+h
	if (i==6) w = parseInt(r)-h
		
	if (
	(u<=5 && u>0 && v<=5 && v>0 && w<=5 && w>0)
	&& (cell[u][v][w].piece.indexOf(self)==-1) // Rook does not capture own piece
	&& (true)
	) {
	showLayer('red'+u+v+w);
	if (cell[u][v][w].piece!="Empty") eval("rook"+i+"=false");
	} else {eval("rook"+i+"=false");}
	}
	}}}

function showBishopMoves(p,q,r,self,total) {
	bishop1=true;bishop2=true;bishop3=true;bishop4=true;
	bishop5=true;bishop6=true;bishop7=true;bishop8=true;
	bishop9=true;bishop10=true;bishop11=true;bishop12=true;
	for (var h=1; h<=total; h++) {
	for (var i=1; i<=12; i++) { // twelve directions of movement
	if (eval("bishop"+i)) {
	var u = parseInt(p);
	var v = parseInt(q);
	var w = parseInt(r);
	if (i==1) {v=parseInt(q)+h; w=parseInt(r)+h;}
	if (i==2) {v=parseInt(q)-h; w=parseInt(r)+h;}
	if (i==3) {v=parseInt(q)+h; w=parseInt(r)-h;}
	if (i==4) {v=parseInt(q)-h; w=parseInt(r)-h;}
	if (i==5) {w=parseInt(r)+h; u=parseInt(p)+h;}
	if (i==6) {w=parseInt(r)-h; u=parseInt(p)+h;}
	if (i==7) {w=parseInt(r)+h; u=parseInt(p)-h;}
	if (i==8) {w=parseInt(r)-h; u=parseInt(p)-h;}
	if (i==9) {u=parseInt(p)+h; v=parseInt(q)+h;}
	if (i==10) {u=parseInt(p)-h; v=parseInt(q)+h;}
	if (i==11) {u=parseInt(p)+h; v=parseInt(q)-h;}
 	if (i==12) {u=parseInt(p)-h; v=parseInt(q)-h;}
		
	if (
	(u<=5 && u>0 && v<=5 && v>0 && w<=5 && w>0)
	&& (cell[u][v][w].piece.indexOf(self)==-1) // Bishop does not capture own piece
	&& (true)
	) {
	showLayer('red'+u+v+w);
	if (cell[u][v][w].piece!="Empty") eval("bishop"+i+"=false");
	} else {eval("bishop"+i+"=false");}
	}
	}}}

function showKingMoves(p,q,r,self) {
	for (var i=1; i<=2; i++) {
	for (var j=1; j<=2; j++) {
	for (var k=1; k<=2; k++) {
	var u = parseInt(p);
	var v = parseInt(q);
	var w = parseInt(r);
	if (i==1) u = parseInt(p)+1
	if (i==2) u = parseInt(p)-1
	if (j==1) v = parseInt(q)+1
	if (j==2) v = parseInt(q)-1
	if (k==1) w = parseInt(r)+1
	if (k==2) w = parseInt(r)-1
	
	if (
	(u<=5 && u>0 && v<=5 && v>0 && w<=5 && w>0)
	&& (cell[u][v][w].piece.indexOf(self)==-1) // King does not capture own piece
	) showLayer('red'+u+v+w);
	}}}}

