
var _cellsize = 40;
var _promotion = '';
var clickable = 0; 

var _pgn = new Array();

function prev()
{
	_halfMoveNumber --;
	if(_halfMoveNumber < 0) _halfMoveNumber = 0;
	_fen = ignToFen();
	_position = fenToPosition(_fen);
	copyPosition(_position, _positionOrg);
	draw();
}

function first()
{
	_halfMoveNumber = 0;
	_lastmove = '';
	_fen = ignToFen();
	_position = fenToPosition(_fen);
	copyPosition(_position, _positionOrg);
	draw();
}

function last()
{
	_halfMoveNumber =99999;
	_fen = ignToFen();
	_position = fenToPosition(_fen);
	copyPosition(_position, _positionOrg);
	draw();
}

function next()
{
	_halfMoveNumber ++;
//	if(_halfMoveNumber < 0) _halfMoveNumber = 0;
	_fen = ignToFen();
	_position = fenToPosition(_fen);
	copyPosition(_position, _positionOrg);
	draw();
}

function ignToPgn()
{
	var movesArray = _ign.split(' ');
	var position = new Array(64);
	position[0] = 'R';
	position[1] = 'N';
	position[2] = 'B';
	position[3] = 'Q';
	position[4] = 'K';
	position[5] = 'B';
	position[6] = 'N';
	position[7] = 'R';

	for(i=8; i<16; i++) position[i] = 'P';
	for(i=16; i<48; i++) position[i] = '';
	for(i=48; i<56; i++) position[i] = 'p';

	position[56] = 'r';
	position[57] = 'n';
	position[58] = 'b';
	position[59] = 'q';
	position[60] = 'k';
	position[61] = 'b';
	position[62] = 'n';
	position[63] = 'r';

	var piece = '';
	var curmove = '';
	var j = 0;
	for(i = 0; i <movesArray.length; i++)
	{
		var move = movesArray[i];
		if(move.length < 4) continue;
		start = move.substr(0,2);
		end = move.substr(2, 2);

/*		istart = moveToCell(start);
		iend = moveToCell(end);

		if(position[istart] == 'K' || position[istart] == 'k') piece = 'K';
		else if(position[istart] == 'Q' || position[istart] == 'q') piece = 'Q';
		else if(position[istart] == 'B' || position[istart] == 'b') piece = 'B';
		else if(position[istart] == 'N' || position[istart] == 'n') piece = 'N';
		else if(position[istart] == 'R' || position[istart] == 'r') piece = 'R';
		else piece = '';

		if(position[istart] == 'P' && position[iend] == '' && (iend - istart == 7 || iend - istart == 9) && iend == enpassant) position[enpassant-8] = '';
		if(position[istart] == 'p' && position[iend] == '' && (iend - istart == -7 || iend - istart == -9) && iend == enpassant) position[enpassant+8] = '';

		position[iend] = position[istart];
		position[istart] = '';

		if(move.length == 5) position[iend] = move.substr(4,1);

// castling
		if(istart == 4 && iend == 6 && position[iend] == 'K') {position[5] = 'R'; position[7] = '';}
		else if(istart == 4 && iend == 2 && position[iend] == 'K') {position[3] = 'R'; position[0] = '';}
		else if(istart == 60 && iend == 62 && position[iend] == 'k') {position[61] = 'r'; position[63] = '';}
		else if(istart == 60 && iend == 58 && position[iend] == 'k') {position[59] = 'r'; position[56] = '';}
*/

		_pgn[j] = move;
		j++;
	}
//	alert(_pgn[0]);
}

function ignToFen()
{
//	alert("dans ignToFen");
//	return('');

	var wCastleLong = true;
	var wCastleShort = true;
	var bCastleLong = true;
	var bCastleShort = true;

//	alert("test");

	var position = new Array(64);
	position[0] = 'R';
	position[1] = 'N';
	position[2] = 'B';
	position[3] = 'Q';
	position[4] = 'K';
	position[5] = 'B';
	position[6] = 'N';
	position[7] = 'R';

	for(i=8; i<16; i++) position[i] = 'P';
	for(i=16; i<48; i++) position[i] = '';
	for(i=48; i<56; i++) position[i] = 'p';

	position[56] = 'r';
	position[57] = 'n';
	position[58] = 'b';
	position[59] = 'q';
	position[60] = 'k';
	position[61] = 'b';
	position[62] = 'n';
	position[63] = 'r';

	var movesArray = _ign.split(' ');
//	alert(movesArray.length);

	var activeColor = 1;
	var enpassant = -1;
	for(i = 0; i <= _halfMoveNumber; i++)
	{
		if(i >= movesArray.length) 
		{
			_halfMoveNumber = i-1;
//			alert(_halfMoveNumber);
			break;
		}

//		alert("i "+movesArray[i]);
		var move = movesArray[i];
		if(move.length < 4) continue;
		start = move.substr(0,2);
		end = move.substr(2, 2);

		istart = moveToCell(start);
		iend = moveToCell(end);

		_lastmove = istart+'-'+iend;

		if(position[istart] == 'K') {wCastleLong = false; wCastleShort = false;}
		if(position[istart] == 'k') {bCastleLong = false; bCastleShort = false;}
		if(istart == 0) wCastleLong = false;
		else if(istart == 7) wCastleShort = false;
		else if(istart == 56) bCastleLong = false;
		else if(istart == 63) bCastleShort = false;

		if(iend == 0) wCastleLong = false;
		else if(iend == 7) wCastleShort = false;
		else if(iend == 56) bCastleLong = false;
		else if(iend == 63) bCastleShort = false;

		if(position[istart] == 'P' && position[iend] == '' && (iend - istart == 7 || iend - istart == 9) && iend == enpassant) position[enpassant-8] = '';
		if(position[istart] == 'p' && position[iend] == '' && (iend - istart == -7 || iend - istart == -9) && iend == enpassant) position[enpassant+8] = '';

		position[iend] = position[istart];
		position[istart] = '';

		if(move.length == 5) position[iend] = move.substr(4,1);

// castling
		if(istart == 4 && iend == 6 && position[iend] == 'K') {position[5] = 'R'; position[7] = '';}
		else if(istart == 4 && iend == 2 && position[iend] == 'K') {position[3] = 'R'; position[0] = '';}
		else if(istart == 60 && iend == 62 && position[iend] == 'k') {position[61] = 'r'; position[63] = '';}
		else if(istart == 60 && iend == 58 && position[iend] == 'k') {position[59] = 'r'; position[56] = '';}

// en passant
		enpassant = -1;
		if(position[iend] == 'P' && iend - istart == 16) enpassant = iend - 8;
		else if(position[iend] == 'p' && istart - iend == 16) enpassant = istart - 8;

		activeColor = -activeColor;
	}

	var fen = '';
	castling = '';
	if(wCastleShort) castling += 'K';
	if(wCastleLong) castling += 'Q';
	if(bCastleShort) castling += 'k';
	if(bCastleLong) castling += 'q';
	if(castling == '') castling = '-';


	t_enpassant = '-';
	if(enpassant >= 0)
	{
		t_enpassant = moveToText(enpassant);
//		line = parseInt(enpassant / 8) + 1;
//		col = enpassant - 8*(line -1);
//		t_enpassant = chr(col+97).line;
	}


	for(iline=7; iline>=0; iline--)
	{
		empty = 0;
		for(icol = 0; icol<8; icol++)
		{
			ii = iline*8+icol;
			if(position[ii] == '') empty++;
			else
			{
				if(empty > 0) fen += empty;
				empty = 0;
				fen += position[ii];
			}
		}
		if(empty > 0) fen += empty;
		if(iline > 0) fen += '/';
	}

	if(activeColor == 1) fen += ' w ';
	else if(activeColor == -1) fen += ' b ';
	fen += castling+' ';
	fen += t_enpassant;

	return(fen);
}

function moveToCell(move)
{
	var a = move.substr(0,1);
	var b = move.substr(1,1);

	if(a == 'a') c = 0;
	else if(a == 'b') c = 1;
	else if(a == 'c') c = 2;
	else if(a == 'd') c = 3;
	else if(a == 'e') c = 4;
	else if(a == 'f') c = 5;
	else if(a == 'g') c = 6;
	else if(a == 'h') c = 7;

//	return(0);
	return(c+8*(parseInt(b)-1));
}

function drawPgn()
{
	var nlines = 10;
	var output = '';

	var bg = '';
	for(var i=0; i<_pgn.length; i++)
	{
		if(i%2 == 0) 
		{
			var n = i/2 + 1;
			output += '<span style="width: 50px; text-align: right; margin-right: 10px;">'+n+'.</span>';
		}
		if(i == _halfMoveNumber-1) bg = 'color: rgb(0, 0, 255)';
		else bg = '';
//		alert(bg);
		output += '<span id="pgn'+i+'" style="width: 70px; text-align: left; '+bg+'">'+_pgn[i]+'</span>';
		if(i%2 != 0) output += '<br>';
		else output += '&nbsp;';
	}
	_write_display('pgnid',output);
}

function drawFen()
{
	var help = 'La notation FEN est une façon compacte de représenter une position aux échecs (y compris les droits aux roques et les possibilités de prise en passant).';
	var output = '<form><a href="javascript:" onclick="_toggle_help(\'Notation FEN\',\''+help+'\'); return(false);"><img src="./images/icons/help.png" border="0" alt="aide"></a>&nbsp;FEN:&nbsp;<input size="75" type="text" style="text-align: center; color: #000;" value="'+_fen+'"></form>';
	_write_display('fenid',output);
	
}

function flipSide()
{
	if(flip == 0) flip = 1;
	else flip = 0;
	draw();
}

function drawBoard(position)
{
	var output = '';
	var color = 1;
	var bgcolor = '';
	var click = '';
	var i;
	var p = '';

//	alert(_lastmove);
	var start = -1;
	var end = -1;

	if(_lastmove != '')
	{
		var lmove = _lastmove.split('-');
		start = parseInt(lmove[0]);
		end = parseInt(lmove[1]);
	}


	output += '<table width="100%" align="center" cellpadding="0" cellspacing="1">';
	for(var line=0; line<8; line++)
	{
		var ii = line + 1;
		if(flip == 0) ii = 8-line;
		output += '<tr><td height="40" valign="middle" align="center"><font class="small">'+ii+'</font></td></tr>';
	}
	output += '</table>';
	_write_display('boardleftid', output);

	output = '<table align="center" cellpadding="0" cellspacing="1" style="background-color: #000;">';
	for(var line=0; line<8; line++)
	{
		output += '<tr>';
		for(var col=0; col<8; col++)
		{
			if(flip == 0) i = (7-line)*8 + col;
			else i = line*8+(7-col);
			p = position[i];

			bgcolor = 'background-image:url(\'board/lightbg.png\');';
			if(color == -1) bgcolor='background-image:url(\'board/darkbg.png\');';
			if(i == cell1 || i == cell2) bgcolor = 'background-color: rgb(5, 130, 255);';
			else if(i == start || i == end) bgcolor = 'background-color: rgb(167, 211, 255);';

			click = '';
			if(clickable != 0) click = 'onclick="cl('+i+'); return(false);"';
			output += '<td width="'+_cellsize+'" height="'+_cellsize+'" style="'+bgcolor+'" '+click+' id="id'+i+'">';

/*			
			bgcolor = '#fff';
			if(color == -1) bgcolor='#ddd';
			if(i == cell1 || i == cell2) bgcolor = 'rgb(5, 130, 255)';
			else if(i == start || i == end) bgcolor = 'rgb(167, 211, 255)';

			click = '';
			if(clickable != 0) click = 'onclick="cl('+i+'); return(false);"';
			output += '<td width="'+_cellsize+'" height="'+_cellsize+'" style="background-color: '+bgcolor+';" '+click+' id="id'+i+'">';*/

			if(p == 'R') output += '<img src="./board/default/wr.gif" alt="" border="0">';
			else if(p == 'B') output += '<img src="./board/default/wb.gif" alt="" border="0">';
			else if(p == 'N') output += '<img src="./board/default/wn.gif" alt="" border="0">';
			else if(p == 'P') output += '<img src="./board/default/wp.gif" alt="" border="0">';
			else if(p == 'K') output += '<img src="./board/default/wk.gif" alt="" border="0">';
			else if(p == 'Q') output += '<img src="./board/default/wq.gif" alt="" border="0">';
			else if(p == 'r') output += '<img src="./board/default/br.gif" alt="" border="0">';
			else if(p == 'b') output += '<img src="./board/default/bb.gif" alt="" border="0">';
			else if(p == 'n') output += '<img src="./board/default/bn.gif" alt="" border="0">';
			else if(p == 'p') output += '<img src="./board/default/bp.gif" alt="" border="0">';
			else if(p == 'k') output += '<img src="./board/default/bk.gif" alt="" border="0">';
			else if(p == 'q') output += '<img src="./board/default/bq.gif" alt="" border="0">';
			else output += '<img src="./board/default/empty.gif" alt="" border="0">';

			output += '</td>';
			color = -color;
		}
		output += '</tr>';
		color = -color;
	}

	output += '</table>';
//	if(cell1 >= 0 && document.getElementById('id'+cell1)) document.getElementById('id'+cell1).style.backgroundColor = '#339966';
	_write_display('boardid',output);

	output = '<table width="100%" align="center" cellpadding="0" cellspacing="1">';
	output += '<tr>';
	for(var icol=1; icol<9; icol++)
	{
		var ii = icol;
		if(flip != 0) ii = (9-icol);
		output += '<td width="40" align="center" valign="middle">';
		output += '<font class="small">'+column(ii)+'</font>';
		output += '</td>';
	}
	output += '</tr></table>';
	_write_display('boardbottomid',output);

}

function column(icol)
{
	if(icol == 1) return('a');
	else if(icol == 2) return('b');
	else if(icol == 3) return('c');
	else if(icol == 4) return('d');
	else if(icol == 5) return('e');
	else if(icol == 6) return('f');
	else if(icol == 7) return('g');
	else if(icol == 8) return('h');
	else return('');
}

function displayPromotionPanel(color)
{
	var output = '';
	output += '<table align="center" cellpadding="0" cellspacing="1" style="background-color: #F00;">';
	output += '<tr>';

	if(color == 'w')
	{
		output += '<td style="background-color: #fff;"><a href="javascript:" onclick="setPromotion(\'Q\'); return(false);"><img src="./board/default/wq.gif" alt="" border="0"></a></td>';
		output += '<td style="background-color: #fff;"><a href="javascript:" onclick="setPromotion(\'R\'); return(false);"><img src="./board/default/wr.gif" alt="" border="0"></a></td>';
		output += '<td style="background-color: #fff;"><a href="javascript:" onclick="setPromotion(\'B\'); return(false);"><img src="./board/default/wb.gif" alt="" border="0"></a></td>';
		output += '<td style="background-color: #fff;"><a href="javascript:" onclick="setPromotion(\'N\'); return(false);"><img src="./board/default/wn.gif" alt="" border="0"></a></td>';
	}
	else if(color == 'b')
	{
		output += '<td style="background-color: #fff;"><a href="javascript:" onclick="setPromotion(\'q\'); return(false);"><img src="./board/default/bq.gif" alt="" border="0"></a></td>';
		output += '<td style="background-color: #fff;"><a href="javascript:" onclick="setPromotion(\'r\'); return(false);"><img src="./board/default/br.gif" alt="" border="0"></a></td>';
		output += '<td style="background-color: #fff;"><a href="javascript:" onclick="setPromotion(\'b\'); return(false);"><img src="./board/default/bb.gif" alt="" border="0"></a></td>';
		output += '<td style="background-color: #fff;"><a href="javascript:" onclick="setPromotion(\'n\'); return(false);"><img src="./board/default/bn.gif" alt="" border="0"></a></td>';
	}
	output += '</tr>';
	output += '</table>';
	_write_display('errorid',output);
}

function setPromotion(piece)
{
	_promotion = piece;
	_position[cell2] = piece;
	var movetext = '';
	if(cell1 >= 0) movetext += moveToText(cell1);
	if(cell2 >= 0) movetext += '-'+moveToText(cell2);
	document.getElementById('moveform').move.value = movetext+_promotion;

    drawBoard(_position);
}

function _write_display(id, text)
{
	if(document.getElementById(id)) {document.getElementById(id).innerHTML = text;}
}

//	var _value = document.getElementById('task').tasktext.value;

function cl(cell)
{
	if(usercolor == 0) return;
	var piece = _position[cell];
//	if(whichcolor(piece) != usercolor) return;
	_write_display('errorid','');
	if(cell1 == -1)
	{
		if(whichcolor(piece) != usercolor) return;
		cell1 = cell;
		_write_display('errorid','');
	}
	else if(cell == cell1)
	{
		cell1 = -1;
		cell2 = -1;
		_promotion = '';
		copyPosition(_positionOrg, _position);
		_write_display('errorid','');
	}
	else if(cell2 == -1)
	{
		if(whichcolor(piece) == usercolor) return;
		cell2 = cell;
		if(islegal(_position, cell1, cell2, _castling, _enpassant) == true)
		{
			_position[cell2] = _position[cell1];
			_position[cell1] = '';

			if(isKingAttacked(_position, usercolor))
			{
				copyPosition(_positionOrg, _position);
				cell2 = -1;
				_promotion = '';
				_write_display('errorid','<font class="error">Echec !</font>');
			}
			else if(_position[cell2] == 'K' && cell2 - cell1 == 2)
			{
				if(isCellAttacked(_position, cell1, 'b') || isCellAttacked(_position, cell1+1, 'b')) printEchec();
			}
			else if(_position[cell2] == 'K' && cell2 - cell1 == -2)
			{
				if(isCellAttacked(_position, cell1, 'b') || isCellAttacked(_position, cell1-1, 'b')) printEchec();
			}
			else if(_position[cell2] == 'k' && cell2 - cell1 == 2)
			{
				if(isCellAttacked(_position, cell1, 'w') || isCellAttacked(_position, cell1+1, 'w')) printEchec();
			}
			else if(_position[cell2] == 'k' && cell2 - cell1 == -2)
			{
				if(isCellAttacked(_position, cell1, 'w') || isCellAttacked(_position, cell1-1, 'w')) printEchec();
			}
			else if(cell2 > 55 && _position[cell2] == 'P')
			{
				_promotion = 'Q';
				_position[cell2] = 'Q';
				displayPromotionPanel('w');
			}
		}
		else
		{
			_write_display('errorid','<font class="error">Coup illégal</font>');
		}
	}
	else if(cell == cell2)
	{
		copyPosition(_positionOrg, _position);
		cell2 = -1;
		_promotion = '';
		_write_display('errorid','');
	}
	else
	{
		if(whichcolor(piece) == usercolor) return;
		copyPosition(_positionOrg, _position);
		cell2 = cell;
		_position[cell2] = _position[cell1];
		_position[cell1] = '';
	}
	var movetext = '';
	if(cell1 >= 0) movetext += moveToText(cell1);
	if(cell2 >= 0) movetext += '-'+moveToText(cell2);
	document.getElementById('moveform').move.value = movetext+_promotion;

    drawBoard(_position);
}

function printEchec()
{
	copyPosition(_positionOrg, _position);
	cell2 = -1;
	_promotion = '';
	_write_display('errorid','<font class="error">Echec !</font>');
}

function islegal(position, start, end, castling, enpassant)
{
	if(position[start] == '') return(false);

	var startl = parseInt(start/8);
	var startc = start - startl*8;
	var endl = parseInt(end/8);
	var endc = end - endl*8;
	var dl = endl - startl;
	var dc = endc - startc;

	if(position[start] == 'P')
	{
		if(dl == 1 && dc == 0 && position[end] == '') return(true);
		if(dl == 2 && dc == 0 && position[end] == '' && position[end-8] == '' && start > 7 && start < 16) return(true);
		if(dl == 1 && (dc == 1 || dc == -1))
		{
			if(position[end] != '') return(true); // a verifier: couleur opposee
			else if(moveToText(end) == enpassant) return(true);
		}

		return(false);
	}
	if(position[start] == 'p')
	{
		if(dl == -1 && dc == 0 && position[end] == '') return(true);
		if(dl == -2 && dc == 0 && position[end] == '' && position[end+8] == '' && start > 47 && start < 56) return(true);
		if(dl == -1 && (dc == 1 || dc == -1))
		{
			if(position[end] != '') return(true); // a verifier: couleur opposee
			else if(moveToText(end) == enpassant) return(true);
		}

		return(false);
	}

	if(position[start] == 'N' || position[start] == 'n')
	{

		if(dl * dc == 2 || dl*dc == -2) return(true);
		else return(false);
	}

	if(position[start] == 'K' || position[start] == 'k')
	{
		if(dl > -2 && dl < 2 && dc > -2 && dc < 2 && (dc != 0 || dl != 0)) return(true);
		if(dl == 0 && dc == 2)
		{
			if(position[start] == 'K' && start != 4) return(false);
			if(position[start] == 'k' && start != 60) return(false);
			if(castling.indexOf('K') < 0 && position[start] == 'K') return(false);
			if(castling.indexOf('k') < 0 && position[start] == 'k') return(false);
			if(position[start+1] != '' || position[start+2] != '') return(false);
			return(true);
		}
		else if(dl == 0 && dc == -2)
		{
			if(position[start] == 'K' && start != 4) return(false);
			if(position[start] == 'k' && start != 60) return(false);
			if(castling.indexOf('Q') < 0 && position[start] == 'K') return(false);
			if(castling.indexOf('q') < 0 && position[start] == 'k') return(false);
			if(position[start-1] != '' || position[start-2] != '' || position[start-3] != '') return(false);
			return(true);
		}
		return(false);
	}

	if(position[start] == 'R' || position[start] == 'r')
	{
		if(dl == 0 && dc > 0)
		{
			for(var i=1; i<dc; i++) { if(position[start+i] != '') return(false); }
			return(true);
		}
		else if(dl == 0 && dc < 0)
		{
			for(var i=1; i<-dc; i++) { if(position[start-i] != '') return(false); }
			return(true);
		}
		else if(dc == 0 && dl > 0)
		{
			for(var i=1; i<dl; i++) { if(position[start+8*i] != '') return(false); }
			return(true);
		}
		else if(dc == 0 && dl > 0)
		{
			for(var i=1; i<-dl; i++) { if(position[start-8*i] != '') return(false); }
			return(true);
		}
		return(false);
	}

	if(position[start] == 'B' || position[start] == 'b')
	{
		if(Math.abs(dl) != Math.abs(dc)) return(false);

		for(var i=1; i<Math.abs(dl); i++) { if(position[start+i*sign(dc)+8*i*sign(dl)] != '') return(false); }
		return(true);
	}
	
	if(position[start] == 'Q' || position[start] == 'q')
	{
		if(Math.abs(dl) == Math.abs(dc))
		{
			for(var i=1; i<Math.abs(dl); i++) { if(position[start+i*sign(dc)+8*i*sign(dl)] != '') return(false); }
			return(true);
		}
		else if(dl == 0)
		{
			for(var i=1; i<Math.abs(dc); i++) { if(position[start+i*sign(dc)] != '') return(false); }
			return(true);
		}
		else if(dc == 0)
		{
			for(var i=1; i<Math.abs(dl); i++) { if(position[start+8*i*sign(dl)] != '') return(false); }
			return(true);
		}
		return(false);
	}

	return(true);
}

function isKingAttacked(position, color)
{
	var acolor = '';
	if(color == 1) {piece = 'K'; acolor = 'b'; }
	else { piece = 'k'; acolor = 'w'; }

//	alert(color+'-'+piece);

	for(var i=0; i<63; i++)
	{
		if(position[i] == piece) return(isCellAttacked(position, i, acolor));
	}
	return(false);
}

function isCellAttacked(position, cell, color)
{
	if(isCellAttackedByKnight(position, cell, color)) return(true);
	if(isCellAttackedByBishop(position, cell, color)) return(true);
	if(isCellAttackedByRook(position, cell, color)) return(true);
	if(isCellAttackedByPawn(position, cell, color)) return(true);
	if(isCellAttackedByKing(position, cell, color)) return(true);
	return(false);
}

function isCellAttackedByKnight(position, cell, color)
{
	var line = parseInt(cell/8);
	var col = cell - line*8;

	if(color == 'w') piece = 'N';
	else piece = 'n';

//	alert(piece+'-'+cell+'-'+color);
	if(fcell(position, line+1, col-2) == piece) return(true);
	if(fcell(position, line+1, col+2) == piece) return(true);
	if(fcell(position, line+2, col-1) == piece) return(true);
	if(fcell(position, line+2, col+1) == piece) return(true);
	if(fcell(position, line-1, col-2) == piece) return(true);
	if(fcell(position, line-1, col+2) == piece) return(true);
	if(fcell(position, line-2, col-1) == piece) return(true);
	if(fcell(position, line-2, col+1) == piece) return(true);

	return(false);
}

function isCellAttackedByBishop(position, cell, color)
{
	var line = parseInt(cell/8);
	var col = cell - line*8;

	if(color == 'w') { piece1 = 'B'; piece2 = 'Q'; }
	else { piece1 = 'b'; piece2 = 'q'; }

	for(var dl=-1; dl<=1; dl+=2)
	{
		for(var dc=-1; dc<=1; dc+=2)
		{
			for(var i=1; i<9; i++)
			{
				var a = fcell(position, line+dl*i, col+dc*i);
				if(a == piece1 || a == piece2) return(true);
				else if(a != '') break; 
			}
		}
	}

	return(false);
}

function isCellAttackedByRook(position, cell, color)
{
	var line = parseInt(cell/8);
	var col = cell - line*8;

	if(color == 'w') { piece1 = 'R'; piece2 = 'Q'; }
	else { piece1 = 'r'; piece2 = 'q'; }

	for(var i=1; i<9; i++)
	{
		var a = fcell(position, line, col+i);
		if(a == piece1 || a == piece2) return(true);
		else if(a != '') break; 
	}
	for(var i=1; i<9; i++)
	{
		var a = fcell(position, line, col-i);
		if(a == piece1 || a == piece2) return(true);
		else if(a != '') break; 
	}
	for(var i=1; i<9; i++)
	{
		var a = fcell(position, line+i, col);
		if(a == piece1 || a == piece2) return(true);
		else if(a != '') break; 
	}
	for(var i=1; i<9; i++)
	{
		var a = fcell(position, line-i, col);
		if(a == piece1 || a == piece2) return(true);
		else if(a != '') break; 
	}

	return(false);
}

function isCellAttackedByPawn(position, cell, color)
{
	var line = parseInt(cell/8);
	var col = cell - line*8;

	if(color == 'w')
	{
		if(fcell(position, line-1, col-1) == 'P') return(true);
		if(fcell(position, line-1, col+1) == 'P') return(true);
	}
	else if(color == 'b')
	{
		if(fcell(position, line+1, col-1) == 'p') return(true);
		if(fcell(position, line+1, col+1) == 'p') return(true);
	}

	return(false);
}

function isCellAttackedByKing(position, cell, color)
{
	var line = parseInt(cell/8);
	var col = cell - line*8;

	if(color == 'w') piece = 'K';
	else piece = 'k';

	for(var i=-1; i<=1; i++)
	{
		for(var j=-1; j<=1; j++)
		{
			if(i==0 && j==0) continue;
			if(fcell(position, line+i, col+j) == piece) return(true);
		}
	}

	return(false);
}



function fcell(position, line, col)
{
	if(line < 0 || line > 7 || col < 0 || col > 7) return('-');
	var i = 8*line+col;
	return(position[i]);
}

function splitFEN(fen)
{
	var arr = fen.split(' ');
	_castling = arr[2];
	_enpassant = arr[3];
}

function fenToPosition(fen)
{
	var arr = fen.split(' ');
	var pfen = arr[0];

	var position = new Array(64);
	var col = 0;
	var line = 7;

	for(var i=0; i<pfen.length; i++)
	{
		var c = pfen.charAt(i);
		if(c == '/')
		{
			col = 0;
			line --;
			continue;
		}
		else if(isNaN(parseInt(c))) 
		{
			position[col+8*line] = c;
			col++;
		}
		else
		{
			for(var j=0; j<parseInt(c); j++)
			{
				position[col+8*line] = '';
				col++;
			}
		}
	}
	return(position);
//	alert(position);
}

function sign(a)
{
	if(a < 0) return(-1);
	else if(a > 0) return(1);

	return(0);
}

function whichcolor(p)
{
   if(p=='p' || p=='r' || p=='b' || p=='n' || p=='q' || p=='k') return(-1);
   else if(p=='P' || p=='R' || p=='B' || p=='N' || p=='Q' || p=='K') return(1);
   return(0);
}

function copyPosition(pos1, pos2)
{
	for(var i=0; i<64; i++) pos2[i] = pos1[i];
}

function moveToText(move)
{
	var out = '';
	var line = parseInt(move/8);
	var col = move - line*8;
	if(col == 0) out += 'a';
	else if(col == 1) out += 'b';
	else if(col == 2) out += 'c';
	else if(col == 3) out += 'd';
	else if(col == 4) out += 'e';
	else if(col == 5) out += 'f';
	else if(col == 6) out += 'g';
	else if(col == 7) out += 'h';

	line += 1;
	out += line;
	return(out);
}
