//
//Copyright David Hayes 23 April 1997
//Others may use this code by permission only.
//

//Global Declarations (to work around bug in IE 7)
var stm

var size = 36;
var today = new Date();
var expiresInOneYear = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);
var currentPosition = "8/8/8/8/8/8/8/8"

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";
 Display_Diagram(currentPosition);
 Hide_Size_Controls()
 document.getElementById("title").innerHTML = "Endgame Explorer";
}

function getData() {
 document.getElementById("title").innerHTML = "Please wait...";
 var fenQuery = "http://www.logicalchess.com/cgi-bin/dtx-hayes?fen=" + document.fen.entry.value;
 getDataReturnXml(fenQuery, displayHandler);
}

function IncrementalResize(button) {
 if (button.value == "+") {
  size++;
 }
 if (button.value == "-") {
  size--;
 }
 if (size > 72) {
  size = 72;
 }
 if (size < 18) {
  size = 18;
 }
 Display_Diagram(currentPosition);
}

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 getPosition(fen, san) {
 if (san.substring(san.length - 1, san.length) == "#") {
  document.getElementById("stm").innerHTML = stm + " wins!";
  document.getElementById("commentLead").innerHTML = "";
  document.getElementById("stmdtm").innerHTML = "";
  document.getElementById("stmdtc").innerHTML = "";
  document.getElementById("sntmdtm").innerHTML = "";
  document.getElementById("sntmdtc").innerHTML = "";
  document.getElementById("moveOptions").innerHTML = "<CENTER><h2>Checkmate</h2><h2>Try another position.</h2></center>";
  document.getElementById("currentPosition").innerHTML = '<input type="text" name="entry" size="55" maxlength="80" value="' + fen + '"><br><br>';
  fenlessstm = fen.substring(0, fen.length - 2);
  Display_Diagram(fenlessstm);
 } else {
  var fenQuery = "http://www.logicalchess.com/cgi-bin/dtx-hayes?fen=" + fen;
  getDataReturnXml(fenQuery, displayHandler);
 }
}

function displayHandler(ajaxResponse) {

//******* Check for no future moves ... as in stalemate or checkmate. *******

 try {
  stmfennode = ajaxResponse.getElementsByTagName("stmFen");
  stmfen = stmfennode[0].firstChild.nodeValue;
  stmfensub = stmfen.substring(0,stmfen.length-2);
  movefennodes = ajaxResponse.getElementsByTagName("moveFen");
  if(movefennodes[0].firstChild.nodeValue == "?") {
   throw "TypeError: Found a don\'t care value in the movefennodes."
  }
  movesannodes = ajaxResponse.getElementsByTagName("moveSan");
  if(movesannodes[0].firstChild.nodeValue == "?") {
   throw "TypeError: Found a don\'t care value in the movesannodes."
  }
  movedtmresultnodes = ajaxResponse.getElementsByTagName("moveDtmResult");
  if(movedtmresultnodes[0].firstChild.nodeValue == "?") {
   throw "TypeError: Found a don\'t care value in the movedtmresultnodes."
  }
  movedtcresultnodes = ajaxResponse.getElementsByTagName("moveDtcResult");
  if(movedtcresultnodes[0].firstChild.nodeValue == "?") {
   throw "TypeError: Found a don\'t care value in the movedtcresultnodes."
  }
 }
 catch (e) {
  document.getElementById("stm").innerHTML = "Mate";
  document.getElementById("commentLead").innerHTML = "";
  document.getElementById("stmdtm").innerHTML = "";
  document.getElementById("stmdtc").innerHTML = "";
  document.getElementById("sntmdtm").innerHTML = "";
  document.getElementById("sntmdtc").innerHTML = "";
  document.getElementById("moveOptions").innerHTML = "<center><h2>Mate</h2><h2>Try another position.</h2></center>";
  document.getElementById("currentPosition").innerHTML = '<input type="text" name="entry" size="55" maxlength="80" value="' + stmfen + '"><br><br>';
  Display_Diagram(stmfensub);
 }

//******* Check for most serious errors or illegal positions. *******

 try {
  stm = stmfen.substring(stmfen.length-1,stmfen.length);
  if(stm == "w") {
   stm = "White";
   sntm = "Black";
   sntmFen = "b";
  } else {
   stm = "Black";
   sntm = "White";
   sntmFen = "w";
  }
  stmdtmresultnode = ajaxResponse.getElementsByTagName("stmDtmResult");
  stmdtmresult = stmdtmresultnode[0].firstChild.nodeValue;
  if(stmdtmresult == "?") {
   throw "TypeError: Found a don\'t care value in the stmdtmresultnode."
  }
  if (stmdtmresult == "D") {
   stmdtmmessage = "<li>" + stm + " would only draw as verified by DTM result.</li>";
  } else {
   stmdtmdepthnode = ajaxResponse.getElementsByTagName("stmDtmDepth");
   stmdtmdepth = stmdtmdepthnode[0].firstChild.nodeValue;
   if(stmdtmdepth == "?") {
   throw "TypeError: Found a don\'t care value in the stmdtmdepthnode."
   }
   if(stmdtmdepth == 1) {
    plursing = " move.";
   } else {
    plursing = " moves.";
   }
  }
  if(stmdtmresult == "W") {
   stmdtmmessage = "<li>" + stm + " would win in " + stmdtmdepth + plursing + "</li>";
  }
  if(stmdtmresult == "L") {
   stmdtmmessage = "<li>" + stm + " would lose in " + stmdtmdepth + plursing + "</li>";
  }

  stmdtcresultnode = ajaxResponse.getElementsByTagName("stmDtcResult");
  stmdtcresult = stmdtcresultnode[0].firstChild.nodeValue;
  if(stmdtcresult == "?") {
   throw "TypeError: Found a don\'t care value in the stmdtcresultnode."
  }
  if (stmdtcresult == "D") {
   stmdtcmessage = "<li>" + stm + " would only draw as verified by DTC result.</li>";
  } else {
   stmdtcdepthnode = ajaxResponse.getElementsByTagName("stmDtcDepth");
   stmdtcdepth = stmdtcdepthnode[0].firstChild.nodeValue;
   if(stmdtcdepth == "?") {
    throw "TypeError: Found a don\'t care value in the stmdtcdepthnode."
   }
   if(stmdtcdepth == 1) {
    plursing = " move.";
   } else {
    plursing = " moves.";
   }
  }
  if(stmdtcresult == "W") {
   stmdtcmessage = "<li>" + stm + " can promote a pawn or capture a piece in " + stmdtcdepth + plursing + "</li>";
  }
  if(stmdtcresult == "L") {
   stmdtcmessage = "<li>" + stm + " cannot promote a pawn or capture a piece.</li>";
  }
 }
 catch (e) {
  alert("Sorry.  I encountered an error while trying to process your chess position.  This error is usually the result of an illegal chess position or a position not in the endgame database.\nPlease fix your FEN entry and try again.\n\nYou may have to wait awhile until the endgame database resets itself.  Also, you may need to clear your cache before trying again.\n\nIf you feel that your FEN position is correct and should be in the endgame database, then please email the exact TypeError below, your browser version, and your FEN to the email address described on the About page.\n\n" + e)
  Display_Diagram("3ppp2/2p3p1/6p1/5p2/4p3/4p3/8/4p3");
 }

 try {
  sntmdtmresultnode = ajaxResponse.getElementsByTagName("sntmDtmResult");
  sntmdtmresult = sntmdtmresultnode[0].firstChild.nodeValue;
  if(sntmdtmresult == "?") {
   throw "TypeError: Found a don\'t care value in the sntmdtmresultnode."
  }
  if (sntmdtmresult == "D") {
   sntmdtmmessage = "<li>If " + sntm + " were on the move, then a draw would result as verified by DTM result.</li>";
  } else {
   sntmdtmdepthnode = ajaxResponse.getElementsByTagName("sntmDtmDepth");
   sntmdtmdepth = sntmdtmdepthnode[0].firstChild.nodeValue;
   if(sntmdtmdepth == "?") {
   throw "TypeError: Found a don\'t care value in the sntmdtmdepthnode."
   }
   if(sntmdtmdepth == 1) {
    plursing = " move.";
   } else {
    plursing = " moves.";
   }
  }
  if(sntmdtmresult == "W") {
   sntmdtmmessage = "<li>If " + sntm + " were on the move, then " + stm + " would still win in " + sntmdtmdepth + plursing + "</li>";
  }
  if(sntmdtmresult == "L") {
   sntmdtmmessage = "<li>If " + sntm + " were on the move, then " + sntm + " would win in " + sntmdtmdepth + plursing + "</li>";
  }
  sntmdtcresultnode = ajaxResponse.getElementsByTagName("sntmDtcResult");
  sntmdtcresult = sntmdtcresultnode[0].firstChild.nodeValue;
  if(sntmdtcresult == "?") {
   throw "TypeError: Found a don\'t care value in the sntmdtcresultnode."
  }
  if (sntmdtcresult == "D") {
   sntmdtcmessage = "<li>If " + sntm + " were on the move, then a draw would result as verified by DTC result.</li>";
  } else {
   sntmdtcdepthnode = ajaxResponse.getElementsByTagName("sntmDtcDepth");
   sntmdtcdepth = sntmdtcdepthnode[0].firstChild.nodeValue;
   if(sntmdtcdepth == "?") {
    throw "TypeError: Found a don\'t care value in the sntmdtcdepthnode."
   }
   if(sntmdtcdepth == 1) {
    plursing = " move.";
   } else {
    plursing = " moves.";
   }
  }
  if(sntmdtcresult == "W") {
   sntmdtcmessage = "<li>If " + sntm + " were on the move, then " + stm + " can promote a pawn or capture a piece in " + sntmdtcdepth + plursing + "</li>";
  }
  if(sntmdtcresult == "L") {
   sntmdtcmessage = "<li>If " + sntm + " were on the move, then " + stm + " cannot promote a pawn or capture a piece.</li>";
  }
 }
 catch (e) {
  sntmdtmmessage = "";
  sntmdtcmessage = "";
 }

 movedtmdepthnodes = ajaxResponse.getElementsByTagName("moveDtmDepth");
 movedtcdepthnodes = ajaxResponse.getElementsByTagName("moveDtcDepth");

 document.getElementById("commentLead").innerHTML = "From this position, and with best play by both sides, we know with certainty that...";
 document.getElementById("stm").innerHTML = stm + " to move.";
 document.getElementById("currentPosition").innerHTML = '<input type="text" name="entry" size="55" maxlength="80" value="' + stmfen + '"><br><br>';
 document.getElementById("stmdtm").innerHTML = stmdtmmessage;
 document.getElementById("stmdtc").innerHTML = stmdtcmessage;
 document.getElementById("sntmdtm").innerHTML = sntmdtmmessage;
 document.getElementById("sntmdtc").innerHTML = sntmdtcmessage;

 skipdtmdepth = 0;
 skipdtcdepth = 0;
 var movefen = new Array(movefennodes.length);
 var movefensub = new Array(movefennodes.length);
 var movesan = new Array(movefennodes.length);
 var movedtmresult = new Array(movefennodes.length);
 var movedtmdepth = new Array(movefennodes.length);
 var movedtcresult = new Array(movefennodes.length);
 var movedtcdepth = new Array(movefennodes.length);
 for(loopIndex = 0; loopIndex < movefennodes.length; loopIndex++) {
  movefen[loopIndex] = movefennodes[loopIndex].firstChild.nodeValue;
  movefensub[loopIndex] = movefen[loopIndex].substring(0, movefen[loopIndex].length - 2);
  movesan[loopIndex] = movesannodes[loopIndex].firstChild.nodeValue;
  movedtmresult[loopIndex] = movedtmresultnodes[loopIndex].firstChild.nodeValue;
  if(movedtmresult[loopIndex] == "D") {
   movedtmdepth[loopIndex] = "N/A";
   skipdtmdepth++;
  } else {
   movedtmdepth[loopIndex] = movedtmdepthnodes[loopIndex - skipdtmdepth].firstChild.nodeValue;
  }
  if(movedtmresult[loopIndex] == "W") {
   if(movedtmdepth[loopIndex] == 0) {
    movedtmresult[loopIndex] = stm + " wins!";
   } else {
    movedtmresult[loopIndex] = stm + " can win";
   }
  }
  if(movedtmresult[loopIndex] == "L") {
   movedtmresult[loopIndex] = stm + " to lose";
  }
  if(movedtmresult[loopIndex] == "D") {
   movedtmresult[loopIndex] = "Draw";
  }
  movedtcresult[loopIndex] = movedtcresultnodes[loopIndex].firstChild.nodeValue;
  if(movedtcresult[loopIndex] == "D") {
   movedtcdepth[loopIndex] = "N/A";
   skipdtcdepth++;
  } else {
   movedtcdepth[loopIndex] = movedtcdepthnodes[loopIndex - skipdtcdepth].firstChild.nodeValue;
  }
  if(movedtcresult[loopIndex] == "W") {
   if(movedtcdepth[loopIndex] == 0) {
    movedtcresult[loopIndex] = stm + " wins!";
   } else {
    movedtcresult[loopIndex] = stm + " can win";
   }
  }
  if(movedtcresult[loopIndex] == "L") {
   movedtcresult[loopIndex] = stm + " to lose";
  }
  if(movedtcresult[loopIndex] == "D") {
   movedtcresult[loopIndex] = "Draw";
  }
 }

 var nextMoveTable = '<CENTER><h2>Select the Next Move</h2></center>\n<table align="center" border="1" cellspacing="0" cellpadding="0" width="100%">\n  <tr align="left" valign="middle">\n    <td align="center" valign="middle">' + stm + '\'s<br>Next<br>Move</td>\n    <td align="center" valign="middle">Distance to Mate<br>Result</td>\n    <td align="center" valign="middle">Moves to<br>Mate</td>\n    <td align="center" valign="middle">Distance to Conversion<br>Result</td>\n    <td align="center" valign="middle">Move to<br>Conversion</td>\n  </tr>';
 for(loopIndex = 0; loopIndex < movefen.length; loopIndex++) {
  nextMoveTable = nextMoveTable + '\n  <TR ALIGN="left" VALIGN="middle">\n    <td align="center" valign="middle"><a href="javascript:getPosition(\'' + movefensub[loopIndex] + '+' + sntmFen + '\', \'' + movesan[loopIndex] + '\')" return true">' + movesan[loopIndex] + '</a></td>\n    <td align="center" valign="middle">' + movedtmresult[loopIndex] + '</td>\n    <td align="center" valign="middle">' + movedtmdepth[loopIndex] + '</td>\n    <td align="center" valign="middle">' + movedtcresult[loopIndex] + '</td>\n    <td align="center" valign="middle">' + movedtcdepth[loopIndex] + '</td>\n  </tr>';
 }
 nextMoveTable = nextMoveTable + '</table>';
 document.getElementById("moveOptions").innerHTML = nextMoveTable;
 document.getElementById("title").innerHTML = "Endgame Explorer";
 Display_Diagram(stmfensub);
}

function Display_Diagram(Forsythe_Notation) {
 currentPosition = Forsythe_Notation;
 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;
}

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);
 }
}
