-
Notifications
You must be signed in to change notification settings - Fork 6
/
pgn4web.min.js
191 lines (191 loc) · 82.3 KB
/
pgn4web.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var pgn4web_version="2.04+",pgn4web_project_url="http://pgn4web.casaschi.net",pgn4web_project_author="Paolo Casaschi",pgn4web_project_email;if(pgn4web_project_email==undefined)pgn4web_project_email="[email protected]";var about="\tpgn4web v"+pgn4web_version+"\n\t"+pgn4web_project_url+"\n",helpWin=null;
function displayHelp(a){a||(a="top");helpWin&&!helpWin.closed&&helpWin.close();helpWin=window.open(detectHelpLocation()+"?"+(Math.floor(900*Math.random())+100)+"#"+a,"pgn4web_help","resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no,status=no");helpWin!==null&&window.focus&&helpWin.window.focus()}function customFunctionOnPgnTextLoad(){}function customFunctionOnPgnGameLoad(){}function customFunctionOnMove(){}function customFunctionOnAlert(){}window.onload=start_pgn4web;
document.onkeydown=handlekey;function start_pgn4web(){if(alertFirstResetLoadingPgn)alertFirstResetLoadingPgn=false;else resetAlert();createBoard();LiveBroadcastDelay>0&&restartLiveBroadcastTimeout()}var alertLog,alertLast,alertNum,alertNumSinceReset,fatalErrorNumSinceReset,alertPromptInterval=null,alertPromptOn=false,alertFirstResetLoadingPgn=true;resetAlert();
function resetAlert(){alertLog=Array(5);alertLast=alertLog.length-1;alertNum=alertNumSinceReset=fatalErrorNumSinceReset=0;stopAlertPrompt();alertFirstResetLoadingPgn||configBoardShrortcut(debugShortcutSquare,"pgn4web v"+pgn4web_version+" debug info","keep")}
function myAlert(a,b){alertNum++;alertNumSinceReset++;b&&fatalErrorNumSinceReset++;alertLast=(alertLast+1)%alertLog.length;alertLog[alertLast]=a;alertPlural=alertNum>1?"s":"";configBoardShrortcut(debugShortcutSquare,"pgn4web v"+pgn4web_version+" debug info, "+alertNum+" alert"+alertPlural,"keep");if(LiveBroadcastDelay===0||LiveBroadcastAlert===true)startAlertPrompt();customFunctionOnAlert(a)}
function startAlertPrompt(){if(!alertPromptOn){alertPromptInterval&&clearTimeout(alertPromptInterval);alertPromptInterval=setTimeout("alertPromptTick(true);",500)}}function stopAlertPrompt(){if(alertPromptInterval){clearTimeout(alertPromptInterval);alertPromptInterval=null}alertPromptOn&&alertPromptTick(false)}
function alertPromptTick(a){if(alertPromptInterval){clearTimeout(alertPromptInterval);alertPromptInterval=null}if(document.getElementById("tcol0trow0")){theObject=document.getElementById("tcol0trow0");theObject.className=alertPromptOn?highlightOption&&(lastColFromHighlighted===0&&lastRowFromHighlighted===7||lastColToHighlighted===0&&lastRowToHighlighted===7)?"highlightWhiteSquare":"whiteSquare":"blackSquare";alertPromptDelay=(alertPromptOn=!alertPromptOn)?500:3E3}else alertPromptDelay=1500;if(a)alertPromptInterval=
setTimeout("alertPromptTick(true);",alertPromptDelay)}function stopKeyPropagation(a){a.cancelBubble=true;a.stopPropagation&&a.stopPropagation();a.preventDefault&&a.preventDefault();return false}var shortcutKeysWereEnabled=false;function disableShortcutKeysAndStoreStatus(){if((shortcutKeysWereEnabled=shortcutKeysEnabled)===true)SetShortcutKeysEnabled(false)}function restoreShortcutKeysStatus(){shortcutKeysWereEnabled===true&&SetShortcutKeysEnabled(true);shortcutKeysWereEnabled=false}
function customShortcutKey_Shift_0(){}function customShortcutKey_Shift_1(){}function customShortcutKey_Shift_2(){}function customShortcutKey_Shift_3(){}function customShortcutKey_Shift_4(){}function customShortcutKey_Shift_5(){}function customShortcutKey_Shift_6(){}function customShortcutKey_Shift_7(){}function customShortcutKey_Shift_8(){}function customShortcutKey_Shift_9(){}var shortcutKeysEnabled=false;
function handlekey(a){var b;if(!a)a=window.event;b=a.keyCode;if(a.altKey||a.ctrlKey||a.metaKey)return true;if(b!=27&&shortcutKeysEnabled===false)return true;switch(b){case 8:case 9:case 16:case 17:case 18:case 32:case 33:case 34:case 35:case 36:case 45:case 46:case 92:case 93:return true;case 27:a.shiftKey?interactivelyToggleShortcutKeys():displayHelp();return stopKeyPropagation(a);case 90:a.shiftKey?window.open(pgn4web_project_url):displayDebugInfo();return stopKeyPropagation(a);case 37:case 74:MoveBackward(1);
return stopKeyPropagation(a);case 38:case 72:GoToMove(StartPly);return stopKeyPropagation(a);case 39:case 75:MoveForward(1);return stopKeyPropagation(a);case 40:case 76:GoToMove(StartPly+PlyNumber);return stopKeyPropagation(a);case 85:MoveToPrevComment();return stopKeyPropagation(a);case 73:MoveToNextComment();return stopKeyPropagation(a);case 83:searchPgnGamePrompt();return stopKeyPropagation(a);case 13:searchPgnGame(lastSearchPgnExpression);return stopKeyPropagation(a);case 65:MoveForward(1);SetAutoPlay(true);
return stopKeyPropagation(a);case 48:a.shiftKey?customShortcutKey_Shift_0():SetAutoPlay(false);return stopKeyPropagation(a);case 49:if(a.shiftKey)customShortcutKey_Shift_1();else{MoveForward(1);SetAutoplayDelay(1E3);SetAutoPlay(true)}return stopKeyPropagation(a);case 50:if(a.shiftKey)customShortcutKey_Shift_2();else{MoveForward(1);SetAutoplayDelay(2E3);SetAutoPlay(true)}return stopKeyPropagation(a);case 51:if(a.shiftKey)customShortcutKey_Shift_3();else{MoveForward(1);SetAutoplayDelay(3E3);SetAutoPlay(true)}return stopKeyPropagation(a);
case 52:if(a.shiftKey)customShortcutKey_Shift_4();else{MoveForward(1);SetAutoplayDelay(4E3);SetAutoPlay(true)}return stopKeyPropagation(a);case 53:if(a.shiftKey)customShortcutKey_Shift_5();else{MoveForward(1);SetAutoplayDelay(5E3);SetAutoPlay(true)}return stopKeyPropagation(a);case 54:if(a.shiftKey)customShortcutKey_Shift_6();else{MoveForward(1);SetAutoplayDelay(6E3);SetAutoPlay(true)}return stopKeyPropagation(a);case 55:if(a.shiftKey)customShortcutKey_Shift_7();else{MoveForward(1);SetAutoplayDelay(7E3);
SetAutoPlay(true)}return stopKeyPropagation(a);case 56:if(a.shiftKey)customShortcutKey_Shift_8();else{MoveForward(1);SetAutoplayDelay(8E3);SetAutoPlay(true)}return stopKeyPropagation(a);case 57:if(a.shiftKey)customShortcutKey_Shift_9();else{MoveForward(1);SetAutoplayDelay(9E3);SetAutoPlay(true)}return stopKeyPropagation(a);case 81:MoveForward(1);SetAutoplayDelay(1E4);SetAutoPlay(true);return stopKeyPropagation(a);case 87:MoveForward(1);SetAutoplayDelay(2E4);SetAutoPlay(true);return stopKeyPropagation(a);
case 69:MoveForward(1);SetAutoplayDelay(3E4);SetAutoPlay(true);return stopKeyPropagation(a);case 82:pauseLiveBroadcast();return stopKeyPropagation(a);case 84:refreshPgnSource();return stopKeyPropagation(a);case 89:resumeLiveBroadcast();return stopKeyPropagation(a);case 70:FlipBoard();return stopKeyPropagation(a);case 71:SetHighlight(!highlightOption);return stopKeyPropagation(a);case 68:IsRotated&&FlipBoard();return stopKeyPropagation(a);case 88:if(numberOfGames>1){currentGame=Math.floor(Math.random()*
numberOfGames);Init();GoToMove(StartPly+Math.floor(Math.random()*(StartPly+PlyNumber+1)))}return stopKeyPropagation(a);case 67:if(numberOfGames>1){currentGame=Math.floor(Math.random()*numberOfGames);Init()}return stopKeyPropagation(a);case 86:if(numberOfGames>1){currentGame=0;Init()}return stopKeyPropagation(a);case 66:if(currentGame>0){currentGame--;Init()}return stopKeyPropagation(a);case 78:if(numberOfGames>currentGame+1){currentGame++;Init()}return stopKeyPropagation(a);case 77:if(numberOfGames>
1){currentGame=numberOfGames-1;Init()}return stopKeyPropagation(a);case 79:SetCommentsOnSeparateLines(!commentsOnSeparateLines);Init();return stopKeyPropagation(a);case 80:SetCommentsIntoMoveText(!commentsIntoMoveText);Init();return stopKeyPropagation(a);default:return true}}boardOnClick=Array(8);boardTitle=Array(8);for(col=0;col<8;col++){boardOnClick[col]=Array(8);boardTitle[col]=Array(8);for(row=0;row<8;row++){boardTitle[col][row]="";boardOnClick[col][row]=function(){}}}
function configBoardShrortcut(a,b,c){if(a.charCodeAt!==null){var e=a.charCodeAt(0)-65;if(!(e<0||e>7)){var d=56-a.charCodeAt(1);if(!(d<0||d>7)){boardTitle[e][d]=b;if(c!="keep")boardOnClick[e][d]=c;if(theObject=document.getElementById("link_tcol"+e+"trow"+d)){if(IsRotated)a=String.fromCharCode(72-e,49+d);squareTitle=boardTitle[e][d]!==""?a+": "+boardTitle[e][d]:a;theObject.title=squareTitle}}}}}debugShortcutSquare="A8";configBoardShrortcut("A8","pgn4web v"+pgn4web_version+" debug info",function(){displayDebugInfo()});
configBoardShrortcut("B8","show this position FEN string",function(){displayFenData()});configBoardShrortcut("C8","show this game PGN source data",function(){displayPgnData(false)});configBoardShrortcut("D8","show full PGN source data",function(){displayPgnData(true)});configBoardShrortcut("E8","search help",function(){displayHelp("search")});configBoardShrortcut("F8","shortcut keys help",function(){displayHelp("keys")});configBoardShrortcut("G8","shortcut squares help",function(){displayHelp("squares")});
configBoardShrortcut("H8","pgn4web help",function(){displayHelp()});configBoardShrortcut("A7","pgn4web website",function(){window.open(pgn4web_project_url)});configBoardShrortcut("B7","toggle show comments in game text",function(){SetCommentsIntoMoveText(!commentsIntoMoveText);thisPly=CurrentPly;Init();GoToMove(thisPly)});configBoardShrortcut("C7","toggle show comments on separate lines in game text",function(){SetCommentsOnSeparateLines(!commentsOnSeparateLines);thisPly=CurrentPly;Init();GoToMove(thisPly)});
configBoardShrortcut("D7","toggle highlight last move",function(){SetHighlight(!highlightOption)});configBoardShrortcut("E7","flip board",function(){FlipBoard()});configBoardShrortcut("F7","show white on bottom",function(){IsRotated&&FlipBoard()});configBoardShrortcut("G7","toggle autoplay next game",function(){SetAutoplayNextGame(!autoplayNextGame)});configBoardShrortcut("H7","toggle enabling shortcut keys",function(){interactivelyToggleShortcutKeys()});
configBoardShrortcut("A6","pause live broadcast automatic refresh",function(){pauseLiveBroadcast()});configBoardShrortcut("B6","restart live broadcast automatic refresh",function(){restartLiveBroadcast()});configBoardShrortcut("C6","load previous finished game",function(){for(ii=currentGame-1;ii>=0;ii--)if(checkHeaderDefined(gameResult[ii])&&gameResult[ii]!="*")break;if(ii>=0){currentGame=ii;Init()}});
configBoardShrortcut("D6","load previous unfinished game",function(){for(ii=currentGame-1;ii>=0;ii--)if(!checkHeaderDefined(gameResult[ii])||gameResult[ii]=="*")break;if(ii>=0){currentGame=ii;Init()}});configBoardShrortcut("E6","load next unfinished game",function(){for(ii=currentGame+1;ii<numberOfGames;ii++)if(!checkHeaderDefined(gameResult[ii])||gameResult[ii]=="*")break;if(ii<numberOfGames){currentGame=ii;Init()}});
configBoardShrortcut("F6","load next finished game",function(){for(ii=currentGame+1;ii<numberOfGames;ii++)if(checkHeaderDefined(gameResult[ii])&&gameResult[ii]!="*")break;if(ii<numberOfGames){currentGame=ii;Init()}});configBoardShrortcut("G6","",function(){});configBoardShrortcut("H6","force games refresh during live broadcast",function(){refreshPgnSource()});configBoardShrortcut("A5","repeat last search",function(){searchPgnGame(lastSearchPgnExpression)});
configBoardShrortcut("B5","search prompt",function(){searchPgnGamePrompt()});configBoardShrortcut("C5","",function(){});configBoardShrortcut("D5","",function(){});configBoardShrortcut("E5","",function(){});configBoardShrortcut("F5","",function(){});configBoardShrortcut("G5","",function(){});configBoardShrortcut("H5","",function(){});
configBoardShrortcut("A4","jump to previous event",function(){if(checkHeaderDefined(gameEvent[currentGame])){for(ii=currentGame-1;ii>=0;ii--)if(checkHeaderDefined(gameEvent[ii])&&gameEvent[ii]!=gameEvent[currentGame])break;if(ii>=0){currentGame=ii;Init()}}});
configBoardShrortcut("B4","jump to previous round of same event",function(){if(checkHeaderDefined(gameRound[currentGame])){for(ii=currentGame-1;ii>=0;ii--)if(checkHeaderDefined(gameRound[ii])&&gameEvent[ii]==gameEvent[currentGame]&&gameRound[ii]!=gameRound[currentGame])break;if(ii>=0){currentGame=ii;Init()}}});
configBoardShrortcut("C4","load previous game of same black player",function(){if(checkHeaderDefined(gameBlack[currentGame])){for(ii=currentGame-1;ii>=0;ii--)if(checkHeaderDefined(gameBlack[ii])&&gameBlack[ii]==gameBlack[currentGame])break;if(ii>=0){currentGame=ii;Init()}}});
configBoardShrortcut("D4","load previous game of same white player",function(){if(checkHeaderDefined(gameWhite[currentGame])){for(ii=currentGame-1;ii>=0;ii--)if(checkHeaderDefined(gameWhite[ii])&&gameWhite[ii]==gameWhite[currentGame])break;if(ii>=0){currentGame=ii;Init()}}});
configBoardShrortcut("E4","load next game of same white player",function(){if(checkHeaderDefined(gameWhite[currentGame])){for(ii=currentGame+1;ii<numberOfGames;ii++)if(checkHeaderDefined(gameWhite[ii])&&gameWhite[ii]==gameWhite[currentGame])break;if(ii<numberOfGames){currentGame=ii;Init()}}});
configBoardShrortcut("F4","load next game of same black player",function(){if(checkHeaderDefined(gameBlack[currentGame])){for(ii=currentGame+1;ii<numberOfGames;ii++)if(checkHeaderDefined(gameBlack[ii])&&gameBlack[ii]==gameBlack[currentGame])break;if(ii<numberOfGames){currentGame=ii;Init()}}});
configBoardShrortcut("G4","jump to next round of same event",function(){if(checkHeaderDefined(gameRound[currentGame])){for(ii=currentGame+1;ii<numberOfGames;ii++)if(checkHeaderDefined(gameRound[ii])&&gameEvent[ii]==gameEvent[currentGame]&&gameRound[ii]!=gameRound[currentGame])break;if(ii<numberOfGames){currentGame=ii;Init()}}});
configBoardShrortcut("H4","jump to next event",function(){if(checkHeaderDefined(gameEvent[currentGame])){for(ii=currentGame+1;ii<numberOfGames;ii++)if(checkHeaderDefined(gameEvent[ii])&&gameEvent[ii]!=gameEvent[currentGame])break;if(ii<numberOfGames){currentGame=ii;Init()}}});configBoardShrortcut("A3","load first game",function(){if(numberOfGames>1){currentGame=0;Init()}});
configBoardShrortcut("B3","jump 50 games backward",function(){if(currentGame>=50){currentGame-=50;Init()}else if(numberOfGames>1){currentGame=0;Init()}});configBoardShrortcut("C3","load previous game",function(){if(currentGame>0){currentGame--;Init()}});configBoardShrortcut("D3","load random game",function(){if(numberOfGames>1){currentGame=Math.floor(Math.random()*numberOfGames);Init()}});
configBoardShrortcut("E3","load random game at random position",function(){currentGame=Math.floor(Math.random()*numberOfGames);Init();GoToMove(StartPly+Math.floor(Math.random()*(StartPly+PlyNumber+1)))});configBoardShrortcut("F3","load next game",function(){if(numberOfGames>currentGame+1){currentGame++;Init()}});configBoardShrortcut("G3","jump 50 games forward",function(){if(numberOfGames>currentGame+50){currentGame+=50;Init()}else if(numberOfGames>1){currentGame=numberOfGames-1;Init()}});
configBoardShrortcut("H3","load last game",function(){if(numberOfGames>1){currentGame=numberOfGames-1;Init()}});configBoardShrortcut("A2","stop autoplay",function(){SetAutoPlay(false)});configBoardShrortcut("B2","toggle autoplay",function(){SwitchAutoPlay()});configBoardShrortcut("C2","autoplay 1 second",function(){MoveForward(1);SetAutoplayDelay(1E3);SetAutoPlay(true)});configBoardShrortcut("D2","autoplay 2 seconds",function(){MoveForward(1);SetAutoplayDelay(2E3);SetAutoPlay(true)});
configBoardShrortcut("E2","autoplay 3 seconds",function(){MoveForward(1);SetAutoplayDelay(3E3);SetAutoPlay(true)});configBoardShrortcut("F2","autoplay 5 seconds",function(){MoveForward(1);SetAutoplayDelay(5E3);SetAutoPlay(true)});configBoardShrortcut("G2","autoplay 10 seconds",function(){MoveForward(1);SetAutoplayDelay(1E4);SetAutoPlay(true)});configBoardShrortcut("H2","autoplay 30 seconds",function(){MoveForward(1);SetAutoplayDelay(3E4);SetAutoPlay(true)});
configBoardShrortcut("A1","go to game start",function(){GoToMove(StartPly)});configBoardShrortcut("B1","go to previous comment",function(){MoveToPrevComment()});configBoardShrortcut("C1","move 6 half-moves backward",function(){MoveBackward(6)});configBoardShrortcut("D1","move backward",function(){MoveBackward(1)});configBoardShrortcut("E1","move forward",function(){MoveForward(1)});configBoardShrortcut("F1","move 6 half-moves forward",function(){MoveForward(6)});
configBoardShrortcut("G1","go to next comment",function(){MoveToNextComment()});configBoardShrortcut("H1","go to game end",function(){GoToMove(StartPly+PlyNumber)});function detectJavascriptLocation(){jspath="";for(var a=document.getElementsByTagName("script"),b=0;b<a.length;b++)if(a[b].src&&a[b].src.match(/(pgn4web|pgn4web-compacted)\.js/))jspath=a[b].src;return jspath}
function detectHelpLocation(){helpfile="help.html";return detectJavascriptLocation().replace(/(pgn4web|pgn4web-compacted)\.js/,helpfile)}function detectBaseLocation(){base="";for(var a=document.getElementsByTagName("base"),b=0;b<a.length;b++)if(a[b].href)base=a[b].href;return base}debugWin=null;
function displayDebugInfo(){stopAlertPrompt();debugInfo="pgn4web: version="+pgn4web_version+" homepage="+pgn4web_project_url+"\n\n";debugInfo+="HTML URL: length="+location.href.length+" url=";debugInfo+=(location.href.length<100?location.href:location.href.substring(0,99)+"...")+"\n";baseLocation=detectBaseLocation();debugInfo+="BASE URL: url="+(baseLocation!==""?baseLocation:"none")+"\n";debugInfo+="JS URL: url="+detectJavascriptLocation()+"\n\n";debugInfo+="PGN URL: url="+(pgnUrl!==""?pgnUrl:"none")+
"\n";debugInfo+="PGN TEXT: length=";if(document.getElementById("pgnText")!==null)debugInfo+=document.getElementById("pgnText").tagName.toLowerCase()=="textarea"?document.getElementById("pgnText").value.length:document.getElementById("pgnText").innerHTML.length+" container="+document.getElementById("pgnText").tagName.toLowerCase();debugInfo+="\n\n";debugInfo+="GAMES: current="+(currentGame+1)+" number="+numberOfGames+"\nPLY: start="+StartPly+" current="+CurrentPly+" number="+PlyNumber+"\n";debugInfo+=
"AUTOPLAY: "+(isAutoPlayOn?"delay="+Delay+"ms autoplaynext="+autoplayNextGame:"off");debugInfo+="\n\n";debugInfo+="LIVE BROADCAST: "+(LiveBroadcastDelay>0?"ticker="+LiveBroadcastTicker+" delay="+LiveBroadcastDelay+"m started="+LiveBroadcastStarted+" ended="+LiveBroadcastEnded+" paused="+LiveBroadcastPaused+" demo="+LiveBroadcastDemo+" alert="+LiveBroadcastAlert:"off");debugInfo+="\n\n";debugInfo+="ALERT LOG: fatalnew="+fatalErrorNumSinceReset+" new="+alertNumSinceReset+" shown="+Math.min(alertNum,
alertLog.length)+" total="+alertNum+"\n--";if(alertNum>0)for(ii=0;ii<alertLog.length;ii++)if(alertLog[(alertNum-1-ii)%alertLog.length]===undefined)break;else debugInfo+="\n"+alertLog[(alertNum-1-ii)%alertLog.length]+"\n--";if(confirm(debugInfo+"\n\nclick OK to show this debug info in a browser window for cut and paste")){debugWin&&!debugWin.closed&&debugWin.close();debugWin=window.open("","debug_data","resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no,status=no");if(debugWin!==null){debugWin.document.open("text/html",
"replace");debugWin.document.write("<html>");debugWin.document.write("<head><title>pgn4web debug info</title><link rel='shortcut icon' href='pawn.ico' /></head>");debugWin.document.write("<body>\n<pre>\n");debugWin.document.write(debugInfo);debugWin.document.write("\n</pre>\n</body></html>");debugWin.document.close();window.focus&&debugWin.window.focus()}}alertNumSinceReset=fatalErrorNumSinceReset=0}pgnWin=null;
function displayPgnData(a){if(a===null)a=true;pgnWin&&!pgnWin.closed&&pgnWin.close();pgnWin=window.open("","pgn_data","resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no,status=no");if(pgnWin!==null){pgnWin.document.open("text/html","replace");pgnWin.document.write("<html>");pgnWin.document.write("<head><title>pgn4web PGN source</title><link rel='shortcut icon' href='pawn.ico' /></head>");pgnWin.document.write("<body>\n<pre>\n");if(a)for(ii=0;ii<numberOfGames;++ii)pgnWin.document.write(pgnGame[ii]);
else pgnWin.document.write(pgnGame[currentGame]);pgnWin.document.write("\n</pre>\n</body></html>");pgnWin.document.close();window.focus&&pgnWin.window.focus()}}
function CurrentFEN(){currentFEN="";emptyCounterFen=0;for(row=7;row>=0;row--){for(col=0;col<=7;col++)if(Board[col][row]===0)emptyCounterFen++;else{if(emptyCounterFen>0){currentFEN+=""+emptyCounterFen;emptyCounterFen=0}if(Board[col][row]>0)currentFEN+=FenPieceName.toUpperCase().charAt(Board[col][row]-1);else if(Board[col][row]<0)currentFEN+=FenPieceName.toLowerCase().charAt(-Board[col][row]-1)}if(emptyCounterFen>0){currentFEN+=""+emptyCounterFen;emptyCounterFen=0}if(row>0)currentFEN+="/"}currentFEN+=
CurrentPly%2===0?" w":" b";CastlingShortFEN=Array(2);CastlingShortFEN[0]=CastlingShort[0];CastlingShortFEN[1]=CastlingShort[1];CastlingLongFEN=Array(2);CastlingLongFEN[0]=CastlingLong[0];CastlingLongFEN[1]=CastlingLong[1];for(thisPly=StartPly;thisPly<CurrentPly;thisPly++){SideToMoveFEN=thisPly%2;BackrowSideToMoveFEN=SideToMoveFEN*7;if(HistType[0][thisPly]==1)CastlingShortFEN[SideToMoveFEN]=CastlingLongFEN[SideToMoveFEN]=0;if(HistCol[0][thisPly]===7&&HistRow[0][thisPly]==BackrowSideToMoveFEN)CastlingShortFEN[SideToMoveFEN]=
0;if(HistCol[0][thisPly]===0&&HistRow[0][thisPly]==BackrowSideToMoveFEN)CastlingLongFEN[SideToMoveFEN]=0}CastlingFEN="";if(CastlingShortFEN[0]!==0)CastlingFEN+=FenPieceName.toUpperCase().charAt(0);if(CastlingLongFEN[0]!==0)CastlingFEN+=FenPieceName.toUpperCase().charAt(1);if(CastlingShortFEN[1]!==0)CastlingFEN+=FenPieceName.toLowerCase().charAt(0);if(CastlingLongFEN[1]!==0)CastlingFEN+=FenPieceName.toLowerCase().charAt(1);if(CastlingFEN==="")CastlingFEN="-";currentFEN+=" "+CastlingFEN;if(HistEnPassant[CurrentPly-
1]){currentFEN+=" "+String.fromCharCode(HistEnPassantCol[CurrentPly-1]+97);currentFEN+=CurrentPly%2===0?"6":"3"}else currentFEN+=" -";HalfMoveClock=InitialHalfMoveClock;for(thisPly=StartPly;thisPly<CurrentPly;thisPly++)if(HistType[0][thisPly]==6||HistPieceId[1][thisPly]>=16)HalfMoveClock=0;else HalfMoveClock++;currentFEN+=" "+HalfMoveClock;currentFEN+=" "+(Math.floor(CurrentPly/2)+1);return currentFEN}fenWin=null;
function displayFenData(){fenWin&&!fenWin.closed&&fenWin.close();currentFEN=CurrentFEN();currentMovesString="";lastLineStart=0;for(thisPly=CurrentPly;thisPly<=StartPly+PlyNumber;thisPly++){addToMovesString="";if(thisPly==StartPly+PlyNumber){if(gameResult[currentGame]&&gameResult[currentGame]!="*")addToMovesString=gameResult[currentGame]}else{if(thisPly%2===0)addToMovesString=Math.floor(thisPly/2)+1+". ";else if(thisPly==CurrentPly)addToMovesString=Math.floor(thisPly/2)+1+"... ";addToMovesString+=
Moves[thisPly]}if(currentMovesString.length+addToMovesString.length+1>lastLineStart+80){lastLineStart=currentMovesString.length;currentMovesString+="\n"+addToMovesString}else{if(currentMovesString.length>0)currentMovesString+=" ";currentMovesString+=addToMovesString}}fenWin=window.open("","fen_data","resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no,status=no");if(fenWin!==null){fenWin.document.open("text/html","replace");fenWin.document.write("<html>");fenWin.document.write("<head><title>pgn4web FEN string</title><link rel='shortcut icon' href='pawn.ico' /></head>");
fenWin.document.write("<body>\n");fenWin.document.write("<b><pre>\n\n"+currentFEN+"\n\n</pre></b>\n<hr>\n");fenWin.document.write("<pre>\n\n");tmpString=gameEvent[currentGame]?gameEvent[currentGame]:"?";fenWin.document.write('[Event "'+tmpString+'"]\n');tmpString=gameSite[currentGame]?gameSite[currentGame]:"?";fenWin.document.write('[Site "'+tmpString+'"]\n');tmpString=gameDate[currentGame]?gameDate[currentGame]:"????.??.??";fenWin.document.write('[Date "'+tmpString+'"]\n');tmpString=gameRound[currentGame]?
gameRound[currentGame]:"?";fenWin.document.write('[Round "'+tmpString+'"]\n');tmpString=gameWhite[currentGame]?gameWhite[currentGame]:"?";fenWin.document.write('[White "'+tmpString+'"]\n');tmpString=gameBlack[currentGame]?gameBlack[currentGame]:"?";fenWin.document.write('[Black "'+tmpString+'"]\n');tmpString=gameResult[currentGame]?gameResult[currentGame]:"*";fenWin.document.write('[Result "'+tmpString+'"]\n');fenWin.document.write('[SetUp "1"]\n');fenWin.document.write('[FEN "'+CurrentFEN()+'"]\n\n');
fenWin.document.write(currentMovesString);fenWin.document.write("\n</pre>\n</body></html>");fenWin.document.close();window.focus&&fenWin.window.focus()}}
var pgnGame=[],numberOfGames=-1,currentGame=-1,firstStart=true,gameDate=[],gameWhite=[],gameBlack=[],gameEvent=[],gameSite=[],gameRound=[],gameResult=[],gameSetUp=[],gameFEN=[],gameInitialWhiteClock=[],gameInitialBlackClock=[],oldAnchor=-1,isAutoPlayOn=false,AutoPlayInterval=null,Delay=1E3,autostartAutoplay=false,autoplayNextGame=false,initialGame=1,initialHalfmove=0,alwaysInitialHalfmove=false,LiveBroadcastInterval=null,LiveBroadcastDelay=0,LiveBroadcastAlert=false,LiveBroadcastDemo=false,LiveBroadcastStarted=
false,LiveBroadcastEnded=false,LiveBroadcastPaused=false,LiveBroadcastTicker=0,LiveBroadcastStatusString="",LiveBroadcastPlaceholderEvent="pgn4web live broadcast",LiveBroadcastPlaceholderPgn='[Event "'+LiveBroadcastPlaceholderEvent+'"]',gameDemoMaxPly=[],gameDemoLength=[],MaxMove=500,castleRook=-1,mvCapture=0,mvIsCastling=0,mvIsPromotion=0,mvFromCol=-1,mvFromRow=-1,mvToCol=-1,mvToRow=-1,mvPiece=-1,mvPieceId=-1,mvPieceOnTo=-1,mvCaptured=-1,mvCapturedId=-1;Board=Array(8);for(i=0;i<8;++i)Board[i]=Array(8);
HistCol=Array(3);HistRow=Array(3);HistPieceId=Array(2);HistType=Array(2);PieceCol=Array(2);PieceRow=Array(2);PieceType=Array(2);PieceMoveCounter=Array(2);for(i=0;i<2;++i){PieceCol[i]=Array(16);PieceRow[i]=Array(16);PieceType[i]=Array(16);PieceMoveCounter[i]=Array(16);HistType[i]=Array(MaxMove);HistPieceId[i]=Array(MaxMove)}for(i=0;i<3;++i){HistCol[i]=Array(MaxMove);HistRow[i]=Array(MaxMove)}HistEnPassant=Array(MaxMove);HistEnPassant[0]=false;HistEnPassantCol=Array(MaxMove);
startingImageSize=startingSquareSize=HistEnPassantCol[0]=-1;PiecePicture=Array(2);for(i=0;i<2;++i)PiecePicture[i]=Array(6);PieceCode=Array(6);PieceCode[0]="K";PieceCode[1]="Q";PieceCode[2]="R";PieceCode[3]="B";PieceCode[4]="N";PieceCode[5]="P";
var FenPieceName="KQRBNP",FenStringStart="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",ImageOffset=-1,ImagePath="",ImagePathOld,imageType="png",defaultImagesSize=40,highlightOption=true,commentsIntoMoveText=true,commentsOnSeparateLines=false,pgnUrl="";CastlingLong=Array(2);CastlingShort=Array(2);Moves=Array(MaxMove);MoveComments=Array(MaxMove);pgn4webMoveComments=Array(MaxMove);var MoveColor,MoveCount,PlyNumber,StartPly,CurrentPly,IsRotated=false;ClearImg=new Image;DocumentImages=[];
var pgnHeaderTagRegExp=/\[\s*(\w+)\s*"([^"]*)"\s*\]/,pgnHeaderTagRegExpGlobal=/\[\s*(\w+)\s*"([^"]*)"\s*\]/g,dummyPgnHeader='[x""]',emptyPgnHeader='[Event ""]\n[Site ""]\n[Date ""]\n[Round ""]\n[White ""]\n[Black ""]\n[Result ""]\n\n',templatePgnHeader='[Event "?"]\n[Site "?"]\n[Date "?"]\n[Round "?"]\n[White "?"]\n[Black "?"]\n[Result "?"]\n',alertPgnHeader='[Event ""]\n[Site ""]\n[Date ""]\n[Round ""]\n[White ""]\n[Black ""]\n[Result ""]\n\n{error: click on the top left chessboard square for debug info}',
gameSelectorHead=" ...",gameSelectorMono=true,gameSelectorNum=false,gameSelectorNumLenght=0,gameSelectorChEvent=0,gameSelectorChSite=0,gameSelectorChRound=0,gameSelectorChWhite=15,gameSelectorChBlack=15,gameSelectorChResult=0,gameSelectorChDate=10;
function CheckLegality(a,b){var c,e,d;if(a=="O-O"){if(!CheckLegalityOO())return false;c=PieceCol[MoveColor][0];for(e=6;c<e;){if(d=IsCheck(c,MoveColor*7,MoveColor))return false;++c}StoreMove(b);return true}else if(a=="O-O-O"){if(!CheckLegalityOOO())return false;c=PieceCol[MoveColor][0];for(e=2;c>e;){if(d=IsCheck(c,MoveColor*7,MoveColor))return false;--c}StoreMove(b);return true}if(!mvCapture)if(Board[mvToCol][mvToRow]!==0)return false;if(mvCapture&&Color(Board[mvToCol][mvToRow])!=1-MoveColor)if(mvPiece!=
6||!HistEnPassant[b-1]||HistEnPassantCol[b-1]!=mvToCol||mvToRow!=5-3*MoveColor)return false;if(mvIsPromotion){if(mvPiece!=6)return false;if(mvPieceOnTo>=6)return false;if(mvToRow!=7*(1-MoveColor))return false}for(e=0;e<16;++e)if(PieceType[MoveColor][e]==mvPiece){if(mvPiece==1)c=CheckLegalityKing(e);else if(mvPiece==2)c=CheckLegalityQueen(e);else if(mvPiece==3)c=CheckLegalityRook(e);else if(mvPiece==4)c=CheckLegalityBishop(e);else if(mvPiece==5)c=CheckLegalityKnight(e);else if(mvPiece==6)c=CheckLegalityPawn(e);
if(c){mvPieceId=e;StoreMove(b);if(d=IsCheck(PieceCol[MoveColor][0],PieceRow[MoveColor][0],MoveColor))UndoMove(b);else return true}}return false}function CheckLegalityKing(a){if(mvFromCol>=0&&mvFromCol!=PieceCol[MoveColor][a])return false;if(mvFromRow>0&&mvFromRow!=PieceRow[MoveColor][a])return false;if(Math.abs(PieceCol[MoveColor][a]-mvToCol)>1)return false;if(Math.abs(PieceRow[MoveColor][a]-mvToRow)>1)return false;return true}
function CheckLegalityQueen(a){if(mvFromCol>=0&&mvFromCol!=PieceCol[MoveColor][a])return false;if(mvFromRow>=0&&mvFromRow!=PieceRow[MoveColor][a])return false;if((PieceCol[MoveColor][a]-mvToCol)*(PieceRow[MoveColor][a]-mvToRow)!==0&&Math.abs(PieceCol[MoveColor][a]-mvToCol)!=Math.abs(PieceRow[MoveColor][a]-mvToRow))return false;if(!CheckClearWay(a))return false;return true}
function CheckLegalityRook(a){if(mvFromCol>=0&&mvFromCol!=PieceCol[MoveColor][a])return false;if(mvFromRow>=0&&mvFromRow!=PieceRow[MoveColor][a])return false;if((PieceCol[MoveColor][a]-mvToCol)*(PieceRow[MoveColor][a]-mvToRow)!==0)return false;if(!CheckClearWay(a))return false;return true}
function CheckLegalityBishop(a){if(mvFromCol>=0&&mvFromCol!=PieceCol[MoveColor][a])return false;if(mvFromRow>=0&&mvFromRow!=PieceRow[MoveColor][a])return false;if(Math.abs(PieceCol[MoveColor][a]-mvToCol)!=Math.abs(PieceRow[MoveColor][a]-mvToRow))return false;if(!CheckClearWay(a))return false;return true}
function CheckLegalityKnight(a){if(mvFromCol>=0&&mvFromCol!=PieceCol[MoveColor][a])return false;if(mvFromRow>=0&&mvFromRow!=PieceRow[MoveColor][a])return false;if(Math.abs(PieceCol[MoveColor][a]-mvToCol)*Math.abs(PieceRow[MoveColor][a]-mvToRow)!=2)return false;return true}
function CheckLegalityPawn(a){if(mvFromCol>=0&&mvFromCol!=PieceCol[MoveColor][a])return false;if(mvFromRow>=0&&mvFromRow!=PieceRow[MoveColor][a])return false;if(Math.abs(PieceCol[MoveColor][a]-mvToCol)!=mvCapture)return false;if(mvCapture){if(PieceRow[MoveColor][a]-mvToRow!=2*MoveColor-1)return false}else if(PieceRow[MoveColor][a]-mvToRow==4*MoveColor-2){if(PieceRow[MoveColor][a]!=1+5*MoveColor)return false;if(Board[mvToCol][mvToRow+2*MoveColor-1]!==0)return false}else if(PieceRow[MoveColor][a]-mvToRow!=
2*MoveColor-1)return false;return true}
function CheckLegalityOO(){if(CastlingShort[MoveColor]===0)return false;if(PieceMoveCounter[MoveColor][0]>0)return false;for(var a=false,b=0;b<16;){if(PieceCol[MoveColor][b]>PieceCol[MoveColor][0]&&PieceRow[MoveColor][b]==MoveColor*7&&PieceType[MoveColor][b]==3){a=true;break}++b}if(!a)return false;if(PieceMoveCounter[MoveColor][b]>0)return false;Board[PieceCol[MoveColor][0]][MoveColor*7]=0;Board[PieceCol[MoveColor][b]][MoveColor*7]=0;a=PieceRow[MoveColor][b];if(a<6)a=6;for(;a>PieceCol[MoveColor][0]||
a>=5;){if(Board[a][MoveColor*7]!==0)return false;--a}castleRook=b;return true}
function CheckLegalityOOO(){if(CastlingLong[MoveColor]===0)return false;if(PieceMoveCounter[MoveColor][0]>0)return false;for(var a=false,b=0;b<16;){if(PieceCol[MoveColor][b]<PieceCol[MoveColor][0]&&PieceRow[MoveColor][b]==MoveColor*7&&PieceType[MoveColor][b]==3){a=true;break}++b}if(!a)return false;if(PieceMoveCounter[MoveColor][b]>0)return false;Board[PieceCol[MoveColor][0]][MoveColor*7]=0;Board[PieceCol[MoveColor][b]][MoveColor*7]=0;a=PieceRow[MoveColor][b];if(a>2)a=2;for(;a>PieceCol[MoveColor][0]||
a<=3;){if(Board[a][MoveColor*7]!==0)return false;++a}castleRook=b;return true}function CheckClearWay(a){var b=sign(mvToCol-PieceCol[MoveColor][a]),c=sign(mvToRow-PieceRow[MoveColor][a]),e=PieceCol[MoveColor][a]+b;for(a=PieceRow[MoveColor][a]+c;e!=mvToCol||a!=mvToRow;){if(Board[e][a]!==0)return false;e+=b;a+=c}return true}
function ClearMove(a){for(var b=a.length,c=-1,e=0,d="";e<b;){c=a.charCodeAt(e);if(c==45||c>=48&&c<=57||c==61||c>=65&&c<=90||c>=97&&c<=122)d+=a.charAt(e);++e}if(d.match("^[Oo0]-?[Oo0]-?[Oo0]$"))return"O-O-O";if(d.match("^[Oo0]-?[Oo0]$"))return"O-O";return d}function GoToMove(a){a=a-CurrentPly;a>0?MoveForward(a):MoveBackward(-a)}function SetShortcutKeysEnabled(a){shortcutKeysEnabled=a}
function interactivelyToggleShortcutKeys(){if(confirm("Shortcut keys currently "+(shortcutKeysEnabled?"enabled":"disabled")+".\nToggle shortcut keys to "+(shortcutKeysEnabled?"DISABLED":"ENABLED")+"?"))SetShortcutKeysEnabled(!shortcutKeysEnabled)}function SetCommentsIntoMoveText(a){commentsIntoMoveText=a}function SetCommentsOnSeparateLines(a){commentsOnSeparateLines=a}function SetAutostartAutoplay(a){autostartAutoplay=a}function SetAutoplayNextGame(a){autoplayNextGame=a}
function SetInitialHalfmove(a,b){if(b===true)alwaysInitialHalfmove=true;initialHalfmove=a;if(initialHalfmove!="start")if(initialHalfmove!="end")if(initialHalfmove!="random")if(initialHalfmove!="comment")if((initialHalfmove=parseInt(initialHalfmove,10))==NaN)initialHalfmove=0}function SetInitialGame(a){if(a)initialGame=a}function clockFromComment(a){var b="";if((DGTclock=a.match(/\[%clk\s*(.*?)\]/))!==null)b=DGTclock[1];else if(!(b=a.match(/^\s*[0-9:\.]+/)))b="";return b}
function HighlightLastMove(){var a;if(oldAnchor>=0){a="Mv"+oldAnchor;theAnchor=document.getElementById(a);if(theAnchor!==null)theAnchor.className="move"}var b=CurrentPly-1;if(b>StartPly+PlyNumber)b=StartPly+PlyNumber;a=document.getElementById("GameLastComment");if(a!==null){if(MoveComments[b+1]!=undefined){thisComment=MoveComments[b+1].replace(/\[%.*?\]\s*/g,"");if(thisComment.match(/^\s*$/))thisComment=""}else thisComment="";a.innerHTML=thisComment!==""?MoveComments[b+1]:"-";a.className="GameLastComment"}text=
(b+1)%2===0?"white":"black";theObject=document.getElementById("GameSideToMove");if(theObject!==null)theObject.innerHTML=text;if((b+1)%2==1){lastMoverClockObject=document.getElementById("GameWhiteClock");initialLastMoverClock=gameInitialWhiteClock[currentGame];beforeLastMoverClockObject=document.getElementById("GameBlackClock");initialBeforeLastMoverClock=gameInitialBlackClock[currentGame]}else{lastMoverClockObject=document.getElementById("GameBlackClock");initialLastMoverClock=gameInitialBlackClock[currentGame];
beforeLastMoverClockObject=document.getElementById("GameWhiteClock");initialBeforeLastMoverClock=gameInitialWhiteClock[currentGame]}if(lastMoverClockObject!==null)lastMoverClockObject.innerHTML=b+1>StartPly?clockFromComment(MoveComments[b+1]):initialLastMoverClock;if(beforeLastMoverClockObject!==null)beforeLastMoverClockObject.innerHTML=b+1>StartPly+1?clockFromComment(MoveComments[b]):initialBeforeLastMoverClock;a=document.getElementById("GameNextMove");if(a!==null){text=b+1>=StartPly+PlyNumber?gameResult[currentGame]:
Math.floor((b+1)/2)+1+((b+1)%2===0?". ":"... ")+Moves[b+1];a.innerHTML=text;a.className="GameNextMove";a.style.whiteSpace="nowrap"}a=document.getElementById("GameLastMove");if(a!==null){text=b<StartPly?"-":Math.floor(b/2)+1+(b%2===0?". ":"... ")+Moves[b];a.innerHTML=text;a.className="GameLastMove";a.style.whiteSpace="nowrap"}if(b>=StartPly-1){a="Mv"+(b+1);theAnchor=document.getElementById(a);if(theAnchor!==null)theAnchor.className="move moveOn";oldAnchor=b+1;if(highlightOption){if(b<StartPly)highlightColTo=
highlightRowTo=highlightColFrom=highlightRowFrom=-1;else{highlightColFrom=HistCol[0][b];if(highlightColFrom==undefined)highlightColFrom=-1;highlightRowFrom=HistRow[0][b];if(highlightRowFrom==undefined)highlightRowFrom=-1;highlightColTo=HistCol[2][b];if(highlightColTo==undefined)highlightColTo=-1;highlightRowTo=HistRow[2][b];if(highlightRowTo==undefined)highlightRowTo=-1}highlightMove(highlightColFrom,highlightRowFrom,highlightColTo,highlightRowTo)}}}
function SetHighlightOption(a){highlightOption=a}function SetHighlight(a){SetHighlightOption(a);a?HighlightLastMove():highlightMove(-1,-1,-1,-1)}var lastColFromHighlighted=-1,lastRowFromHighlighted=-1,lastColToHighlighted=-1,lastRowToHighlighted=-1;
function highlightMove(a,b,c,e){highlightSquare(lastColFromHighlighted,lastRowFromHighlighted,false);highlightSquare(lastColToHighlighted,lastRowToHighlighted,false);if(highlightSquare(a,b,true)){lastColFromHighlighted=a;lastRowFromHighlighted=b}else lastRowFromHighlighted=lastColFromHighlighted=-1;if(highlightSquare(c,e,true)){lastColToHighlighted=c;lastRowToHighlighted=e}else lastRowToHighlighted=lastColToHighlighted=-1}
function highlightSquare(a,b,c){if(a==undefined||b==undefined)return false;if(!SquareOnBoard(a,b))return false;if(IsRotated){trow=b;tcol=7-a}else{trow=7-b;tcol=a}theObject=document.getElementById("tcol"+tcol+"trow"+trow);if(theObject===null)return false;theObject.className=c?(trow+tcol)%2===0?"highlightWhiteSquare":"highlightBlackSquare":(trow+tcol)%2===0?"whiteSquare":"blackSquare";return true}
function pgnGameFromPgnText(a){a=a.replace(/</g,"<");a=a.replace(/>/g,">");lines=a.split("\n");inGameBody=inGameHeader=false;gameIndex=-1;pgnGame.length=0;for(ii in lines)if(lines[ii].charAt(0)!="%"){if(pgnHeaderTagRegExp.test(lines[ii])===true){if(!inGameHeader){gameIndex++;pgnGame[gameIndex]=""}inGameHeader=true;inGameBody=false}else if(inGameHeader){inGameHeader=false;inGameBody=true}lines[ii]=lines[ii].replace(/^\s*/,"");lines[ii]=lines[ii].replace(/\s*$/,"");if(gameIndex>=0)pgnGame[gameIndex]+=
lines[ii]+" \n"}numberOfGames=pgnGame.length;return gameIndex>=0}
function loadPgnFromPgnUrl(a){var b=false;if(window.XMLHttpRequest){b=new XMLHttpRequest;b.overrideMimeType&&b.overrideMimeType("text/xml")}else if(window.ActiveXObject)try{b=new ActiveXObject("Msxml2.XMLHTTP")}catch(c){try{b=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){}}if(!b){myAlert("error: XMLHttpRequest failed for PGN URL\n"+a,true);return false}try{if(LiveBroadcastDelay>0){dd=new Date;b.open("GET",a+"?nocahce="+Math.random(),false)}else b.open("GET",a,false);LiveBroadcastDelay>0&&b.setRequestHeader("If-Modified-Since",
"Sat, 1 Jan 2000 00:00:00 GMT");b.send(null)}catch(d){myAlert("error: request failed for PGN URL\n"+a,true);return false}if(b.readyState==4&&(b.status==200||b.status===0)){if(!pgnGameFromPgnText(b.responseText)){myAlert("error: no games found in PGN file\n"+a,true);return false}}else{myAlert("error: failed reading PGN from URL\n"+a,true);return false}return true}function SetPgnUrl(a){pgnUrl=a}
function pauseLiveBroadcast(){if(LiveBroadcastDelay!==0){LiveBroadcastPaused=true;clearTimeout(LiveBroadcastInterval);LiveBroadcastInterval=null}}function restartLiveBroadcast(){if(LiveBroadcastDelay!==0){LiveBroadcastPaused=false;refreshPgnSource()}}
function checkLiveBroadcastStatus(){if(LiveBroadcastDelay===0){LiveBroadcastEnded=false;LiveBroadcastStatusString=""}else{if(LiveBroadcastStarted===false||pgnGame==undefined||numberOfGames==1&&gameEvent[0]==LiveBroadcastPlaceholderEvent){LiveBroadcastEnded=false;LiveBroadcastStatusString="live broadcast yet to start"}else{for(ii=liveGamesRunning=0;ii<numberOfGames;ii++)gameResult[ii].indexOf("*")>=0&&liveGamesRunning++;LiveBroadcastStatusString=(LiveBroadcastEnded=liveGamesRunning===0)?"live broadcast ended":
"live games: "+liveGamesRunning+" finished: "+(numberOfGames-liveGamesRunning)}theObject=document.getElementById("GameLiveStatus");if(theObject!==null)theObject.innerHTML=LiveBroadcastStatusString}}
function restartLiveBroadcastTimeout(){if(LiveBroadcastDelay!==0){if(LiveBroadcastInterval){clearTimeout(LiveBroadcastInterval);LiveBroadcastInterval=null}checkLiveBroadcastStatus();needRestart=!LiveBroadcastEnded;if(needRestart===true&&!LiveBroadcastPaused)LiveBroadcastInterval=setTimeout("refreshPgnSource()",LiveBroadcastDelay*6E4);LiveBroadcastTicker++}}var LiveBroadcastFoundOldGame=false;
function refreshPgnSource(){if(LiveBroadcastDelay!==0){if(LiveBroadcastInterval){clearTimeout(LiveBroadcastInterval);LiveBroadcastInterval=null}if(LiveBroadcastDemo)for(ii=0;ii<numberOfGames;ii++){rnd=Math.random();if(rnd<=0.05)gameDemoMaxPly[ii]+=3;else if(rnd<=0.2)gameDemoMaxPly[ii]+=2;else if(rnd<=0.6)gameDemoMaxPly[ii]+=1}oldGameWhite=gameWhite[currentGame];oldGameBlack=gameBlack[currentGame];oldGameEvent=gameEvent[currentGame];oldGameRound=gameRound[currentGame];oldGameSite=gameSite[currentGame];
oldGameDate=gameDate[currentGame];initialGame=currentGame+1;firstStart=true;textSelectOptions="";oldCurrentPly=CurrentPly!=StartPly+PlyNumber?CurrentPly:-1;(oldAutoplay=isAutoPlayOn)&&SetAutoPlay(false);(LiveBroadcastStarted=loadPgnFromPgnUrl(pgnUrl))||pgnGameFromPgnText(LiveBroadcastPlaceholderPgn);LoadGameHeaders();LiveBroadcastFoundOldGame=false;for(ii=0;ii<numberOfGames;ii++)if(LiveBroadcastFoundOldGame=gameWhite[ii]==oldGameWhite&&gameBlack[ii]==oldGameBlack&&gameEvent[ii]==oldGameEvent&&gameRound[ii]==
oldGameRound&&gameSite[ii]==oldGameSite&&gameDate[ii]==oldGameDate)break;if(LiveBroadcastFoundOldGame)initialGame=ii+1;if(LiveBroadcastFoundOldGame&&oldCurrentPly>=0){oldInitialHalfmove=initialHalfmove;initialHalfmove=oldCurrentPly}Init();if(LiveBroadcastFoundOldGame&&oldCurrentPly>=0)initialHalfmove=oldInitialHalfmove;checkLiveBroadcastStatus();customFunctionOnPgnTextLoad();restartLiveBroadcastTimeout();LiveBroadcastFoundOldGame&&oldAutoplay&&SetAutoPlay(true)}}
function createBoard(){theObject=document.getElementById("GameBoard");if(theObject!==null)theObject.innerHTML='<DIV STYLE="font-size: small; font-family: sans-serif; padding: 10px; text-align: center;">...loading PGN data<br />please wait...</DIV>';if(pgnUrl)if(loadPgnFromPgnUrl(pgnUrl)){if(LiveBroadcastDelay>0)LiveBroadcastStarted=true;Init();LiveBroadcastDelay>0&&checkLiveBroadcastStatus();customFunctionOnPgnTextLoad()}else if(LiveBroadcastDelay===0){pgnGameFromPgnText(alertPgnHeader);Init();customFunctionOnPgnTextLoad();
myAlert("error: failed loading games from PGN URL\n"+pgnUrl,true)}else{LiveBroadcastStarted=false;pgnGameFromPgnText(LiveBroadcastPlaceholderPgn);Init();checkLiveBroadcastStatus();customFunctionOnPgnTextLoad()}else if(document.getElementById("pgnText")){if(document.getElementById("pgnText").tagName.toLowerCase()=="textarea")tmpText=document.getElementById("pgnText").value;else{tmpText=document.getElementById("pgnText").innerHTML;if(tmpText.indexOf("\n")<0)tmpText=tmpText.replace(/((\[[^\[\]]*\]\s*)+)/g,
"\n$1\n");if(tmpText.indexOf('"')<0)tmpText=tmpText.replace(/(")/g,'"')}if(pgnHeaderTagRegExp.test(tmpText)===false)tmpText=emptyPgnHeader+tmpText;if(pgnGameFromPgnText(tmpText)){Init();customFunctionOnPgnTextLoad()}else{pgnGameFromPgnText(alertPgnHeader);Init();customFunctionOnPgnTextLoad();myAlert("error: no games found in PGN text",true)}}else{pgnGameFromPgnText(alertPgnHeader);Init();customFunctionOnPgnTextLoad();myAlert("error: missing PGN URL location or pgnText in the HTML file",true)}}
function Init(){isAutoPlayOn&&SetAutoPlay(false);InitImages();if(firstStart){LoadGameHeaders();switch(initialGame){case "first":currentGame=0;break;case "last":currentGame=numberOfGames-1;break;case "random":currentGame=Math.floor(Math.random()*numberOfGames);break;default:if(isNaN(parseInt(initialGame,10)))(currentGame=gameNumberSearchPgn(initialGame))||(currentGame=0);else{initialGame=parseInt(initialGame,10);currentGame=initialGame<0?0:initialGame===0?Math.floor(Math.random()*numberOfGames):initialGame<=
numberOfGames?initialGame-1:numberOfGames-1}break}}gameSetUp[currentGame]!=undefined&&gameSetUp[currentGame]!="1"?InitFEN():InitFEN(gameFEN[currentGame]);OpenGame(currentGame);if(ImageOffset<0)for(ii=0;ii<document.images.length;++ii)if(document.images[ii].src==ClearImg.src){ImageOffset=ii;break}RefreshBoard();CurrentPly=StartPly;HighlightLastMove();if(firstStart||alwaysInitialHalfmove)switch(initialHalfmove){case "start":GoToMove(0);break;case "end":GoToMove(StartPly+PlyNumber);break;case "random":GoToMove(StartPly+
Math.floor(Math.random()*(StartPly+PlyNumber)));break;case "comment":GoToMove(0);MoveToNextComment();break;default:if(isNaN(initialHalfmove))initialHalfmove=0;if(initialHalfmove<-3)initialHalfmove=0;if(initialHalfmove==-3)GoToMove(StartPly+PlyNumber);else if(initialHalfmove==-2){GoToMove(0);MoveToNextComment()}else initialHalfmove==-1?GoToMove(StartPly+Math.floor(Math.random()*(StartPly+PlyNumber))):GoToMove(initialHalfmove);break}else customFunctionOnMove();firstStart&&autostartAutoplay&&SetAutoPlay(true);
customFunctionOnPgnGameLoad();firstStart=false}
function InitFEN(a){FenString=a!=undefined?a:FenStringStart;var b;for(a=0;a<8;++a)for(b=0;b<8;++b)Board[a][b]=0;var c;MoveCount=StartPly=0;MoveColor=StartPly%2;StartMove=0;var e=false;for(a=0;a<2;a++)CastlingLong[a]=CastlingShort[a]=1;InitialHalfMoveClock=0;if(FenString=="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")for(b=0;b<2;++b){PieceType[b][0]=1;PieceCol[b][0]=4;PieceType[b][1]=2;PieceCol[b][1]=3;PieceType[b][6]=3;PieceType[b][7]=3;PieceCol[b][6]=0;PieceCol[b][7]=7;PieceType[b][4]=
4;PieceType[b][5]=4;PieceCol[b][4]=2;PieceCol[b][5]=5;PieceType[b][2]=5;PieceType[b][3]=5;PieceCol[b][2]=1;PieceCol[b][3]=6;for(c=0;c<8;++c){PieceType[b][c+8]=6;PieceCol[b][c+8]=c}for(a=0;a<16;++a){PieceMoveCounter[b][a]=0;PieceRow[b][a]=(1-b)*Math.floor(a/8)+b*(7-Math.floor(a/8))}for(a=0;a<16;a++){var d=PieceCol[b][a],g=PieceRow[b][a];Board[d][g]=(1-2*b)*PieceType[b][a]}}else{var f,h,j;for(a=0;a<2;a++)for(b=0;b<16;b++){PieceType[a][b]=-1;PieceCol[a][b]=0;PieceRow[a][b]=0;PieceMoveCounter[a][b]=0}a=
0;b=7;f=0;j=h=1;for(d=FenString.charAt(f++);d!=" ";){if(d=="/"){if(a!=8){myAlert("error: invalid FEN [1] char "+f+" '"+d+"' in game "+(currentGame+1)+"\n"+FenString,true);InitFEN();return}a=0;b--}if(a==8){myAlert("error: invalid FEN [2] char "+f+" '"+d+"' in game "+(currentGame+1)+"\n"+FenString,true);InitFEN();return}if(!isNaN(d)){a+=parseInt(d,10);if(a<0||a>8){myAlert("error: invalid FEN [3] char "+f+" '"+d+"' in game "+(currentGame+1)+"\n"+FenString,true);InitFEN();return}}if(d.charCodeAt(0)==
FenPieceName.toUpperCase().charCodeAt(0)){if(PieceType[0][0]!=-1){myAlert("error: invalid FEN [4] char "+f+" '"+d+"' in game "+(currentGame+1)+"\n"+FenString,true);InitFEN();return}PieceType[0][0]=1;PieceCol[0][0]=a;PieceRow[0][0]=b;a++}if(d.charCodeAt(0)==FenPieceName.toLowerCase().charCodeAt(0)){if(PieceType[1][0]!=-1){myAlert("error: invalid FEN [5] char "+f+" '"+d+"' in game "+(currentGame+1)+"\n"+FenString,true);InitFEN();return}PieceType[1][0]=1;PieceCol[1][0]=a;PieceRow[1][0]=b;a++}for(g=1;g<
6;g++){if(d.charCodeAt(0)==FenPieceName.toUpperCase().charCodeAt(g)){if(h==16){myAlert("error: invalid FEN [6] char "+f+" '"+d+"' in game "+(currentGame+1)+"\n"+FenString,true);InitFEN();return}PieceType[0][h]=g+1;PieceCol[0][h]=a;PieceRow[0][h]=b;h++;a++}if(d.charCodeAt(0)==FenPieceName.toLowerCase().charCodeAt(g)){if(j==16){myAlert("error: invalid FEN [7] char "+f+" '"+d+"' in game "+(currentGame+1)+"\n"+FenString,true);InitFEN();return}PieceType[1][j]=g+1;PieceCol[1][j]=a;PieceRow[1][j]=b;j++;
a++}}d=f<FenString.length?FenString.charAt(f++):" "}if(a!=8||b!==0){myAlert("error: invalid FEN [8] char "+f+" in game "+(currentGame+1)+"\n"+FenString,true);InitFEN()}else if(PieceType[0][0]==-1||PieceType[1][0]==-1){myAlert("error: invalid FEN [9]: missing king in game "+(currentGame+1)+"\n"+FenString,true);InitFEN()}else{if(f==FenString.length){FenString+=" w ";FenString+=FenPieceName.toUpperCase().charAt(0);FenString+=FenPieceName.toUpperCase().charAt(1);FenString+=FenPieceName.toLowerCase().charAt(0);
FenString+=FenPieceName.toLowerCase().charAt(1);FenString+=" - 0 1";f++}d=FenString.charAt(f++);if(d=="w"||d=="b"){if(d=="b"){StartMove=1;StartPly+=1;MoveColor=1}f++;if(f>=FenString.length)myAlert("error: invalid FEN [12]: char "+f+" missing castling availability in game "+(currentGame+1)+"\n"+FenString,true);else{CastlingShort[0]=CastlingLong[0]=CastlingShort[1]=CastlingLong[1]=0;for(d=FenString.charAt(f++);d!=" ";){if(d.charCodeAt(0)==FenPieceName.toUpperCase().charCodeAt(0))CastlingShort[0]=1;
if(d.charCodeAt(0)==FenPieceName.toUpperCase().charCodeAt(1))CastlingLong[0]=1;if(d.charCodeAt(0)==FenPieceName.toLowerCase().charCodeAt(0))CastlingShort[1]=1;if(d.charCodeAt(0)==FenPieceName.toLowerCase().charCodeAt(1))CastlingLong[1]=1;if(d=="E"||d=="F"||d=="G"||d=="H")CastlingShort[0]=1;if(d=="A"||d=="B"||d=="C"||d=="D")CastlingLong[0]=1;if(d=="e"||d=="f"||d=="g"||d=="h")CastlingShort[1]=1;if(d=="a"||d=="b"||d=="c"||d=="d")CastlingLong[1]=1;d=f<FenString.length?FenString.charAt(f++):" "}for(b=
0;b<2;++b)for(a=0;a<16;a++)if(PieceType[b][a]!=-1){d=PieceCol[b][a];g=PieceRow[b][a];Board[d][g]=(1-2*b)*PieceType[b][a]}if(f>=FenString.length)myAlert("error: invalid FEN [13]: char "+f+" missing en passant target square in game "+(currentGame+1)+"\n"+FenString,true);else{for(d=FenString.charAt(f++);d!=" ";){if(d.charCodeAt(0)-97>=0&&d.charCodeAt(0)-97<=7){e=true;c=d.charCodeAt(0)-97}d=f<FenString.length?FenString.charAt(f++):" "}if(f>=FenString.length)myAlert("error: invalid FEN [14]: char "+f+
" missing halfmove clock in game "+(currentGame+1)+"\n"+FenString,true);else{InitialHalfMoveClock=0;for(d=FenString.charAt(f++);d!=" ";){if(isNaN(d)){myAlert("error: invalid FEN [15]: char "+f+" '"+d+"' invalid halfmove clock in game "+(currentGame+1)+"\n"+FenString,true);return}InitialHalfMoveClock=InitialHalfMoveClock*10+parseInt(d,10);d=f<FenString.length?FenString.charAt(f++):" "}if(f>=FenString.length)myAlert("error: invalid FEN [16]: char "+f+" missing fullmove number in game "+(currentGame+
1)+"\n"+FenString,true);else{d=FenString.substring(f++);if(isNaN(d))myAlert("error: invalid FEN [17]: char "+f+" '"+d+"' invalid fullmove number in game "+(currentGame+1)+"\n"+FenString,true);else if(d<=0)myAlert("error: invalid FEN [18]: char "+f+" '"+d+"' invalid fullmove number in game "+(currentGame+1)+"\n"+FenString,true);else{StartPly+=2*(parseInt(d,10)-1);HistEnPassant[StartPly-1]=e;HistEnPassantCol[StartPly-1]=c}}}}}}else myAlert("error: invalid FEN [11]: char "+f+" '"+d+"' invalid active color in game "+
(currentGame+1)+"\n"+FenString,true)}}}function SetImageType(a){imageType=a}
function InitImages(){DocumentImages.length=0;if(ImagePathOld!=ImagePath){if(ImagePath.length>0&&ImagePath[ImagePath.length-1]!="/")ImagePath+="/";ClearImg.src=ImagePath+"clear."+imageType;var a;ColorName=Array("w","b");for(a=0;a<2;++a){PiecePicture[a][1]=new Image;PiecePicture[a][1].src=ImagePath+ColorName[a]+"k."+imageType;PiecePicture[a][2]=new Image;PiecePicture[a][2].src=ImagePath+ColorName[a]+"q."+imageType;PiecePicture[a][3]=new Image;PiecePicture[a][3].src=ImagePath+ColorName[a]+"r."+imageType;
PiecePicture[a][4]=new Image;PiecePicture[a][4].src=ImagePath+ColorName[a]+"b."+imageType;PiecePicture[a][5]=new Image;PiecePicture[a][5].src=ImagePath+ColorName[a]+"n."+imageType;PiecePicture[a][6]=new Image;PiecePicture[a][6].src=ImagePath+ColorName[a]+"p."+imageType}ImagePathOld=ImagePath}}
function IsCheck(a,b,c){var e,d=2*c-1;if(Math.abs(PieceCol[1-c][0]-a)<=1&&Math.abs(PieceRow[1-c][0]-b)<=1)return true;for(c=-2;c<=2;c+=4)for(e=-1;e<=1;e+=2){if(SquareOnBoard(a+c,b+e))if(Board[a+c][b+e]==d*5)return true;if(SquareOnBoard(a+e,b+c))if(Board[a+e][b+c]==d*5)return true}for(c=-1;c<=1;c+=2)if(SquareOnBoard(a+c,b-d))if(Board[a+c][b-d]==d*6)return true;for(c=-1;c<=1;++c)for(e=-1;e<=1;++e)if(c!==0||e!==0)for(var g=a+c,f=b+e,h=0;SquareOnBoard(g,f)&&h===0;){h=Board[g][f];if(h===0){g+=c;f+=e}else{if(h==
d*2)return true;if(h==d*3&&(c===0||e===0))return true;if(h==d*4&&c!==0&&e!==0)return true}}return false}function checkHeaderDefined(a){return a!=undefined&&a!==""&&a!=" "&&a!="?"}
function LoadGameHeaders(){var a;gameEvent.length=gameSite.length=gameRound.length=gameDate.length=0;gameWhite.length=gameBlack.length=gameResult.length=0;gameSetUp.length=gameFEN.length=0;gameInitialWhiteClock.length=gameInitialBlackClock.length=0;pgnHeaderTagRegExpGlobal.exec("");for(a=0;a<numberOfGames;++a){var b=pgnGame[a],c;gameEvent[a]=gameSite[a]=gameRound[a]=gameDate[a]="";gameWhite[a]=gameBlack[a]=gameResult[a]="";for(gameInitialWhiteClock[a]=gameInitialBlackClock[a]="";(c=pgnHeaderTagRegExpGlobal.exec(b))!==
null;)if(c[1]=="Event")gameEvent[a]=c[2];else if(c[1]=="Site")gameSite[a]=c[2];else if(c[1]=="Round")gameRound[a]=c[2];else if(c[1]=="Date")gameDate[a]=c[2];else if(c[1]=="White")gameWhite[a]=c[2];else if(c[1]=="Black")gameBlack[a]=c[2];else if(c[1]=="Result")gameResult[a]=c[2];else if(c[1]=="SetUp")gameSetUp[a]=c[2];else if(c[1]=="FEN")gameFEN[a]=c[2];else if(c[1]=="WhiteClock")gameInitialWhiteClock[a]=c[2];else if(c[1]=="BlackClock")gameInitialBlackClock[a]=c[2]}if(LiveBroadcastDemo&&numberOfGames>
0)for(a=0;a<numberOfGames;++a){if(gameDemoLength[a]==undefined){InitFEN(gameFEN[a]);ParsePGNGameString(pgnGame[a]);gameDemoLength[a]=PlyNumber}if(gameDemoMaxPly[a]==undefined)gameDemoMaxPly[a]=0;if(gameDemoMaxPly[a]<=gameDemoLength[a])gameResult[a]="*"}}
function MoveBackward(a){var b=CurrentPly-1;a=b-a;if(a<StartPly)a=StartPly-1;for(b=b;b>a;--b){CurrentPly--;MoveColor=1-MoveColor;var c=HistPieceId[0][b];Board[PieceCol[MoveColor][c]][PieceRow[MoveColor][c]]=0;Board[HistCol[0][b]][HistRow[0][b]]=HistType[0][b]*(1-2*MoveColor);PieceType[MoveColor][c]=HistType[0][b];PieceCol[MoveColor][c]=HistCol[0][b];PieceRow[MoveColor][c]=HistRow[0][b];PieceMoveCounter[MoveColor][c]--;c=HistPieceId[1][b];if(c>=0&&c<16){Board[PieceCol[MoveColor][c]][PieceRow[MoveColor][c]]=
0;Board[HistCol[1][b]][HistRow[1][b]]=HistType[1][b]*(1-2*MoveColor);PieceType[MoveColor][c]=HistType[1][b];PieceCol[MoveColor][c]=HistCol[1][b];PieceRow[MoveColor][c]=HistRow[1][b];PieceMoveCounter[MoveColor][c]--}c-=16;if(c>=0&&c<16){Board[PieceCol[1-MoveColor][c]][PieceRow[1-MoveColor][c]]=0;Board[HistCol[1][b]][HistRow[1][b]]=HistType[1][b]*(2*MoveColor-1);PieceType[1-MoveColor][c]=HistType[1][b];PieceCol[1-MoveColor][c]=HistCol[1][b];PieceRow[1-MoveColor][c]=HistRow[1][b];PieceMoveCounter[1-
MoveColor][c]--}}RefreshBoard();HighlightLastMove();if(AutoPlayInterval){clearTimeout(AutoPlayInterval);AutoPlayInterval=null}if(isAutoPlayOn)if(a>=StartPly)AutoPlayInterval=setTimeout("MoveBackward(1)",Delay);else SetAutoPlay(false);customFunctionOnMove()}
function MoveForward(a){goToPly=CurrentPly+parseInt(a,10);if(goToPly>StartPly+PlyNumber)goToPly=StartPly+PlyNumber;for(a=CurrentPly;a<goToPly;++a){var b=Moves[a],c=ParseMove(b,a);if(!c){text=Math.floor(a/2)+1+(a%2===0?". ":"... ");myAlert("error: invalid ply "+text+b+" in game "+(currentGame+1),true);break}MoveColor=1-MoveColor}CurrentPly=a;RefreshBoard();HighlightLastMove();if(AutoPlayInterval){clearTimeout(AutoPlayInterval);AutoPlayInterval=null}if(c){if(a==goToPly)if(isAutoPlayOn)if(goToPly<StartPly+
PlyNumber)AutoPlayInterval=setTimeout("MoveForward(1)",Delay);else if(autoplayNextGame)AutoPlayInterval=setTimeout("AutoplayNextGame()",Delay);else SetAutoPlay(false)}else SetAutoPlay(false);customFunctionOnMove()}function AutoplayNextGame(){if(fatalErrorNumSinceReset===0){if(++currentGame>=numberOfGames)currentGame=0;Init();if(numberOfGames>0||PlyNumber>0){SetAutoPlay(true);return}}SetAutoPlay(false)}
function MoveToNextComment(){for(ii=CurrentPly+1;ii<=StartPly+PlyNumber;ii++)if(MoveComments[ii]!==""){GoToMove(ii);break}}function MoveToPrevComment(){for(ii=CurrentPly-1;ii>=0;ii--)if(MoveComments[ii]!==""){GoToMove(ii);break}}function OpenGame(a){ParsePGNGameString(pgnGame[a]);currentGame=a;if(LiveBroadcastDemo)if(gameDemoMaxPly[a]<=PlyNumber)PlyNumber=gameDemoMaxPly[a];PrintHTML()}
function ParsePGNGameString(a){a=a;a=a.replace(pgnHeaderTagRegExpGlobal,"");a=a.replace(/^\s/,"");a=a.replace(/\s$/,"");for(ii=PlyNumber=0;ii<StartPly;ii++)Moves[ii]="";MoveComments[StartPly+PlyNumber]="";for(start=0;start<a.length;start++)switch(a.charAt(start)){case " ":case "\u0008":case "\u000c":case "\n":case "\r":case "\t":break;case "$":commentStart=start;for(commentEnd=commentStart+1;"0123456789".indexOf(a.charAt(commentEnd))>=0;){commentEnd++;if(commentEnd==a.length)break}if(MoveComments[StartPly+
PlyNumber].length>0)MoveComments[StartPly+PlyNumber]+=" ";MoveComments[StartPly+PlyNumber]+=a.substring(commentStart,commentEnd);start=commentEnd;break;case "!":case "?":commentStart=start;commentEnd=commentStart+(a.charAt(start+1)=="?"||a.charAt(start+1)=="!"?2:1);if(MoveComments[StartPly+PlyNumber].length>0)MoveComments[StartPly+PlyNumber]+=" ";MoveComments[StartPly+PlyNumber]+=a.substring(commentStart,commentEnd);start=commentEnd;break;case "{":commentStart=start+1;commentEnd=a.indexOf("}",start+
1);if(commentEnd>0){if(MoveComments[StartPly+PlyNumber].length>0)MoveComments[StartPly+PlyNumber]+=" ";MoveComments[StartPly+PlyNumber]+=a.substring(commentStart,commentEnd);start=commentEnd}else{myAlert("error: missing end comment char } while parsing game "+(currentGame+1),true);return}break;case "%":if(start>0&&a.charAt(start-1)!="\n")break;commentStart=start+1;commentEnd=a.indexOf("\n",start+1);if(commentEnd<0)commentEnd=a.length;start=commentEnd;break;case ";":commentStart=start+1;commentEnd=
a.indexOf("\n",start+1);if(commentEnd<0)commentEnd=a.length;if(MoveComments[StartPly+PlyNumber].length>0)MoveComments[StartPly+PlyNumber]+=" ";MoveComments[StartPly+PlyNumber]+=a.substring(commentStart,commentEnd);start=commentEnd;break;case "(":openVariation=1;variationStart=start;for(variationEnd=start+1;openVariation>0&&variationEnd<a.length;){nextOpen=a.indexOf("(",variationEnd);nextClosed=a.indexOf(")",variationEnd);if(nextClosed<0){myAlert("error: missing end variation char ) while parsing game "+
(currentGame+1),true);return}if(nextOpen>=0&&nextOpen<nextClosed){openVariation++;variationEnd=nextOpen+1}else{openVariation--;variationEnd=nextClosed+1}}if(MoveComments[StartPly+PlyNumber].length>0)MoveComments[StartPly+PlyNumber]+=" ";MoveComments[StartPly+PlyNumber]+=a.substring(variationStart,variationEnd+1);start=variationEnd;break;default:searchThis="1-0";if(a.indexOf(searchThis,start)==start){start+=searchThis.length;MoveComments[StartPly+PlyNumber]+=a.substring(start,a.length);start=a.length;
break}searchThis="0-1";if(a.indexOf(searchThis,start)==start){start+=searchThis.length;MoveComments[StartPly+PlyNumber]+=a.substring(start,a.length);start=a.length;break}searchThis="1/2-1/2";if(a.indexOf(searchThis,start)==start){start+=searchThis.length;MoveComments[StartPly+PlyNumber]+=a.substring(start,a.length);start=a.length;break}searchThis="*";if(a.indexOf(searchThis,start)==start){start+=searchThis.length;MoveComments[StartPly+PlyNumber]+=a.substring(start,a.length);start=a.length;break}moveCount=
Math.floor((StartPly+PlyNumber)/2)+1;searchThis=moveCount.toString()+".";if(a.indexOf(searchThis,start)==start)for(start+=searchThis.length;a.charAt(start)=="."||a.charAt(start)==" "||a.charAt(start)=="\n"||a.charAt(start)=="\r";)start++;else{searchThis=moveCount.toString()+String.fromCharCode(8230);if(a.indexOf(searchThis,start)==start)for(start+=searchThis.length;a.charAt(start)=="."||a.charAt(start)==" "||a.charAt(start)=="\n"||a.charAt(start)=="\r";)start++}end=a.indexOf(" ",start);end2=a.indexOf("$",
start);if(end2>0&&end2<end)end=end2;end2=a.indexOf("{",start);if(end2>0&&end2<end)end=end2;end2=a.indexOf(";",start);if(end2>0&&end2<end)end=end2;end2=a.indexOf("(",start);if(end2>0&&end2<end)end=end2;end2=a.indexOf("!",start);if(end2>0&&end2<end)end=end2;end2=a.indexOf("?",start);if(end2>0&&end2<end)end=end2;if(end<0)end=a.length;move=a.substring(start,end);Moves[StartPly+PlyNumber]=ClearMove(move);start=a.charAt(end)==" "?end:end-1;if(Moves[StartPly+PlyNumber]!==""){PlyNumber++;MoveComments[StartPly+
PlyNumber]=""}break}for(ii=StartPly;ii<=PlyNumber;ii++){MoveComments[ii]=MoveComments[ii].replace(/\s+/g," ");pgn4webCommentTmp=MoveComments[ii].match(/\[%pgn4web\s*(.*?)\]/);pgn4webMoveComments[ii]=pgn4webCommentTmp?pgn4webCommentTmp[1]:"";MoveComments[ii]=translateNAGs(MoveComments[ii]);MoveComments[ii]=MoveComments[ii].replace(/\s+$/g,"")}}var NAG=[];NAG[0]="";NAG[1]="!";NAG[2]="?";NAG[3]="!!";NAG[4]="??";NAG[5]="!?";NAG[6]="?!";NAG[7]="forced move";NAG[8]="singular move";NAG[9]="worst move";
NAG[10]="drawish position";NAG[11]="equal chances, quiet position";NAG[12]="equal chances, active position";NAG[13]="unclear position";NAG[14]="White has a slight advantage";NAG[15]="Black has a slight advantage";NAG[16]="White has a moderate advantage";NAG[17]="Black has a moderate advantage";NAG[18]="White has a decisive advantage";NAG[19]="Black has a decisive advantage";NAG[20]="White has a crushing advantage";NAG[21]="Black has a crushing advantage";NAG[22]="White is in zugzwang";NAG[23]="Black is in zugzwang";
NAG[24]="White has a slight space advantage";NAG[25]="Black has a slight space advantage";NAG[26]="White has a moderate space advantage";NAG[27]="Black has a moderate space advantage";NAG[28]="White has a decisive space advantage";NAG[29]="Black has a decisive space advantage";NAG[30]="White has a slight time (development) advantage";NAG[31]="Black has a slight time (development) advantage";NAG[32]="White has a moderate time (development) advantage";NAG[33]="Black has a moderate time (development) advantage";
NAG[34]="White has a decisive time (development) advantage";NAG[35]="Black has a decisive time (development) advantage";NAG[36]="White has the initiative";NAG[37]="Black has the initiative";NAG[38]="White has a lasting initiative";NAG[39]="Black has a lasting initiative";NAG[40]="White has the attack";NAG[41]="Black has the attack";NAG[42]="White has insufficient compensation for material deficit";NAG[43]="Black has insufficient compensation for material deficit";NAG[44]="White has sufficient compensation for material deficit";
NAG[45]="Black has sufficient compensation for material deficit";NAG[46]="White has more than adequate compensation for material deficit";NAG[47]="Black has more than adequate compensation for material deficit";NAG[48]="White has a slight center control advantage";NAG[49]="Black has a slight center control advantage";NAG[50]="White has a moderate center control advantage";NAG[51]="Black has a moderate center control advantage";NAG[52]="White has a decisive center control advantage";NAG[53]="Black has a decisive center control advantage";
NAG[54]="White has a slight kingside control advantage";NAG[55]="Black has a slight kingside control advantage";NAG[56]="White has a moderate kingside control advantage";NAG[57]="Black has a moderate kingside control advantage";NAG[58]="White has a decisive kingside control advantage";NAG[59]="Black has a decisive kingside control advantage";NAG[60]="White has a slight queenside control advantage";NAG[61]="Black has a slight queenside control advantage";NAG[62]="White has a moderate queenside control advantage";
NAG[63]="Black has a moderate queenside control advantage";NAG[64]="White has a decisive queenside control advantage";NAG[65]="Black has a decisive queenside control advantage";NAG[66]="White has a vulnerable first rank";NAG[67]="Black has a vulnerable first rank";NAG[68]="White has a well protected first rank";NAG[69]="Black has a well protected first rank";NAG[70]="White has a poorly protected king";NAG[71]="Black has a poorly protected king";NAG[72]="White has a well protected king";NAG[73]="Black has a well protected king";
NAG[74]="White has a poorly placed king";NAG[75]="Black has a poorly placed king";NAG[76]="White has a well placed king";NAG[77]="Black has a well placed king";NAG[78]="White has a very weak pawn structure";NAG[79]="Black has a very weak pawn structure";NAG[80]="White has a moderately weak pawn structure";NAG[81]="Black has a moderately weak pawn structure";NAG[82]="White has a moderately strong pawn structure";NAG[83]="Black has a moderately strong pawn structure";NAG[84]="White has a very strong pawn structure";
NAG[85]="Black has a very strong pawn structure";NAG[86]="White has poor knight placement";NAG[87]="Black has poor knight placement";NAG[88]="White has good knight placement";NAG[89]="Black has good knight placement";NAG[90]="White has poor bishop placement";NAG[91]="Black has poor bishop placement";NAG[92]="White has good bishop placement";NAG[93]="Black has good bishop placement";NAG[84]="White has poor rook placement";NAG[85]="Black has poor rook placement";NAG[86]="White has good rook placement";
NAG[87]="Black has good rook placement";NAG[98]="White has poor queen placement";NAG[99]="Black has poor queen placement";NAG[100]="White has good queen placement";NAG[101]="Black has good queen placement";NAG[102]="White has poor piece coordination";NAG[103]="Black has poor piece coordination";NAG[104]="White has good piece coordination";NAG[105]="Black has good piece coordination";NAG[106]="White has played the opening very poorly";NAG[107]="Black has played the opening very poorly";NAG[108]="White has played the opening poorly";
NAG[109]="Black has played the opening poorly";NAG[110]="White has played the opening well";NAG[111]="Black has played the opening well";NAG[112]="White has played the opening very well";NAG[113]="Black has played the opening very well";NAG[114]="White has played the middlegame very poorly";NAG[115]="Black has played the middlegame very poorly";NAG[116]="White has played the middlegame poorly";NAG[117]="Black has played the middlegame poorly";NAG[118]="White has played the middlegame well";
NAG[119]="Black has played the middlegame well";NAG[120]="White has played the middlegame very well";NAG[121]="Black has played the middlegame very well";NAG[122]="White has played the ending very poorly";NAG[123]="Black has played the ending very poorly";NAG[124]="White has played the ending poorly";NAG[125]="Black has played the ending poorly";NAG[126]="White has played the ending well";NAG[127]="Black has played the ending well";NAG[128]="White has played the ending very well";NAG[129]="Black has played the ending very well";
NAG[130]="White has slight counterplay";NAG[131]="Black has slight counterplay";NAG[132]="White has moderate counterplay";NAG[133]="Black has moderate counterplay";NAG[134]="White has decisive counterplay";NAG[135]="Black has decisive counterplay";NAG[136]="White has moderate time control pressure";NAG[137]="Black has moderate time control pressure";NAG[138]="White has severe time control pressure";NAG[139]="Black has severe time control pressure";
function translateNAGs(a){var b,c=0;for(numString="01234567890";(c=a.indexOf("$",c))>=0;){for(b=c+1;"0123456789".indexOf(a.charAt(b))>=0&&b<a.length;){b++;if(b==a.length)break}nag=parseInt(a.substring(c+1,b),10);if(nag!=undefined&&NAG[nag]!=undefined)a=a.replace(a.substring(c,b),'<SPAN CLASS="nag">'+NAG[nag]+"</SPAN>");c++}return a}
function ParseMove(a,b){var c,e,d,g=-1;castleRook=-1;mvCapture=mvIsPromotion=mvIsCastling=0;mvCapturedId=mvCaptured=mvPieceOnTo=mvPieceId=mvPiece=mvToRow=mvToCol=mvFromRow=mvFromCol=-1;for(c=1;c<a.length;){if(!isNaN(a.charAt(c))){mvToCol=a.charCodeAt(c-1)-97;mvToRow=a.charAt(c)-1;d=a.substring(0,c-1);g=c}++c}if(mvToCol<0||mvToCol>7||mvToRow<0||mvToRow>7){if(a.indexOf("O")>=0||a.indexOf("o")>=0||a.indexOf("0")>=0){if(a.match("^[Oo0]-?[Oo0]-?[Oo0]$")!==null){mvPiece=mvIsCastling=1;mvPieceId=0;mvPieceOnTo=
1;mvFromCol=4;mvToCol=2;mvFromRow=7*MoveColor;mvToRow=7*MoveColor;return CheckLegality("O-O-O",b)}if(a.match("^[Oo0]-?[Oo0]$")!==null){mvPiece=mvIsCastling=1;mvPieceId=0;mvPieceOnTo=1;mvFromCol=4;mvToCol=6;mvFromRow=7*MoveColor;mvToRow=7*MoveColor;return CheckLegality("O-O",b)}}return false}e=d.length;if(e>3)return false;mvPiece=-1;if(e===0)mvPiece=6;else{for(c=1;c<6;++c)if(d.charAt(0)==PieceCode[c-1])mvPiece=c;if(mvPiece==-1)if("abcdefgh".indexOf(d.charAt(0))>=0)mvPiece=6;if(mvPiece==-1)return false;
if(d.charAt(e-1)=="x")mvCapture=1;if(isNaN(a.charAt(e-1-mvCapture))){mvFromCol=a.charCodeAt(e-1-mvCapture)-97;if(mvFromCol<0||mvFromCol>7)mvFromCol=-1}else{mvFromRow=a.charAt(e-1-mvCapture)-1;if(mvFromRow<0||mvFromRow>7)mvFromRow=-1}if(e>1&&!mvCapture&&mvFromCol==-1&&mvFromRow==-1)return false;if(mvPiece==6&&!mvCapture&&mvFromCol==-1&&mvFromRow==-1)return false;if(mvPiece==6&&mvFromCol==mvToCol)return false}mvPieceOnTo=mvPiece;if(Board[mvToCol][mvToRow]!==0)mvCapture=1;else if(mvPiece==6&&HistEnPassant[b-
1]&&mvToCol==HistEnPassantCol[b-1]&&mvToRow==5-3*MoveColor)mvCapture=1;c=a.indexOf("=");if(c<0)c=g;if(c>0&&c<a.length-1)if(mvPiece==6){c=a.charAt(c+1);if(c==PieceCode[1])mvPieceOnTo=2;else if(c==PieceCode[2])mvPieceOnTo=3;else if(c==PieceCode[3])mvPieceOnTo=4;else if(c==PieceCode[4])mvPieceOnTo=5;mvIsPromotion=1}if(mvCapture){for(mvCapturedId=15;mvCapturedId>=0&&mvCaptured<0;)if(PieceType[1-MoveColor][mvCapturedId]>0&&PieceCol[1-MoveColor][mvCapturedId]==mvToCol&&PieceRow[1-MoveColor][mvCapturedId]==
mvToRow)mvCaptured=PieceType[1-MoveColor][mvCapturedId];else--mvCapturedId;if(mvPiece==6&&mvCapturedId<1&&HistEnPassant[b-1])for(mvCapturedId=15;mvCapturedId>=0&&mvCaptured<0;)if(PieceType[1-MoveColor][mvCapturedId]==6&&PieceCol[1-MoveColor][mvCapturedId]==mvToCol&&PieceRow[1-MoveColor][mvCapturedId]==4-MoveColor)mvCaptured=PieceType[1-MoveColor][mvCapturedId];else--mvCapturedId}if(!CheckLegality(PieceCode[mvPiece-1],b))return false;HistEnPassant[b]=false;HistEnPassantCol[b]=-1;if(mvPiece==6)if(Math.abs(HistRow[0][b]-
mvToRow)==2){HistEnPassant[b]=true;HistEnPassantCol[b]=mvToCol}return true}function SetGameSelectorOptions(a,b,c,e,d,g,f,h,j){if(a!==null)gameSelectorHead=a;if(b!==null)gameSelectorNum=b;if(c!==null)gameSelectorChEvent=c>32?32:c;if(e!==null)gameSelectorChSite=e>32?32:e;if(d!==null)gameSelectorChRound=d>32?32:d;if(g!==null)gameSelectorChWhite=g>32?32:g;if(f!==null)gameSelectorChBlack=f>32?32:f;if(h!==null)gameSelectorChResult=h>32?32:h;if(j!==null)gameSelectorChDate=j>32?32:j}
var clickedSquareInterval=null;function clickedSquare(a,b){if(!clickedSquareInterval){squareId="tcol"+b+"trow"+a;theObject=document.getElementById(squareId);originalClass=theObject.className;newClass=(a+b)%2===0?"blackSquare":"whiteSquare";theObject.className=newClass;clickedSquareInterval=setTimeout("reset_after_click("+a+","+b+",'"+originalClass+"','"+newClass+"')",66)}}
function reset_after_click(a,b,c,e){squareId="tcol"+b+"trow"+a;theObject=document.getElementById(squareId);if(theObject.className==e)theObject.className=c;clickedSquareInterval=null}var lastSearchPgnExpression="";
function gameNumberSearchPgn(a){lastSearchPgnExpression=a;if(a==="")return false;newlinesRegExp=RegExp("[\n\r]","gm");searchExpressionRegExp=RegExp(a,"im");currentGameSearch=(currentGame+1)%numberOfGames;for(checkGame=(currentGameSearch+1)%numberOfGames;checkGame!=currentGameSearch;checkGame=(checkGame+1)%numberOfGames)if(pgnGame[checkGame].replace(newlinesRegExp," ").match(searchExpressionRegExp))return checkGame;return false}
function searchPgnGame(a){lastSearchPgnExpression=a;if(!(a===""||!a))if(!(numberOfGames<2)){checkGame=gameNumberSearchPgn(a);if(checkGame!=currentGame){currentGame=checkGame;Init()}}}
function searchPgnGamePrompt(){if(numberOfGames<2)alert("info: search prompt disabled with less than 2 games");else if(searchExpression=prompt("Please enter search pattern for PGN games:",lastSearchPgnExpression)){if(theObject=document.getElementById("searchPgnExpression"))theObject.value=searchExpression;searchPgnGame(searchExpression)}}var tableSize=null;
function PrintHTML(){var a,b,c;c='<TABLE CLASS="boardTable" ID="boardTable" CELLSPACING=0 CELLPADDING=0';c+=tableSize!==null&&tableSize!==0?' STYLE="width: '+tableSize+"px; height: "+tableSize+'px;">':">";for(a=0;a<8;++a){c+="<TR>";for(b=0;b<8;++b){squareId="tcol"+b+"trow"+a;imageId="img_"+squareId;linkId="link_"+squareId;c+=(a+b)%2===0?'<TD CLASS="whiteSquare" ID="'+squareId+'" BGCOLOR="white"':'<TD CLASS="blackSquare" ID="'+squareId+'" BGCOLOR="lightgray"';c+=' ALIGN="center" VALIGN="middle" ONCLICK="clickedSquare('+
a+","+b+')">';squareTitle=squareCoord=IsRotated?String.fromCharCode(72-b,49+a):String.fromCharCode(b+65,56-a);if(boardTitle[b][a]!=="")squareTitle+=": "+boardTitle[b][a];c+='<A HREF="javascript:boardOnClick['+b+"]["+a+']()" ID="'+linkId+'" TITLE="'+squareTitle+'" STYLE="text-decoration: none; outline: none;" ONFOCUS="this.blur()"><IMG CLASS="pieceImage" ID="'+imageId+'" SRC="'+ImagePath+"clear."+imageType+'" BORDER=0></A></TD>'}c+="</TR>"}c+="</TABLE>";theObject=document.getElementById("GameBoard");
if(theObject!==null)theObject.innerHTML=c;theObject=document.getElementById("boardTable");if(theObject!==null){tableSize=theObject.offsetWidth;if(tableSize>0)theObject.style.height=tableSize}numberOfButtons=5;spaceSize=3;buttonSize=(tableSize-spaceSize*(numberOfButtons-1))/numberOfButtons;c='<FORM NAME="GameButtonsForm" STYLE="display:inline;"><TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD><INPUT ID="startButton" TYPE="BUTTON" VALUE="<<" STYLE="';if(buttonSize!=undefined&&buttonSize>0)c+=
"width: "+buttonSize+";";c+='"; CLASS="buttonControl" TITLE="go to game start" ID="btnGoToStart" onClick="javascript:GoToMove(StartPly)" ONFOCUS="this.blur()"></TD><TD CLASS="buttonControlSpace" WIDTH="'+spaceSize+'"></TD><TD><INPUT ID="backButton" TYPE="BUTTON" VALUE="<" STYLE="';if(buttonSize!=undefined&&buttonSize>0)c+="width: "+buttonSize+";";c+='"; CLASS="buttonControl" TITLE="move backward" ID="btnMoveBackward1" onClick="javascript:MoveBackward(1)" ONFOCUS="this.blur()"></TD><TD CLASS="buttonControlSpace" WIDTH="'+
spaceSize+'"></TD><TD>';c+='<INPUT ID="autoplayButton" TYPE="BUTTON" VALUE=';c+=isAutoPlayOn?"=":"+";c+=' STYLE="';if(buttonSize!=undefined&&buttonSize>0)c+="width: "+buttonSize+";";c+=isAutoPlayOn?'"; CLASS="buttonControlStop" TITLE="toggle autoplay (stop)" ':'"; CLASS="buttonControlPlay" TITLE="toggle autoplay (start)" ';c+=' ID="btnPlay" NAME="AutoPlay" onClick="javascript:SwitchAutoPlay()" ONFOCUS="this.blur()"></TD><TD CLASS="buttonControlSpace" WIDTH="'+spaceSize+'"></TD><TD><INPUT ID="forwardButton" TYPE="BUTTON" VALUE=">" STYLE="';
if(buttonSize!=undefined&&buttonSize>0)c+="width: "+buttonSize+";";c+='"; CLASS="buttonControl" TITLE="move forward" ID="btnMoveForward1" onClick="javascript:MoveForward(1)" ONFOCUS="this.blur()"></TD><TD CLASS="buttonControlSpace" WIDTH="'+spaceSize+'"></TD><TD><INPUT ID="endButton" TYPE="BUTTON" VALUE=">>" STYLE="';if(buttonSize!=undefined&&buttonSize>0)c+="width: "+buttonSize+";";c+='"; CLASS="buttonControl" TITLE="go to game end" ID="btnGoToEnd" onClick="javascript:GoToMove(StartPly + PlyNumber)" ONFOCUS="this.blur()"></TD></TR></TABLE></FORM>';
theObject=document.getElementById("GameButtons");if(theObject!==null)theObject.innerHTML=c;if(firstStart)textSelectOptions="";theObject=document.getElementById("GameSelector");if(theObject!==null)if(numberOfGames<2){for(;theObject.firstChild;)theObject.removeChild(theObject.firstChild);textSelectOptions=""}else if(textSelectOptions===""){if(gameSelectorNum)gameSelectorNumLenght=Math.floor(Math.log(numberOfGames)/Math.log(10))+1;c='<FORM NAME="GameSel" STYLE="display:inline;"> <SELECT ID="GameSelSelect" NAME="GameSelSelect" STYLE="';
if(tableSize!=undefined&&tableSize>0)c+="width: "+tableSize+"; ";c+='font-family: monospace;" CLASS="selectControl" TITLE="Select a game" ONCHANGE="this.blur(); if(this.value >= 0) {currentGame=parseInt(this.value); document.GameSel.GameSelSelect.value = -1; Init();}" ONFOCUS="disableShortcutKeysAndStoreStatus();" ONBLUR="restoreShortcutKeysStatus();" > <OPTION value=-1>';blanks="";for(a=0;a<32;a++)blanks+=" ";gameSelectorHeadDisplay=gameSelectorNum?blanks.substring(0,gameSelectorNumLenght)+" "+
gameSelectorHead:gameSelectorHead;c+=gameSelectorHeadDisplay.replace(/ /g," ");for(a=0;a<numberOfGames;a++){textSelectOptions+="<OPTION value="+a+">";textSO="";if(gameSelectorNum){numText=" "+(a+1);howManyBlanks=gameSelectorNumLenght-(numText.length-1);if(howManyBlanks>0)textSO+=blanks.substring(0,howManyBlanks);textSO+=numText+" "}if(gameSelectorChEvent>0){textSO+=" "+gameEvent[a].substring(0,gameSelectorChEvent);howManyBlanks=gameSelectorChEvent-gameEvent[a].length;if(howManyBlanks>0)textSO+=
blanks.substring(0,howManyBlanks);textSO+=" "}if(gameSelectorChSite>0){textSO+=" "+gameSite[a].substring(0,gameSelectorChSite);howManyBlanks=gameSelectorChSite-gameSite[a].length;if(howManyBlanks>0)textSO+=blanks.substring(0,howManyBlanks);textSO+=" "}if(gameSelectorChRound>0){textSO+=" "+gameRound[a].substring(0,gameSelectorChRound);howManyBlanks=gameSelectorChRound-gameRound[a].length;if(howManyBlanks>0)textSO+=blanks.substring(0,howManyBlanks);textSO+=" "}if(gameSelectorChWhite>0){textSO+=" "+
gameWhite[a].substring(0,gameSelectorChWhite);howManyBlanks=gameSelectorChWhite-gameWhite[a].length;if(howManyBlanks>0)textSO+=blanks.substring(0,howManyBlanks);textSO+=" "}if(gameSelectorChBlack>0){textSO+=" "+gameBlack[a].substring(0,gameSelectorChBlack);howManyBlanks=gameSelectorChBlack-gameBlack[a].length;if(howManyBlanks>0)textSO+=blanks.substring(0,howManyBlanks);textSO+=" "}if(gameSelectorChResult>0){textSO+=" "+gameResult[a].substring(0,gameSelectorChResult);howManyBlanks=gameSelectorChResult-
gameResult[a].length;if(howManyBlanks>0)textSO+=blanks.substring(0,howManyBlanks);textSO+=" "}if(gameSelectorChDate>0){textSO+=" "+gameDate[a].substring(0,gameSelectorChDate);howManyBlanks=gameSelectorChDate-gameDate[a].length;if(howManyBlanks>0)textSO+=blanks.substring(0,howManyBlanks);textSO+=" "}textSelectOptions+=textSO.replace(/ /g," ")}c+=textSelectOptions+"</SELECT></FORM>";theObject.innerHTML=c}theObject=document.getElementById("GameEvent");if(theObject!==null)theObject.innerHTML=gameEvent[currentGame];
theObject=document.getElementById("GameRound");if(theObject!==null){theObject.innerHTML=gameRound[currentGame];theObject.style.whiteSpace="nowrap"}theObject=document.getElementById("GameSite");if(theObject!==null)theObject.innerHTML=gameSite[currentGame];theObject=document.getElementById("GameDate");if(theObject!==null){theObject.innerHTML=gameDate[currentGame];theObject.style.whiteSpace="nowrap"}theObject=document.getElementById("GameWhite");if(theObject!==null)theObject.innerHTML=gameWhite[currentGame];
theObject=document.getElementById("GameBlack");if(theObject!==null)theObject.innerHTML=gameBlack[currentGame];theObject=document.getElementById("GameResult");if(theObject!==null){theObject.innerHTML=gameResult[currentGame];theObject.style.whiteSpace="nowrap"}c='<SPAN ID="ShowPgnText">';for(a=StartPly;a<StartPly+PlyNumber;++a){printedComment=false;thisComment=MoveComments[a].replace(/\[%.*?\]\s*/g,"");if(thisComment.match(/^\s*$/))thisComment="";if(commentsIntoMoveText&&thisComment!==""){if(commentsOnSeparateLines&&
a>StartPly)c+='<DIV CLASS="comment" STYLE="line-height: 33%;"> </DIV>';c+='<SPAN CLASS="comment">'+thisComment+'</SPAN><SPAN CLASS="move"> </SPAN>';if(commentsOnSeparateLines)c+='<DIV CLASS="comment" STYLE="line-height: 33%;"> </DIV>';printedComment=true}b=Math.floor(a/2)+1;c+='<SPAN STYLE="white-space: nowrap;">';if(a%2===0)c+='<SPAN CLASS="move">'+b+". </SPAN>";else if(printedComment||a==StartPly)c+='<SPAN CLASS="move">'+b+"... </SPAN>";b=a+1;c+='<A HREF="javascript:GoToMove('+
b+')" CLASS="move" ID="Mv'+b+'" ONFOCUS="this.blur()">'+Moves[a]+'</A></SPAN><SPAN CLASS="move"> </SPAN>'}thisComment=MoveComments[StartPly+PlyNumber].replace(/\[%.*?\]\s*/g,"");if(thisComment.match(/^\s*$/))thisComment="";if(commentsIntoMoveText&&thisComment!==""){if(commentsOnSeparateLines)c+='<DIV CLASS="comment" STYLE="line-height: 33%;"> </DIV>';c+='<SPAN CLASS="comment">'+thisComment+'</SPAN><SPAN CLASS="move"> </SPAN>'}c+="</SPAN>";theObject=document.getElementById("GameText");if(theObject!==
null)theObject.innerHTML=c;theObject=document.getElementById("GameSearch");if(firstStart&&theObject!==null)if(numberOfGames<2)for(;theObject.firstChild;)theObject.removeChild(theObject.firstChild);else{c='<FORM ID="searchPgnForm" STYLE="display: inline;" ACTION="javascript:searchPgnGame(document.getElementById(\'searchPgnExpression\').value);">';c+='<INPUT ID="searchPgnButton" CLASS="searchPgnButton" STYLE="display: inline; ';if(tableSize!=undefined&&tableSize>0)c+="width: "+tableSize/4+"; ";c+='" TITLE="find games matching the search string (or regular expression)" ';
c+='TYPE="submit" VALUE="?">';c+='<INPUT ID="searchPgnExpression" CLASS="searchPgnExpression" TITLE="find games matching the search string (or regular expression)" TYPE="input" VALUE="'+lastSearchPgnExpression+'" STYLE="display: inline; ';if(tableSize!=undefined&&tableSize>0)c+="width: "+3*tableSize/4+"; ";c+='" ONFOCUS="disableShortcutKeysAndStoreStatus();" ONBLUR="restoreShortcutKeysStatus();">';c+="</FORM>";theObject.innerHTML=c}}
function FlipBoard(){(tmpHighlightOption=highlightOption)&&SetHighlight(false);IsRotated=!IsRotated;PrintHTML();RefreshBoard();tmpHighlightOption&&SetHighlight(true)}
function RefreshBoard(){InitImages();var a,b,c;for(a=0;a<8;++a)for(b=0;b<8;++b)if(Board[a][b]===0){c=IsRotated?63-a-(7-b)*8:a+(7-b)*8;SetImage(c,ClearImg.src)}for(a=0;a<2;++a)for(b=0;b<16;++b)if(PieceType[a][b]>0){c=IsRotated?63-PieceCol[a][b]-(7-PieceRow[a][b])*8:PieceCol[a][b]+(7-PieceRow[a][b])*8;SetImage(c,PiecePicture[a][PieceType[a][b]].src)}}
function SetAutoPlay(a){isAutoPlayOn=a;if(AutoPlayInterval){clearTimeout(AutoPlayInterval);AutoPlayInterval=null}if(isAutoPlayOn){if(document.GameButtonsForm)if(document.GameButtonsForm.AutoPlay){document.GameButtonsForm.AutoPlay.value="=";document.GameButtonsForm.AutoPlay.title="toggle autoplay (stop)";document.GameButtonsForm.AutoPlay.className="buttonControlStop"}if(CurrentPly<StartPly+PlyNumber)AutoPlayInterval=setTimeout("MoveForward(1)",Delay);else if(autoplayNextGame)AutoPlayInterval=setTimeout("AutoplayNextGame()",
Delay);else SetAutoPlay(false)}else if(document.GameButtonsForm)if(document.GameButtonsForm.AutoPlay){document.GameButtonsForm.AutoPlay.value="+";document.GameButtonsForm.AutoPlay.title="toggle autoplay (start)";document.GameButtonsForm.AutoPlay.className="buttonControlPlay"}}function SetAutoplayDelay(a){Delay=a}function SetLiveBroadcast(a,b,c){LiveBroadcastDelay=a;LiveBroadcastAlert=b===true;LiveBroadcastDemo=c===true}
function SetImage(a,b){if(DocumentImages[a]!=b){document.images[a+ImageOffset].src=b;DocumentImages[a]=b}}function SetImagePath(a){ImagePath=a}function SwitchAutoPlay(){if(isAutoPlayOn)SetAutoPlay(false);else{MoveForward(1);SetAutoPlay(true)}}
function StoreMove(a){HistPieceId[0][a]=mvPieceId;HistCol[0][a]=PieceCol[MoveColor][mvPieceId];HistRow[0][a]=PieceRow[MoveColor][mvPieceId];HistType[0][a]=PieceType[MoveColor][mvPieceId];HistCol[2][a]=mvToCol;HistRow[2][a]=mvToRow;if(mvIsCastling){HistPieceId[1][a]=castleRook;HistCol[1][a]=PieceCol[MoveColor][castleRook];HistRow[1][a]=PieceRow[MoveColor][castleRook];HistType[1][a]=PieceType[MoveColor][castleRook]}else if(mvCapturedId>=0){HistPieceId[1][a]=mvCapturedId+16;HistCol[1][a]=PieceCol[1-
MoveColor][mvCapturedId];HistRow[1][a]=PieceRow[1-MoveColor][mvCapturedId];HistType[1][a]=PieceType[1-MoveColor][mvCapturedId]}else HistPieceId[1][a]=-1;Board[PieceCol[MoveColor][mvPieceId]][PieceRow[MoveColor][mvPieceId]]=0;if(mvCapturedId>=0){PieceType[1-MoveColor][mvCapturedId]=-1;PieceMoveCounter[1-MoveColor][mvCapturedId]++;Board[PieceCol[1-MoveColor][mvCapturedId]][PieceRow[1-MoveColor][mvCapturedId]]=0}PieceType[MoveColor][mvPieceId]=mvPieceOnTo;PieceMoveCounter[MoveColor][mvPieceId]++;PieceCol[MoveColor][mvPieceId]=
mvToCol;PieceRow[MoveColor][mvPieceId]=mvToRow;if(mvIsCastling){PieceMoveCounter[MoveColor][castleRook]++;PieceCol[MoveColor][castleRook]=mvToCol==2?3:5;PieceRow[MoveColor][castleRook]=mvToRow}Board[mvToCol][mvToRow]=PieceType[MoveColor][mvPieceId]*(1-2*MoveColor);if(mvIsCastling)Board[PieceCol[MoveColor][castleRook]][PieceRow[MoveColor][castleRook]]=PieceType[MoveColor][castleRook]*(1-2*MoveColor)}
function UndoMove(a){Board[mvToCol][mvToRow]=0;Board[HistCol[0][a]][HistRow[0][a]]=HistType[0][a]*(1-2*MoveColor);PieceCol[MoveColor][mvPieceId]=HistCol[0][a];PieceRow[MoveColor][mvPieceId]=HistRow[0][a];PieceType[MoveColor][mvPieceId]=HistType[0][a];PieceMoveCounter[MoveColor][mvPieceId]--;if(mvCapturedId>=0){PieceType[1-MoveColor][mvCapturedId]=mvCapturedId;PieceCol[1-MoveColor][mvCapturedId]=HistCol[1][a];PieceRow[1-MoveColor][mvCapturedId]=HistRow[1][a];PieceCol[1-MoveColor][mvCapturedId]=HistCol[1][a]}else if(mvIsCastling){PieceCol[MoveColor][castleRook]=
HistCol[1][a];PieceRow[MoveColor][castleRook]=HistRow[1][a];PieceMoveCounter[MoveColor][castleRook]--}}function Color(a){if(a<0)return 1;if(a>0)return 0;return 2}function sign(a){if(a>0)return 1;if(a<0)return-1;return 0}function SquareOnBoard(a,b){if(a<0||a>7)return false;if(b<0||b>7)return false;return true};