//
//Copyright David Hayes 23 April 1997
//Others may use this code by permission only.
//

var Left_Button = '<input type="button" value="&lt; Previous" onclick="Previous_Move()">'
var Right_Button = '<input type="button" value="Next Move &gt;" onclick="Next_Move()">'
var Text = new Array();
var Position = new Array();
var Comment = new Array();
var currentPly = 0;
var stm = "";
var size = 36;
var today = new Date();
var expiresInOneYear = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);

function getCookieVal (offset) {
 var endstr = document.cookie.indexOf (";", offset);
 if (endstr == -1) {
  endstr = document.cookie.length;
 }
 return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
 var arg = name + "=";
 var alen = arg.length;
 var clen = document.cookie.length;
 var i = 0;
 while (i < clen) {
  var j = i + alen;
  if (document.cookie.substring(i, j) == arg) {
   return getCookieVal (j);
   }
  i = document.cookie.indexOf(" ", i) + 1;
  if (i == 0) break; 
  }
 return null;
}

function SetCookie (name,value,expires,path,domain,secure) {
 document.cookie = name + "=" + escape (value) +
  ((expires) ? "; expires=" + expires.toGMTString() : "") +
  ((path) ? "; path=" + path : "") +
  ((domain) ? "; domain=" + domain : "") +
  ((secure) ? "; secure" : "");
}

function InstantiateBoard(){
 shade = new Array();
 square = 0;
 for (var row = 0; row < 8; row++){
  for (var col = 0; col < 8; col++){
   if ((row + col) % 2 != 0) {
    shade[square] = "d.jpg";
    square++;
   } else {
    shade[square] = "l.jpg";
    square++;
   }
  }
 }
 if (GetCookie('Board_Size') == null) {
  SetCookie('Board_Size', '' + size + '', expiresInOneYear);
 } else {
  size = GetCookie('Board_Size');
 }
 var dataURL = "/common/xml/" + top.name + ".xml";
 Hide_Size_Controls()
 getDataReturnXml(dataURL, ExtractData);
}

function LoadStub(Instance) {
 top.name = Instance;
 window.location = "/common/boardstub.html";
}

function ExtractData(ajaxResponse) {
 var boardNodes = ajaxResponse.getElementsByTagName("board");
 var pgnNodes = ajaxResponse.getElementsByTagName("pgn");
 var whiteNodes = ajaxResponse.getElementsByTagName("white");
 var stmNodes = ajaxResponse.getElementsByTagName("stm");
 var moveNodes = ajaxResponse.getElementsByTagName("move");
 var positionNodes = ajaxResponse.getElementsByTagName("position");
 var commentNodes = ajaxResponse.getElementsByTagName("comment");
 if (whiteNodes[0].firstChild.nodeValue != "No Data") {
  var blackNodes = ajaxResponse.getElementsByTagName("black");
  var eventNodes = ajaxResponse.getElementsByTagName("event");
  var dateNodes = ajaxResponse.getElementsByTagName("date");
  var openingNodes = ajaxResponse.getElementsByTagName("opening");
  var resultNodes = ajaxResponse.getElementsByTagName("result");
  var content = "<br><br>White: " + whiteNodes[0].firstChild.nodeValue + "<br>";
  content = content + "Black: " + blackNodes[0].firstChild.nodeValue + "<br>";
  content = content + "Event: " + eventNodes[0].firstChild.nodeValue + "<br>";
  content = content + "Date: " + dateNodes[0].firstChild.nodeValue + "<br>";
  content = content + "Opening: " + openingNodes[0].firstChild.nodeValue + "<br>";
  content = content + "Result: " + resultNodes[0].firstChild.nodeValue + "<br><br>";
  document.getElementById("Details").innerHTML = content;
 }
 if (stmNodes[0].firstChild.nodeValue != "No Data") {
  stm = stmNodes[0].firstChild.nodeValue;
  var content = "Find the best move for " + stm + ".<br><br>";
  document.getElementById("Challenge").innerHTML = content;
 }
 document.getElementById("Title").innerHTML = boardNodes[0].firstChild.nodeValue;
 for (loopIndex = 0; loopIndex < moveNodes.length; loopIndex++) {
  Text[loopIndex] = moveNodes[loopIndex].firstChild.nodeValue;
  Position[loopIndex] = positionNodes[loopIndex].firstChild.nodeValue;
  Comment[loopIndex] = commentNodes[loopIndex];
 }
 lastPlyIndex = Text.length - 1;
 Display_Diagram(Position[0], Comment[0], Text[0], size)
}

function IncrementalResize(button) {
 if (button.value == "+") {
  size++;
 }
 if (button.value == "-") {
  size--;
 }
 if (size > 72) {
  size = 72;
 }
 if (size < 18) {
  size = 18;
 }
 Display_Diagram(Position[currentPly], Comment[currentPly], Text[currentPly], size);
}

function Reveal_Size_Controls() {
 document.getElementById("Sizing").innerHTML = '<font class="darkonlight">\n<a class="darkonlight" href="javascript:Hide_Size_Controls()">Save and Hide Size Controls</a>\n<br><br><font class="darkonlight">\n<form>\n<input type="button" value="-" onclick="IncrementalResize(this)">\n<input type="button" value="+" onclick="IncrementalResize(this)">\n<br></form>';
}

function Hide_Size_Controls() {
 document.getElementById("Sizing").innerHTML = '<a class="darkonlight" href="javascript:Reveal_Size_Controls()">Resize Board</a><br><br>';
 SetCookie('Board_Size', '' + size + '', expiresInOneYear);
}

function Next_Move() {
 currentPly = currentPly + 1;
 if (stm != "") {
  if (stm == "White") {
   stm = "Black";
  } else {
   stm = "White";
  }
  if (currentPly < lastPlyIndex) {
   var content = "Find the best move for " + stm + ".<br><br>";
  } else {
   var content = "<br><br>";
  }
  document.getElementById("Challenge").innerHTML = content;
 }
 Display_Diagram(Position[currentPly], Comment[currentPly], Text[currentPly], size)
 Hide_Size_Controls()
}

function Previous_Move() {
 currentPly = currentPly - 1;
 if (stm != "") {
  if (stm == "White") {
   stm = "Black";
  } else {
   stm = "White";
  }
  var content = "Find the best move for " + stm + ".<br><br>";
  document.getElementById("Challenge").innerHTML = content;
 }
 Display_Diagram(Position[currentPly], Comment[currentPly], Text[currentPly], size)
 Hide_Size_Controls()
}

function Display_Diagram(Forsythe_Notation, Comment, Ply_Text, size){
 piece = new Array();
 square = 0;
 row = 8;
 col = 1;
 for (index = 0; index < Forsythe_Notation.length; index++){
  current_char = Forsythe_Notation.charAt(index)
  rowcol = row * 10 + col;
  if (current_char == "/"){
   row--
   col = 1;
   continue;
  }
  if (current_char == "p"){
   piece[square] = "bp";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  if (current_char == "P"){
   piece[square] = "wp";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  if (current_char == "n"){
   piece[square] = "bn";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  if (current_char == "N"){
   piece[square] = "wn";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  if (current_char == "b"){
   piece[square] = "bb";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  if (current_char == "B"){
   piece[square] = "wb";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  if (current_char == "r"){
   piece[square] = "br";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  if (current_char == "R"){
   piece[square] = "wr";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  if (current_char == "k"){
   piece[square] = "bk";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  if (current_char == "K"){
   piece[square] = "wk";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  if (current_char == "q"){
   piece[square] = "bq";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  if (current_char == "Q"){
   piece[square] = "wq";
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
   continue;
  }
  num_of_empty_squares = parseInt(current_char)
  for (num = 0; num < num_of_empty_squares; num++){
   piece[square] = "s";
   rowcol = row * 10 + col;
   content = '<img src="/common/figurines/lc/' + piece[square] + shade[square] + '" height="' + size + '" width="' + size + '">';
   document.getElementById(rowcol).innerHTML = content;
   square++;
   col++;
  } 
 }
 offset = size * 1 + 10;
 fontSize = size / 54 + .2;
 numberWidth = size / 3 + 6;
 letterHeight = size / 1.5 + 10;
 content = '<table border="0" cellspacing="0" cellpadding="0"><tr><td height="' + letterHeight + '" width="' + offset + '"></td><td height="' + letterHeight + '" width="' + size + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">a</td><td height="' + letterHeight + '" width="' + size + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">b</td><td height="' + letterHeight + '" width="' + size + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">c</td><td height="' + letterHeight + '" width="' + size + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">d</td><td height="' + letterHeight + '" width="' + size + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">e</td><td height="' + letterHeight + '" width="' + size + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">f</td><td height="' + letterHeight + '" width="' + size + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">g</td><td height="' + letterHeight + '" width="' + size + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">h</td></tr></table>';
 document.getElementById("lettertable").innerHTML = content;
 content = '<table class="board"><tr><td height="' + size + '" width="' + numberWidth + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">8</td></tr><tr><td height="' + size + '" width="' + numberWidth + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">7</td></tr><tr><td height="' + size + '" width="' + numberWidth + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">6</td></tr><tr><td height="' + size + '" width="' + numberWidth + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">5</td></tr><tr><td height="' + size + '" width="' + numberWidth + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">4</td></tr><tr><td height="' + size + '" width="' + numberWidth + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">3</td></tr><tr><td height="' + size + '" width="' + numberWidth + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">2</td></tr><tr><td height="' + size + '" width="' + numberWidth + '" style="font-family: Arial; color: #000000; font-size: ' + fontSize + 'em; font-weight: bold; line-height: 100%">1</td></tr></table>';
 document.getElementById("boardnumbers").innerHTML = content;
 if (currentPly > 0) {
  document.getElementById("Previous").innerHTML = Left_Button;
 } else {
  document.getElementById("Previous").innerHTML = "";
 }
 document.getElementById("Move").innerHTML = Ply_Text;
 if (currentPly < lastPlyIndex) {
  document.getElementById("Next_Move").innerHTML = Right_Button;
 } else {
  document.getElementById("Next_Move").innerHTML = "";
 }
 point = new Array()
 var pointNodes = Comment.getElementsByTagName("point");
 var wholeComment = ""
 if (pointNodes[0].firstChild.nodeValue != "No Data") {
  for (loopIndex = 0; loopIndex < pointNodes.length; loopIndex++) {
   point[loopIndex] = pointNodes[loopIndex].firstChild.nodeValue;
   wholeComment =  wholeComment + point[loopIndex] + "<br><br>";
  }
 }
 document.getElementById("Comment").innerHTML = wholeComment;
}

function getDataReturnXml(url, callback) {
 var XMLHttpRequestObject = false;
 if (window.XMLHttpRequest) {
  XMLHttpRequestObject = new XMLHttpRequest();
 } else if (window.ActiveXObject) {
  XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
 }
 if(XMLHttpRequestObject) {
  XMLHttpRequestObject.open("GET", url);
  XMLHttpRequestObject.onreadystatechange = function() {
   if (XMLHttpRequestObject.readyState == 4 &&
    XMLHttpRequestObject.status == 200) {
    callback(XMLHttpRequestObject.responseXML);
    delete XMLHttpRequestObject;
    XMLHttpRequestObject = null;
   }
  }
  XMLHttpRequestObject.send(null);
 }
}
