function repeatString(strInput, intCount) 
{
   var arrTmp = new Array(intCount+1);
   return arrTmp.join(strInput);
}

function jstranslate2()
{

/* INTRODUCTION
 javascript to translate pgn file from playchess.com chess to a pgn file that can be read by a chess viewer removing all the comments in the process
' 20070508
' (c) 2007 Jan Nordgreen

This program removes all the comment.
This program puts line breaks after each move.
*/

// PROGRAM 

// start loop 
counter = 0

// read inputfile 
instr = document.frm1.inputfile.value 

// remove all comments, a comment is on the form "{text}"
oldpos = -1
outstr = ""
while (instr.indexOf("{", oldpos + 1) != -1)
  {
  pos = instr.indexOf("{", oldpos)
  pos2 = instr.indexOf("}", oldpos + 1)
  outstr += instr.substring(oldpos + 1, pos)   
  oldpos = pos2
  }
  
// put line breaks after each move, e.g. "1. e4 e5" is a move
// look for "2.", when found put a line break before it, look for "3." ...
counter = 2
dummy = counter + "."
p = outstr.lastIndexOf("]") + 1 // we do not want to look in the header
while (outstr.indexOf(dummy) != -1)
  {
  pos = outstr.indexOf(dummy, p)
  outstr = outstr.substring(0, pos)  + '\n' + outstr.substring(pos)   
  counter ++
  dummy = counter + "."
}

// show the result
document.frm1.outputfile.value =  outstr + instr.substring(oldpos + 1)

/*
//  replace any ++ with + 
instr = instr.replace("++", "+")

// read a line of instr at a time 
if(document.all) 
  { // IE
  lines=instr.split("\r\n");
  }
else 
  { //Mozilla
  lines=instr.split("\n");
  }

// look at each line, i.e. move for White and Black 

for(var j=0; j<lines.length; j++) 
  {

  // split line - e.g. myarray[0] = "8.", myarray[1] = "b2-c3", myarray[2] = "b4xc3+" 
  myarray=lines[j].split(" ");
 
  // look at White's move (i=1) and Black's move (i=2) 
  for (i = 1; i < myarray.length; i++)  // instead of using 2 I use this since White may make the last move 
    {
       if (myarray[i] == "o-o")
         { 
         myarray[i] = "0-0"  
         if (i == 1)
           {
           board = board.substring(0, 4) + " " + board.substring(5) // remove king 
           board = board.substring(0, 7) + " " + board.substring(8) // remove rook
           board = board.substring(0, 6) + "K" + board.substring(7) // position king
           board = board.substring(0, 5) + "R" + board.substring(6) // position rook 
           }
         else
           {
           board = board.substring(0, 60) + " " + board.substring(61) // remove king
           board = board.substring(0, 63) + " " // remove rook 
           board = board.substring(0, 62) + "K" + board.substring(63) // position king 
           board = board.substring(0, 61) + "R" + board.substring(62) // position rook 
           }     
         }  //  if (myarray[i] == "o-o")
       else if (myarray[i] == "o-o-o")
         {
          myarray[i] = "0-0-0"
          if (i == 1)
            {
            board = board.substring(0, 4) + " " + board.substring(5) // remove king 
            board = " " + board.substring(1) // remove rook 
            board = board.substring(0, 2) + "K" + board.substring(3) // position king 
            board = board.substring(0, 3) + "R" + board.substring(4) // position rook
            }
         else
            {
            board = board.substring(0, 60) + " " + board.substring(61) // remove king
            board = board.substring(0, 56) + " " + board.substring(0, 57)  // remove rook 
            board = board.substring(0, 58) + "K" + board.substring(59) // position king 
            board = board.substring(0, 59) + "R" + board.substring(60) // position rook
            }
          }  // else if (myarray[i] = "o-o-o")
      else
          {
          // read startsquare
          startcolumn = myarray[i].substring(0, 1).charCodeAt(0) - 96  // a=1, b=2, ...
          startrow = myarray[i].substring(1, 2)
          
          // find out which piece is on startsquare
          // 'e4 -> '54', 5 col 4 row, (4-1) x 8 + 5 - 1 = 28 th position in board
          position = (startrow - 1) * 8 + startcolumn - 1
          piece = board.substring(position, position + 1)       
         
          // clear piece from startsquare
          // board = left(board, position - 1) & " " & mid(board, position + 1)         
          //alert('before clearing xxx' + board + 'xxx')
          board = board.substring(0, position) + " " + board.substring(position + 1) 
         
          // read endsquare
          endcolumn = myarray[i].substring(3, 4).charCodeAt(0) - 96  // a=1, b=2, ...
          endrow = myarray[i].substring(4, 5) 
          
          // put piece on endsquare, if queening promote the pawn to queen by default
          // I use replace in case the promoted piece gave check so the moved ends with "+"
          position = (endrow - 1) * 8 + endcolumn - 1
          if (myarray[i].indexOf("1") != -1 && piece == "P")          
            {
            board = board.substring(0, position) + "Q" + board.substring(position + 1)
            myarray[i] = myarray[i].replace("1","1=Q")
            }
          else if (myarray[i].indexOf("8") != -1 && piece == "P")   
            {
            board = board.substring(0, position) + "Q" + board.substring(position + 1)
            myarray[i] = myarray[i].replace("8","8=Q")               
            }
         else  
           {
            board = board.substring(0, position) + piece + board.substring(position + 1)
           }        
           
         // if RNBQK then add that letter to the move
         if ("RNBQK".indexOf(piece) != -1)   
           {
           myarray[i] = piece + myarray[i]
           }
                           
          } // if
          
    }  // for (i = 1; i <= myarray.length; i++)
    
    // write to outputfile
   dummy = ""
   for (k = 0; k < myarray.length; k++)
     {
     dummy = dummy + myarray[k] + " "
     }
     
   // write the default result if White moved last, I have to do it here before this line is written
   if (myarray.length == 2)
     {
     dummy = dummy + "1-0"
     result = -1
     }
     
   document.frm1.outputfile.value =  document.frm1.outputfile.value + "\r" + dummy   
   
   counter++  
       
  } // for(var j=0; j<lines.length; j++) 
  
  // write the default result if Black moved last
   counter++
  if (result != -1)
    {
    document.frm1.outputfile.value =  document.frm1.outputfile.value + "\r" + counter +  ". 1-0"    
    }
*/
return true
}

function jstranslate()
{

/* INTRODUCTION
 vbscript to translate history email file from yahoo games chess to a pgn file that can be read by a chess viewer
' 20070504
' (c) 2007 Jan Nordgreen

'When you play chess on Yahoo Games you can email yourself the match recorded. Unfortunately it is not in pgn format. 
' Here is a program that translates it into pgn format 'if you follow these guidelines carefully.

'    * (1) Store the file from Yahoo Games with the moves only in C:\game.txt. An example file.
'    * (2) Store the vbs program as C:\p2.vbs.
'    * (3) Run the vbs program from the command prompt at C:\. Type cscript p2.vbs and press Enter.
'    * (4) The pgn file is created as C:\game.pgn.
'    * (5) You have to manually edit the file C:\game.pgn a little bit. Use Notepad or a similar text editor.
'      (a) If there was a pawn promotion Yahoo Games does not indicate what the pawn promoted to. 
'           The program adds =Q by default so e.g. a7-a8 becomes a7-a8=Q. 'Replace =Q with =R, =N, =B if another piece was chosen.
'      (b) The program puts the result at the end as 1-0 by default. Change that to 1/2-1/2 or 0-1 if needed.
'      (c) Replace the default player information etc at the top.
*/

// PROGRAM 

// remember if the result (1-0 e.g.) has been written to the outputfile 
result = 0

// store where the pieces are on the board as a 64 character string
// start with a1 and move to the right and up 
board = "RNBQKBNR"
board = board + repeatString("P", 8)
board = board + repeatString(" ", 32)
board = board + repeatString("P", 8)
board = board + "RNBQKBNR"

// the header 
dummy   = "[Event \"EVENT\"]\r" 
dummy += "[Site \"SITE\"]\r" 
dummy += "[Date \"May 3 2007\"]\r" 
dummy += "[White \"NAME1\"]\r" 
dummy += "[Black \"NAME2\"]\r" 
dummy += "[Result \"1-0\"]\r" 
dummy += "[Round \"N\"]\r" 

// start loop 
counter = 0

// read inputfile 
instr = document.frm1.inputfile.value 
document.frm1.outputfile.value = dummy

//  replace any ++ with + 
instr = instr.replace("++", "+")

// read a line of instr at a time 
if(document.all) 
  { // IE
  lines=instr.split("\r\n");
  }
else 
  { //Mozilla
  lines=instr.split("\n");
  }

// look at each line, i.e. move for White and Black 

for(var j=0; j<lines.length; j++) 
  {

  // split line - e.g. myarray[0] = "8.", myarray[1] = "b2-c3", myarray[2] = "b4xc3+" 
  myarray=lines[j].split(" ");
 
  // look at White's move (i=1) and Black's move (i=2) 
  for (i = 1; i < myarray.length; i++)  // instead of using 2 I use this since White may make the last move 
    {
       if (myarray[i] == "o-o")
         { 
         myarray[i] = "0-0"  
         if (i == 1)
           {
           board = board.substring(0, 4) + " " + board.substring(5) // remove king 
           board = board.substring(0, 7) + " " + board.substring(8) // remove rook
           board = board.substring(0, 6) + "K" + board.substring(7) // position king
           board = board.substring(0, 5) + "R" + board.substring(6) // position rook 
           }
         else
           {
           board = board.substring(0, 60) + " " + board.substring(61) // remove king
           board = board.substring(0, 63) + " " // remove rook 
           board = board.substring(0, 62) + "K" + board.substring(63) // position king 
           board = board.substring(0, 61) + "R" + board.substring(62) // position rook 
           }     
         }  //  if (myarray[i] == "o-o")
       else if (myarray[i] == "o-o-o")
         {
          myarray[i] = "0-0-0"
          if (i == 1)
            {
            board = board.substring(0, 4) + " " + board.substring(5) // remove king 
            board = " " + board.substring(1) // remove rook 
            board = board.substring(0, 2) + "K" + board.substring(3) // position king 
            board = board.substring(0, 3) + "R" + board.substring(4) // position rook
            }
         else
            {
            board = board.substring(0, 60) + " " + board.substring(61) // remove king
            board = board.substring(0, 56) + " " + board.substring(0, 57)  // remove rook 
            board = board.substring(0, 58) + "K" + board.substring(59) // position king 
            board = board.substring(0, 59) + "R" + board.substring(60) // position rook
            }
          }  // else if (myarray[i] = "o-o-o")
      else
          {
          // read startsquare
          startcolumn = myarray[i].substring(0, 1).charCodeAt(0) - 96  // a=1, b=2, ...
          startrow = myarray[i].substring(1, 2)
          
          // find out which piece is on startsquare
          // 'e4 -> '54', 5 col 4 row, (4-1) x 8 + 5 - 1 = 28 th position in board
          position = (startrow - 1) * 8 + startcolumn - 1
          piece = board.substring(position, position + 1)       
         
          // clear piece from startsquare
          // board = left(board, position - 1) & " " & mid(board, position + 1)         
          //alert('before clearing xxx' + board + 'xxx')
          board = board.substring(0, position) + " " + board.substring(position + 1) 
         
          // read endsquare
          endcolumn = myarray[i].substring(3, 4).charCodeAt(0) - 96  // a=1, b=2, ...
          endrow = myarray[i].substring(4, 5) 
          
          // put piece on endsquare, if queening promote the pawn to queen by default
          // I use replace in case the promoted piece gave check so the moved ends with "+"
          position = (endrow - 1) * 8 + endcolumn - 1
          if (myarray[i].indexOf("1") != -1 && piece == "P")          
            {
            board = board.substring(0, position) + "Q" + board.substring(position + 1)
            myarray[i] = myarray[i].replace("1","1=Q")
            }
          else if (myarray[i].indexOf("8") != -1 && piece == "P")   
            {
            board = board.substring(0, position) + "Q" + board.substring(position + 1)
            myarray[i] = myarray[i].replace("8","8=Q")               
            }
         else  
           {
            board = board.substring(0, position) + piece + board.substring(position + 1)
           }        
           
         // if RNBQK then add that letter to the move
         if ("RNBQK".indexOf(piece) != -1)   
           {
           myarray[i] = piece + myarray[i]
           }
                           
          } // if
          
    }  // for (i = 1; i <= myarray.length; i++)
    
    // write to outputfile
   dummy = ""
   for (k = 0; k < myarray.length; k++)
     {
     dummy = dummy + myarray[k] + " "
     }
     
   // write the default result if White moved last, I have to do it here before this line is written
   if (myarray.length == 2)
     {
     dummy = dummy + "1-0"
     result = -1
     }
     
   document.frm1.outputfile.value =  document.frm1.outputfile.value + "\r" + dummy   
   
   counter++  
       
  } // for(var j=0; j<lines.length; j++) 
  
  // write the default result if Black moved last
   counter++
  if (result != -1)
    {
    document.frm1.outputfile.value =  document.frm1.outputfile.value + "\r" + counter +  ". 1-0"    
    }

return true
}
