// LT-PGN-VIEWER 1.4 by Lutz Tautenhahn (2002)

var i, j, s, StartMove, MoveCount, MoveType, CanPass, EnPass, MaxMove=500, ShortPgnMoveText="";

var AgntUsr=navigator.userAgent.toLowerCase();
var NavYes=AgntUsr.indexOf('mozilla')!=-1&&AgntUsr.indexOf('compatible')==-1?1:0;

var DirImages = "/images/scacchi/";

PieceType = new Array(2);
for (i=0; i<2; i++) PieceType[i] = new Array(16);
PiecePosX = new Array(2);
for (i=0; i<2; i++) PiecePosX[i] = new Array(16);
PiecePosY = new Array(2);
for (i=0; i<2; i++) PiecePosY[i] = new Array(16);
PieceMoves = new Array(2);
for (i=0; i<2; i++) PieceMoves[i] = new Array(16);

var isRotated=false;
var isRecording=false;
var BoardClicked=-1;
var PieceName = "KQRBNP";
var FenString = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
ColorName = new Array("w","b");
Castling = new Array(2);
for (i=0; i<2; i++) Castling[i] = new Array(2);
Board = new Array(8);
for (i=0; i<8; i++) Board[i] = new Array(8);

HalfMove = new Array(MaxMove+1);
HistMove = new Array(MaxMove);
HistPiece = new Array(2);
for (i=0; i<2; i++) HistPiece[i] = new Array(MaxMove);
HistType = new Array(2);
for (i=0; i<2; i++) HistType[i] = new Array(MaxMove);
HistPosX = new Array(2);
for (i=0; i<2; i++) HistPosX[i] = new Array(MaxMove);
HistPosY = new Array(2);
for (i=0; i<2; i++) HistPosY[i] = new Array(MaxMove);

BoardPic = new Array(2);
for (i=0; i<2; i++)
{ BoardPic[i] = new Image(); BoardPic[i].src = DirImages+ColorName[i]+".gif";
}
PiecePic = new Array(2);
for (i=0; i<2; i++)
	PiecePic[i] = new Array(6);
for (i=0; i<2; i++)
{ for (j=0; j<6; j++)
		PiecePic[i][j] = new Array(2);
}
for (i=0; i<2; i++)
{ for (j=0; j<2; j++)
	{ PiecePic[i][0][j] = new Image(); PiecePic[i][0][j].src = DirImages+ColorName[i]+"k"+ColorName[j]+".gif";
		PiecePic[i][1][j] = new Image(); PiecePic[i][1][j].src = DirImages+ColorName[i]+"q"+ColorName[j]+".gif";
		PiecePic[i][2][j] = new Image(); PiecePic[i][2][j].src = DirImages+ColorName[i]+"r"+ColorName[j]+".gif";
		PiecePic[i][3][j] = new Image(); PiecePic[i][3][j].src = DirImages+ColorName[i]+"b"+ColorName[j]+".gif";
		PiecePic[i][4][j] = new Image(); PiecePic[i][4][j].src = DirImages+ColorName[i]+"n"+ColorName[j]+".gif";
		PiecePic[i][5][j] = new Image(); PiecePic[i][5][j].src = DirImages+ColorName[i]+"p"+ColorName[j]+".gif";
	}
}

function sign(nn)
{ if (nn>0) return(1);
	if (nn<0) return(-1);
	return(0);
}

function OpenUrl(ss)
{ if (ss!="")
		window.parent.frames[1].location.href = urlstr;
	else
	{ var urlstr=window.document.BoardForm.Url.value;
		if (urlstr!="")	
			window.parent.frames[1].location.href = urlstr;
	}
}

function Init(rr)
{ var cc, ii, jj, kk, ll, nn, mm;
	if (rr!='')
		FenString=rr;
	if (FenString=='standard')
		FenString="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
	if (FenString == "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
	{ for (ii=0; ii<2; ii++)
		{ PieceType[ii][0]=0;
			PiecePosX[ii][0]=4;
			PieceType[ii][1]=1;
			PiecePosX[ii][1]=3;
			PieceType[ii][6]=2;
			PiecePosX[ii][6]=0;
			PieceType[ii][7]=2;
			PiecePosX[ii][7]=7;
			PieceType[ii][4]=3;
			PiecePosX[ii][4]=2;
			PieceType[ii][5]=3;
			PiecePosX[ii][5]=5;
			PieceType[ii][2]=4;
			PiecePosX[ii][2]=1;
			PieceType[ii][3]=4;
			PiecePosX[ii][3]=6;
			for (jj=0; jj<8; jj++)
			{ PieceType[ii][jj+8]=5;
				PiecePosX[ii][jj+8]=jj;
			}
			for (jj=0; jj<16; jj++)
			{ PieceMoves[ii][jj]=0;
				PiecePosY[ii][jj]=(1-ii)*Math.floor(jj/8)+ii*(7-Math.floor(jj/8));
			}
		}
		for (ii=0; ii<8; ii++)
		{ for (jj=0; jj<8; jj++) Board[ii][jj]=0;
		}
		for (ii=0; ii<2; ii++)
		{ for (jj=0; jj<16; jj++)
				Board[PiecePosX[ii][jj]][PiecePosY[ii][jj]]=(PieceType[ii][jj]+1)*(1-2*ii);
		}
		for (ii=0; ii<2; ii++)
		{ for (jj=0; jj<2; jj++)
				Castling[ii][jj]=1;
		}
		EnPass=-1;
		HalfMove[0]=0;
		StartMove=0;
		MoveCount=StartMove;
		MoveType=StartMove%2;
		if (window.document.BoardForm)
		{ RefreshBoard();
			// window.document.BoardForm.Position.value=""; // GIULIANO
		}
	}
	else
	{ for (ii=0; ii<2; ii++)
		{ for (jj=0; jj<16; jj++)
			{ PieceType[ii][jj]=-1;
				PiecePosX[ii][jj]=0;
				PiecePosY[ii][jj]=0;
				PieceMoves[ii][jj]=0;
			}
		}
		ii=0; jj=7; ll=0; nn=1; mm=1; cc=FenString.charAt(ll++);
		while (cc!=" ")
		{ if (cc=="/")
			{ if (ii!=8)
				{ alert("Invalid FEN [1]: char "+ll+" in "+FenString);
					Init('standard');
					return;
				}
				ii=0;
				jj--;
			}
			if (ii==8) 
			{ alert("Invalid FEN [2]: char "+ll+" in "+FenString);
				Init('standard');
				return;
			}
			if (! isNaN(cc))
			{ ii+=parseInt(cc);
				if ((ii<0)||(ii>8))
				{ alert("Invalid FEN [3]: char "+ll+" in "+FenString);
					Init('standard');
					return;
				}
			}
			if (cc==PieceName.toUpperCase().charAt(0))
			{ if (PieceType[0][0]!=-1)
				{ alert("Invalid FEN [4]: char "+ll+" in "+FenString);
					Init('standard');
					return;
				}		 
				PieceType[0][0]=0;
				PiecePosX[0][0]=ii;
				PiecePosY[0][0]=jj;
				ii++;
			}
			if (cc==PieceName.toLowerCase().charAt(0))
			{ if (PieceType[1][0]!=-1)
				{ alert("Invalid FEN [5]: char "+ll+" in "+FenString);
					Init('standard');
					return;
				}	
				PieceType[1][0]=0;
				PiecePosX[1][0]=ii;
				PiecePosY[1][0]=jj;
				ii++;
			}
			for (kk=1; kk<6; kk++)
			{ if (cc==PieceName.toUpperCase().charAt(kk))
				{ if (nn==16)
					{ alert("Invalid FEN [6]: char "+ll+" in "+FenString);
						Init('standard');
						return;
					}					
					PieceType[0][nn]=kk;
					PiecePosX[0][nn]=ii;
					PiecePosY[0][nn]=jj;
					nn++;
					ii++;
				}
				if (cc==PieceName.toLowerCase().charAt(kk))
				{ if (mm==16)
					{ alert("Invalid FEN [7]: char "+ll+" in "+FenString);
						Init('standard');
						return;
					}	
					PieceType[1][mm]=kk;
					PiecePosX[1][mm]=ii;
					PiecePosY[1][mm]=jj;
					mm++;
					ii++;
				}
			}
			if (ll<FenString.length)
				cc=FenString.charAt(ll++);
			else cc=" ";
		}
		if ((ii!=8)||(jj!=0))
		{ alert("Invalid FEN [8]: char "+ll+" in "+FenString);
			Init('standard');
			return;
		}
		if ((PieceType[0][0]==-1)||(PieceType[1][0]==-1))
		{ alert("Invalid FEN [9]: char "+ll+" missing king");
			Init('standard');
			return;
		}
		if (ll==FenString.length)
		{ alert("Invalid FEN [10]: char "+ll+" missing active color");
			Init('standard');
			return;
		}
		cc=FenString.charAt(ll++);
		if ((cc=="w")||(cc=="b"))
		{ if (cc=="w") StartMove=0;
			else StartMove=1;
		}
		else
		{ alert("Invalid FEN [11]: char "+ll+" invalid active color");
			Init('standard');
			return;
		}
		ll++;
		if (ll>=FenString.length)
		{ alert("Invalid FEN [12]: char "+ll+" missing castling availability");
			Init('standard');
			return;
		}
		Castling[0][0]=0; Castling[0][1]=0; Castling[1][0]=0; Castling[1][1]=0;
		cc=FenString.charAt(ll++);
		while (cc!=" ")
		{ if (cc==PieceName.toUpperCase().charAt(0))
				Castling[0][0]=1; 
			if (cc==PieceName.toUpperCase().charAt(1))
				Castling[0][1]=1; 
			if (cc==PieceName.toLowerCase().charAt(0))
				Castling[1][0]=1; 
			if (cc==PieceName.toLowerCase().charAt(1))
				Castling[1][1]=1; 
			if (ll<FenString.length)
				cc=FenString.charAt(ll++);
			else cc=" ";
		}
		if (ll==FenString.length)
		{ alert("Invalid FEN [13]: char "+ll+" missing en passant target square");
			Init('standard');
			return;
		}
		EnPass=-1;
		cc=FenString.charAt(ll++);
		while (cc!=" ")
		{ if ((cc.charCodeAt(0)-97>=0)&&(cc.charCodeAt(0)-97<=7))
				EnPass=cc.charCodeAt(0)-97; 
			if (ll<FenString.length)
				cc=FenString.charAt(ll++);
			else cc=" ";
		}
		if (ll==FenString.length)
		{ alert("Invalid FEN [14]: char "+ll+" missing halfmove clock");
			Init('standard');
			return;
		}
		HalfMove[0]=0;
		cc=FenString.charAt(ll++);
		while (cc!=" ")
		{ if (isNaN(cc))
			{ alert("Invalid FEN [15]: char "+ll+" invalid halfmove clock");
				Init('standard');
				return;
			}
			HalfMove[0]=HalfMove[0]*10+parseInt(cc);
			if (ll<FenString.length)
				cc=FenString.charAt(ll++);
			else cc=" ";
		}
		if (ll==FenString.length)
		{ alert("Invalid FEN [16]: char "+ll+" missing fullmove number");
			Init('standard');
			return;
		}
		cc=FenString.substring(ll++);
		if (isNaN(cc))
		{ alert("Invalid FEN [17]: char "+ll+" invalid fullmove number");
			Init('standard');
			return;
		}
		if (cc<=0)
		{ alert("Invalid FEN [18]: char "+ll+" invalid fullmove number");
			Init('standard');
			return;
		}
		StartMove+=2*(parseInt(cc)-1);
		for (ii=0; ii<8; ii++)
		{ for (jj=0; jj<8; jj++) Board[ii][jj]=0;
		}
		for (ii=0; ii<2; ii++)
		{ for (jj=0; jj<16; jj++)
				Board[PiecePosX[ii][jj]][PiecePosY[ii][jj]]=(PieceType[ii][jj]+1)*(1-2*ii);
		}
		if (window.document.BoardForm)
		{ RefreshBoard();
			// window.document.BoardForm.Position.value=""; // GIULIANO
		}
		MoveCount=StartMove;
		MoveType=StartMove%2;
		BoardClicked=-1;
	}
}

function MoveBack(nn)
{ var ii, jj, cc;
	for (jj=0; (jj<nn)&&(MoveCount>StartMove); jj++)
	{ MoveCount--;
		MoveType=1-MoveType;
		cc=MoveCount-StartMove;
		ii=HistPiece[0][cc];	 
		Board[PiecePosX[MoveType][ii]][PiecePosY[MoveType][ii]]=0;
		Board[HistPosX[0][cc]][HistPosY[0][cc]]=(HistType[0][cc]+1)*(1-2*MoveType);
		PieceType[MoveType][ii]=HistType[0][cc];
		PiecePosX[MoveType][ii]=HistPosX[0][cc];
		PiecePosY[MoveType][ii]=HistPosY[0][cc];
		PieceMoves[MoveType][ii]--;
		ii=HistPiece[1][cc];
		if ((0<=ii)&&(ii<16))
		{ Board[PiecePosX[MoveType][ii]][PiecePosY[MoveType][ii]]=0;
			Board[HistPosX[1][cc]][HistPosY[1][cc]]=(HistType[1][cc]+1)*(1-2*MoveType);
			PieceType[MoveType][ii]=HistType[1][cc];
			PiecePosX[MoveType][ii]=HistPosX[1][cc];
			PiecePosY[MoveType][ii]=HistPosY[1][cc];
			PieceMoves[MoveType][ii]--;
		}
		ii-=16;
		if (0<=ii)
		{ Board[HistPosX[1][cc]][HistPosY[1][cc]]=(HistType[1][cc]+1)*(2*MoveType-1);
			PieceType[1-MoveType][ii]=HistType[1][cc];
			PiecePosX[1-MoveType][ii]=HistPosX[1][cc];
			PiecePosY[1-MoveType][ii]=HistPosY[1][cc];
			PieceMoves[1-MoveType][ii]--;
		}
	}
	if (window.document.BoardForm)
	{ RefreshBoard();
		// if (MoveCount>StartMove) // GIULIANO
		//	 window.document.BoardForm.Position.value=HistMove[MoveCount-StartMove-1]; // GIULIANO
		// else // GIULIANO
		//	 window.document.BoardForm.Position.value=""; // GIULIANO
	}
}

function Uncomment(ss)
{ var ii, uu="", cc, bb=false;
	for (ii=0; ii<ss.length; ii++)	
	{ cc=ss.substr(ii,1);
		if (cc=="{") bb=true;
		if (! bb) uu+=cc;
		if (cc=="}") bb=false;
	}
	return(uu);
}

function MoveForward(nn)
{ var ii,ffst=0,llst,ssearch,ssub,ffull,mmove0="",mmove1="";
	if ((window.document.BoardForm)&&(document.BoardForm.PgnMoveText))
		ShortPgnMoveText=Uncomment(document.BoardForm.PgnMoveText.value);

	// GIULIANO: FORZATA LA VARIABILE ShortPgnMoveText PER CONTENERE TUTTE LE MOSSE DELLA PARTITA
	ShortPgnMoveText = '1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 4. Ba4 Nf6 5. O-O Be7 6. Qe2 b5 7. Bb3 O-O 8. c3 d5 9. exd5 Nxd5 10. Nxe5 Nf4 11. Qe4 Nxe5 12. Qxa8 Qd3 13. Bd1 Bh3 14. Qxa6 Bxg2 15. Re1 Qf3 16. Bxf3 Nxf3# 0-1';



	ffull=ShortPgnMoveText;
	for (ii=0; (ii<nn)&&(ffst>=0)&&(MoveCount<MaxMove); ii++)
	{ ssearch=eval(Math.floor(MoveCount/2)+2)+".";
		llst=ffull.indexOf(ssearch);
		ssearch=eval(Math.floor(MoveCount/2)+1)+".";
		ffst=ffull.indexOf(ssearch);
		if (ffst>=0)
		{ ffst+=ssearch.length;
			if (llst<0)
				ssub=ffull.substring(ffst);
			else
				ssub=ffull.substring(ffst, llst);
			mmove0=GetMove(ssub,MoveType);
			if (mmove0!="")
			{ if (ParseMove(mmove0, true)>0)
				{ mmove1=mmove0;
					if (MoveType==0)
						HistMove[MoveCount-StartMove]=Math.floor((MoveCount+2)/2)+"."+mmove1;
					else
						HistMove[MoveCount-StartMove]=Math.floor((MoveCount+2)/2)+". ... "+mmove1;
					MoveCount++;
					MoveType=1-MoveType;
				}	
				else
				{ if (MoveType==1)
					{ ssearch=eval(Math.floor(MoveCount/2)+1)+"....";
						ffst=ffull.indexOf(ssearch);
						if (ffst<0)
						{ ssearch=eval(Math.floor(MoveCount/2)+1)+"...";
							ffst=ffull.indexOf(ssearch);
						}
						if (ffst<0)
						{ ssearch=eval(Math.floor(MoveCount/2)+1)+". ...";
							ffst=ffull.indexOf(ssearch);
						}
						if (ffst<0)
						{ ssearch=eval(Math.floor(MoveCount/2)+1)+". ..";
							ffst=ffull.indexOf(ssearch);
						}
						if (ffst<0)
						{ ssearch=eval(Math.floor(MoveCount/2)+1)+" ...";
							ffst=ffull.indexOf(ssearch);
						}
						if (ffst<0)
						{ ssearch=eval(Math.floor(MoveCount/2)+1)+" ..";
							ffst=ffull.indexOf(ssearch);
						}
						if (ffst>=0) 
						{ ffst+=ssearch.length;
							if (llst<0)
								ssub=ffull.substring(ffst);
							else
								ssub=ffull.substring(ffst, llst);
							mmove0=GetMove(ssub,0);
							if (mmove0!="")
							{ if (ParseMove(mmove0, true)>0)
								{ mmove1=mmove0;
									HistMove[MoveCount-StartMove]=Math.floor((MoveCount+2)/2)+". ... "+mmove1;
									MoveCount++;
									MoveType=1-MoveType;
								}	
								else
								{ ffst=-1;
									//alert(mmove0+" is not a valid move.");
								}
							}
						}
					}
					else
					{ ffst=-1;
						//alert(mmove0+" is not a valid move.");
					}
				}
			}
		}
	}
	if (window.document.BoardForm)
	{ // if (mmove1!="") // GIULIANO
		//	 window.document.BoardForm.Position.value=HistMove[MoveCount-StartMove-1]; // GIULIANO
		RefreshBoard();
	}
}

function ParseMove(mm, sstore)
{ var ii, ffrom="", ccapt=0, ll;
	var ttype0=-1, xx0=-1, yy0=-1, ttype1=-1, xx1=-1, yy1=-1;
	if (MoveCount>StartMove)
	{ CanPass=-1;
		ii=HistPiece[0][MoveCount-StartMove-1];
		if ((HistType[0][MoveCount-StartMove-1]==5)&&(Math.abs(HistPosY[0][MoveCount-StartMove-1]-PiecePosY[1-MoveType][ii])==2))
			CanPass=PiecePosX[1-MoveType][ii];
	}
	else
		CanPass=EnPass;
	ii=1;
	while (ii<mm.length)	
	{ if (! isNaN(mm.charAt(ii)))
		{ xx1=mm.charCodeAt(ii-1)-97;
			yy1=mm.charAt(ii)-1;
			ffrom=mm.substring(0, ii-1);
		}
		ii++;
	}
	if ((xx1<0)||(xx1>7)||(yy1<0)||(yy1>7))
	{ if ((mm.search("O-O-O")>=0)||(mm.search("0-0-0")>=0))
		{ if (EvalMove(ttype0, 6, xx0, yy0, ttype1, xx1, yy1, ccapt, sstore))
				return(1);
			return(0);
		}
		if ((mm.search("O-O")>=0)||(mm.search("0-0")>=0))
		{ if (EvalMove(ttype0, 7, xx0, yy0, ttype1, xx1, yy1, ccapt, sstore))
				return(1);
			return(0);
		}		
		return(0);
	}
	ll=ffrom.length;
	ttype0=5;
	if (ll>0)
	{ for (ii=0; ii<5; ii++)
		{ if (ffrom.charAt(0)==PieceName.charAt(ii)) 
				ttype0=ii;
		}
		if (ffrom.charAt(ll-1)=="x") ccapt=1;
		if (isNaN(mm.charAt(ll-1-ccapt)))
		{ xx0=ffrom.charCodeAt(ll-1-ccapt)-97;
			if ((xx0<0)||(xx0>7)) xx0=-1;
		}
		else
		{ yy0=ffrom.charAt(ll-1-ccapt)-1;
			if ((yy0<0)||(yy0>7)) yy0=-1;
		}
	}
	ttype1=ttype0;
	ii=mm.search("=");
	if ((ii>0)&&(ii<mm.length-1))
	{ if (ttype0!=5) return(0);
		if (mm.charAt(ii+1)==PieceName.charAt(1)) ttype1=1;
		if (mm.charAt(ii+1)==PieceName.charAt(2)) ttype1=2;
		if (mm.charAt(ii+1)==PieceName.charAt(3)) ttype1=3;
		if (mm.charAt(ii+1)==PieceName.charAt(4)) ttype1=4;
		if (ttype1==5) return(0);
	}
	if (sstore)
	{ for (ii=0; ii<16; ii++)
		{ if (PieceType[MoveType][ii]==ttype0)
			{ if (EvalMove(ii, ttype0, xx0, yy0, ttype1, xx1, yy1, ccapt, true))
					return(1);
			}
		}
	}
	else
	{ ll=0
		for (ii=0; ii<16; ii++)
		{ if (PieceType[MoveType][ii]==ttype0)
			{ if (EvalMove(ii, ttype0, xx0, yy0, ttype1, xx1, yy1, ccapt, false))
					ll++;
			}
		}
		return(ll);
	}		
	return(0);
}

function EvalMove(ii, ttype0, xx0, yy0, ttype1, xx1, yy1, ccapt, sstore)
{ var ddx, ddy, xx, yy, jj=-1, ttype2=-1, xx2=xx1, yy2=xx1, ttype3=-1, xx3=-1, yy3=-1;
	if (ttype0==6)
	{ if (Board[0][MoveType*7]!=(1-2*MoveType)*3) return(false);
		if (Board[1][MoveType*7]!=0) return(false);
		if (Board[2][MoveType*7]!=0) return(false);
		if (Board[3][MoveType*7]!=0) return(false);
		if (Castling[MoveType][1]==0) return(false);
		if (PieceMoves[MoveType][0]>0) return(false);
		jj=0;
		while (jj<16)
		{ if ((PiecePosX[MoveType][jj]==0)&&
					(PiecePosY[MoveType][jj]==MoveType*7)&&
					(PieceType[MoveType][jj]==2))
				jj+=100;
			else jj++;
		}
		if (jj==16) return(false);
		jj-=100;
		if (PieceMoves[MoveType][jj]>0) return(false);
		if (StoreMove(0, 0, 2, MoveType*7, jj, 2, 3, MoveType*7, sstore))
			return(true);
		return(false);
	}
	if (ttype0==7)
	{ if (Board[5][MoveType*7]!=0) return(false);
		if (Board[6][MoveType*7]!=0) return(false);
		if (Board[7][MoveType*7]!=(1-2*MoveType)*3) return(false);
		if (Castling[MoveType][0]==0) return(false);
		if (PieceMoves[MoveType][0]>0) return(false);
		jj=0;
		while (jj<16)
		{ if ((PiecePosX[MoveType][jj]==7)&&
					(PiecePosY[MoveType][jj]==MoveType*7)&&
					(PieceType[MoveType][jj]==2))
				jj+=100;
			else jj++;
		}
		if (jj==16) return(false);
		jj-=100;
		if (PieceMoves[MoveType][jj]>0) return(false);
		if (StoreMove(0, 0, 6, MoveType*7, jj, 2, 5, MoveType*7, sstore))
			return(true);
		return(false);
	}
	if ((PiecePosX[MoveType][ii]==xx1)&&(PiecePosY[MoveType][ii]==yy1))
		return(false);
	if ((ccapt==0)&&(Board[xx1][yy1]!=0))
		return(false);
	if ((ccapt>0)&&(sign(Board[xx1][yy1])!=(2*MoveType-1)))
	{ if ((ttype0!=5)||(CanPass!=xx1)||(yy1!=5-3*MoveType))
			return(false);
	}
	if ((xx0>=0)&&(xx0!=PiecePosX[MoveType][ii])) return(false);
	if ((yy0>=0)&&(yy0!=PiecePosY[MoveType][ii])) return(false);
	if (ttype0==0)
	{ if ((xx0>=0)||(yy0>=0)) return(false);
		if (Math.abs(PiecePosX[MoveType][ii]-xx1)>1) return(false);
		if (Math.abs(PiecePosY[MoveType][ii]-yy1)>1) return(false);
	}
	if (ttype0==1)
	{ if ((Math.abs(PiecePosX[MoveType][ii]-xx1)!=Math.abs(PiecePosY[MoveType][ii]-yy1))&&
				((PiecePosX[MoveType][ii]-xx1)*(PiecePosY[MoveCount%2][ii]-yy1)!=0))
			return(false);
	}
	if (ttype0==2)
	{ if ((PiecePosX[MoveType][ii]-xx1)*(PiecePosY[MoveType][ii]-yy1)!=0)
			return(false);
	}
	if (ttype0==3)
	{ if (Math.abs(PiecePosX[MoveType][ii]-xx1)!=Math.abs(PiecePosY[MoveType][ii]-yy1))
			return(false);
	}
	if (ttype0==4)
	{ if (Math.abs(PiecePosX[MoveType][ii]-xx1)*Math.abs(PiecePosY[MoveType][ii]-yy1)!=2)
			return(false);
	}
	if ((ttype0==1)||(ttype0==2)||(ttype0==3))
	{ ddx=sign(xx1-PiecePosX[MoveType][ii]);
		ddy=sign(yy1-PiecePosY[MoveType][ii]);
		xx=PiecePosX[MoveType][ii]+ddx;
		yy=PiecePosY[MoveType][ii]+ddy;
		while ((xx!=xx1)||(yy!=yy1))
		{ if (Board[xx][yy]!=0) return(false);
			xx+=ddx;
			yy+=ddy;
		}
	}
	if (ttype0==5)
	{ if (Math.abs(PiecePosX[MoveType][ii]-xx1)!=ccapt) return(false);
		if ((yy1==7*(1-MoveType))&&(ttype0==ttype1)) return(false);
		if (ccapt==0)
		{ if (PiecePosY[MoveType][ii]-yy1==4*MoveType-2)
			{ if (PiecePosY[MoveType][ii]!=1+5*MoveType) return(false);
				if (Board[xx1][yy1+2*MoveType-1]!=0) return(false);
			}
			else
			{ if (PiecePosY[MoveType][ii]-yy1!=2*MoveType-1) return(false);
			}
		}
		else
		{ if (PiecePosY[MoveType][ii]-yy1!=2*MoveType-1) return(false);
		}
	}
	if (ttype1!=ttype0)
	{ if (ttype0!=5) return(false);
		if (ttype1>=5) return(false);
		if (yy1!=7-7*MoveType) return(false);
	}
	if ((ttype0<=5)&&(ccapt>0))
	{ jj=15;
		while ((jj>=0)&&(ttype3<0))
		{ if ((PieceType[1-MoveType][jj]>0)&&
					(PiecePosX[1-MoveType][jj]==xx1)&&
					(PiecePosY[1-MoveType][jj]==yy1))
				ttype3=PieceType[1-MoveType][jj];
			else
				jj--;
		}
		if ((ttype3==-1)&&(ttype0==5)&&(CanPass))
		{ jj=15;
			while ((jj>=0)&&(ttype3<0))
			{ if ((PieceType[1-MoveType][jj]==5)&&
						(PiecePosX[1-MoveType][jj]==xx1)&&
						(PiecePosY[1-MoveType][jj]==yy1-1+2*MoveType))
					ttype3=PieceType[1-MoveType][jj];
				else
					jj--;
			}
		}
		ttype3=-1;
	}	
	if (StoreMove(ii, ttype1, xx1, yy1, jj, ttype3, xx3, yy3, sstore))
		return(true);
	return(false);
}

function StoreMove(ii, ttype1, xx1, yy1, jj, ttype3, xx3, yy3, sstore)
{ var iis_check=0, ll, cc=MoveCount-StartMove;
	if ((ttype1==5)||(ttype3>=0))
		HalfMove[cc+1]=0;
	else
		HalfMove[cc+1]=HalfMove[cc]+1;
	HistPiece[0][cc] = ii;
	HistType[0][cc] = PieceType[MoveType][ii];
	HistPosX[0][cc] = PiecePosX[MoveType][ii];
	HistPosY[0][cc] = PiecePosY[MoveType][ii];
	if (jj<0) 
		HistPiece[1][cc] = -1;
	else
	{ if (ttype3>=0)
		{ HistPiece[1][cc] = jj;
			HistType[1][cc] = PieceType[MoveType][jj];
			HistPosX[1][cc] = PiecePosX[MoveType][jj];
			HistPosY[1][cc] = PiecePosY[MoveType][jj];
		}
		else
		{ HistPiece[1][cc] = 16+jj;
			HistType[1][cc] = PieceType[1-MoveType][jj];
			HistPosX[1][cc] = PiecePosX[1-MoveType][jj];
			HistPosY[1][cc] = PiecePosY[1-MoveType][jj];
		}
	}

	Board[PiecePosX[MoveType][ii]][PiecePosY[MoveType][ii]]=0;
	if (jj>=0)
	{ if (ttype3<0)
			Board[PiecePosX[1-MoveType][jj]][PiecePosY[1-MoveType][jj]]=0;
		else
			Board[PiecePosX[MoveType][jj]][PiecePosY[MoveType][jj]]=0;
	}
	PieceType[MoveType][ii]=ttype1;
	PiecePosX[MoveType][ii]=xx1;
	PiecePosY[MoveType][ii]=yy1;
	PieceMoves[MoveType][ii]++;
	if (jj>=0)
	{ if (ttype3<0)
		{ PieceType[1-MoveType][jj]=ttype3;
			PieceMoves[1-MoveType][jj]++;
		}
		else
		{ PiecePosX[MoveType][jj]=xx3;
			PiecePosY[MoveType][jj]=yy3;
			PieceMoves[MoveType][jj]++;
		}
	}
	if (jj>=0)
	{ if (ttype3<0)
			Board[PiecePosX[1-MoveType][jj]][PiecePosY[1-MoveType][jj]]=0;		
		else
			Board[PiecePosX[MoveType][jj]][PiecePosY[MoveType][jj]]=(PieceType[MoveType][jj]+1)*(1-2*MoveType);
	}
	Board[PiecePosX[MoveType][ii]][PiecePosY[MoveType][ii]]=(PieceType[MoveType][ii]+1)*(1-2*MoveType);
	if (ttype3==6)
	{ iis_check+=IsCheck(PiecePosX[MoveType][0]-1, PiecePosY[MoveType][0], MoveType);
		iis_check+=IsCheck(PiecePosX[MoveType][0]+1, PiecePosY[MoveType][0], MoveType);
		iis_check+=IsCheck(PiecePosX[MoveType][0]+2, PiecePosY[MoveType][0], MoveType);
	}
	if (ttype3==7)
	{ iis_check+=IsCheck(PiecePosX[MoveType][0]-2, PiecePosY[MoveType][0], MoveType);
		iis_check+=IsCheck(PiecePosX[MoveType][0]-1, PiecePosY[MoveType][0], MoveType);
	}
	iis_check+=IsCheck(PiecePosX[MoveType][0], PiecePosY[MoveType][0], MoveType);

	if ((iis_check==0)&&(sstore)) return(true);

	Board[PiecePosX[MoveType][ii]][PiecePosY[MoveType][ii]]=0;
	Board[HistPosX[0][cc]][HistPosY[0][cc]]=(HistType[0][cc]+1)*(1-2*MoveType);
	PieceType[MoveType][ii]=HistType[0][cc];
	PiecePosX[MoveType][ii]=HistPosX[0][cc];
	PiecePosY[MoveType][ii]=HistPosY[0][cc];
	PieceMoves[MoveType][ii]--;
	if (jj>=0)	 
	{ if (ttype3>=0)
		{ Board[PiecePosX[MoveType][jj]][PiecePosY[MoveType][jj]]=0;
			Board[HistPosX[1][cc]][HistPosY[1][cc]]=(HistType[1][cc]+1)*(1-2*MoveType);
			PieceType[MoveType][jj]=HistType[1][cc];
			PiecePosX[MoveType][jj]=HistPosX[1][cc];
			PiecePosY[MoveType][jj]=HistPosY[1][cc];
			PieceMoves[MoveType][jj]--;
		}
		else
		{ Board[HistPosX[1][cc]][HistPosY[1][cc]]=(HistType[1][cc]+1)*(2*MoveType-1);
			PieceType[1-MoveType][jj]=HistType[1][cc];
			PiecePosX[1-MoveType][jj]=HistPosX[1][cc];
			PiecePosY[1-MoveType][jj]=HistPosY[1][cc];
			PieceMoves[1-MoveType][jj]--;
		}
	}
	if (iis_check==0) return(true);
	return(false); 
}

function IsCheck(xx, yy, tt)
{ var ii0=xx, jj0=yy, ddi, ddj, bb;
	for (ddi=-2; ddi<=2; ddi+=4)
	{ for (ddj=-1; ddj<=1; ddj+=2)
		{ if (IsOnBoard(ii0+ddi, jj0+ddj))	
			{ if (Board[ii0+ddi][jj0+ddj]==((2*tt-1)*5)) return(1);
			}
		}
	}
	for (ddi=-1; ddi<=1; ddi+=2)
	{ for (ddj=-2; ddj<=2; ddj+=4)
		{ if (IsOnBoard(ii0+ddi, jj0+ddj)) 
			{ if (Board[ii0+ddi][jj0+ddj]==((2*tt-1)*5)) return(1);
			}
		}
	}
	for (ddi=-1; ddi<=1; ddi+=2)
	{ ddj=1-2*tt;
		{ if (IsOnBoard(ii0+ddi, jj0+ddj)) 
			{ if (Board[ii0+ddi][jj0+ddj]==((2*tt-1)*6)) return(1);
			}
		}
	}
	if ((Math.abs(PiecePosX[1-tt][0]-xx)<2)&&(Math.abs(PiecePosY[1-tt][0]-yy)<2)) 
		return(1);
	for (ddi=-1; ddi<=1; ddi+=1)
	{ for (ddj=-1; ddj<=1; ddj+=1)
		{ ii0=xx+ddi; 
			jj0=yy+ddj;
			bb=0;
			while ((IsOnBoard(ii0, jj0))&&(bb==0))
			{ bb=Board[ii0][jj0];
				if (bb==0)
				{ ii0+=ddi;
					jj0+=ddj;
				}
				else
				{ if (bb==(2*tt-1)*2) return(1); 
					if ((bb==(2*tt-1)*3)&&((ddi==0)||(ddj==0))) return(1); 
					if ((bb==(2*tt-1)*4)&&(ddi!=0)&&(ddj!=0)) return(1); 
				}
			}
		}
	}
	return(0);
}

function IsOnBoard(ii, jj)
{ if (ii<0) return(false);
	if (ii>7) return(false);
	if (jj<0) return(false);
	if (jj>7) return(false);
	return(true);
}

function GetMove(ss,nn)
{ var ii=0, jj=0, mm="", ll=-1,cc;
	while (ii<ss.length)
	{ cc=ss.charCodeAt(ii);
		if ((cc<=32)||(cc>=127))
		{ if (ll+1!=ii) jj++;
			ll=ii;
		}
		else
		{ if (jj==nn) mm=mm+ss.charAt(ii);
		}
		ii++;
	}
	return(mm);
}

function RefreshBoard()
{ var ii, jj, nameimg;
	// GIULIANO: DISATTIVO RICONOSCIMENTO NAVIGATOR (31/03/2010):
	NavYes=0;
	for (ii=0; ii<8; ii++)
	{ for (jj=0; jj<8; jj++)
		{ if (Board[ii][jj]==0)
			{ if (isRotated)
					window.document.images[63-ii-(7-jj)*8].src = BoardPic[(ii+jj+1)%2].src;
				else
					if (NavYes) {
						nameimg = "i";
						nameimg += ii+(7-jj)*8;
						window.document.eval(nameimg).src = BoardPic[(ii+jj+1)%2].src;
					} else {
						window.document.images[ii+(7-jj)*8].src = BoardPic[(ii+jj+1)%2].src;
					}
			}
		}
	}
	for (ii=0; ii<2; ii++)
	{ for (jj=0; jj<16; jj++)
		{ if (PieceType[ii][jj]>=0)
			{ kk=PiecePosX[ii][jj]+8*(7-PiecePosY[ii][jj]);
				if (isRotated)
					if (NavYes) {
						nameimg = "i";
						nameimg += kk;
						window.document.eval(nameimg).src = PiecePic[ii][PieceType[ii][jj]][(kk+Math.floor(kk/8))%2].src;	
					} else {
						window.document.images[63-kk].src = PiecePic[ii][PieceType[ii][jj]][(kk+Math.floor(kk/8))%2].src;	
					}
				else
					if (NavYes) {
						nameimg = "i";
						nameimg += kk;
						window.document.eval(nameimg).src = PiecePic[ii][PieceType[ii][jj]][(kk+Math.floor(kk/8))%2].src;
					} else {
						window.document.images[kk].src = PiecePic[ii][PieceType[ii][jj]][(kk+Math.floor(kk/8))%2].src;
					}
			}
		}
	}
	// GIULIANO: INSERITA LA IF PER VISUALIZZARE LA SEQUENZA DELLE MOSSE (FILE JPG)
	if (MoveCount) {
		window.document.BoardForm.mossa.src="/images/scacchi/mosse/mossa"+MoveCount+".jpg";
	} else {
		window.document.BoardForm.mossa.src="/images/scacchi/mosse/mossa0.jpg";
	}
	return;
	alert("Showing Board:");
	for (ii=0; ii<8; ii++)
	{ for (jj=0; jj<8; jj++)
		{ if (Board[ii][jj]==0)
				if (NavYes) {
					nameimg = "i";
					nameimg += ii+(7-jj)*8;
					window.document.eval(nameimg).src = BoardPic[(ii+jj+1)%2].src;
				} else {
					window.document.images[ii+(7-jj)*8].src = BoardPic[(ii+jj+1)%2].src;
				}
			else
				if (NavYes) {
					nameimg = "i";
					nameimg += ii+(7-jj)*8;
					window.document.eval(nameimg).src = PiecePic[(1-sign(Board[ii][jj]))/2][Math.abs(Board[ii][jj])-1][(ii+jj+1)%2].src;
	} else {
					window.document.images[ii+(7-jj)*8].src = PiecePic[(1-sign(Board[ii][jj]))/2][Math.abs(Board[ii][jj])-1][(ii+jj+1)%2].src;
				}
		}
	}
}

function BoardClick(nn)
{ var ii0, jj0, ii1, jj1, mm, nnn;
	if (! isRecording) return;
	if (MoveCount==MaxMove) return;
	if (isRotated) nnn=63-nn;
	else nnn=nn;
	if (BoardClicked==nnn) { BoardClicked=-1; return; }
	if (BoardClicked<0) 
	{ ii0=nnn%8;
		jj0=7-(nnn-ii0)/8;
		if (sign(Board[ii0][jj0])!=((MoveCount+1)%2)*2-1) return;
		BoardClicked=nnn; 
		return; 
	} 
	ii0=BoardClicked%8;
	jj0=7-(BoardClicked-ii0)/8;
	ii1=nnn%8;
	jj1=7-(nnn-ii1)/8;
	if (Math.abs(Board[ii0][jj0])==6) mm="";
	else mm=PieceName.charAt(Math.abs(Board[ii0][jj0])-1);
	if (Board[ii1][jj1]!=0)
	{ if (mm=="") mm+=String.fromCharCode(ii0+97)+"x";
		else mm+="x";
	}
	else
	{ if ((mm=="")&&(ii0!=ii1)) { BoardClicked=-1; return; }
	}
	BoardClicked=-1;
	mm+=String.fromCharCode(ii1+97)+eval(jj1+1);
	if (Math.abs(Board[ii0][jj0])==1)
	{ if (PiecePosY[MoveType][0]==jj1)
		{ if (PiecePosX[MoveType][0]+2==ii1) mm="O-O";
			if (PiecePosX[MoveType][0]-2==ii1) mm="O-O-O";
		}	
	}		 
	var pp, ffst=0, ssearch, ssub;
	if ((window.document.BoardForm)&&(document.BoardForm.PgnMoveText))
		ShortPgnMoveText=Uncomment(document.BoardForm.PgnMoveText.value);
	ssearch=eval(Math.floor(MoveCount/2)+1)+".";
	ffst=ShortPgnMoveText.indexOf(ssearch);
	if (ffst>=0)
		ssub=ShortPgnMoveText.substring(0, ffst);
	else
		ssub=ShortPgnMoveText; 
	if ((jj1==(1-MoveType)*7)&&(Math.abs(Board[ii0][jj0])==6)&&(Math.abs(jj0-jj1)<=1)&&(Math.abs(ii0-ii1)<=1))
	{ pp=0;
		while(pp==0)
		{ if (pp==0) { if (confirm(PieceName.charAt(1)+" ?")) pp=1; }
			if (pp==0) { if (confirm(PieceName.charAt(2)+" ?")) pp=2; }
			if (pp==0) { if (confirm(PieceName.charAt(3)+" ?")) pp=3; }
			if (pp==0) { if (confirm(PieceName.charAt(4)+" ?")) pp=4; }						
		}
		mm=mm+"="+PieceName.charAt(pp);
	}
	pp=ParseMove(mm, false);
	if (pp==0) return;
	if (pp>1)
	{ mm=mm.substr(0,1)+String.fromCharCode(ii0+97)+mm.substr(1,11);
		if (ParseMove(mm, false)==1)
			ParseMove(mm, true);
		else
		{ mm=mm.substr(0,1)+eval(jj0+1)+mm.substr(2,11);
			if (ParseMove(mm, false)==1)
				ParseMove(mm, true);
			else
			{ mm=mm.substr(0,1)+String.fromCharCode(ii0+97)+eval(jj0+1)+mm.substr(2,11);
				ParseMove(mm, true);
			}	
		}	
	}
	else ParseMove(mm,true);
	if (IsCheck(PiecePosX[1-MoveType][0], PiecePosY[1-MoveType][0], 1-MoveType)) mm+="+";
	if (MoveType==0)
	{ HistMove[MoveCount-StartMove]=Math.floor((MoveCount+2)/2)+"."+mm;
		ssub+=Math.floor((MoveCount+2)/2)+".";
	}	
	else
	{ HistMove[MoveCount-StartMove]=Math.floor((MoveCount+2)/2)+". ... "+mm;
		if (MoveCount==StartMove) ssub+=Math.floor((MoveCount+2)/2)+". ... ";
		else ssub+=HistMove[MoveCount-StartMove-1]+" ";
	}
	MoveCount++;
	MoveType=1-MoveType;
	SetPgnMoveText(ssub+mm+" ");
	if (window.document.BoardForm)
	{ // window.document.BoardForm.Position.value=HistMove[MoveCount-StartMove-1]; // GIULIANO
		RefreshBoard();		
	}

}

function RotateBoard(bb)
{ isRotated=bb;
	if ((window.document.BoardForm)&&(document.BoardForm.Rotated))
		document.BoardForm.Rotated.checked=bb;
	RefreshBoard();
}
function AllowRecording(bb)
{ if ((window.document.BoardForm)&&(document.BoardForm.Recording))
		document.BoardForm.Recording.checked=bb;
	isRecording=bb;
}
function SetPgnMoveText(ss)
{ if ((window.document.BoardForm)&&(document.BoardForm.PgnMoveText))
		document.BoardForm.PgnMoveText.value=ss;
	else
		ShortPgnMoveText=ss;
}

function ApplySAN(ss)
{ if (ss.length<6)
	{ PieceName = "KQRBNP";
		if ((window.document.BoardForm)&&(document.BoardForm.SAN))
			document.BoardForm.SAN.value=PieceName;
	}
	else
	{ PieceName = ss;
		if ((window.document.BoardForm)&&(document.BoardForm.SAN))
			document.BoardForm.SAN.value=ss;
	}
}

function ApplyFEN(ss)
{ if (ss.length==0)
	{ FenString = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
		if ((window.document.BoardForm)&&(document.BoardForm.FEN))
			document.BoardForm.FEN.value=FenString;
	}
	else
	{ FenString = ss;
		if ((BoardForm)&&(document.BoardForm.FEN))
			document.BoardForm.FEN.value=ss;
	}
}

function GetFEN()
{ var ii, jj, ee, ss="";
	for (jj=7; jj>=0; jj--)
	{ ee=0;
		for (ii=0; ii<8; ii++)
		{ if (Board[ii][jj]==0) ee++;
			else
			{ if (ee>0)
				{ ss=ss+""+ee;
					ee=0;
				}
				if (Board[ii][jj]>0) 
					ss=ss+PieceName.toUpperCase().charAt(Board[ii][jj]-1);
				else
					ss=ss+PieceName.toLowerCase().charAt(-Board[ii][jj]-1);
			}
		}
		if (ee>0) ss=ss+""+ee;
		if (jj>0) ss=ss+"/";
	}
	if (MoveType==0) ss=ss+" w";
	else ss=ss+" b";
	ee="";
	if ((Castling[0][0]>0)&&(PieceMoves[0][0]==0))
	{ for (ii=0; ii<16; ii++)
		{ if ((PieceType[0][ii]==2)&&(PiecePosX[0][ii]==7)&&(PiecePosY[0][ii]==0))
			ee=ee+PieceName.toUpperCase().charAt(0);
		}
	}
	if ((Castling[0][1]>0)&&(PieceMoves[0][0]==0))
	{ for (ii=0; ii<16; ii++)
		{ if ((PieceType[0][ii]==2)&&(PiecePosX[0][ii]==0)&&(PiecePosY[0][ii]==0))
			ee=ee+PieceName.toUpperCase().charAt(1);
		}
	}
	if ((Castling[1][0]>0)&&(PieceMoves[1][0]==0))
	{ for (ii=0; ii<16; ii++)
		{ if ((PieceType[1][ii]==2)&&(PiecePosX[1][ii]==7)&&(PiecePosY[1][ii]==7))
			ee=ee+PieceName.toLowerCase().charAt(0);
		}
	}
	if ((Castling[1][1]>0)&&(PieceMoves[1][0]==0))
	{ for (ii=0; ii<16; ii++)
		{ if ((PieceType[1][ii]==2)&&(PiecePosX[1][ii]==0)&&(PiecePosY[1][ii]==7))
			ee=ee+PieceName.toLowerCase().charAt(1);
		}
	}
	if (ee=="") ss=ss+" -";
	else ss=ss+" "+ee;
	if (MoveCount>StartMove)
	{ CanPass=-1;
		ii=HistPiece[0][MoveCount-StartMove-1];
		if ((HistType[0][MoveCount-StartMove-1]==5)&&(Math.abs(HistPosY[0][MoveCount-StartMove-1]-PiecePosY[1-MoveType][ii])==2))
			CanPass=PiecePosX[1-MoveType][ii];
	}
	else
		CanPass=EnPass;
	if (CanPass>=0)
	{ ss=ss+" "+String.fromCharCode(97+CanPass);
		if (MoveType==0) ss=ss+"6";
		else ss=ss+"3";
	}
	else ss=ss+" -";
	ss=ss+" "+HalfMove[MoveCount-StartMove];
	ss=ss+" "+Math.floor((MoveCount+2)/2);
	if ((window.document.BoardForm)&&(document.BoardForm.FEN))
		document.BoardForm.FEN.value=ss;
	return(ss);
}

