function Move(){this.san="";this.mark="";this.NAG=0;this.variant="";this.comment=""};var vb_idLoad;function pgnParser(){if(!vb_idLoad){var tmp=document.createElement('div');tmp.className="vbidLoad";tmp.setAttribute("id","vb_idLoad");var p=document.createElement('p');p.innerHTML="Paste a PGN chess game:";tmp.appendChild(p);var tx=document.createElement('textarea');tx.setAttribute("id","vb_idPgn");tx.setAttribute("rows","20");tx.setAttribute("cols","60");tmp.appendChild(tx);p=document.createElement('br');tmp.appendChild(p);var b=document.createElement('button');b.innerHTML="Load PGN";b.onclick=function(){loadPGN();document.getElementById('vb_idLoad').style.display='none'};tmp.appendChild(b);b=document.createElement('button');b.innerHTML="Close";b.onclick=function(){document.getElementById('vb_idLoad').style.display='none'};tmp.appendChild(b);document.body.appendChild(tmp);vb_idLoad=tmp}this.pgn=document.getElementById('vb_idPgn').value;this.tags={};this.moves=[];this.in_comm="";this.errors="";this.extract()};pgnParser.prototype.extract=function(){var pgn=this.pgn;var rgx,mtch,i,n,tmp;rgx=/\[([^\]]*)\"\]/g;mtch=pgn.match(rgx);if(mtch){for(i=0,n=mtch.length;i<n;i++){tmp=mtch[i].substring(1,mtch[i].length-1);var fs=tmp.indexOf(" ");var key=tmp.substring(0,fs);var value=tmp.substring(fs+1).replace(/^\"/,"").replace(/\"$/,"");this.tags[key]=value;pgn=pgn.replace(mtch[i],"")}}pgn=pgn.replace(/\s+/g,' ');if((mtch=pgn.match(/1\/2-1\/2\s*$/))||(mtch=pgn.match(/0-1\s*$/))||(mtch=pgn.match(/1-0\s*$/))||(mtch=pgn.match(/\*\s*$/))){pgn=pgn.replace(mtch,"");if(!this.tags['Result'])this.tags['Result']=mtch}rgx=/^\s*\{[^\}]*\}/;mtch=pgn.match(rgx);if(mtch){this.in_comm=String(mtch).replace(/^\s*\{/,"").replace(/\}$/,"");pgn=pgn.replace(mtch,"")}pgn=pgn.replace(/^\s+/,"");var thm=0;var san="";var mark="";var variant="";var comment="";var err="";var rg_move=/^[KQRBN]?[a-h]?[1-8]?x?[a-h][1-8](?:\=?[QRBN])?/;var rg_castle1=/^O-O/;var rg_castle10=/^0-0/;var rg_castle2=/^O-O-O/;var rg_castle20=/^0-0-0/;var rg_marks=/^[+|#|?|!]+/;var err=false;while((pgn.length>0)&&!err){if(mtch=pgn.match(/^\d+\.{1,3}\s*/)){pgn=pgn.replace(mtch,"");if(pgn.length==0){this.errors+=" Incorrectly terminated MOVESECTION (with "+mtch+"). ";break}}if(mtch=pgn.match(rg_move)){tmp=new Move();tmp.san=mtch;this.moves[thm++]=tmp;pgn=pgn.replace(mtch,"");pgn=pgn.replace(/^\s+/,"")}if((mtch=pgn.match(rg_castle2))||(mtch=pgn.match(rg_castle20))||(mtch=pgn.match(rg_castle1))||(mtch=pgn.match(rg_castle10))){tmp=new Move();tmp.san=mtch;this.moves[thm++]=tmp;pgn=pgn.replace(mtch,"");pgn=pgn.replace(/^\s+/,"")}if(mtch=pgn.match(rg_marks)){this.moves[thm-1].mark=mtch;pgn=pgn.replace(mtch,"");pgn=pgn.replace(/^\s+/,"")}if(/^\$([0-9]+)/.test(pgn)){this.moves[thm-1].NAG=RegExp.$1;mtch='$'+RegExp.$1;pgn=pgn.replace(mtch,"");pgn=pgn.replace(/^\s+/,"")}if(pgn.charAt(0)=='{'){var par0=1,par1=0,j,n=pgn.length;for(j=1;(j<n)&&(par0!=par1);j++)switch(pgn.charAt(j)){case"}":par1++;break;case"{":par0++;break}if(par0!=par1){this.errors+=" Unmached braces in comment. ";err=true;break}else{var mtch=pgn.substring(0,j);var dpg=mtch.substring(1,j-1);if(/\$\d/.test(dpg)){dpg=dpg.replace(/\ \$1[0-9]*/g,"!");dpg=dpg.replace(/\ \$2[0-9]*/g,"?");dpg=dpg.replace(/\ \$3[0-9]*/g,"!!");dpg=dpg.replace(/\ \$4[0-9]*/g,"??");dpg=dpg.replace(/\ \$5[0-9]*/g,"!?");dpg=dpg.replace(/\ \$6[0-9]*/g,"?!")}this.moves[thm-1].comment+="<br>"+dpg}pgn=pgn.replace(mtch,"");pgn=pgn.replace(/^\s+/,"")}if(pgn.charAt(0)=='('){var par0=1,par1=0,j,n=pgn.length;for(j=1;(j<n)&&(par0!=par1);j++)switch(pgn.charAt(j)){case")":par1++;break;case"(":par0++;break}if(par0!=par1){this.errors+=" Unmached parantheses in variant. ";err=true;break}else{var mtch=pgn.substring(0,j);var dpg=mtch.substring(1,j-1).replace(/[\{\}]/g,"");if(/\$\d/.test(dpg)){dpg=dpg.replace(/\ \$1[0-9]*/g,"!");dpg=dpg.replace(/\ \$2[0-9]*/g,"?");dpg=dpg.replace(/\ \$3[0-9]*/g,"!!");dpg=dpg.replace(/\ \$4[0-9]*/g,"??");dpg=dpg.replace(/\ \$5[0-9]*/g,"!?");dpg=dpg.replace(/\ \$6[0-9]*/g,"?!")}this.moves[thm-1].variant+="<br>"+dpg}pgn=pgn.replace(mtch,"");pgn=pgn.replace(/^\s+/,"")}if(pgn.charAt(0)==';'){this.errors=' Unaccepted EOL comment. ';err=true}}};var castle=0;var to_move=0;var en_pass=-1;var fifty=0;var nr_move=0;var sq_king=[0,0];var BOARD=new Array(128);var game_hist=new Array();var FEN_STD='rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';var pc_cod={'P':2,'p':3,'N':4,'n':5,'K':6,'k':7,'B':8,'b':9,'R':10,'r':11,'Q':12,'q':13};var pc_char=[,,'P','p','N','n','K','k','B','b','R','r','Q','q'];function setBOARD(fen){for(var i=0;i<128;i++)BOARD[i]=0;var recs=fen.split(/\s+/);var board=recs[0].split(/\x2f/).reverse().join('');var ofs=0;board.replace(/[prbnqk]|[1-8]/ig,function(x){if(x<='8')ofs+=x-0;else{var sq=((ofs>>3)<<4)|(ofs&7);BOARD[sq]=pc_cod[x];if(x=='K')sq_king[0]=sq;else if(x=='k')sq_king[1]=sq;ofs++}});to_move=(/^w$/i.test(recs[1]))?0:1;castle=0;en_pass=-1;var roq=recs[2].split('');for(var i=0,n=roq.length;i<n;i++){switch(roq[i]){case'K':castle|=1;break;case'Q':castle|=2;break;case'k':castle|=4;break;case'q':castle|=8}}var ep=recs[3]||"-";if(ep!="-")en_pass=(ep.charCodeAt(0)-97)+16*(ep.charCodeAt(1)-49);fifty=recs[4]||0;nr_move=recs[5]||0;gh_top=0};function getFEN(){var a=[];for(var row=7;row>=0;row--){var str="",empty=0;for(var col=0;col<8;col++){var pc=BOARD[(row<<4)|col];if(pc>0){if(empty)str+=empty;empty=0;str+=pc_char[pc]}else{empty++}}if(empty)str+=empty;a.push(str)}var pos=a.join("/");a=[pos];a[1]=to_move?"b":"w";var q="";if(castle){if(castle&1)q+='K';if(castle&2)q+='Q';if(castle&4)q+='k';if(castle&8)q+='q'}else q="-";a[2]=q;a[3]=en_pass>0?(String.fromCharCode(97+(en_pass&7))+((en_pass>>4)+1)):"-";a[4]=fifty;a[5]=to_move?nr_move-1:nr_move;return a.join(" ")};function dumpBOARD(){var fen=getFEN();var a=fen.split(/ /);fen=a[0].replace(/([1-8])/g,function(s){var h='';for(var i=0;i<s;i++)h+=" ";return h}).replace(/(\w| )/g,function(s){return"|"+s}).replace(/\x2f/g,'|<br>|-+-+-+-+-+-+-+-|<br>');fen+='|';return"+-+-+-+-+-+-+-+-+<br>"+fen+"<br>+-+-+-+-+-+-+-+-+"};var offs={6:[-17,-16,-15,-1,1,15,16,17],12:[-17,-16,-15,-1,1,15,16,17],10:[-16,-1,1,16],8:[-17,-15,15,17],4:[-33,-31,-18,-14,14,18,31,33]};var offs_n=[-33,-31,-18,-14,14,18,31,33];var offs_k=[-17,-16,-15,-1,1,15,16,17];var offs_r=[-16,-1,1,16];var offs_b=[-17,-15,15,17];function isAttacked(sq,side){var sq1,dir;var knight=4,rook=10,queen=12,bishop=8,king=6;if(!side){sq1=sq-15;if(!(sq1&0x88)&&(BOARD[sq1]==2))return 1;sq1-=2;if(!(sq1&0x88)&&(BOARD[sq1]==2))return 1}else{sq1=sq+15;if(!(sq1&0x88)&&(BOARD[sq1]==3))return 1;sq1+=2;if(!(sq1&0x88)&&(BOARD[sq1]==3))return 1;knight++;rook++;queen++;bishop++;king++}for(dir=0;dir<8;dir++){sq1=sq+offs_n[dir];if(!(sq1&0x88)&&(BOARD[sq1]==knight))return 1;sq1=sq+offs_k[dir];if(!(sq1&0x88)&&(BOARD[sq1]==king))return 1}for(dir=0;dir<4;dir++){var step=offs_r[dir];sq1=sq+step;while(!(sq1&0x88)){var p=BOARD[sq1];if(p>0){if((p==rook)||(p==queen))return 1;break}sq1+=step}step=offs_b[dir];sq1=sq+step;while(!(sq1&0x88)){var p=BOARD[sq1];if(p>0){if((p==bishop)||(p==queen))return 1;break}sq1+=step}}return 0};function gen_moves(moves){var ocsq=[];for(var i=0;i<120;i++)if(!(i&0x88)&&BOARD[i]&&((BOARD[i]&1)==to_move))ocsq.push(i);if(!to_move){if((castle&3)&&!isAttacked(4,1)){if((castle&1)&&!(BOARD[5]||BOARD[6]||isAttacked(5,1)||isAttacked(6,1)))moves.push(0x01040660);if((castle&2)&&!(BOARD[3]||BOARD[2]||BOARD[1]||isAttacked(3,1)||isAttacked(2,1)))moves.push(0x02040260)}}else{if((castle&0x0C)&&!isAttacked(0x74,0)){if((castle&4)&&!(BOARD[0x75]||BOARD[0x76]||isAttacked(0x75,0)||isAttacked(0x76,0)))moves.push(0x01747670);if((castle&8)&&!(BOARD[0x73]||BOARD[0x72]||BOARD[0x71]||isAttacked(0x73,0)||isAttacked(0x72,0)))moves.push(0x02747270)}}for(var i=0,np=ocsq.length;i<np;i++){var sq=ocsq[i];var p=BOARD[sq];if(p==(2|to_move)){if(!to_move){if(sq<0x60){if(!BOARD[sq+16]){moves.push(0x06000020|(((sq<<8)|(sq+16))<<8))}if((sq<0x18)&&!BOARD[sq+16]&&!BOARD[sq+32]){moves.push(0x0E000020|(((sq<<8)|(sq+32))<<8))}}else{if(!BOARD[sq+16]){var frto=((sq<<8)|(sq+16))<<8;moves.push(0x0C000020|frto,0x0A000020|frto,0x08000020|frto,0x04000020|frto)}}for(var j=15;j<=17;j+=2){var sq1=sq+j;if(!(sq1&0x88)){var p1=BOARD[sq1];if(en_pass==sq1){moves.push(0x0F000023|(((sq<<8)|sq1)<<8))}else if(p1&1){if(sq>=0x60){var frto=((sq<<8)|sq1)<<8;moves.push(0x0C000020|frto|p1,0x0A000020|frto|p1,0x08000020|frto|p1,0x04000020|frto|p1)}else{moves.push(0x06000020|(((sq<<8)|sq1)<<8)|p1)}}}}}else{if(sq>=0x20){if(!BOARD[sq-16]){moves.push(0x06000030|(((sq<<8)|(sq-16))<<8))}if((sq>=0x60)&&!BOARD[sq-16]&&!BOARD[sq-32]){moves.push(0x0E000030|(((sq<<8)|(sq-32))<<8))}}else{if(!BOARD[sq-16]){var frto=((sq<<8)|(sq-16))<<8;moves.push(0x0D000030|frto,0x0B000030|frto,0x09000030|frto,0x05000030|frto)}}for(var j=15;j<=17;j+=2){var sq1=sq-j;if(!(sq1&0x88)){var p1=BOARD[sq1];if(en_pass==sq1){moves.push(0x0F000032|(((sq<<8)|sq1)<<8))}else if((p1>0)&&!(p1&1)){if(sq<0x20){var frto=((sq<<8)|sq1)<<8;moves.push(0x0D000030|frto|p1,0x0B000030|frto|p1,0x09000030|frto|p1,0x05000030|frto|p1)}else{moves.push(0x06000030|(((sq<<8)|sq1)<<8)|p1)}}}}}}else{var ofdir=offs[p-to_move];for(var d=0,nd=ofdir.length;d<nd;d++){var sq1=sq;for(;;){sq1+=ofdir[d];if(sq1&0x88)break;if((p==(6|to_move))&&isAttacked(sq1,to_move^1))break;var p1=BOARD[sq1];if(!p1){moves.push(0x03000000|(((sq<<8)|sq1)<<8)|(p<<4))}else{if((!to_move&&(p1&1))||(to_move&&!(p1&1))){moves.push(0x07000000|(((sq<<8)|sq1)<<8)|((p<<4)|p1));break}else break}if(!(p&8))break}}}}};function makemove(move){var spec=(move>>24)&0x0F;var from=(move>>>16)&0xFF;var to=(move>>>8)&0xFF;var p=BOARD[from],p1=BOARD[to];if((p1==6)||(p1==7))return 0;if((spec!=0x01)&&(spec!=0x02)){BOARD[to]=p;BOARD[from]=0;if(spec==0x0F)BOARD[to+(to_move?16:-16)]=0;var sqk=(p==(6|to_move))?to:sq_king[to_move];if(isAttacked(sqk,to_move^1)){BOARD[from]=p;BOARD[to]=p1;if(spec==0x0F){if(to_move)BOARD[to+16]=2;else BOARD[to-16]=3}return 0}BOARD[from]=p;BOARD[to]=p1}game_hist[gh_top++]=[from,to,p,p1,castle,en_pass,fifty,spec];if(en_pass>0)en_pass=-1;switch(spec){case 0x01:if(!to_move){BOARD[5]=10;BOARD[6]=6;BOARD[7]=BOARD[4]=0}else{BOARD[0x75]=11;BOARD[0x76]=7;BOARD[0x77]=BOARD[0x74]=0}fifty++;break;case 0x02:if(!to_move){BOARD[3]=10;BOARD[2]=6;BOARD[0]=BOARD[4]=0}else{BOARD[0x73]=11;BOARD[0x72]=7;BOARD[0x70]=BOARD[0x74]=0}fifty++;break;case 0x03:BOARD[to]=p;BOARD[from]=0;fifty++;break;case 0x07:case 0x06:BOARD[to]=p;BOARD[from]=0;fifty=0;break;case 0x0C:case 0x0A:case 0x08:case 0x04:case 0x0D:case 0x0B:case 0x09:case 0x05:BOARD[to]=spec;BOARD[from]=0;fifty=0;break;case 0x0F:if(!to_move)BOARD[to-16]=0;else BOARD[to+16]=0;BOARD[to]=p;BOARD[from]=0;fifty=0;break;case 0x0E:BOARD[to]=p;BOARD[from]=0;en_pass=to_move?to+16:to-16;fifty=0;break}switch(p){case 6:if(castle&3)castle&=0x0C;sq_king[0]=to;break;case 7:if(castle&0x0C)castle&=3;sq_king[1]=to;break;case 10:if((castle&1)&&(from==7))castle^=1;else{if((castle&2)&&(from==0))castle^=2}break;case 11:if((castle&4)&&(from==0x77))castle^=4;else{if((castle&8)&&(from==0x70))castle^=8}break}switch(p1){case 10:if((castle&1)&&(to==7))castle^=1;else{if((castle&2)&&(to==0))castle^=2}break;case 11:if((castle&4)&&(to==0x77))castle^=4;else{if((castle&8)&&(to==0x70))castle^=8}break}to_move^=1;if(to_move)++nr_move;return 1};function takeback(){var move=game_hist[--gh_top];castle=move[4];en_pass=move[5];fifty=move[6];var p=move[2];if(p==6)sq_king[0]=move[0];else{if(p==7)sq_king[1]=move[0]}BOARD[move[0]]=p;BOARD[move[1]]=move[3];to_move^=1;switch(move[7]){case 1:if(!to_move){BOARD[7]=10;BOARD[5]=0}else{BOARD[0x77]=11;BOARD[0x75]=0}break;case 2:if(!to_move){BOARD[0]=10;BOARD[3]=0}else{BOARD[0x70]=11;BOARD[0x73]=0}break;case 0x0F:if(!to_move)BOARD[move[1]-16]=3;else BOARD[move[1]+16]=2;break}if(!to_move)--nr_move};function validate(move){var globmoves=[];gen_moves(globmoves);for(var i=0,n=globmoves.length;i<n;i++){var m=globmoves[i];if(((m>>8)&0xFFFF)==move){if(makemove(m)){takeback();return m}}}return 0};function validatepr(move,prom){var globmoves=[];gen_moves(globmoves);for(var i=0,n=globmoves.length;i<n;i++){var m=globmoves[i];if((((m>>8)&0xFFFF)==move)&&((m>>24)==prom)){if(makemove(m)){takeback();return m}}}return 0};var a_h=['a','b','c','d','e','f','g','h'];var playAt=1;var repeat;var REVERSE=false;var tpgn;var black_in;var PIECE_CLASS_NAMES={p:"pawn",n:"knight",b:"bishop",r:"rook",q:"queen",k:"king",P:"pawn",N:"knight",B:"bishop",R:"rook",Q:"queen",K:"king"};var SPACES={1:" ",2:"  ",3:"   ",4:"    ",5:"     ",6:"      ",7:"       ",8:"        "};var MARK={'+':'Check','#':'Checkmate','!':'good move (traditional "!")','?':'poor move (traditional "?")','!!':'very good move (traditional "!!")','??':'very poor move (traditional "??")','!?':'speculative move (traditional "!?")','?!':'questionable move (traditional "?!")'};var LAST_POS=null;var LAST_POS_MOVE;var tb_movs={'N':Knight_Moves,'B':Bishop_Moves,'R':Rook_Moves,'Q':Queen_Moves,'K':King_Moves};function delClass(el,className){if(!(el&&el.className)){return}var cls=el.className.split(" ");var ar=new Array();for(var i=cls.length;i>0;){if(cls[--i]!=className){ar[ar.length]=cls[i]}}el.className=ar.join(" ")};function addClass(el,className){delClass(el,className);el.className+=" "+className};function doInit(ydi){var tgt=document.getElementById(ydi);var ttable=document.createElement('table');ttable.setAttribute('border','0');ttable.setAttribute('id','vb_idTable');var thead=document.createElement('thead');var tfoot=document.createElement('tfoot');ttable.appendChild(thead);ttable.appendChild(tfoot);var tbody=document.createElement('tbody');var TR=document.createElement('tr');var TD=document.createElement('td');var BT=document.createElement('button');var row,cel,but;row=TR.cloneNode(true);cel=TD.cloneNode(true);cel.setAttribute('colspan','2');but=BT.cloneNode(true);but.onclick=function(){document.getElementById('vb_idLoad').style.display='block'};but.setAttribute('title',"enter a 'Portable Game Notation' chess game");but.innerHTML="<b>PGN</b>";cel.appendChild(but);but=BT.cloneNode(true);but.onclick=function(){reverse()};but.setAttribute('title',"Reverse board");but.innerHTML="&uarr;&darr;";cel.appendChild(but);but=document.createElement('span');but.innerHTML="       speed:";cel.appendChild(but);var inp=document.createElement('input');inp.setAttribute('id',"f_depth");inp.setAttribute('size','2');inp.setAttribute('title',"Play at MAX 3 seconds; fractions admitted: 0.5");inp.setAttribute('value',playAt);inp.onchange=function(){var f_d=this.value||1;if(f_d<=3)playAt=f_d;else this.value=0.5};cel.appendChild(inp);but=BT.cloneNode(true);but.onclick=function(){playGame()};but.setAttribute('title',"from beginning of the game, or from the selected move");but.innerHTML="Play";cel.appendChild(but);but=BT.cloneNode(true);but.onclick=function(){window.clearInterval(repeat)};but.innerHTML="Stop";cel.appendChild(but);but=document.createElement('span');but.innerHTML="         ";cel.appendChild(but);but=BT.cloneNode(true);but.onclick=function(){prevMove()};but.innerHTML="Prev";cel.appendChild(but);but=BT.cloneNode(true);but.onclick=function(){nextMove()};but.innerHTML="Next";cel.appendChild(but);row.appendChild(cel);tbody.appendChild(row);row=TR.cloneNode(true);cel=TD.cloneNode(true);cel.setAttribute('colspan','2');but=document.createElement('p');but.setAttribute('id',"fen");but.setAttribute('class',"vbidFen");but.setAttribute('title',"FEN");cel.appendChild(but);row.appendChild(cel);tbody.appendChild(row);row=TR.cloneNode(true);cel=TD.cloneNode(true);cel.setAttribute('class','board-cont');var div=document.createElement('div');div.setAttribute('id',"container");div.setAttribute('class',"ChessBoard");cel.appendChild(div);row.appendChild(cel);cel=TD.cloneNode(true);div=document.createElement('div');div.setAttribute('id',"moves");cel.appendChild(div);row.appendChild(cel);tbody.appendChild(row);row=TR.cloneNode(true);cel=TD.cloneNode(true);cel.setAttribute('colspan','2');div=document.createElement('div');div.setAttribute('id',"infog");cel.appendChild(div);row.appendChild(cel);tbody.appendChild(row);row=TR.cloneNode(true);cel=TD.cloneNode(true);cel.setAttribute('colspan','2');div=document.createElement('div');div.setAttribute('id',"stats");cel.appendChild(div);row.appendChild(cel);tbody.appendChild(row);ttable.appendChild(tbody);tgt.appendChild(ttable);tpgn=new pgnParser();init()};function init(){var cont=document.getElementById("container");var html=["<div class='ChessFields'>"];for(var row=0;row<8;row++){for(var col=0;col<8;col++){var colID=String.fromCharCode(65+col);var rowID=row+1;var id=colID+rowID;var fieldColor=(row+col)&1?"WhiteField":"BlackField";html.push("<div chessField='",id,"' class='Field ",id," Row-",rowID," Col-",colID," ",fieldColor,"'></div>")}}for(var i=0;i<8;i++){rowID=i+1;colID=String.fromCharCode(65+i);html.push("<div class='Notation-Row Row-",rowID,"'>",rowID,"</div>");html.push("<div class='Notation-Col Col-",colID,"'>",colID.toLowerCase(),"</div>")}html.push("</div>");cont.innerHTML=html.join("");fen=tpgn.tags['FEN']||FEN_STD;setBOARD(fen);initPos(fen);black_in=0;if(to_move){var mdiv=document.getElementById("moves");var tmp=document.createElement("a");tmp.className="oneMove";tmp.href="#";tmp.className+=" whiteMove";tmp.innerHTML=""+nr_move+".  ... ";mdiv.appendChild(tmp);black_in=1}init_moves(tpgn.moves);if(tpgn.tags['White'])document.getElementById('infog').innerHTML="<b>"+tpgn.tags['White']+" - "+tpgn.tags['Black']+"</b> "+tpgn.tags['Result']+" ("+(tpgn.tags['Date'].match(/\d+/)||"")+")"};function init_moves(movs){for(var i=0,n=movs.length;i<n;i++){var u=san_to_coord(movs[i].san);if(u){makemove(u);addToHistory(moveToNotation(u))}else{alert("Residual error: wrong move in PGN (misapprehension).");break}}};function fr_to(m){var from=ah_88[m[0]];var prom;if(m[1].length==3){prom=m[1].charAt(2);prom=pc_cod[prom];if(to_move)prom++;m[1]=m[1].replace(/.$/,"")}var to=ah_88[m[1]];var move=(from<<8)|to;if(prom)return validatepr(move,prom);return validate(move)};function san_to_coord(san){var m=[0,0];var san=String(san);if((san=='O-O-O')||(san=='0-0-0'))return to_move?fr_to(['e8','c8']):fr_to(['e1','c1']);if((san=='O-O')||(san=='0-0'))return to_move?fr_to(['e8','g8']):fr_to(['e1','g1']);var reprom=/=?[QRBN]$/;var recapt=/x$/i;var prom;var capt=false;var mtch;var piece;if(mtch=san.match(reprom)){prom=san.charAt(san.length-1);san=san.replace(mtch,"")}m[1]=san.substr(san.length-2,2);san=san.replace(/..$/,"");if(mtch=san.match(recapt)){capt=true;san=san.replace(mtch,"")}if(prom&&capt){m[0]=san.charAt(0)+(m[1].charAt(1)=='8'?'7':'2');m[1]+=prom;return fr_to(m)}else{if(prom){m[0]=m[1].charAt(0)+(m[1].charAt(1)=='8'?'7':'2');m[1]+=prom;return fr_to(m)}}var field;var table_moves;if(/^[KQRBN]/.test(san)){piece=san.charAt(0);table_moves=tb_movs[piece];san=san.replace(/^./,"");if(/^[a-h][1-8]/.test(san)){m[0]=san;return fr_to(m)}if(to_move)piece=piece.toLowerCase();piece=pc_cod[piece];if(/^[a-h]$/.test(san)){for(var y=1;y<=8;y++){field=san.charAt(0)+y;if(BOARD[ah_88[field]]==piece){m[0]=field;var u=fr_to(m);if(u)return u}}}if(/^[1-8]$/.test(san)){for(var y=0;y<8;y++){field=a_h[y]+san.charAt(0);if(BOARD[ah_88[field]]==piece){m[0]=field;var u=fr_to(m);if(u)return u}}}for(var c=0;c<8;c++)for(var r=1;r<=8;r++){field=a_h[c]+r;if((BOARD[ah_88[field]]==piece)&&(table_moves[field].indexOf(m[1])!=-1)){m[0]=field;var u=fr_to(m);if(u)return u}}}table_moves=to_move?bla_pawn:whi_pawn;piece=to_move?3:2;var c=capt?san.charAt(0):m[1].charAt(0);for(var r=1;r<=8;r++){field=c+r;if(BOARD[ah_88[field]]==piece){if(table_moves[field].indexOf(m[1])!=-1){m[0]=field;var u=fr_to(m);if(u)return u}}}return 0};function initPos(fen,cont){LAST_POS=fen;document.getElementById("fen").innerHTML=fen;if(!cont)cont=document.getElementById("container");var pos=fen.split(/\s+/)[0];pos=pos.replace(/\x2f/g,"").replace(/[1-8]/g,function(s){return SPACES[s]});cont=cont.firstChild;for(var div=cont.firstChild;div;div=div.nextSibling){var id=div.getAttribute("chessField");if(!id)break;id=id.toUpperCase();var index=(id.charCodeAt(0)-65)+(56-id.charCodeAt(1))*8;var piece=pos.charAt(index);if(piece&&piece!=" "){var color=piece.toUpperCase()==piece;color=color?"white":"black";var pieceClass="Piece Piece-"+color+"-"+PIECE_CLASS_NAMES[piece];div.innerHTML="<div class='"+pieceClass+"'></div>"}else{div.innerHTML=""}}LAST_POS_MOVE=null};function reverse(){REVERSE=!REVERSE;var cont=document.getElementById("container").firstChild;if(REVERSE)addClass(cont,"ReverseFields");else delClass(cont,"ReverseFields")};function moveToNotation(move){var movp=(move>>4)&0x0F;if(movp){if(movp>3)movp=String(pc_char[movp]).toUpperCase();else movp=movp==2?" ":""}else movp='';var prom=(move>>24)&0x0F;if(prom&&(prom==0x04||prom==0x05||((prom>=0x08)&&(prom<=0x0D))))prom=String(pc_char[prom]).toUpperCase();else{if(prom==0x01||prom==0x02)return prom==0x01?"   O-O":" O-O-O";else prom=''}move=(move>>8)&0xffff;var to=move&0xff;var from=(move>>8)&0xff;to=String.fromCharCode(97+(to&7))+String.fromCharCode(49+((to>>4)&7));from=String.fromCharCode(97+(from&7))+String.fromCharCode(49+((from>>4)&7));return movp+from+"-"+to+prom};function addToHistory(move){var mdiv=document.getElementById("moves");var count=mdiv.childNodes.length;var html="";var tmp=document.createElement("a");tmp.className="oneMove";tmp.href="#";tmp.onclick=_go_to_pos;if(count%2==0){if(black_in)html=""+nr_move+". ";else html=""+(nr_move-1)+". ";tmp.className+=" whiteMove"}html+=move;tmp.innerHTML=html;var fen=getFEN();tmp.setAttribute("chessFen",fen);tmp.setAttribute("chessMoveId",count);mdiv.appendChild(tmp)};function _go_to_pos(){var pos=this.getAttribute("chessFen");if(LAST_POS_MOVE!=null){var tmp=document.getElementById("moves").childNodes[LAST_POS_MOVE];delClass(tmp,"oneMoveSel")}initPos(pos);LAST_POS_MOVE=parseInt(this.getAttribute("chessMoveId"));var tmp=document.getElementById("moves").childNodes[LAST_POS_MOVE];addClass(tmp,"oneMoveSel");var lpm=tpgn.moves[LAST_POS_MOVE-black_in];var stats=document.getElementById("stats");var in_st='';if(lpm.mark)in_st+="<p>"+MARK[lpm.mark]+"</p>";if(lpm.NAG)in_st+="<p>"+NAG[lpm.NAG]+"</p>";if(lpm.variant)in_st+="<p>"+lpm.variant+"</p>";in_st+="<p>"+lpm.comment+"</p>";stats.innerHTML=in_st};function nextMove(){if(document.getElementById("moves").childNodes.length>0){if(LAST_POS_MOVE!=null){if(LAST_POS_MOVE<document.getElementById("moves").childNodes.length-1){var a=document.getElementById("moves").childNodes[LAST_POS_MOVE+1];_go_to_pos.call(a)}else{if(repeat)window.clearInterval(repeat)}}}};function prevMove(){if(document.getElementById("moves").childNodes.length>0){if(LAST_POS_MOVE==null)LAST_POS_MOVE=document.getElementById("moves").childNodes.length-1;if(LAST_POS_MOVE>0){var a=document.getElementById("moves").childNodes[LAST_POS_MOVE-1];_go_to_pos.call(a)}}};function loadPGN(){document.getElementById("moves").innerHTML='';tpgn=new pgnParser('idpgn');init(tpgn);if(tpgn.in_comm)document.getElementById("stats").innerHTML=tpgn.in_comm;if(tpgn.errors)document.getElementById("stats").innerHTML="<b>Error</b> in PGN text:<br>"+tpgn.errors};function playGame(){if(!LAST_POS_MOVE)LAST_POS_MOVE=0;repeat=window.setInterval(nextMove,playAt*1000)};
