
temp=document.G.data.value;
id=temp.substr(0,11);
if(id.substr(0,1)==" "){id=temp.substr(1,10);}
white=temp.substr(11,15);
black=temp.substr(26,15);
thinktime=temp.substr(41,8);
whitetimeremaining=temp.substr(49,8);
blacktimeremaining=temp.substr(57,8);
toplay=temp.substr(65,5);
whiteofferdraw=temp.substr(70,1);
blackofferdraw=temp.substr(71,1);
addtime=temp.substr(72,8);
lastmovedate=temp.substr(80,10);
matchid=temp.substr(90,11);
if(matchid.substr(0,1)==" "){matchid=temp.substr(91,10);}
whiterating=temp.substr(101,4);
blackrating=temp.substr(105,4);
board=temp.substr(109,64);
color=temp.substr(173,1);

fields=document.G.f.value.split("|");

capturedpieces="";
bdata=new Array;
movedata= new Array;
movelist=ParseMoveList(fields[2]);
//movelist=fields[2];
toplay=temp.substr(65,5);
bdata[state]=board;

document.writeln("<title>"+white+" vs. "+black+"</title>");
document.writeln("<body bgcolor=ffffdd>");

light="ffffff";
dark="bbbbbb";
border="dddddd";
bordersize=2;
piecesize=32;

if(color == 'B'){
   incr=-1;
   init=7;
   end=-1;
   inv=1;
} else {
   incr=1;
   init=0;
   end=8;
   inv=0;
}

document.writeln("<center><a href='/viewgameold.cgi?id="+id+"'><font size=1>Old Display</font></a></center>");
document.writeln("<center><font size=2>Game: "+id+"<br><a href='/follow.cgi?mode=add&id="+id+"'>(Follow this game)</a></font></center>");
if(fields[4] != ""){
   document.writeln("<center>"+fields[4]+"</center>");
}

if(navigator.userAgent.indexOf("WebTV")==-1){
}

document.writeln("<font size=4 color=red><b>");
drawcolor="";
if(whiteofferdraw == "1"){drawcolor="White";}
if(blackofferdraw == "1"){drawcolor="Black";}
if(drawcolor != ""){
   document.writeln("<br><center>"+drawcolor+" offers a draw.  Type acceptdraw in the move box to accept, or make a move to decline.");
}
document.writeln("</b></font></center>");

document.writeln("<table border=0><tr><td valign=top>");

document.writeln("<center><form name=Playback><input type=button value=\"<<\" onClick=\"ToBegining();\">");
document.writeln("<input type=button value=\"<\" onClick=\"Backward();\">");
document.writeln("<input type=button value=\">\" onClick=\"Forward();\">");
document.writeln("<input type=button value=\">>\" onClick=\"ToEnd();\">");
document.writeln("<input type=text name=MoveNumber value=\""+movenumber+"\" size=11>");
document.writeln("</form>");
document.writeln("<table border=0 cellspacing=0>");

document.writeln("<td bgcolor="+border+">&nbsp;</td>");
for(x=init;x!=end;x+=incr){
   document.writeln("<td bgcolor="+border+" align=center><font size="+bordersize+">"+String.fromCharCode(x+65)+"</font></td>");
}
document.writeln("<td bgcolor="+border+">&nbsp;</td>");
count=0;
for(x=init;x!=end;x+=incr){
   document.writeln("<tr>");
   document.writeln("<td bgcolor="+border+"><font size="+bordersize+">"+(9-(x+1)));
   document.writeln(" </font></td>");
   for(y=init;y!=end;y+=incr){
      count++;
      if(x/2==Math.floor(x/2)){
         if(y/2==Math.floor(y/2)){
            bcolor=light;
         } else {
            bcolor=dark;
         }
      } else {
         if(y/2==Math.floor(y/2)){
            bcolor=dark;
         } else {
            bcolor=light;
         }
      }
      z=board.charAt(x*8+y);
      if(z == "."){z="src=\"/images/blank.gif\">";}
      if(z == "P"){z="src=\"/images/wp.gif\">";}
      if(z == "R"){z="src=\"/images/wr.gif\">";}
      if(z == "N"){z="src=\"/images/wn.gif\">";}
      if(z == "B"){z="src=\"/images/wb.gif\">";}
      if(z == "K"){z="src=\"/images/wk.gif\">";}
      if(z == "Q"){z="src=\"/images/wq.gif\">";}
      if(z == "p"){z="src=\"/images/bp.gif\">";}
      if(z == "r"){z="src=\"/images/br.gif\">";}
      if(z == "n"){z="src=\"/images/bn.gif\">";}
      if(z == "b"){z="src=\"/images/bb.gif\">";}
      if(z == "q"){z="src=\"/images/bq.gif\">";}
      if(z == "k"){z="src=\"/images/bk.gif\">";}
      document.writeln("<td bgcolor="+bcolor+">");
      document.writeln("<a href=\"JavaScript:BoardClick('"+String.fromCharCode(97+y)+(9-(x+1))+"',document.sq"+x+""+y+");\"><img border=0 name=\"sq"+x+""+y+"\" "+z+"</a></td>");
   }
   document.writeln("<td bgcolor="+border+"><font size="+bordersize+">"+(9-(x+1)));
   document.writeln(" </font></td>");
   document.writeln('</tr>');
}
document.writeln("<td bgcolor="+border+">&nbsp;</td>");
for(x=init;x!=end;x+=incr){
   document.writeln("<td bgcolor="+border+" align=center><font size="+bordersize+">"+String.fromCharCode(x+65)+"</font></td>");
}
document.writeln("<td bgcolor="+border+">&nbsp;</td>");
document.writeln("</table></center>");
document.writeln("<p>");

wcap="";
bcap="";
for(x=0;x<=capturedpieces.length;x++){
   z=capturedpieces.charAt(x);
   if(z != "."){
      t=z;
      if(z == "P"){z="<img width=16 src=\"/images/wp.gif\">";}
      if(z == "R"){z="<img width=16 src=\"/images/wr.gif\">";}
      if(z == "N"){z="<img width=16 src=\"/images/wn.gif\">";}
      if(z == "B"){z="<img width=16 src=\"/images/wb.gif\">";}
      if(z == "K"){z="<img width=16 src=\"/images/wk.gif\">";}
      if(z == "Q"){z="<img width=16 src=\"/images/wq.gif\">";}
      if(z == "p"){z="<img width=16 src=\"/images/bp.gif\">";}
      if(z == "r"){z="<img width=16 src=\"/images/br.gif\">";}
      if(z == "n"){z="<img width=16 src=\"/images/bn.gif\">";}
      if(z == "b"){z="<img width=16 src=\"/images/bb.gif\">";}
      if(z == "q"){z="<img width=16 src=\"/images/bq.gif\">";}
      if(z == "k"){z="<img width=16 src=\"/images/bk.gif\">";}
      if(t.charCodeAt(0)<90){
         wcap+="<td>"+z+"</td>";
      } else {
         bcap+="<td>"+z+"</td>";
      }
   }
}
document.writeln("<table border=0 cellspacing=0>");
document.writeln("<tr>");
document.writeln(wcap+"</tr><tr>");
document.writeln(bcap+"</tr>");
document.writeln("</table>");
   
blackcomment="";
whitecomment="";
if(fields[3] != ""){
   if(fields[1] != ""){fields[1]="<table border=1><tr><td bgcolor=aaaadd>"+fields[1]+"</td></tr></table>";}
   fields[3]="<table border=1><tr><td bgcolor=ddaaaa>"+fields[3]+"</td></tr></table>";
   whitecomment=fields[3];
   blackcomment=fields[1];
   if(toplay == "white"){
      blackcomment=fields[3];
      whitecomment=fields[1];
   }
}

document.writeln("</td><td>");

ctime=new Date;
document.writeln("<table border=1 cellspacing=0>");
if(toplay == "white"){
   whitetimeremaining-=Math.floor(ctime.getTime()/1000)-lastmovedate;
} else {
   blacktimeremaining-=Math.floor(ctime.getTime()/1000)-lastmovedate;
}
wtime=parsetime(whitetimeremaining);
btime=parsetime(blacktimeremaining);

document.write("<tr><td");
if(toplay == "black"){document.write(" bgcolor=ffdddd");}
//document.writeln("><b>Black: <a href=\"command.cgi?command=about&p1="+black+"\">"+black+"</a>("+blackrating+")</b><br>"+btime);
document.writeln("><b>Black: "+black+"("+blackrating+")</b><br>"+btime);
document.writeln(blackcomment);
document.writeln("</td></tr>");
document.writeln("<tr><td");
if(toplay == "white"){document.write(" bgcolor=ffdddd");}
document.writeln("><b>White: "+white+"("+whiterating+")</b><br>"+wtime);
document.writeln(whitecomment);
document.writeln("</td></tr>");

document.writeln("<tr><td>");
document.writeln("<table border=0>");
document.writeln("<tr><td colspan=2><font size=1>For help on move notation, click <a href=/sanfaq.html>here</a>.</font></td></tr>");
document.writeln("<form name=MoveForm method=get action=command.cgi onSubmit=\"return(CheckPossition());\">");
document.writeln("<input type=hidden name=command value=move>");
document.writeln("<input type=hidden name=p1 value=\""+id+"\">");
document.writeln("<tr><td>Move:</td><td><input type=text size=8 name=p2>");
document.writeln("<font size=1>(PGN, resign, draw, acceptdraw)</font></td>");
document.writeln("<td rowspan=8>");
document.writeln("<table border=1 bgcolor=ffffff><tr><td><font size=1>");
document.writeln("<a href=\"/index.shtml\">Home</a><br>");
document.writeln("</font></tr></td></table></td>");
document.writeln("</tr>");
document.writeln("<tr><td>Offer Draw:</td><td><input type=checkbox name=p6 value='y'>");
document.writeln("&nbsp;<font size=1>Submit with your move</font></td></tr>");
document.writeln("<tr><td>Promote:</td><td><select name=p5>");
document.writeln("<option value=\"Q\">Queen");
document.writeln("<option value=\"R\">Rook");
document.writeln("<option value=\"N\">Knight");
document.writeln("<option value=\"B\">Bishop");
document.writeln("</select></tr>");
document.writeln("<tr><td>Comment:</td><td><textarea name=p3 wrap=virtual rows=3></textarea></td></tr>");
document.writeln("<tr><td></td><td><input type=submit value=Move></td></tr>");
document.write("<tr><td>Notepad:<br>(<a href='javascript:EditNotepad();'>Edit</a>)</td><td><textarea name=p4 wrap=virtual rows=3>");
document.write(fields[0]);
document.writeln("</textarea></td></tr>");
document.writeln("<tr><td colspan=3><font size=1>The notepad is for storing information only you should see.  ");
document.writeln("Use it to remind yourself of your plan.</font></td></tr>");
document.writeln("</table>");
document.writeln("</form>");
document.writeln("<hr><font size=2><b>Note:</b> You can make your move by clicking on the board.  ");
document.writeln("Click on the piece you wish to move, then click on the destination ");
document.writeln("square.  You will see the coordinates displayed in the <b>move</b> ");
document.writeln("box.  Press <b>move</b> when you're ready to commit.");
document.writeln("</font>");

document.writeln("</td></tr></table>");

document.writeln("</td></tr></table>");

document.writeln("<center><font size=1>Time Controls: "+parsetime(thinktime)+"+"+parsetime(addtime)+"</font></center>");
document.writeln("<center>To view the rest of the games in this match click here:");
document.writeln("<a href=\"/match/"+matchid+"\">"+matchid+"</a><BR>");
document.writeln("<td>");
document.writeln("<a href=\"/pgn/"+id+"\"><b>View PGN Move List</b></a>");

document.writeln("<p><table border=1><tr>");
document.writeln("<td>");
document.writeln("<td>");
document.writeln("<td bgcolor=ffdddd valign=top>"+movelist+" <b>"+fields[4]+"</b></table>");

document.writeln("<form name=pieces>");
document.writeln("<input type=hidden name=first value=\"\">");
document.writeln("<input type=hidden name=second value=\"\">");
document.writeln("</form>");

for(x=0;x<30;x++){
   document.writeln("<br>");
}

document.writeln("</body>");

if((state%2)==1){
   document.Playback.MoveNumber.value=(Math.floor(state/2)-0+1)+". "+movedata[state];
} else {
   document.Playback.MoveNumber.value=(Math.floor(state/2)-0)+"..."+movedata[state];
}

function CheckPossition(){
   if(state != lastmove){
      return confirm("The possition displayed on the board is not the actual possition in the game.  The display has changed as a result of using the playback feature.  To get the correct possition to display, either press >>, or press your browser's refresh option.\n\nDo you wish to continue?");
   }
   return true;
}

function EditNotepad(){
   window.open("/editnotepad.cgi?id="+id,"_new","height=400,width=400","");
}

function ToBegining(){
   document.MoveForm.p2.value='';
   state=1;
   Show();
}

function ToEnd(){
   document.MoveForm.p2.value='';
   state=lastmove-0;
   Show();
}

function Forward(){
   document.MoveForm.p2.value='';
   state++;
   if(state-0 > lastmove-0){
        state=lastmove-0;
   }
   Show();
}

function Backward(){
   document.MoveForm.p2.value='';
   state--;
   if(state<1){state=1;}
   Show();
}

function Show(){
   x=0;while((document.images[x].name != 'sq00')&&(document.images[x].name!='sq77')){x++;};
   offset=x;
   if((state%2)==1){
      document.Playback.MoveNumber.value=(Math.floor(state/2)-0+1)+". "+movedata[state];
   } else {
      document.Playback.MoveNumber.value=(Math.floor(state/2)-0)+"..."+movedata[state];
   }
   for(x=0;x<64;x++){
      p=bdata[state].charAt(x);
      s="";
      if(p=="r"){s="/images/br.gif";}
      if(p=="n"){s="/images/bn.gif";}
      if(p=="b"){s="/images/bb.gif";}
      if(p=="q"){s="/images/bq.gif";}
      if(p=="k"){s="/images/bk.gif";}
      if(p=="p"){s="/images/bp.gif";}
      if(p=="."){s="/images/blank.gif";}
      if(p=="R"){s="/images/wr.gif";}
      if(p=="N"){s="/images/wn.gif";}
      if(p=="B"){s="/images/wb.gif";}
      if(p=="Q"){s="/images/wq.gif";}
      if(p=="K"){s="/images/wk.gif";}
      if(p=="P"){s="/images/wp.gif";}

      if(color == 'B'){
         document.images[63-x+offset].src=s;
      } else {
         document.images[x+offset].src=s;
      }
   }
}

function BoardClick(sq,img){
  if(document.MoveForm.p2.value==''){
     if(!(img.src.indexOf('blank.gif')>0)){
        document.MoveForm.p2.value=sq;
     }
  } else if(document.MoveForm.p2.value.length<4){
        document.MoveForm.p2.value=document.MoveForm.p2.value+sq;
  } else if(document.MoveForm.p2.value.length==4){
	     var temp=document.MoveForm.p2.value;
		  if(!(temp=="e1g1" || temp=="e1c1" || temp=="e8g8" || temp=="e8c8")){
           document.MoveForm.p2.value="";
		  }
  }
  if(document.pieces.first.value==''){
     if(!(img.src.indexOf('blank.gif')>0)){
        document.pieces.first.value=img.name;
        offs=img.src.lastIndexOf('/');
        img.src=img.src.substring(0,offs+1)+'s'+img.src.substring(offs+1);
     }
  } else {
     if(img.name == document [document.pieces.first.value].name){
        temp=document [document.pieces.first.value].src;
        offs=temp.lastIndexOf('/');
        img.src=temp.substring(0,offs+1)+temp.substring(offs+2);
        document.MoveForm.p2.value='';
        document.pieces.first.value='';
     } else {
        temp=document [document.pieces.first.value].src;
        offs=temp.lastIndexOf('/');
        img.src=temp.substring(0,offs+1)+temp.substring(offs+2);
        document [document.pieces.first.value].src='/images/blank.gif';
        document.pieces.first.value='';
     }
   }
}

function parsetime(time){

   d=Math.floor(time/86400);
   time-=d*86400;
   h=Math.floor(time/3600);
   time-=h*3600;
   m=Math.floor(time/60);
   time-=m*60;
   s=time;

   return d+" days, "+h+" hours, "+m+" min, "+s+" sec";
}

function ParseMoveList(moves){
   var movelist,x,board,scol,srow,erow,ecol,source,dest,piece,move,temp,capture;

   board= new Array("r","n","b","q","k","b","n","r","p","p","p","p","p","p","p","p",
                    ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".",".",
                    ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".",".",
                    "P","P","P","P","P","P","P","P","R","N","B","Q","K","B","N","R");
   state=1;
   movenumber=0;
   movelist="";
   x=0;
   while(x<moves.length){
      scol=((moves.charCodeAt(x)-48)&56)/8+1;
      srow=((moves.charCodeAt(x)-48)&7)+1;
      ecol=((moves.charCodeAt(x+1)-48)&56)/8+1;
      erow=((moves.charCodeAt(x+1)-48)&7)+1;
      source=((8-srow)*8)+(scol-1);
      dest=((8-erow)*8)+(ecol-1);
      capture="";
      if(board[dest]!="."){
         capturedpieces+=board[dest];
         capture="x";
      }
      piece=board[source];

      move="";
      switch(piece.toUpperCase()){
         case "P":
            move=String.fromCharCode(ecol-0+96)+erow;
            if(ecol!=scol){
               move=String.fromCharCode(scol-0+96)+"x"+move;
               if(board[dest]=="."){
                  if(srow>erow){
                     board[dest-8]=".";
                     capturedpieces+="P";
                  } else {
                     board[dest+8]=".";
                     capturedpieces+="p";
                  }
               }
            }
            break;
         case "K":
            if((scol==5) && (ecol==3)){
               move="O-O-O";
               if(piece=="k"){
                  board[0]=".";
                  board[3]="r";
               } else {
                  board[56]=".";
                  board[59]="R";
               }
            } else if((scol==5) && (ecol==7)){
               move="O-O";
               if(piece=="k"){
                  board[7]=".";
                  board[5]="r";
               } else {
                  board[63]=".";
                  board[61]="R";
               }
            } else {
               move=piece.toUpperCase()+String.fromCharCode(scol-0+96)+srow+capture+String.fromCharCode(ecol-0+96)+erow;
            }
            break;
         default:
            move=piece.toUpperCase()+String.fromCharCode(scol-0+96)+srow+capture+String.fromCharCode(ecol-0+96)+erow;
            //move=piece.toUpperCase()+String.fromCharCode(ecol-0+96)+erow;
      }
      board[dest]=board[source];
      board[source]=".";
      
      if(state%2==1){
         movenumber++;
         movelist+=movenumber+".";
      }
      
      if(((piece=="P") && (erow==8)) ||
         ((piece=="p") && (erow==1))){
         board[dest]=moves.substr(x+2,1);
         if(state%2==0){board[dest]=board[dest].toLowerCase();}
         move+="="+moves.substr(x+2,1);
         x++;
      }
      movelist+=move+" ";

      bdata[state]=board.join("");
      movedata[state]=move;
      state++;
      
      x+=2;
   }
   state--;
   lastmove=state;
   if(state%2==0){
      toplay="white";
   } else {
      toplay="black";
   }
   return(movelist);
}
