/* Queenside.net */
var gx;
var gt;
var mf;
var mt;
var ic;
var b = new Array(64);
mf = '';
mt = '';
ic = false;
game_id = Math.floor(Math.random() * Math.pow(10,12));
if(Math.random() >= 0.5){
gt = 'white';}else{
gt = 'black';}
function getxhr(){
var xhr = null;
if (window.XMLHttpRequest){
xhr = new XMLHttpRequest();}else if (window.ActiveXObject){
try{
onclick_a = true;
xhr = new ActiveXObject("Msxml2.XMLHTTP");}catch (e){
xhr = new ActiveXObject("Microsoft.XMLHTTP");}}else{
xhr = false;}
return (xhr);}
function position(c, l){
switch(c){
case 1: c2 = 'a'; break;
case 2: c2 = 'b'; break;
case 3: c2 = 'c'; break;
case 4: c2 = 'd'; break;
case 5: c2 = 'e'; break;
case 6: c2 = 'f'; break;
case 7: c2 = 'g'; break;
case 8: c2 = 'h'; break;}
return (c2 + l);}
function init(){
var xhr;
build_board();
xhr = getxhr();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
gx = xhr.responseXML;
board();
computing(false);}};
computing(true);
if(gt == 'black'){
xhr.open('GET', 'game.xml?' + game_id + '&go', true);}else{
xhr.open('GET', 'game.xml?' + game_id, true);}
xhr.send(null);}
function build_board(){
var z;
var c; var l;
var pos;
var b;
b = true;
z = '<table>';
if(gt == 'white'){
for(l = 8; l >= 1; l--){
z = z + '<tr>';
for(c = 1; c <= 8; c++){
pos = position(c, l);
if(b == true){
z = z + '<td id="' + pos + '" class="c"></td>';}else{
z = z + '<td id="' + pos + '" class="s"></td>';}
b = !b;}
b = !b;
z = z + '</tr>';}}
else if(gt == 'black'){
for(l = 1; l <= 8; l++){
z = z + '<tr>';
for(c = 8; c >= 1; c--){
pos = position(c, l);
if(b == true){
z = z + '<td id="' + pos + '" class="c"></td>';}else{
z = z + '<td id="' + pos + '" class="s"></td>';}
b = !b;}
b = !b;
z = z + '</tr>';}}
z = z + '</table>';
document.getElementById('board').innerHTML = z;}
function board(){
gt_turn();
gt_board();
fill_board();}
function gt_turn(){
if(gx.getElementsByTagName('turn').item(0).firstChild){
xml_turn = gx.getElementsByTagName('turn').item(0).firstChild.data;
if(xml_turn != gt){
gt = xml_turn;
build_board();}}}
function gt_board(){
var piece;
var i; var j;
for(i = 1; i <= 8; i++){
for(j = 1; j <= 8; j++){
pos = position(i, j);
if(gx.getElementsByTagName(pos).item(0)){
b[pos] = gx.getElementsByTagName(pos).item(0).firstChild.data;}else{
b[pos] = null;}}}}
function fill_board(){
var piece;
var url;
var i; var j;
var pos;
for(i = 1; i <= 8; i++){
for(j = 1; j <= 8; j++){
pos = position(j, i);
piece = b[pos];
if(piece){
url = './img/' + piece + '.png';}else{
url = './img/x.png';}
document.getElementById(pos).innerHTML = '<a href="javascript:selection(\'' + pos + '\')"><img src=\"' + url + '\" width="30" height="30" alt="" /></a>';}}}
function play(move){
var xhr;
var coup;
xhr = getxhr();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
gx = xhr.responseXML;
board();
computing(false);}};
if(mf != '' && mt != ''){
coup = mf + mt;
piece_from = b[mf];
if(piece_from){
url_to = './img/' + piece_from + '.png';}else{
url_to = './img/x.png';}
document.getElementById(mt).innerHTML = '<a href="javascript:selection(\'' + mt + '\')"><img src=\"' + url_to + '\" width="30" height="30" alt="" /></a>';
document.getElementById(mf).innerHTML = '<a href="javascript:selection(\'' + mf + '\')"><img src=\"./img/x.png\" width="30" height="30" alt="" /></a>';
mf = '';
mt = '';
computing(true);
xhr.open('GET', 'game.xml?' + game_id + '&' + coup, true);
xhr.send(null);}else if (move == 'go'){
computing(true);
xhr.open('GET', 'game.xml?' + game_id + '&go', true);
xhr.send(null);}}
function selection(position){
if(!ic){
if(mf == ''){
if(b[position]){
mf = position;
document.getElementById(mf).className = document.getElementById(mf).className.charAt(0) + ' x';}}else if (mf != '' && mt == ''){
if(position == mf){
document.getElementById(mf).className = document.getElementById(mf).className.charAt(0);
mf = '';}else{
mt = position;
document.getElementById(mf).className = document.getElementById(mf).className.charAt(0);
play();}}}}
function computing(sh){
ic = sh
if(sh){
document.getElementById('computing').style.visibility = 'visible';}else{
document.getElementById('computing').style.visibility = 'hidden';}}
function help(){
if(document.getElementById('help').style.visibility == 'hidden'){
document.getElementById('help').style.visibility = 'visible';}else{
document.getElementById('help').style.visibility = 'hidden';}}
function mail (){
h = '<a h'+'ref="'+'mail'+'to'+':queenside';
h = h+'.net@g'+'mail.'+'com">'+'queenside.';
h = h+'net@gm'+'ai'+'l.com</a>';
document.write(h);
return h;}

