<!-- hide script from old browsers
//------------------------------------------------------------------------------
var vers = navigator.appVersion;
if (vers.indexOf("3.") == 0 || vers.indexOf("2.") == 0)
{
    alert("Welcome to mohaniyer.com. Sorry, but you must use Navigator 4.0 or Internet Explorer 4.0 to browse this site - Mohan Iyer");

}
//------------------------------------------------------------------------------

var chesspieces = new Array(9);
var colors = new Array(9);
var pieces = new Array(9);
var swapStore = new Image(35,35);
var  storedToImage = new Image(35,35);
var  storedFromImage = new Image(35,35);
var upperCaseArray = new Array("A","B","C","D","E","F","G","H");
var externalColsArray = new Array("a","b","c","d","e","f","g","h");
var internalColsArray = new Array("1","2","3","4","5","6","7","8");
var rowsArray = new Array("1","2","3","4","5","6","7","8");
var i=0,j=0;
//------------------------------------------------------------------------------

for (i = 0;i < 9;i++)
{
  chesspieces[i] = new Array(9);
  colors[i] = new Array(9);
  pieces[i] = new Array(9);
  for (j = 0;j < 9;j++)
  {
    chesspieces[i][j] = new Image(35,35);
  }
}
//------------------------------------------------------------------------------

for (i=1;i<9;i++)
{
  for (j=1;j<3;j++)
  {
    chesspieces[i][j].src = eval("'images/chess" + i + j + ".gif'");
    colors[i][j] = "b";
  }
}
//------------------------------------------------------------------------------

for (i=1;i<9;i++)
{
  for (j=7;j<9;j++)
  {
    chesspieces[i][j].src = eval("'images/chess" + i + j + ".gif'");
    colors[i][j] = "w";
  }
}
//------------------------------------------------------------------------------

for (i=1;i<9;i++)
{
  for (j=3;j<7;j++)
  {
    chesspieces[i][j].src = "images/chess_blank.gif";
    colors[i][j] = "n";
    pieces[i][j] = "blank";
  }
}
//------------------------------------------------------------------------------

for (i=1;i<9;i++)
{
  pieces[i][2] = "blackpawn";
  pieces[i][7] = "whitepawn";
}
pieces[1][1] = "blackrook";
pieces[1][8] = "whiterook";
pieces[2][1] = "blackknight";
pieces[2][8] = "whiteknight";
pieces[3][1] = "blackelephant";
pieces[3][8] = "whiteelephant";
pieces[4][1] = "blackcounsellor";
pieces[4][8] = "whiteking";
pieces[5][1] = "blackking";
pieces[5][8] = "whitecounsellor";
pieces[6][1] = "blackelephant";
pieces[6][8] = "whiteelephant";
pieces[7][1] = "blackknight";
pieces[7][8] = "whiteknight";
pieces[8][1] = "blackrook";
pieces[8][8] = "whiterook";
//------------------------------------------------------------------------------

var layerRef="null", layerStyleRef="null";
var styleSwitch="null";
var ns=0, ie=0;
var lyrName="null";
var colName="null",rowName="null";
var fileStr="null";
var fileArr="null";
var ArrCount="null";
var swapImage="null", ImgName="null";
var noofWhiteMoves=0;
var noofBlackMoves=0;
var firstTime=1;
var t="null", t1="null", time="null", te="null", se="null";
var timeDisplay="null"; var timeWhiteDisplay="null";
var timeBlackDisplay="null";
var whiteFromPiece="null";
var whiteToPiece="null";
var whiteColMovedFrom="null", whiteRowMovedFrom="null";
var whiteColMovedTo="null", whiteRowMovedTo="null";
var blackFromPiece="null";
var blackToPiece="null";
var blackColMovedFrom="null", blackRowMovedFrom="null";
var blackColMovedTo="null", blackRowMovedTo="null";
var whiteMoved=0, blackMoved=1;
var bkingMadeKnightMove=0;
var bkingJustMadeAKnightMove=0;
var wkingMadeKnightMove=0;
var wkingJustMadeAKnightMove=0;
var fromValueFilled=0;
var isItCheck="n";
var strCheckText="null";
var gameStarted=0;
var goAhead="n";
var canbeUndone="n";
var gameOver = 0;
var  storedColFrom = "null";
var  storedColTo = "null";
var  storedColorFrom = "null";
var  storedRowFrom = "null";
var  storedRowTo = "null";
var  storedPiece = "null";
var  storedColorTo = "null";
var  storedToPiece = "null";
var  storedFromPiece = "null";
//------------------------------------------------------------------------------

function detectBrowser()
{
  if (navigator.appName == "Netscape") {
     ns = 1;
     layerStyleRef = "layer.";
     layerRef = "document.layers";
     styleSwitch = "";
  }
  else {
     ie = 1;
     layerStyleRef = "layer.style.";
     layerRef = "document.all";
     styleSwitch = ".style";
  }
}
//------------------------------------------------------------------------------

function arrangePieces()
{
  for (i = 1;i < 9;i++) {
    for (j = 1;j < 9;j++) {
       eval("lyrName = 'piece" + i + j + "'") ;
       topPos = j * 40;
       leftPos = i * 40;
       if (ie) { topPos = eval(topPos) + "px"; 
          leftPos = eval(leftPos) + "px";}
       if (ns) {
          eval('document.layers["'+lyrName+'"].visibility =' + '"show"');}
       else { 
          eval('document.all["'+lyrName+'"].style.visibility =' + '"visible"');}
       eval(layerRef+'["'+lyrName+'"]'+styleSwitch+'.top = topPos'); 
       eval(layerRef+'["'+lyrName+'"]'+styleSwitch+'.left = leftPos'); 
     }
   }
   for (i = 1;i < 9;i++) {
       eval("colName = 'col" + i + "'") ;
       eval("rowName = 'row" + i + "'") ;
       topPos = i * 42;
       leftPos = i * 42;
       if (ie) { topPos = eval(topPos) + "px"; 
          leftPos = eval(leftPos) + "px";}
       eval(layerRef+'["'+colName+'"]'+styleSwitch+'.left = leftPos'); 
       eval(layerRef+'["'+rowName+'"]'+styleSwitch+'.top = topPos'); 
    }
 if (ns) {
     document.chessMove.document.chessForm.moveFrom.value = ""
     document.chessMove.document.chessForm.moveTo.value = ""
     document.chessStats.document.chessStatForm.lastMoveTime.value = 0;
     document.chessStats.document.chessStatForm.timeElapsed.value = 0;
     document.chessStats.document.chessStatForm.whiteMoves.value = 0;
     document.chessStats.document.chessStatForm.blackMoves.value = 0; }
  else {
     document.chessForm.moveFrom.value = ""
     document.chessForm.moveTo.value = ""
     document.chessStatForm.lastMoveTime.value = 0;
     document.chessStatForm.timeElapsed.value = 0;
     document.chessStatForm.whiteMoves.value = 0;
     document.chessStatForm.blackMoves.value = 0;}
}
//------------------------------------------------------------------------------

function makeWhiteMove()
{
  if (gameOver == 1)
  {
     alert("Game over. Setup board and begin a new game.");
  }
  else {
  if (blackMoved == 1)
  {
   if (ns) {
     document.layers["visitMyHome"].visibility = "show";
     document.layers["someTip"].visibility = "hide";}
   else {
     document.all["visitMyHome"].style.visibility = "visible";
     document.all["someTip"].style.visibility = "hidden";}
    WhiteMove();
  }
  else
  {
   if (ns) {
     document.layers["validationText"].visibility = "show";}
   else {
     document.all["validationText"].style.visibility = "visible";}
  } }
}
//------------------------------------------------------------------------------

function makeBlackMove()
{
  if (gameOver == 1)
  {
     alert("Game over. Setup board and begin a new game.");
  }
  else {
  if (whiteMoved == 1)
  {
    BlackMove();
  }
  else
  {
   if (ns) {
     document.layers["validationText"].visibility = "show";}
   else {
     document.all["validationText"].style.visibility = "visible";}
  }}
}
//------------------------------------------------------------------------------

function WhiteMove()
{
 if (ns) {
     document.layers["validationText"].visibility = "hide";
     whiteFromPiece = document.chessMove.document.chessForm.moveFrom.value;
     whiteToPiece = document.chessMove.document.chessForm.moveTo.value;}
 else {
     document.all["validationText"].style.visibility = "hidden";
     whiteFromPiece = document.chessForm.moveFrom.value;
     whiteToPiece = document.chessForm.moveTo.value;}
convrtCase("w");
goAhead = validateValue(whiteFromPiece);
if (goAhead == "y")
{
  goAhead = validateValue(whiteToPiece);
}
if (goAhead == "y")
{
 for (i=0;i<8;i++)
 {
  if (whiteFromPiece.substring(0,1)==externalColsArray[i]) 
  {
    whiteColMovedFrom = internalColsArray[i];
    whiteRowMovedFrom = whiteFromPiece.substring(1,2);
    i = 9;
  }
 }
 for (i=0;i<8;i++)
 {
  if (whiteToPiece.substring(0,1)==externalColsArray[i]) 
  {
    whiteColMovedTo = internalColsArray[i];
    whiteRowMovedTo = whiteToPiece.substring(1,2);
    i = 9;
  }
 }
 goAhead = findOriginPiece("w",whiteColMovedFrom,whiteRowMovedFrom);
 if (goAhead == "y")
 { 
  goAhead = findDestinationPiece("w",whiteColMovedTo,whiteRowMovedTo);
 }
 if (goAhead == "y")
 {
   goAhead = doCheck("w",whiteColMovedFrom,whiteRowMovedFrom,whiteColMovedTo,whiteRowMovedTo);
 }
 if (goAhead != "y")
 { 
   if (ns) {
    document.layers["validationText"].visibility = "show";}
   else {
    document.all["validationText"].style.visibility = "visible";}
 }
}
else
{ 
  if (ns) {
    document.layers["validationText"].visibility = "show";}
  else {
    document.all["validationText"].style.visibility = "visible";}
} 
if (goAhead == "y")
{
  whiteMoved = 1;
  blackMoved = 0;
  noofWhiteMoves++;
  if (gameStarted == 0) 
  {
   getTimeInfo("t"); 
   gameStarted = 1;
  }
  getTimeInfo("b");
  if (ns) {
     document.chessMove.document.chessForm.moveFrom.value = ""
     document.chessMove.document.chessForm.moveTo.value = ""
     document.chessStats.document.chessStatForm.whiteMoves.value = noofWhiteMoves;
     document.chessStats.document.chessStatForm.blackMoves.value = noofBlackMoves; }
  else {
     document.chessForm.moveFrom.value = ""
     document.chessForm.moveTo.value = ""
     document.chessStatForm.whiteMoves.value = noofWhiteMoves;
     document.chessStatForm.blackMoves.value = noofBlackMoves;}
  canbeUndone=1;
  if (ns) {
    document.layers["whiteMoveText"].visibility = "hide";
    document.layers["blackMoveText"].visibility = "show"; 
    document.layers["undoText"].visibility = "show";}
   else {
    document.all["whiteMoveText"].style.visibility = "hidden";
    document.all["blackMoveText"].style.visibility = "visible";
    document.all["undoText"].style.visibility = "visible";}
 isItCheck = checkForCheck();
 if (isItCheck == "y")
 {
   if (ns)
   {
    document.checkSound.play(false);
   }
   else
   {
    document.all.checkSound.loop= "1"
    document.all.checkSound.src= "midi/Twitch.wav"
   }
 }
}
}
//------------------------------------------------------------------------------

function BlackMove()
{
 if (ns) {
     document.layers["validationText"].visibility = "hide";
     blackFromPiece = document.chessMove.document.chessForm.moveFrom.value;
     blackToPiece = document.chessMove.document.chessForm.moveTo.value;}
 else {
     document.all["validationText"].style.visibility = "hidden";
     blackFromPiece = document.chessForm.moveFrom.value;
     blackToPiece = document.chessForm.moveTo.value;}
convrtCase("b");
goAhead = validateValue(blackFromPiece);
if (goAhead == "y")
{
  goAhead = validateValue(blackToPiece);
}
if (goAhead == "y")
{
 for (i=0;i<8;i++)
 {
  if (blackFromPiece.substring(0,1)==externalColsArray[i]) 
  {
    blackColMovedFrom = internalColsArray[i];
    blackRowMovedFrom = blackFromPiece.substring(1,2);
    i = 9;
  }
 }
 for (i=0;i<8;i++)
 {
  if (blackToPiece.substring(0,1)==externalColsArray[i]) 
  {
    blackColMovedTo = internalColsArray[i];
    blackRowMovedTo = blackToPiece.substring(1,2);
    i = 9;
  }
 }
 goAhead = findOriginPiece("b",blackColMovedFrom,blackRowMovedFrom);
 if (goAhead == "y")
 {
  goAhead = findDestinationPiece("b",blackColMovedTo,blackRowMovedTo);
 }
 if (goAhead == "y")
 {
   goAhead = doCheck("b",blackColMovedFrom,blackRowMovedFrom,blackColMovedTo,blackRowMovedTo);
 }
 if (goAhead != "y")
 { 
   if (ns) {
    document.layers["validationText"].visibility = "show";}
   else {
    document.all["validationText"].style.visibility = "visible";}
 }  
}
else 
{
  if (ns)
  {
    document.layers["validationText"].visibility = "show";}
  else {
    document.all["validationText"].style.visibility = "visible";}
}
if (goAhead == "y")
{
  whiteMoved = 0;
  blackMoved = 1;
  noofBlackMoves++;
  getTimeInfo("w");
  if (ns) {
     document.chessMove.document.chessForm.moveFrom.value = ""
     document.chessMove.document.chessForm.moveTo.value = ""
     document.chessStats.document.chessStatForm.whiteMoves.value = noofWhiteMoves;
     document.chessStats.document.chessStatForm.blackMoves.value = noofBlackMoves; }
  else {
     document.chessForm.moveFrom.value = ""
     document.chessForm.moveTo.value = ""
     document.chessStatForm.whiteMoves.value = noofWhiteMoves;
     document.chessStatForm.blackMoves.value = noofBlackMoves;}
  canbeUndone=1;
  if (ns) {
    document.layers["whiteMoveText"].visibility = "show";
    document.layers["blackMoveText"].visibility = "hide"; 
    document.layers["undoText"].visibility = "show";}
   else {
    document.all["whiteMoveText"].style.visibility = "visible";
    document.all["blackMoveText"].style.visibility = "hidden";
    document.all["undoText"].style.visibility = "visible";}
 isItCheck = checkForCheck();
 if (isItCheck == "y")
 {
   if (ns)
   {
    document.checkSound.play(false);
   }
   else
   {
    document.all.checkSound.loop= "1"
    document.all.checkSound.src= "midi/Twitch.wav"
   }
 }
}
}
//------------------------------------------------------------------------------
function fillFromToValues(col, row)
{
  var tobeFilled = "null";
  var colLetter="null";
  if (fromValueFilled == "1")
  {
    tobeFilled = "to";
    fromValueFilled = "0";
  }
  else
  {
    tobeFilled = "from";
    fromValueFilled = "1";
  }
  colLetter = externalColsArray[col - 1];
  if (tobeFilled == "from")
  {
    if (ns)
    {
      document.chessMove.document.chessForm.moveFrom.value = colLetter + row;
    }
    else
    {
      document.chessForm.moveFrom.value = colLetter + row;
    }
  }
  else
  {
   if (ns)
    {
      document.chessMove.document.chessForm.moveTo.value = colLetter + row;
    }
    else
    {
      document.chessForm.moveTo.value = colLetter + row;
    }
  }
}
//------------------------------------------------------------------------------

function validateValue(valueEntered)
{
var isitOk="y";
if (valueEntered.length <= 0)
{
   isitOk = "n";
   return isitOk;
}
if ((valueEntered.substring(0,1) < "a" && valueEntered.substring(0,1) > "h") ||
    (valueEntered.substring(1,2) < "1" && valueEntered.substring(1,2) > "8"))
{
  isitOk = "n";
  return isitOk;
}
return isitOk;
}
//------------------------------------------------------------------------------

function convrtCase(color)
{
for (i=0;i<8;i++)
{
 if (color == "w") {
  if (whiteFromPiece.substring(0,1) == upperCaseArray[i])
  {
    whiteFromPiece = externalColsArray[i] + whiteFromPiece.substring(1,2);
  }
  if (whiteToPiece.substring(0,1) == upperCaseArray[i])
  {
    whiteToPiece = externalColsArray[i] + whiteToPiece.substring(1,2);
  } }
 if (color == "b") {
  if (blackFromPiece.substring(0,1) == upperCaseArray[i])
  {
    blackFromPiece = externalColsArray[i] + blackFromPiece.substring(1,2);
  }
  if (blackToPiece.substring(0,1) == upperCaseArray[i])
  {
    blackToPiece = externalColsArray[i] + blackToPiece.substring(1,2);
  } }
}
}
//------------------------------------------------------------------------------

function undo()
{
if (gameOver == 1)
{
  canbeUndone = 0;
  alert("Game over. cannot be undone.");
}
if (canbeUndone == 1) {
  if (bkingJustMadeAKnightMove == 1)
  {
    bkingMadeKnightMove = 0;
  }
  if (wkingJustMadeAKnightMove == 1)
  {
    wkingMadeKnightMove = 0;
  }
  bkingJustMadeAKnightMove = 0;
  wkingJustMadeAKnightMove = 0;
  canbeUndone = 0;
  if (ns) {
    document.layers["undoText"].visibility = "hide";}
  else {
    document.all["undoText"].style.visibility = "hidden";}
  chesspieces[storedColTo][storedRowTo].src = storedToImage.src;
  chesspieces[storedColFrom][storedRowFrom].src = storedFromImage.src;
  colors[storedColTo][storedRowTo] = storedColorTo;
  colors[storedColFrom][storedRowFrom] = storedColorFrom;
  pieces[storedColTo][storedRowTo] = storedToPiece;
  pieces[storedColFrom][storedRowFrom] = storedFromPiece;
  swapImage = eval("chesspieces[" + storedColTo + "][" + storedRowTo + "].src");
  eval('ImgName = "chess" + storedColTo + storedRowTo');
  if (ns) 
  {
   eval('document.piece' + storedColTo + storedRowTo + '.document.images["'+ImgName+'"].src=swapImage');
   eval('ImgName = "chess" + storedColFrom + storedRowFrom');
   swapImage = eval("chesspieces[" + storedColFrom + "][" + storedRowFrom + "].src");
   eval('document.piece' + storedColFrom + storedRowFrom + '.document.images["'+ImgName+'"].src=swapImage'); 
  }
  else
  {
   eval('document.images["'+ImgName+'"].src=swapImage');
   eval('ImgName = "chess" + storedColFrom + storedRowFrom');
   swapImage = eval("chesspieces[" + storedColFrom + "][" + storedRowFrom + "].src");
   eval('document.images["'+ImgName+'"].src=swapImage');
  }  
  if (blackMoved == 1) 
  {
   if (ns) {
    document.layers["whiteMoveText"].visibility = "hide";
    document.layers["blackMoveText"].visibility = "show"; 
   }
   else {
    document.all["whiteMoveText"].style.visibility = "hidden";
    document.all["blackMoveText"].style.visibility = "visible";
   }
   noofBlackMoves--;
   blackMoved=0;
   whiteMoved = 1;
  }
  else 
  {
   if (ns) {
    document.layers["whiteMoveText"].visibility = "show";
    document.layers["blackMoveText"].visibility = "hide"; 
   }
   else {
    document.all["whiteMoveText"].style.visibility = "visible";
    document.all["blackMoveText"].style.visibility = "hidden";
   }
   noofWhiteMoves--;
   whiteMoved = 0;
   blackMoved = 1;
  }
  if (ns) {
     document.chessStats.document.chessStatForm.whiteMoves.value = noofWhiteMoves;
     document.chessStats.document.chessStatForm.blackMoves.value = noofBlackMoves; }
  else {
     document.chessStatForm.whiteMoves.value = noofWhiteMoves;
     document.chessStatForm.blackMoves.value = noofBlackMoves;}
 isItCheck = checkForCheck();
 if (isItCheck == "y")
 {
   if (ns)
   {
    document.checkSound.play(false);
   }
   else
   {
    document.all.checkSound.loop= "1"
    document.all.checkSound.src= "midi/Twitch.wav"
   }
 }
}
}
//------------------------------------------------------------------------------

function doCheck(color,colFrom,rowFrom,colTo,rowTo)
{
  var isitOk="n";
  isitOk = validateMove(color,colFrom,rowFrom,colTo,rowTo);
  if (pieces[colTo][rowTo].indexOf("king") != -1)
  {
    if (colors[colFrom][rowFrom] == "b")
    {
      winner = "BLACK";
    }
    else
    {
      winner = "WHITE";
    }
     alert("The game will be over with this move! " + winner + " will win. Cannot be undone.");
     gameOver = 1;
  } 
  if (isitOk == "y")
  {
   fileStr = chesspieces[colTo][rowTo].src;
   fileArr = fileStr.split("/");
   ArrCount = fileArr.length;
   if (fileArr[ArrCount - 1] == "chess_blank.gif") 
   {
    doSwap(color,colFrom,rowFrom,colTo,rowTo,"blank");
   }
   else
   {
    doSwap(color,colFrom,rowFrom,colTo,rowTo,"piece");
   }
  }
return isitOk;
}
//------------------------------------------------------------------------------

function doSwap(color,c1,r1,c2,r2,p)
{
  var blackPawnPromote = 0, whitePawnPromote = 0;
  var pawnPromote = 0;
  var swapR="null";
  if ((pieces[c1][r1].indexOf("pawn") != -1) &&
      (colors[c1][r1] == "b") && (r2 == "8") && (c2 != "4")) {
   blackPawnPromote = 1; 
   pawnPromote = 1;
  }
  if (pawnPromote == 0) 
  {
    if ((pieces[c1][r1].indexOf("pawn") != -1) &&
        (colors[c1][r1] == "w") && (r2 == "1") && (c2 != "5")) 
    {
     whitePawnPromote = 1;
     pawnPromote = 1;
    }
  }
   storedColFrom = c1;
   storedColTo = c2;
   storedColorFrom = color;
   storedRowFrom = r1;
   storedRowTo = r2;
   storedPiece = p;
   storedColorTo = colors[c2][r2];
   storedToImage.src = chesspieces[c2][r2].src;
   storedFromImage.src = chesspieces[c1][r1].src;
   storedToPiece = pieces[c2][r2];
   storedFromPiece = pieces[c1][r1];
   chesspieces[c2][r2].src = chesspieces[c1][r1].src;
   chesspieces[c1][r1].src = "images/chess_blank.gif";
   colors[c2][r2] = color;
   colors[c1][r1] = "n";
   pieces[c2][r2] = pieces[c1][r1];
   pieces[c1][r1] = "blank";
   swapImage = eval("chesspieces[" + c2 + "][" + r2 + "].src");
   eval('ImgName = "chess" + c2 + r2');
   if (ns) 
   {
    eval('document.piece' + c2 + r2 + '.document.images["'+ImgName+'"].src=swapImage');
    eval('ImgName = "chess" + c1 + r1');
    swapImage = eval("chesspieces[" + c1 + "][" + r1 + "].src");
    eval('document.piece' + c1 + r1 + '.document.images["'+ImgName+'"].src=swapImage'); 
   }
   else
   {
    eval('document.images["'+ImgName+'"].src=swapImage');
    eval('ImgName = "chess" + c1 + r1');
    swapImage = eval("chesspieces[" + c1 + "][" + r1 + "].src");
    eval('document.images["'+ImgName+'"].src=swapImage');
   }
   if (pawnPromote == 1)
   {
     pawnPromote = 0;
     if (c2 == "1" || c2 == "8")
     {
       if (blackPawnPromote == 1) {
       pieces[c2][8] = "blackrook"; 
       colors[c2][8] = "b"; }
       else {
       pieces[c2][1] = "whiterook";
       colors[c2][1] = "w";
       }
     }
     if (c2 == "2" || c2 == "7") 
     {
       if (blackPawnPromote == 1) {
       pieces[c2][8] = "blackknight"; 
       colors[c2][8] = "b"; }
       else {
       pieces[c2][1] = "whiteknight";
       colors[c2][1] = "w";
       }
     }
     if (c2 == "3" || c2 == "6") 
     {
       if (blackPawnPromote == 1) {
       pieces[c2][8] = "blackelephant"; 
       colors[c2][8] = "b"; }
       else {
       pieces[c2][1] = "whiteelephant";
       colors[c2][1] = "w";
       }
     }
     if (c2 == "4") 
     {
       pieces[c2][1] = "whitecounsellor";
       colors[c2][1] = "w";
     }
     if (c2 == "5")
     {
       pieces[c2][8] = "blackcounsellor";
       colors[c2][8] = "b"; 
     }
     if (r2 == "1") 
     {
       if (c2 == "4") {swapC = 5;}
       if (c2 == "5") {swapC = 4;}
       if (c2 < "4" || c2 > "5") {
       swapC = c2; }   
      swapR = 8; }
     else {
       if (c2 == "4") {swapC = 5;}
       if (c2 == "5") {swapC = 4;}
       if (c2 < "4" || c2 > "5") {
       swapC = c2; } 
      swapR = 1; }
     chesspieces[c2][r2].src = eval("'images/chess" + swapC + swapR + ".gif'");
     swapImage = eval("chesspieces[" + c2 + "][" + r2 + "].src");
     eval('ImgName = "chess" + c2 + r2');
     if (ns) 
     {
      eval('document.piece' + c2 + r2 + '.document.images["'+ImgName+'"].src=swapImage');
     }
     else
     {
      eval('document.images["'+ImgName+'"].src=swapImage');
     }
   }
} 
//------------------------------------------------------------------------------

function getTimeInfo(id)
{
  time = new Date();
  if (id == "t") 
  {
   t = time.getTime();
   t = Math.floor(t / 1000);
   updateTime(); 
  }
  moveT = time.getTime();
  moveT = Math.floor(moveT / 1000);
  if (id == "w") {
  updateWhiteTime(); }
  if (id == "b") {
  updateBlackTime(); }
}  
//------------------------------------------------------------------------------

function updateTime()
{
  time = new Date();
  t1 = time.getTime();
  t1 = Math.floor(t1 / 1000);
  se = t1 - t;
  te = Math.floor(se / 60);
  se = se % 60;
  if (ns) {
     document.chessStats.document.chessStatForm.timeElapsed.value = te + ":" + se; }
  else {
     document.chessStatForm.timeElapsed.value = te + ":" + se; }
  timeDisplay=setTimeout("updateTime()",1000);
}
//------------------------------------------------------------------------------

function updateWhiteTime()
{
  time = new Date();
  t1 = time.getTime();
  t1 = Math.floor(t1 / 1000);
  se = t1 - moveT;
  te = Math.floor(se / 60);
  se = se % 60;
  if (ns) {
     document.chessStats.document.chessStatForm.lastMoveTime.value = te + ":" + se; }
  else {
     document.chessStatForm.lastMoveTime.value = te + ":" + se; }
  clearTimeout(timeBlackDisplay);
  timeWhiteDisplay=setTimeout("updateWhiteTime()",1000);
}
//------------------------------------------------------------------------------

function updateBlackTime()
{
  time = new Date();
  t1 = time.getTime();
  t1 = Math.floor(t1 / 1000);
  se = t1 - moveT;
  te = Math.floor(se / 60);
  se = se % 60;
  if (ns) {
     document.chessStats.document.chessStatForm.lastMoveTime.value = te + ":" + se; }
  else {
     document.chessStatForm.lastMoveTime.value = te + ":" + se; }
  clearTimeout(timeWhiteDisplay);
  timeBlackDisplay=setTimeout("updateBlackTime()",1000);
}

//------------------------------------------------------------------------------

function setupBoard()
{
whiteMoved = 0;
blackMoved = 1;
gameStarted = 0;
gameOver = 0;
noofWhiteMoves = 0;
noofBlackMoves = 0;
bkingMadeKnightMove = 0;
bkingJustMadeAKnightMove = 0;
wkingMadeKnightMove = 0;
wkingJustMadeAKnightMove = 0;
if (ns) {
  document.layers["visitMyHome"].visibility = "hide";
  document.layers["someTip"].visibility = "show";}
else {
 document.all["visitMyHome"].style.visibility = "hidden";
 document.all["someTip"].style.visibility = "visible";}
for (i=1;i<9;i++)
{
  for (j=1;j<3;j++)
  {
    chesspieces[i][j].src = eval("'images/chess" + i + j + ".gif'");
    colors[i][j] = "b";
    swapImage = eval("chesspieces[" + i + "][" + j + "].src");
    eval('ImgName = "chess" + i + j');
    if (ns) 
    {
     eval('document.piece' + i + j + '.document.images["'+ImgName+'"].src=swapImage');
    }
    else
    {
    eval('document.images["'+ImgName+'"].src=swapImage');
    }  
  }
}
for (i=1;i<9;i++)
{
  for (j=7;j<9;j++)
  {
    chesspieces[i][j].src = eval("'images/chess" + i + j + ".gif'");
    colors[i][j] = "w";
    swapImage = eval("chesspieces[" + i + "][" + j + "].src");
    eval('ImgName = "chess" + i + j');
    if (ns) 
    {
     eval('document.piece' + i + j + '.document.images["'+ImgName+'"].src=swapImage');
    }
    else
    {
    eval('document.images["'+ImgName+'"].src=swapImage');
    }
  }
}
for (i=1;i<9;i++)
{
  for (j=3;j<7;j++)
  {
    chesspieces[i][j].src = "images/chess_blank.gif";
    colors[i][j] = "n";
    swapImage = eval("chesspieces[" + i + "][" + j + "].src");
    eval('ImgName = "chess" + i + j');
    if (ns) 
    {
     eval('document.piece' + i + j + '.document.images["'+ImgName+'"].src=swapImage');
    }
    else
    {
    eval('document.images["'+ImgName+'"].src=swapImage'); 
    }
  }
}
for (i=1;i<9;i++)
{
  pieces[i][2] = "blackpawn";
  pieces[i][7] = "whitepawn";
}
pieces[1][1] = "blackrook";
pieces[1][8] = "whiterook";
pieces[2][1] = "blackknight";
pieces[2][8] = "whiteknight";
pieces[3][1] = "blackelephant";
pieces[3][8] = "whiteelephant";
pieces[4][1] = "blackcounsellor";
pieces[4][8] = "whiteking";
pieces[5][1] = "blackking";
pieces[5][8] = "whitecounsellor";
pieces[6][1] = "blackelephant";
pieces[6][8] = "whiteelephant";
pieces[7][1] = "blackknight";
pieces[7][8] = "whiteknight";
pieces[8][1] = "blackrook";
pieces[8][8] = "whiterook";
 if (ns) {
     document.layers["undoText"].visibility = "hide";
     document.layers["validationText"].visibility = "hide";
     document.layers["whiteMoveText"].visibility = "show";
     document.layers["blackMoveText"].visibility = "hide";
     document.chessMove.document.chessForm.moveFrom.value = "";
     document.chessMove.document.chessForm.moveTo.value = "";
     document.checkLayer.document.checkForm.checkText.value = "";
     clearTimeout(timeDisplay);
     clearTimeout(timeWhiteDisplay);
     clearTimeout(timeBlackDisplay);
     document.chessStats.document.chessStatForm.lastMoveTime.value = 0;
     document.chessStats.document.chessStatForm.timeElapsed.value = 0;
     document.chessStats.document.chessStatForm.whiteMoves.value = 0;
     document.chessStats.document.chessStatForm.blackMoves.value = 0; }
  else {
     document.all["undoText"].style.visibility = "hidden"; 
     document.all["validationText"].style.visibility = "hidden";
     document.all["whiteMoveText"].style.visibility = "visible";
     document.all["blackMoveText"].style.visibility = "hidden";
     document.chessForm.moveFrom.value = "";
     document.chessForm.moveTo.value = "";
     document.checkForm.checkText.value = "";
     clearTimeout(timeDisplay);
     clearTimeout(timeWhiteDisplay);
     clearTimeout(timeBlackDisplay);
     document.chessStatForm.lastMoveTime.value = 0;
     document.chessStatForm.timeElapsed.value = 0;
     document.chessStatForm.whiteMoves.value = 0;
     document.chessStatForm.blackMoves.value = 0;}
}
//------------------------------------------------------------------------------

function findOriginPiece(color,col,row)
{
var isitOk="y";
if (color != colors[col][row])
{
  isitOk = "n";
}
return isitOk;
}
//------------------------------------------------------------------------------

function findDestinationPiece(color,col,row)
{
var isitOk="y";
if (color == colors[col][row])
{
  isitOk = "n";
}
return isitOk;
}
//------------------------------------------------------------------------------

function validateMove(color,c1,r1,c2,r2)
{
var isitOk="y";
if (c1 == c2 && r1 == r2)
{
 isitOk = "n";
}
if (pieces[c1][r1].indexOf("pawn") != -1) 
{ 
 isitOk = validatePawn(color,c1,r1,c2,r2); 
}
if (pieces[c1][r1].indexOf("rook") != -1) 
{ 
 isitOk = validateRook(c1,r1,c2,r2); 
}
if (pieces[c1][r1].indexOf("knight") != -1) 
{ 
 isitOk = validateKnight(c1,r1,c2,r2); 
}
if (pieces[c1][r1].indexOf("elephant") != -1) 
{ 
 isitOk = validateElephant(c1,r1,c2,r2); 
}
if (pieces[c1][r1].indexOf("king") != -1) 
{ 
 isitOk = validateKing(c1,r1,c2,r2); 
}
if (pieces[c1][r1].indexOf("counsellor") != -1) 
{ 
 isitOk = validateCounsellor(c1,r1,c2,r2); 
}
return isitOk;
}
//------------------------------------------------------------------------------

function validatePawn(color,c1,r1,c2,r2)
{
  var moveOk = "y";
  if (color == "w") {
   if (r1 - r2 != 1)
   {
    moveOk = "n";
   }
  }
  else 
  {
   if (r2 - r1 != 1)
   {
     moveOk = "n";
   }
  }
  if (c1 != c2 && Math.abs(c2 - c1) != 1)
  {
    moveOk = "n";
  }
  if (c1 != c2 && pieces[c2][r2] == "blank")
  {
   moveOk = "n";
  }
  return moveOk;
}
//------------------------------------------------------------------------------

function validateRook(c1,r1,c2,r2)
{
  var moveOk = "y";
  if (r1 != r2 && c1 != c2)
  {
    moveOk = "n";
  }
  if (c1 == c2)
  {
    if (r2 > r1)
    {
      for (i=parseInt(r1)+1;i<r2;i++)
      {
        if (pieces[c1][i] != "blank")
        {
          moveOk = "n";
          i = r2;
        }
      }
     }
     else
     {
       for (i=parseInt(r2)+1;i<r1;i++)
       if (pieces[c1][i] != "blank")
       {
         moveOk = "n";
         i = r1;
       }
     }
   }
  if (r1 == r2)
  {
    if (c2 > c1)
    {
      for (i=parseInt(c1)+1;i<c2;i++)
      {
        if (pieces[i][r1] != "blank")
        {
          moveOk = "n";
          i = c2;
        }
      }
     }
     else
     {
       for (i=parseInt(c2)+1;i<c1;i++)
       if (pieces[i][r1] != "blank")
       {
         moveOk = "n";
         i = c1;
       }
     }
   }
  return moveOk;
}
//------------------------------------------------------------------------------

function validateKnight(c1,r1,c2,r2)
{
  var moveOk = "y";
  if ((Math.abs(c2 - c1) == 1 && Math.abs(r2 - r1) != 2) ||
      (Math.abs(c2 - c1) == 2 && Math.abs(r2 - r1) != 1)) 
  {
     moveOk = "n";
  }
  if (Math.abs(c2 - c1) > 2 || Math.abs(r2 - r1) > 2)
  {
    moveOk = "n";
  }  
  if ((c1 == c2) || (r1 == r2)) 
  {
    moveOk = "n";
  }
  return moveOk;
}
//------------------------------------------------------------------------------

function validateElephant(c1,r1,c2,r2)
{
  var moveOk = "y";
  if (Math.abs(r2 - r1) != Math.abs(c2 - c1))
  {
     moveOk = "n";
  }
  if (Math.abs(r2 - r1) != 2)
  {
     moveOk = "n";
  }
  if (Math.abs(c2 - c1) != 2)
  {
     moveOk = "n";
  }
  return moveOk;
}
//------------------------------------------------------------------------------

function validateCounsellor(c1,r1,c2,r2)
{
  var moveOk = "y";
  if ((c1 == c2) || (r1 == r2))
  {
    moveOk = "n";
  }
  if (Math.abs(c2 - c1) != 1)
  {
    moveOk = "n";
  }
  if (Math.abs(r2 - r1) != 1)
  {
    moveOk = "n";
  }
  return moveOk;
}
//------------------------------------------------------------------------------

function validateKing(c1,r1,c2,r2)
{
  var moveOk = "y";
  if (r1 == r2 && Math.abs(c2 - c1) != 1)
  {
    moveOk = "n";
  }
  if (r1 != r2 && Math.abs(r2 - r1) != 1)
  {
    moveOk = "n";
  }
  if (c1 == c2 && Math.abs(r2 - r1) != 1)
  {
    moveOk = "n";
  }
  if (c1 != c2 && Math.abs(c2 - c1) != 1)
  {
    moveOk = "n";
  }
  if (moveOk == "n" && bkingMadeKnightMove == 0 && colors[c1][r1] == "b")
  {
    moveOk = validateKnight(c1,r1,c2,r2);
    if (moveOk == "y")
    {
      bkingMadeKnightMove = 1;
      bkingJustMadeAKnightMove = 1;
    }
  } 
  if (moveOk == "n" && wkingMadeKnightMove == 0 && colors[c1][r1] == "w")
  {
    moveOk = validateKnight(c1,r1,c2,r2);
    if (moveOk == "y")
    {
      wkingMadeKnightMove = 1;
      wkingJustMadeAKnightMove = 1;
    }
  }
  return moveOk;
}
//------------------------------------------------------------------------------

function showRules()
{
 if (ns)
 {
  document.layers["moveStats"].visibility = "hide";
  document.layers["chessStats"].visibility = "hide";
  document.layers["chessMove"].visibility = "hide";
  document.layers["howToPlayGame"].visibility="hide";
  document.layers["gameRules"].visibility = "show";
 }
 else
 {
  document.all["moveStats"].style.visibility = "hidden";
  document.all["chessStats"].style.visibility = "hidden";
  document.all["chessMove"].style.visibility = "hidden";
  document.all["howToPlayGame"].style.visibility = "hidden";
  document.all["gameRules"].style.visibility = "visible";
 }
}
// -----------------------------------------------------------------
function howToPlay()
{
 if (ns)
 {
  document.layers["moveStats"].visibility = "hide";
  document.layers["chessStats"].visibility = "hide";
  document.layers["chessMove"].visibility = "hide";
  document.layers["gameRules"].visibility = "hide";
  document.layers["howToPlayGame"].visibility="show";
  document.layers["moreRules"].visibility="show";
 }
 else
 {
  document.all["moveStats"].style.visibility = "hidden";
  document.all["chessStats"].style.visibility = "hidden";
  document.all["chessMove"].style.visibility = "hidden";
  document.all["gameRules"].style.visibility = "hidden";
  document.all["howToPlayGame"].style.visibility = "visible";
  document.all["moreRules"].style.visibility = "visible";
 }
}
// -----------------------------------------------------------------
function hidehowToPlay()
{
 if (ns)
 {
  document.layers["moveStats"].visibility = "show";
  document.layers["chessStats"].visibility = "show";
  document.layers["chessMove"].visibility = "show";
  document.layers["gameRules"].visibility = "hide";
  document.layers["howToPlayGame"].visibility="hide";
  document.layers["moreRules"].visibility="hide";
 }
 else
 {
  document.all["moveStats"].style.visibility = "visible";
  document.all["chessStats"].style.visibility = "visible";
  document.all["chessMove"].style.visibility = "visible";
  document.all["gameRules"].style.visibility = "hidden";
  document.all["howToPlayGame"].style.visibility = "hidden";
  document.all["moreRules"].style.visibility = "hidden";
 }
}
// -----------------------------------------------------------------

function hideRules()
{
 if (ns)
 {
  document.layers["moveStats"].visibility = "show";
  document.layers["chessStats"].visibility = "show";
  document.layers["chessMove"].visibility = "show";
  document.layers["howToPlayGame"].visibility = "hide";
  document.layers["gameRules"].visibility = "hide";
 }
 else
 {
  document.all["moveStats"].style.visibility = "visible";
  document.all["chessStats"].style.visibility = "visible";
  document.all["chessMove"].style.visibility = "visible";
  document.all["howToPlayGame"].style.visibility = "hidden";
  document.all["gameRules"].style.visibility = "hidden";
 }
}
// -----------------------------------------------------------------

function checkForCheck()
{
   var confirmCheck = "n";
   var i=0, j=0;
   if (ns) {
      document.checkLayer.document.checkForm.checkText.value = "";
   }
   else
     { 
      document.checkForm.checkText.value = "";
     }
   for (i=1; i<9; i++)
  {
        for (j=1;j<9;j++)
        {
            if (pieces[i][j].indexOf("pawn") != -1)
            {
                 confirmCheck = checkByPawn(i,j);
                 if (confirmCheck == "y")
                 {
                      return confirmCheck;
                 }
            }
            if (pieces[i][j].indexOf("rook") != -1)
            {
                 confirmCheck = checkByRook(i,j);
                 if (confirmCheck == "y")
                 {
                      return confirmCheck;
                 }
            }
            if (pieces[i][j].indexOf("knight") != -1)
            {
                 confirmCheck = checkByKnight(i,j);
                 if (confirmCheck == "y")
                 {
                      return confirmCheck;
                 }
            }
            if (pieces[i][j].indexOf("elephant") != -1)
            {
                 confirmCheck = checkByElephant(i,j);
                 if (confirmCheck == "y")
                 {
                      return confirmCheck;
                 }
            }
            if (pieces[i][j].indexOf("king") != -1)
            {
                 confirmCheck = checkByKing(i,j);
                 if (confirmCheck == "y")
                 {
                      return confirmCheck;
                 }
            }
            if (pieces[i][j].indexOf("counsellor") != -1)
            {
                 confirmCheck = checkByCounsellor(i,j);
                 if (confirmCheck == "y")
                 {
                      return confirmCheck;
                 }
            }
      }
   }
    return confirmCheck;
}  
// -----------------------------------------------------------------

function checkByPawn(col, row)
{
   var checkMade = "n";
   if (colors[col][row] == "w")
   {
     if (col > 1 && row > 1) {
       if (pieces[col - 1][row - 1].indexOf("king") != -1 && 
            colors[col][row] != colors[col - 1][row - 1])
       {
            checkMade = "y";
            strCheckText =  "Check on BLACK KING at " + upperCaseArray[col - 2] +
                  internalColsArray[row - 2] + " by WHITE PAWN at " +
                  upperCaseArray[col - 1] + internalColsArray[row - 1];

             if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
            else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
            return checkMade;
        } }
       if (row > 1 && col < 8) {
       if (pieces[parseInt(col) + 1][row - 1].indexOf("king") != -1 && 
            colors[col][row] != colors[parseInt(col) + 1][row - 1])
       {
            checkMade = "y";
            strCheckText =  "Check on BLACK KING at " + upperCaseArray[col] +
                  internalColsArray[row - 2] + " by WHITE PAWN at " +
                  upperCaseArray[col - 1] + internalColsArray[row - 1];

             if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
            else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
            return checkMade;
        } }
    }
    else
    {
       if (col > 1 && row < 8) {
       if (pieces[col - 1][parseInt(row) + 1].indexOf("king") != -1 && 
            colors[col][row] != colors[col - 1][parseInt(row) + 1])
       {
            checkMade = "y";
            strCheckText =  "Check on WHITE KING at " + upperCaseArray[col - 2] +
                  internalColsArray[row] + " by BLACK PAWN at " +
                  upperCaseArray[col - 1] + internalColsArray[row - 1];

             if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
            else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
            return checkMade;
        }}
       if (col < 8 && row < 8) {
       if (pieces[parseInt(col) + 1][parseInt(row) + 1].indexOf("king") != -1 && 
            colors[col][row] != colors[parseInt(col) + 1][parseInt(row) + 1])
       {
            checkMade = "y";
            strCheckText =  "Check on WHITE KING at " + upperCaseArray[col] +
                  internalColsArray[row] + " by BLACK PAWN at " +
                  upperCaseArray[col - 1] + internalColsArray[row - 1];
             if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
            else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
            return checkMade;
        } }
    }
}
// -----------------------------------------------------------------

function checkByRook(col, row)
{
   var i=0;
   var checkMade = "n";
   var checkColor = "null";
   var kingColor = "null";
   var rightcheckMade = "y", leftcheckMade = "y";
   var bottomcheckMade = "y", topcheckMade = "y";
   if (col < 8) {
   for (i=parseInt(col)+1;i<9;i++)
   {
        if (pieces[i][row].indexOf("blank") == -1 &&
             pieces[i][row].indexOf("king") == -1)
             rightcheckMade = "n";
        if (pieces[i][row].indexOf("king") != -1 && rightcheckMade != "n" &&
             colors[i][row] != colors[col][row])
        {
             checkMade = "y";
             if (colors[col][row] == "w") { checkColor = "WHITE"; }
             else { checkColor = "BLACK"; }
             if (colors[i][row] == "w") { kingColor = "WHITE"; }
             else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[i - 1] + internalColsArray[row - 1] + 
                                  " by " + checkColor + " ROOK at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
             if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
            else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
            return checkMade;
        }
    } }
   if (col > 1) {
   for (i=col-1;i>0;i--)
   {
        if (pieces[i][row].indexOf("blank") == -1 &&
             pieces[i][row].indexOf("king") == -1)
             leftcheckMade = "n";
        if (pieces[i][row].indexOf("king") != -1 && leftcheckMade != "n" &&
             colors[i][row] != colors[col][row])
        {
             checkMade = "y";
             if (colors[col][row] == "w") { checkColor = "WHITE"; }
             else { checkColor = "BLACK"; }
             if (colors[i][row] == "w") { kingColor = "WHITE"; }
             else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[i - 1] + internalColsArray[row - 1] + 
                                  " by " + checkColor + " ROOK at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
             if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
            else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
            return checkMade;
        }
    } }
   if (row < 8) {
   for (i=parseInt(row)+1;i<9;i++)
   {
        if (pieces[col][i].indexOf("blank") == -1 &&
             pieces[col][i].indexOf("king") == -1)
             bottomcheckMade = "n";
        if (pieces[col][i].indexOf("king") != -1 && bottomcheckMade != "n" &&
             colors[col][i] != colors[col][row])
        {
             checkMade = "y";
             if (colors[col][row] == "w") { checkColor = "WHITE"; }
             else { checkColor = "BLACK"; }
             if (colors[col][i] == "w") { kingColor = "WHITE"; }
             else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 1] + internalColsArray[i - 1] + 
                                  " by " + checkColor + " ROOK at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
             if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
            else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
            return checkMade;
        }
    } }
   if (row > 1) {
   for (i=row-1;i>0;i--)
   {
        if (pieces[col][i].indexOf("blank") == -1 &&
             pieces[col][i].indexOf("king") == -1)
             topcheckMade = "n";
        if (pieces[col][i].indexOf("king") != -1 && topcheckMade != "n" &&
             colors[col][i] != colors[col][row])
        {
             checkMade = "y";
             if (colors[col][row] == "w") { checkColor = "WHITE"; }
             else { checkColor = "BLACK"; }
             if (colors[col][i] == "w") { kingColor = "WHITE"; }
             else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 1] + internalColsArray[i - 1] + 
                                  " by " + checkColor + " ROOK at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
             if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
            else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
            return checkMade;
        }
    } }
return checkMade;
}
// -----------------------------------------------------------------
  
function checkByElephant(col, row)
{
    var checkMade = "n";
    var checkColor = "null", kingColor = "null";
    if (col > 3 && row > 3) {
    if (pieces[col - 2][row - 2].indexOf("king") != -1 &&
         colors[col - 2][row - 2] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col - 2][row - 2] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 3] + internalColsArray[row - 3] + 
                                  " by " + checkColor + " ELEPHANT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     }}
    if (col > 2 && row < 7) {
    if (pieces[col - 2][parseInt(row) + 2].indexOf("king") != -1 &&
         colors[col - 2][parseInt(row) + 2] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col - 2][parseInt(row) + 2] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 3] + internalColsArray[parseInt(row) + 1] + 
                                  " by " + checkColor + " ELEPHANT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
    if (col < 7 && row > 2) {
    if (pieces[parseInt(col) + 2][row - 2].indexOf("king") != -1 &&
         colors[parseInt(col) + 2][row - 2] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[parseInt(col) + 2][row - 2] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[parseInt(col) + 1] + internalColsArray[row - 3] + 
                                  " by " + checkColor + " ELEPHANT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
    if (col < 7 && row < 7) {
    if (pieces[parseInt(col) + 2][parseInt(row) + 2].indexOf("king") != -1 &&
         colors[parseInt(col) + 2][parseInt(row) + 2] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[parseInt(col) + 2][parseInt(row) + 2] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[parseInt(col) + 1] + internalColsArray[parseInt(row) + 1] + 
                                  " by " + checkColor + " ELEPHANT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
return checkMade;
}
// -----------------------------------------------------------------

function checkByKnight(col, row)
{
  var checkMade = "n";
  var checkColor = "null";
  var kingColor = "null";
  if (col > 1 && row > 2) {
   if (pieces[col - 1][row - 2].indexOf("king") != -1 &&
         colors[col - 1][row - 2] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col - 1][row - 2] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 2] + internalColsArray[row - 3] + 
                                  " by " + checkColor + " KNIGHT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     }
    }
    if (col < 8 && row > 2) {
    if (pieces[parseInt(col) + 1][row - 2].indexOf("king") != -1 &&
         colors[parseInt(col) + 1][row - 2] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[parseInt(col) + 1][row - 2] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col] + internalColsArray[row - 3] + 
                                  " by " + checkColor + " KNIGHT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
    if (col > 1 && row < 7) {
    if (pieces[col - 1][parseInt(row) + 2].indexOf("king") != -1 &&
         colors[col - 1][parseInt(row) + 2] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col - 1][parseInt(row) + 2] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 2] + internalColsArray[parseInt(row) + 1] + 
                                  " by " + checkColor + " KNIGHT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
    if (col < 8 && row < 7) {
    if (pieces[parseInt(col) + 1][parseInt(row) + 2].indexOf("king") != -1 &&
         colors[parseInt(col) + 1][parseInt(row) + 2] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[parseInt(col) + 1][parseInt(row) + 2] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col] + internalColsArray[parseInt(row) + 1] + 
                                  " by " + checkColor + " KNIGHT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
   if (col > 2 && row > 1) {
   if (pieces[col - 2][row - 1].indexOf("king") != -1 &&
         colors[col - 2][row - 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col - 2][row - 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 3] + internalColsArray[row - 2] + 
                                  " by " + checkColor + " KNIGHT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
    if (col < 7 && row > 1) {
    if (pieces[parseInt(col) + 2][row - 1].indexOf("king") != -1 &&
         colors[parseInt(col) + 2][row - 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[parseInt(col) + 2][row - 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[parseInt(col) + 1] + internalColsArray[row - 2] + 
                                  " by " + checkColor + " KNIGHT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
    if (col > 2 && row < 8) {
    if (pieces[col - 2][parseInt(row) + 1].indexOf("king") != -1 &&
         colors[col - 2][parseInt(row) + 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col - 2][parseInt(row) + 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 3] + internalColsArray[row] + 
                                  " by " + checkColor + " KNIGHT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
    if (col < 7 && row < 8) {
    if (pieces[parseInt(col) + 2][parseInt(row) + 1].indexOf("king") != -1 &&
         colors[parseInt(col) + 2][parseInt(row) + 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[parseInt(col) + 2][parseInt(row) + 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[parseInt(col) + 1] + internalColsArray[row] + 
                                  " by " + checkColor + " KNIGHT at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
}  
function checkByCounsellor(col, row)
{
   var checkMade = "n";
   var checkColor = "null";
   var kingColor = "null";
   if (col > 1 && row > 1) {
   if (pieces[col - 1][row - 1].indexOf("king") != -1 &&
         colors[col - 1][row - 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col - 1][row - 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 2] + internalColsArray[row - 2] + 
                                  " by " + checkColor + " COUNSELLOR at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     }}
    if (col < 8 && row > 1) {
    if (pieces[parseInt(col) + 1][row - 1].indexOf("king") != -1 &&
         colors[parseInt(col) + 1][row - 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[parseInt(col) + 1][row - 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col] + internalColsArray[row - 2] + 
                                  " by " + checkColor + " COUNSELLOR at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
    if (col > 1 && row < 8) {
    if (pieces[col - 1][parseInt(row) + 1].indexOf("king") != -1 &&
         colors[col - 1][parseInt(row) + 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col - 1][parseInt(row) + 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 2] + internalColsArray[row] + 
                                  " by " + checkColor + " COUNSELLOR at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
    if (col < 8 && row < 8) {
    if (pieces[parseInt(col) + 1][parseInt(row) + 1].indexOf("king") != -1 &&
         colors[parseInt(col) + 1][parseInt(row) + 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[parseInt(col) + 1][parseInt(row) + 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col] + internalColsArray[row] + 
                                  " by " + checkColor + " COUNSELLOR at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     }}
return checkMade;
}
// -----------------------------------------------------------------

function checkByKing(col, row)
{
    var checkMade = "n";
    var checkColor = "null";
    var kingColor = "null";
    if (col > 1 && row > 1) {
    if (pieces[col - 1][row - 1].indexOf("king") != -1 &&
         colors[col - 1][row - 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col - 1][row - 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 2] + internalColsArray[row - 2] + 
                                  " by " + checkColor + " KING at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     }}
    if (col < 8 && row > 1) {
    if (pieces[parseInt(col) + 1][row - 1].indexOf("king") != -1 &&
         colors[parseInt(col) + 1][row - 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[parseInt(col) + 1][row - 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col] + internalColsArray[row - 2] + 
                                  " by " + checkColor + " KING at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     }}
    if (col < 8 && row < 8) {
    if (pieces[parseInt(col) + 1][parseInt(row) + 1].indexOf("king") != -1 &&
         colors[parseInt(col) + 1][parseInt(row) + 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[parseInt(col) + 1][parseInt(row) + 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col] + internalColsArray[row] + 
                                  " by " + checkColor + " KING at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     } }
    if (col > 1 && row < 8) {
    if (pieces[col - 1][parseInt(row) + 1].indexOf("king") != -1 &&
         colors[col - 1][parseInt(row) + 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col - 1][parseInt(row) + 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 2] + internalColsArray[row] + 
                                  " by " + checkColor + " KING at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     }}
    if (row > 1) {
    if (pieces[col][row - 1].indexOf("king") != -1 &&
         colors[col][row - 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col][row - 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 1] + internalColsArray[row - 2] + 
                                  " by " + checkColor + " KING at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     }}
    if (col < 8) {
    if (pieces[parseInt(col) + 1][row].indexOf("king") != -1 &&
         colors[parseInt(col) + 1][row] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[parseInt(col) + 1][row] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col] + internalColsArray[row - 1] + 
                                  " by " + checkColor + " KING at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     }}
    if (row < 8) {
    if (pieces[col][parseInt(row) + 1].indexOf("king") != -1 &&
         colors[col][parseInt(row) + 1] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col][parseInt(row) + 1] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 1] + internalColsArray[row] + 
                                  " by " + checkColor + " KING at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     }}
    if (col > 1) {
    if (pieces[col - 1][row].indexOf("king") != -1 &&
         colors[col - 1][row] != colors[col][row])
   {
       checkMade = "y";
       if (colors[col][row] == "w") { checkColor = "WHITE"; }
          else { checkColor = "BLACK"; }
       if (colors[col - 1][row] == "w") { kingColor = "WHITE"; }
          else { kingColor = "BLACK"; }
             strCheckText = "Check on " + kingColor + " KING at " +
                                   upperCaseArray[col - 2] + internalColsArray[row - 1] + 
                                  " by " + checkColor + " KING at " + 
                                  upperCaseArray[col - 1] + internalColsArray[row - 1];
       if (ns) {
                document.checkLayer.document.checkForm.checkText.value = strCheckText;
              }
       else
            { 
                document.checkForm.checkText.value = strCheckText;
            }
       return checkMade;
     }}
return checkMade;
}
// -->

