var RED = 2;
var BLACK = 1;
var GREY = 0;

var GENERAL = 7;
var GUARD = 6;
var STAFF = 5;
var HORSE = 4;
var CHARIOT = 3;
var CANNON = 2;
var SOLDIER = 1;
var BLANK = 0;

var gridSize = 40;			//棋盘格子大小

var situation = new Array();

function Control()
{
	this.initChess = initChess;
	this.addBlank = addBlank;
	this.clearBoard = clearBoard;
	this.moveChess = moveChess;

	var chessBoard = new ChessBoard();
	chessBoard.drawBoard();
	this.board = chessBoard.board;
	
	this.selection = null;
	this.end = false;
	this.side = RED;

	for (var i=0; i<9; i++)
	{
		situation[i] = new Array();
	}

	function moveChess(from, to)
	{
		//下面这段，不得已出此下策
		for (var i=0; i<this.board.children.length; i++)
		{
			if ((this.board.children[i].x == situation[to.x][to.y].x)
				&& (this.board.children[i].y == situation[to.x][to.y].y))
			{
				this.board.removeChild(this.board.children[i]);
			}
		}

		situation[to.x][to.y] = from;
		situation[from.x][from.y] = null;
		var x = from.x;
		var y = from.y;
		from.x = to.x;
		from.y = to.y;
		from.style.left = to.style.left;
		from.style.top = to.style.top;
		this.clearBoard();
		this.side = 3 - this.side;
	}

	function clearBoard()
	{
		for (var i=0; i<9; i++)
		{
			for (var j=0; j<10; j++)
			{
				if (situation[i][j] != null)
				{
					if (situation[i][j].color == GREY)
					{
						this.board.removeChild(situation[i][j]);
						situation[i][j] = null;
					}
					else
					{
						situation[i][j].style.border = "none";
						situation[i][j].beAttack = false;
					}
				}
			}
		}
	}

	function addBlank(x, y)
	{
		if (situation[x][y] == null)
		{
			situation[x][y] = new Blank(x, y);
			this.board.appendChild(situation[x][y]);
		}
		else
		{
			situation[x][y].style.border = "1px #ff0000 solid";
			situation[x][y].beAttack = true;
		}
	}

	function initChess()
	{
		var redGeneral = new General(RED);
		this.board.appendChild(redGeneral);
		situation[redGeneral.x][redGeneral.y] = redGeneral;

		var redGuard0 = new Guard(RED, 0);
		this.board.appendChild(redGuard0);
		situation[redGuard0.x][redGuard0.y] = redGuard0;
		
		var redGuard1 = new Guard(RED, 1);
		this.board.appendChild(redGuard1);
		situation[redGuard1.x][redGuard1.y] = redGuard1;
		
		var redStaff0 = new Staff(RED, 0);
		this.board.appendChild(redStaff0);
		situation[redStaff0.x][redStaff0.y] = redStaff0;
		
		var redStaff1 = new Staff(RED, 1);
		this.board.appendChild(redStaff1);
		situation[redStaff1.x][redStaff1.y] = redStaff1;

		var redHorse0 = new Horse(RED, 0);
		this.board.appendChild(redHorse0);
		situation[redHorse0.x][redHorse0.y] = redHorse0;
		
		var redHorse1 = new Horse(RED, 1);
		this.board.appendChild(redHorse1);
		situation[redHorse1.x][redHorse1.y] = redHorse1;

		var redChariot0 = new Chariot(RED, 0);
		this.board.appendChild(redChariot0);
		situation[redChariot0.x][redChariot0.y] = redChariot0;
		
		var redChariot1 = new Chariot(RED, 1);
		this.board.appendChild(redChariot1);
		situation[redChariot1.x][redChariot1.y] = redChariot1;

		var redCannon0 = new Cannon(RED, 0);
		this.board.appendChild(redCannon0);
		situation[redCannon0.x][redCannon0.y] = redCannon0;
		
		var redCannon1 = new Cannon(RED, 1);
		this.board.appendChild(redCannon1);
		situation[redCannon1.x][redCannon1.y] = redCannon1;
		
		var redSoldier0 = new Soldier(RED, 0);
		this.board.appendChild(redSoldier0);
		situation[redSoldier0.x][redSoldier0.y] = redSoldier0;
		
		var redSoldier1 = new Soldier(RED, 1);
		this.board.appendChild(redSoldier1);
		situation[redSoldier1.x][redSoldier1.y] = redSoldier1;
		
		var redSoldier2 = new Soldier(RED, 2);
		this.board.appendChild(redSoldier2);
		situation[redSoldier2.x][redSoldier2.y] = redSoldier2;
		
		var redSoldier3 = new Soldier(RED, 3);
		this.board.appendChild(redSoldier3);
		situation[redSoldier3.x][redSoldier3.y] = redSoldier3;
		
		var redSoldier4 = new Soldier(RED, 4);
		this.board.appendChild(redSoldier4);
		situation[redSoldier4.x][redSoldier4.y] = redSoldier4;
		
		var blackGeneral = new General(BLACK);
		this.board.appendChild(blackGeneral);
		situation[blackGeneral.x][blackGeneral.y] = blackGeneral;

		var blackGuard0 = new Guard(BLACK, 0);
		this.board.appendChild(blackGuard0);
		situation[blackGuard0.x][blackGuard0.y] = blackGuard0;
		
		var blackGuard1 = new Guard(BLACK, 1);
		this.board.appendChild(blackGuard1);
		situation[blackGuard1.x][blackGuard1.y] = blackGuard1;
		
		var blackStaff0 = new Staff(BLACK, 0);
		this.board.appendChild(blackStaff0);
		situation[blackStaff0.x][blackStaff0.y] = blackStaff0;
		
		var blackStaff1 = new Staff(BLACK, 1);
		this.board.appendChild(blackStaff1);
		situation[blackStaff1.x][blackStaff1.y] = blackStaff1;

		var blackHorse0 = new Horse(BLACK, 0);
		this.board.appendChild(blackHorse0);
		situation[blackHorse0.x][blackHorse0.y] = blackHorse0;
		
		var blackHorse1 = new Horse(BLACK, 1);
		this.board.appendChild(blackHorse1);
		situation[blackHorse1.x][blackHorse1.y] = blackHorse1;

		var blackChariot0 = new Chariot(BLACK, 0);
		this.board.appendChild(blackChariot0);
		situation[blackChariot0.x][blackChariot0.y] = blackChariot0;
		
		var blackChariot1 = new Chariot(BLACK, 1);
		this.board.appendChild(blackChariot1);
		situation[blackChariot1.x][blackChariot1.y] = blackChariot1;

		var blackCannon0 = new Cannon(BLACK, 0);
		this.board.appendChild(blackCannon0);
		situation[blackCannon0.x][blackCannon0.y] = blackCannon0;
		
		var blackCannon1 = new Cannon(BLACK, 1);
		this.board.appendChild(blackCannon1);
		situation[blackCannon1.x][blackCannon1.y] = blackCannon1;
		
		var blackSoldier0 = new Soldier(BLACK, 0);
		this.board.appendChild(blackSoldier0);
		situation[blackSoldier0.x][blackSoldier0.y] = blackSoldier0;
		
		var blackSoldier1 = new Soldier(BLACK, 1);
		this.board.appendChild(blackSoldier1);
		situation[blackSoldier1.x][blackSoldier1.y] = blackSoldier1;
		
		var blackSoldier2 = new Soldier(BLACK, 2);
		this.board.appendChild(blackSoldier2);
		situation[blackSoldier2.x][blackSoldier2.y] = blackSoldier2;
		
		var blackSoldier3 = new Soldier(BLACK, 3);
		this.board.appendChild(blackSoldier3);
		situation[blackSoldier3.x][blackSoldier3.y] = blackSoldier3;
		
		var blackSoldier4 = new Soldier(BLACK, 4);
		this.board.appendChild(blackSoldier4);
		situation[blackSoldier4.x][blackSoldier4.y] = blackSoldier4;
	}
}

function ChessBoard()
{
	this.drawBoard = drawBoard;

	//创建棋盘所在的层
	var boardBase = document.createElement("<div class='chessBoard' id='boardLayer'>");
	document.body.appendChild(boardBase); 
	this.board = boardBase;

	function drawBoard()
	{
		this.width = 8 * gridSize;
		this.height = 9 * gridSize;

		//绘制棋盘边框
		var boardRect = document.createElement("<v:Rect strokeweight='2pt' style='position:absolute;width:"+this.width+"px;height:"+this.height+"px'/>");
		boardBase.appendChild(boardRect);

		//绘制水平线
		for (var i=0; i<10; i++)
		{
			var hLine = document.createElement("<v:line from='0,"+gridSize*i+"' to='"+this.width+","+gridSize*i+">");
			boardBase.appendChild(hLine);
		}
		
		//绘制竖直线
		for (var i=0; i<9; i++)
		{
			var vLine = document.createElement("<v:line from='"+gridSize*i+",0' to='"+gridSize*i+","+gridSize*4+">");
			boardBase.appendChild(vLine);
		}
		
		for (var i=0; i<9; i++)
		{
			var vLine = document.createElement("<v:line from='"+gridSize*i+","+gridSize*5+"' to='"+gridSize*i+","+this.height+">");
			boardBase.appendChild(vLine);
		}

		//绘制斜线
		var upNW2SELine = document.createElement("<v:line from='"+3*gridSize+",0' to='"+5*gridSize+","+2*gridSize+"'>");
		boardBase.appendChild(upNW2SELine);
		
		var upNE2SWLine = document.createElement("<v:line from='"+5*gridSize+",0' to='"+3*gridSize+","+2*gridSize+"'>");
		boardBase.appendChild(upNE2SWLine);
		
		var downNW2SELine = document.createElement("<v:line from='"+3*gridSize+","+7*gridSize+"' to='"+5*gridSize+","+this.height+"'>");
		boardBase.appendChild(downNW2SELine);
		
		var downNE2SWLine = document.createElement("<v:line from='"+5*gridSize+","+7*gridSize+"' to='"+3*gridSize+","+this.height+"'>");
		boardBase.appendChild(downNE2SWLine);

		//绘制炮位
		for (var i=0; i<2; i++)
		{
			for (var j=0; j<2; j++)
			{
				drawStar(1+i*6, 2+j*5);
			}
		}
		
		//绘制兵位
		for (var i=0; i<5; i++)
		{
			for (var j=0; j<2; j++)
			{
				drawStar(i*2, 3+j*3);
			}
		}
	}

	//绘制兵位、炮位的方法
	function drawStar(x, y)
	{
		var centerX = x * gridSize;
		var centerY = y * gridSize;

		var distance = gridSize / 10;
		var length = gridSize / 4;

		//靠左边的位置不画左边一半
		if (x != 0)
		{
			var startX = centerX - distance;
			var startY = centerY - distance - length;
			var endX = centerX - distance - length;
			var endY = centerY - distance;

			var vLine = document.createElement("<v:line from='"+startX+","+startY+"' to='"+startX+","+endY+"'>");
			boardBase.appendChild(vLine);
			var hLine = document.createElement("<v:line from='"+startX+","+endY+"' to='"+endX+","+endY+"'>");
			boardBase.appendChild(hLine);
			
			startX = centerX - distance;
			startY = centerY + distance + length;
			endX = centerX - distance - length;
			endY = centerY + distance;

			vLine = document.createElement("<v:line from='"+startX+","+startY+"' to='"+startX+","+endY+"'>");
			boardBase.appendChild(vLine);
			hLine = document.createElement("<v:line from='"+startX+","+endY+"' to='"+endX+","+endY+"'>");
			boardBase.appendChild(hLine);
		}

		//靠右边的位置不画右边一半
		if (x != 8)
		{
			var startX = centerX + distance;
			var startY = centerY - distance - length;
			var endX = centerX + distance + length;
			var endY = centerY - distance;

			var vLine = document.createElement("<v:line from='"+startX+","+startY+"' to='"+startX+","+endY+"'>");
			boardBase.appendChild(vLine);
			var hLine = document.createElement("<v:line from='"+startX+","+endY+"' to='"+endX+","+endY+"'>");
			boardBase.appendChild(hLine);
			
			startX = centerX + distance;
			startY = centerY + distance + length;
			endX = centerX + distance + length;
			endY = centerY + distance;

			vLine = document.createElement("<v:line from='"+startX+","+startY+"' to='"+startX+","+endY+"'>");
			boardBase.appendChild(vLine);
			hLine = document.createElement("<v:line from='"+startX+","+endY+"' to='"+endX+","+endY+"'>");
			boardBase.appendChild(hLine);
		}
	}
}

function ChessMan(color, kind, number)
{
	this.color = color;
	this.kind = kind;
	this.number = number;
	this.beAttack = false;

	this.fileName = "./res/";

	switch (this.color)
	{
		case RED:
		{
			this.fileName += "red";
			break;
		}
		case BLACK:
		{
			this.fileName += "black";
			break;
		}
	}

	switch (this.kind)
	{
		case GENERAL:
		{
			this.fileName += "general";
			this.x = 4;
			this.y = 9 * (this.color - 1);
			break;
		}
		case GUARD:
		{
			this.fileName += "guard";
			this.x = 3 + 2 * this.number;
			this.y = 9 * (this.color - 1);
			break;
		}
		case STAFF:
		{
			this.fileName += "staff";
			this.x = 2 + 4 * this.number;
			this.y = 9 * (this.color - 1);
			break;
		}
		case HORSE:
		{
			this.fileName += "horse";
			this.x = 1 + 6 * this.number;
			this.y = 9 * (this.color - 1);
			break;
		}
		case CHARIOT:
		{
			this.fileName += "chariot";
			this.x = 8 * this.number;
			this.y = 9 * (this.color - 1);
			break;
		}
		case CANNON:
		{
			this.fileName += "cannon";
			this.x = 1 + 6 * this.number;
			this.y = 2 + 5 * (this.color - 1);
			break;
		}
		case SOLDIER:
		{
			this.fileName += "soldier";
			this.x = 2 * this.number;
			this.y = 3 * this.color;
			break;
		}
		case BLANK:
		{
			this.fileName += "blank";
			this.x = 0;
			this.y = 0;
			break;
		}
	}

	this.fileName += ".ico";
	
	var chessMan = document.createElement("<div class='chessMan'>");
	var imgsrc = document.createElement("<img src='"+this.fileName+"'>");

	chessMan.style.top = this.y * gridSize - 16;
	chessMan.style.left = this.x * gridSize - 16;
	chessMan.appendChild(imgsrc);

	chessMan.color = this.color;
	chessMan.kind = this.kind;
	chessMan.x = this.x;
	chessMan.y = this.y;

	return chessMan;
}

function General(color)
{
	var general = new ChessMan(color, GENERAL, 0);
	general.canGo = canGo;

	function isNormal(posX, posY)
	{
		if (general.color == BLACK)
		{
			if ((posX<3) || (posX>5) || (posY>2))
			{
				return false;
			}
		}
		if (general.color == RED)
		{
			if ((posX<3) || (posX>5) || (posY<7))
			{
				return false;
			}
		}

		return true;
	}

	function canGo(toX, toY)
	{
		if (isNormal(toX, toY)
			&& ((situation[toX][toY] == null) 
			|| ((situation[toX][toY] != null)
			&&(situation[toX][toY].color != this.color))))
		{
			if (Math.abs(general.y-toY)+Math.abs(toX-general.x) != 1)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		return false;
	}

	return general;
}

function Guard(color, number)
{
	var guard = new ChessMan(color, GUARD, number);
	guard.canGo = canGo;

	function isNormal(posX, posY)
	{
		if (guard.color == RED)
		{
			if (((posX==3) && (posY==7))
				|| ((posX==3) && (posY==9))
				|| ((posX==5) && (posY==7))
				|| ((posX==5) && (posY==9))
				|| ((posX==4) && (posY==8)))
			{
				return true;
			}
		}

		if (guard.color == BLACK)
		{
			if (((posX==3) && (posY==0))
				|| ((posX==3) && (posY==2))
				|| ((posX==5) && (posY==0))
				|| ((posX==5) && (posY==2))
				|| ((posX==4) && (posY==1)))
			{
				return true;
			}
		}

		return false;
	}

	function canGo(toX, toY)
	{
		if (isNormal(toX, toY)
			&& ((situation[toX][toY] == null) 
			|| ((situation[toX][toY] != null)
			&&(situation[toX][toY].color != this.color))))
		{
			if ((Math.abs(guard.x-toX) > 1)
				||(Math.abs(guard.y-toY) > 1)
				||((Math.abs(guard.x-toX) == 0)
				&&(Math.abs(guard.y-toY) == 0)))
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		return false;
	}

	return guard;
}

function Staff(color, number)
{
	var staff = new ChessMan(color, STAFF, number);
	staff.canGo = canGo;

	function isNormal(posX, posY)
	{
		if (staff.color == BLACK)
		{
			if (((posX==0) && (posY==2))
				|| ((posX==2) && (posY==0))
				|| ((posX==2) && (posY==4))
				|| ((posX==4) && (posY==2))
				|| ((posX==6) && (posY==0))
				|| ((posX==6) && (posY==4))
				|| ((posX==8) && (posY==2)))
			{
				return true;
			}
		}
		
		if (staff.color == RED)
		{
			if (((posX==0) && (posY==7))
				|| ((posX==2) && (posY==5))
				|| ((posX==2) && (posY==9))
				|| ((posX==4) && (posY==7))
				|| ((posX==6) && (posY==5))
				|| ((posX==6) && (posY==9))
				|| ((posX==8) && (posY==7)))
			{
				return true;
			}
		}

		return false;
	}

	function canGo(toX, toY)
	{
		if (isNormal(toX, toY)
			&& ((situation[toX][toY] == null) 
			|| ((situation[toX][toY] != null)
			&&(situation[toX][toY].color != this.color))))
		{
			if ((Math.abs(staff.x-toX) != 2)
				||(Math.abs(staff.y-toY) != 2))
			{
				return false;
			}
			else
			{
				var i = (staff.x + toX) / 2;
				var j = (staff.y + toY) / 2;
				if (situation[i][j] == null)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
		return false;
	}

	return staff;
}

function Horse(color, number)
{
	var horse = new ChessMan(color, HORSE, number);
	horse.canGo = canGo;

	function isNormal(posX, posY)
	{
		return true;
	}

	function canGo(toX, toY)
	{
		if (isNormal(toX, toY)
			&& ((situation[toX][toY] == null) 
			|| ((situation[toX][toY] != null)
			&&(situation[toX][toY].color != this.color))))
		{
			if (((Math.abs(horse.x-toX) == 1)
				&&(Math.abs(horse.y-toY) == 2))
				||((Math.abs(horse.x-toX) == 2)
				&&(Math.abs(horse.y-toY) == 1)))
			{
				var i = -1;
				var j = -1;
				if (toX-horse.x == 2)
				{
					i = horse.x + 1;
					j = horse.y;
				}
				else if (horse.x-toX == 2)
				{
					i = horse.x - 1;
					j = horse.y;
				}
				else if (toY-horse.y == 2)
				{
					i = horse.x;
					j = horse.y + 1;
				}
				else if (horse.y-toY == 2)
				{
					i = horse.x;
					j = horse.y - 1;
				}

				if (situation[i][j] == null)
				{
					return true;
				}
			}
		}

		return false;
	}

	return horse;
}

function Chariot(color, number)
{
	var chariot = new ChessMan(color, CHARIOT, number);
	chariot.canGo = canGo;

	function isNormal(posX, posY)
	{
		return true;
	}

	function canGo(toX, toY)
	{
		if (isNormal(toX, toY)
			&& ((situation[toX][toY] == null) 
			|| ((situation[toX][toY] != null)
			&&(situation[toX][toY].color != this.color))))
		{
			if ((chariot.x!=toX) && (chariot.y!=toY))
			{
				return false;
			}
			else
			{
				if (chariot.y == toY)
				{
					if (chariot.x < toX)
					{
						for (var x=chariot.x+1; x<toX; x++)
						{
							if ((situation[x][chariot.y] != null)
								&& (situation[x][chariot.y].color != GREY))
							{
								return false;
							}
						}
						return true;
					}

					if (chariot.x > toX)
					{
						for (var x=chariot.x-1; x>toX; x--)
						{
							if ((situation[x][chariot.y] != null)
								&& (situation[x][chariot.y].color != GREY))
							{
								return false;
							}
						}
						return true;
					}
				}
				else
				{
					if (chariot.y < toY)
					{
						for (var y=chariot.y+1; y<toY; y++)
						{
							if ((situation[chariot.x][y] != null)
								&& (situation[chariot.x][y].color != GREY))
							{
								return false;
							}
						}
						return true;
					}

					if (chariot.y > toY)
					{
						for (var y=chariot.y-1; y>toY; y--)
						{
							if ((situation[chariot.x][y] != null)
								&& (situation[chariot.x][y].color != GREY))
							{
								return false;
							}
						}
						return true;
					}
				}
			}
		}
	}

	return chariot;
}

function Cannon(color, number)
{
	var cannon = new ChessMan(color, CANNON, number);
	cannon.canGo = canGo;

	function isNormal(posX, posY)
	{
		return true;
	}

	function canGo(toX, toY)
	{
		if (isNormal(toX, toY)
			&& ((situation[toX][toY] == null) 
			|| ((situation[toX][toY] != null)
			&&(situation[toX][toY].color != this.color))))
		{
			if ((cannon.x!=toX) && (cannon.y!=toY))
			{
				return false;
			}
			else
			{
				if (situation[toX][toY] == null)
				{
					if (cannon.y == toY)
					{
						if (cannon.x < toX)
						{
							for (var x=cannon.x+1; x<toX; x++)
							{
								if ((situation[x][cannon.y] != null)
									&& (situation[x][cannon.y].color != GREY))
								{
									return false;
								}
							}
							return true;
						}

						if (cannon.x > toX)
						{
							for (var x=cannon.x-1; x>toX; x--)
							{
								if ((situation[x][cannon.y] != null)
									&& (situation[x][cannon.y].color != GREY))
								{
									return false;
								}
							}
							return true;
						}
					}
					else
					{
						if (cannon.y < toY)
						{
							for (var y=cannon.y+1; y<toY; y++)
							{
								if ((situation[cannon.x][y] != null)
									&& (situation[cannon.x][y].color != GREY))
								{
									return false;
								}
							}
							return true;
						}

						if (cannon.y > toY)
						{
							for (var y=cannon.y-1; y>toY; y--)
							{
								if ((situation[cannon.x][y] != null)
									&& (situation[cannon.x][y].color != GREY))
								{
									return false;
								}
							}
							return true;
						}
					}
				}
				else
				{
					var count = 0;
						
					if (cannon.y == toY)
					{
						if (cannon.x < toX)
						{
							for (var x=cannon.x+1; x<toX; x++)
							{
								if ((situation[x][cannon.y] != null)
									&& (situation[x][cannon.y].color != GREY))
								{
									count ++;
								}
							}
							if (count == 1)
							{
								return true;
							}
						}

						if (cannon.x > toX)
						{
							for (var x=cannon.x-1; x>toX; x--)
							{
								if ((situation[x][cannon.y] != null)
									&& (situation[x][cannon.y].color != GREY))
								{
									count ++;
								}
							}
							if (count == 1)
							{
								return true;
							}
						}
					}
					else
					{
						if (cannon.y < toY)
						{
							for (var y=cannon.y+1; y<toY; y++)
							{
								if ((situation[cannon.x][y] != null)
									&& (situation[cannon.x][y].color != GREY))
								{
									count ++;
								}
							}
							if (count == 1)
							{
								return true;
							}
						}

						if (cannon.y > toY)
						{
							for (var y=cannon.y-1; y>toY; y--)
							{
								if ((situation[cannon.x][y] != null)
									&& (situation[cannon.x][y].color != GREY))
								{
									count ++;
								}
							}
							if (count == 1)
							{
								return true;
							}
						}
					}
				}
			}
		}

		return false;
	}

	return cannon;
}

function Soldier(color, number)
{
	var soldier = new ChessMan(color, SOLDIER, number);
	soldier.canGo = canGo;

	function isNormal(posX, posY)
	{
		if (soldier.color == BLACK)
		{
			if ((posY < 3)
				|| ((posY < 5) && (posX % 2 == 1)))
			{
				return false;
			}
		}
		
		if (soldier.color == RED)
		{
			if ((posY > 6)
				|| ((posY > 4) && (posX % 2 == 1)))
			{
				return false;
			}
		}

		return true;
	}

	function canGo(toX, toY)
	{
		if (isNormal(toX, toY)
			&& ((situation[toX][toY] == null) 
			|| ((situation[toX][toY] != null)
			&&(situation[toX][toY].color != this.color))))
		{
			if (soldier.color == BLACK)
			{
				if (toY < soldier.y)
				{
					return false;
				}
				else
				{
					if (Math.abs(toX-soldier.x) + toY-soldier.y != 1)
					{
						return false;
					}
				}
			}
			if (soldier.color == RED)
			{
				if (toY > soldier.y)
				{
					return false;
				}
				else
				{
					if (Math.abs(toX-soldier.x) + soldier.y-toY != 1)
					{
						return false;
					}
				}
			}
			return true;
		}
		return false;
	}

	return soldier;
}

function Blank(x, y)
{
	var blank = new ChessMan(0, BLANK, 0);
	blank.x = x;
	blank.y = y;
	blank.color = GREY;
	blank.style.top = 40 * blank.y - 16;
	blank.style.left = 40 * blank.x - 16;

	return blank;
}