// LT-PGN-VIEWER 1.63 by Lutz Tautenhahn (2001-2003)
// Site de l'auteur: http://www.lutanho.net/pgn/pgnviewer.html
// Code francisé adapté et simplifié par Normand Lamoureux en 2005.
// Code retouché le 6 septembre 2006 pour tolérer une image placée avant le JS.
// Code retouché le 17 septembre 2006 pour partir d'une position donnée autre que la position initiale.

var i, j, s, StartMove, MoveCount, MoveType, CanPass, EnPass, MaxMove=500, ShortPgnMoveText="";
var ImgPath="../img/pgnviewer/";

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 AutoPlayInterval, isAutoPlay=false, Delay=1000;
var PieceName = "RDTFCP";
var FenString = "tcfdrfct/pppppppp/8/8/8/8/PPPPPPPP/TCFDRFCT w RDrd - 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 = ImgPath+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 = ImgPath+ColorName[i]+"r"+ColorName[j]+".gif";
    PiecePic[i][1][j] = new Image(); PiecePic[i][1][j].src = ImgPath+ColorName[i]+"d"+ColorName[j]+".gif";
    PiecePic[i][2][j] = new Image(); PiecePic[i][2][j].src = ImgPath+ColorName[i]+"t"+ColorName[j]+".gif";
    PiecePic[i][3][j] = new Image(); PiecePic[i][3][j].src = ImgPath+ColorName[i]+"f"+ColorName[j]+".gif";
    PiecePic[i][4][j] = new Image(); PiecePic[i][4][j].src = ImgPath+ColorName[i]+"c"+ColorName[j]+".gif";
    PiecePic[i][5][j] = new Image(); PiecePic[i][5][j].src = ImgPath+ColorName[i]+"p"+ColorName[j]+".gif";
  }
}

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

function Init(rr)
{ var cc, ii, jj, kk, ll, nn, mm;
  if (rr!='')
    FenString=rr;
  if (FenString=='standard')
    FenString="tcfdrfct/pppppppp/8/8/8/8/PPPPPPPP/TCFDRFCT w RDrd - 0 1";
  if (FenString == "tcfdrfct/pppppppp/8/8/8/8/PPPPPPPP/TCFDRFCT w RDrd - 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;
    if (document.BoardForm)
    { RefreshBoard();
      if (document.BoardForm.Position)
        document.BoardForm.Position.value="";
    }
    StartMove=0;
    MoveCount=StartMove;
    MoveType=StartMove%2;
/*    SetBoardClicked(-1);  */
    RecordCount=0;
    CurVar=0;
/*    MoveArray.length=0;  */
/*    if (TargetDocument) HighlightMove("m"+MoveCount+"v"+CurVar);  */
/*    UpdateAnnotation(true);  */
  }
  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.charCodeAt(0)==PieceName.toUpperCase().charCodeAt(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.charCodeAt(0)==PieceName.toLowerCase().charCodeAt(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.charCodeAt(0)==PieceName.toUpperCase().charCodeAt(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.charCodeAt(0)==PieceName.toLowerCase().charCodeAt(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)
    { FenString+=" w ";
      FenString+=PieceName.toUpperCase().charAt(0);
      FenString+=PieceName.toUpperCase().charAt(1);
      FenString+=PieceName.toLowerCase().charAt(0);
      FenString+=PieceName.toLowerCase().charAt(1);
      FenString+=" - 0 1";
      ll++;
    }
//    { 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.charCodeAt(0)==PieceName.toUpperCase().charCodeAt(0))
        Castling[0][0]=1;
      if (cc.charCodeAt(0)==PieceName.toUpperCase().charCodeAt(1))
        Castling[0][1]=1;
      if (cc.charCodeAt(0)==PieceName.toLowerCase().charCodeAt(0))
        Castling[1][0]=1;
      if (cc.charCodeAt(0)==PieceName.toLowerCase().charCodeAt(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++)
      { if (PieceType[ii][jj]!=-1)
          Board[PiecePosX[ii][jj]][PiecePosY[ii][jj]]=(PieceType[ii][jj]+1)*(1-2*ii);
      }
    }
    if (document.BoardForm)
    { RefreshBoard();
      if (document.BoardForm.Position)
      { if (StartMove%2==0) document.BoardForm.Position.value="white to move";
        else document.BoardForm.Position.value="black to move";
      }
    }
    MoveCount=StartMove;
    MoveType=StartMove%2;
/*    SetBoardClicked(-1);  */
    RecordCount=0;
    CurVar=0;
/*    MoveArray.length=0;  */
/*    if (TargetDocument) HighlightMove("m"+MoveCount+"v"+CurVar);  */
/*    UpdateAnnotation(true);  */
  }
}

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)
      window.document.BoardForm.Position.value=HistMove[MoveCount-StartMove-1];
    else
      window.document.BoardForm.Position.value="";
  }
}

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);
  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!="")
      window.document.BoardForm.Position.value=HistMove[MoveCount-StartMove-1];
    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.charCodeAt(0)==PieceName.charCodeAt(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.charCodeAt(ii+1)==PieceName.charCodeAt(1)) ttype1=1;
    if (mm.charCodeAt(ii+1)==PieceName.charCodeAt(2)) ttype1=2;
    if (mm.charCodeAt(ii+1)==PieceName.charCodeAt(3)) ttype1=3;
    if (mm.charCodeAt(ii+1)==PieceName.charCodeAt(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;
  for (ii=0; ii<8; ii++)
  { for (jj=0; jj<8; jj++)
    { if (Board[ii][jj]==0)
      {
      window.document.images[2+ii+(7-jj)*8].src = BoardPic[(ii+jj+1)%2].src; // Ajouter avant ii le nombre d'élément <img> placé avant le js.
      }
    }
  }
  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]);
      window.document.images[kk+2].src = PiecePic[ii][PieceType[ii][jj]][(kk+Math.floor(kk/8))%2].src; // Ajouter après kk le nombre d'élément <img> placé avant le js.
      }
    }
  }
  return;
}

function SetPgnMoveText(ss)
{ if ((window.document.BoardForm)&&(document.BoardForm.PgnMoveText))
    document.BoardForm.PgnMoveText.value=ss;
  else
    ShortPgnMoveText=ss;
}

document.write('<form name="BoardForm" action="#"><div style="line-height:0; margin:0 auto; border:1px solid #000; border-width:1px 0 0 1px; width:208px;"><img src="../img/pgnviewer/btw.gif" /><img src="../img/pgnviewer/bcb.gif" /><img src="../img/pgnviewer/bfw.gif" /><img src="../img/pgnviewer/bdb.gif" /><img src="../img/pgnviewer/brw.gif" /><img src="../img/pgnviewer/bfb.gif" /><img src="../img/pgnviewer/bcw.gif" /><img src="../img/pgnviewer/btb.gif" /><br /><img src="../img/pgnviewer/bpb.gif" /><img src="../img/pgnviewer/bpw.gif" /><img src="../img/pgnviewer/bpb.gif" /><img src="../img/pgnviewer/bpw.gif" /><img src="../img/pgnviewer/bpb.gif" /><img src="../img/pgnviewer/bpw.gif" /><img src="../img/pgnviewer/bpb.gif" /><img src="../img/pgnviewer/bpw.gif" /><br /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/b.gif" /><br /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/bqw.gif" /><img src="../img/pgnviewer/bkb.gif" /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/w.gif" /><br /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/wqb.gif" /><img src="../img/pgnviewer/wkw.gif" /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/b.gif" /><br /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/w.gif" /><img src="../img/pgnviewer/b.gif" /><img src="../img/pgnviewer/w.gif" /><br /><img src="../img/pgnviewer/wpw.gif" /><img src="../img/pgnviewer/wpb.gif" /><img src="../img/pgnviewer/wpw.gif" /><img src="../img/pgnviewer/wpb.gif" /><img src="../img/pgnviewer/wpw.gif" /><img src="../img/pgnviewer/wpb.gif" /><img src="../img/pgnviewer/wpw.gif" /><img src="../img/pgnviewer/wpb.gif" /><br /><img src="../img/pgnviewer/wtb.gif" /><img src="../img/pgnviewer/wcw.gif" /><img src="../img/pgnviewer/wfb.gif" /><img src="../img/pgnviewer/wdw.gif" /><img src="../img/pgnviewer/wrb.gif" /><img src="../img/pgnviewer/wfw.gif" /><img src="../img/pgnviewer/wcb.gif" /><img src="../img/pgnviewer/wtw.gif" /></div><div style="padding-top:6px; margin:0 auto; text-align:center; width:208px;"><input type="button" value="I&lt;" style="width:25px" onclick="javascript:Init(\'\')" title="Position initiale" /><input type="button" value="&lt;&lt;" style="width:25px" onclick="javascript:MoveBack(10)" title="Reculer de 10 coups" /><input type="button" value="&lt;" style="width:25px" onclick="javascript:MoveBack(1)" title="Reculer" /><input type="button" value="&gt;" style="width:25px" onclick="javascript:MoveForward(1)" title="Avancer" /><input type="button" value="&gt;&gt;" style="width:25px" onclick="javascript:MoveForward(10)" title="Avancer de 10 coups" /><input type="button" value="&gt;I" style="width:25px" onclick="javascript:MoveForward(1000)" title="Position finale" /><input type="hidden" name="Position" value="" /></div></form>');
