var ie=document.all;var nn6=document.getElementById&&!document.all;var ie7=document.documentElement&&typeof document.documentElement.style.maxHeight!="undefined";var isdrag=false;var x,y;var dobj;var switch_counter=0;var boardx=0,boardy=0;var counter=1;var boardv=new Array();var output=new Array();var waiting=0;var move_count=0;var fen_back_even="";var fen_back_odd="";var player_move;var same_count=0;var fen="";var piece="";var spot="";var start_spot="";var initPos="";var curBoard="";var error=0;for(i=0;i<64;i++){boardv[i]="";output[i]=""}function set_spot(a,c,b){boardv[a*8+c]=b}function get_spot(a,b){return boardv[a*8+b]}function valid(a,b){return(a>=0&&a<8&&b>=0&&b<8)}function light_square(a,b){if(!valid(a,b)){return}spot=get_square(a,b);curclass=spot.className;if(curclass=="dark"){spot.className="darkh"}else{if(curclass=="white"){spot.className="whiteh"}}one_lit=1}function get_value(a,b){return b*8+a}function get_square(a,b){spot="b"+get_value(a,b);return document.getElementById(spot)}function unlight_board(){ter=0;one_lit=0;for(i=0;i<8;i++){for(j=0;j<8;j++){ter++;bid=get_square(i,j);curclass=bid.className;if(ter%2){bid.className="dark"}else{bid.className="white"}}ter++}}function set_output(a,c,b){output[a*8+c]=b}function get_output(a,b){return output[a*8+b]}function reset_board(){for(i=0;i<64;i++){boardv[i]="";output[i]="--"}}function make_new(d,a,c){var b=document.createElement("img");b.src="http://www.chessvideos.tv/boardimg/"+d+".gif";b.alt=d;b.className="dg";b.id="p"+a+""+c;document.getElementById("board").appendChild(b);set_spot(a,c,b.id)}function highlight_moves(a){for(i=0;i<64;i++){first=a.substring(i*2,i*2+2);y=Math.floor(i/8);x=i%8;if(first.charAt(0)=="B"){light_square(x,y)}if(first.charAt(0)=="+"){light_square(x,y)}}}function load_board(a){clear_board();reset_board();initPos=a;for(i=0;i<64;i++){first=a.substring(i*2,i*2+2);y=Math.floor(i/8);x=i%8;if(first.charAt(0)=="B"){first="b"+first.charAt(1)}if_set(first,"bp",5,x,y);if_set(first,"bq",1,x,y);if_set(first,"bk",0,x,y);if_set(first,"bb",3,x,y);if_set(first,"bn",4,x,y);if_set(first,"br",2,x,y);if_set(first,"wp",11,x,y);if_set(first,"wq",7,x,y);if_set(first,"wk",6,x,y);if_set(first,"wb",9,x,y);if_set(first,"wn",10,x,y);if_set(first,"wr",8,x,y)}}function if_set(d,c,b,a,e){if(d==c){make_new(c,a,e)}}function button_init(b,a){move_count=0;same_count=0;setstatus("Moves so Far: "+move_count);init(b,a)}function init(b,a){unlight_board();if(!error){highlight_moves(b)}update_board=function(){load_board(b);fix_resize()};setstatus("Moves so Far: "+move_count);if(error){update_board()}else{setTimeout("update_board();",200);setTimeout("unlight_board();",700)}}function locateboard(){var a=document.getElementById("board");boardx=a.offsetLeft;boardy=a.offsetTop}function clear_board(){for(i=0;i<8;i++){for(j=0;j<8;j++){var a=document.getElementById("p"+i+""+j);if(a!=null){document.getElementById("board").removeChild(a)}}}calculateboard()}function calculateboard(){reset_board();for(i=0;i<8;i++){for(j=0;j<8;j++){var b=document.getElementById("p"+i+""+j);if(b!=null&&onboard(b)==true){px=getcol(b);py=getrow(b);set_spot(px,py,b.id);tempstr=b.src.substring(b.src.length-6,b.src.length-4);set_output(px,py,tempstr)}}}var a="";for(i=0;i<64;i++){a+=output[i]}fen="";var c=0;for(j=0;j<8;j++){for(i=0;i<8;i++){if(get_output(i,j)=="--"){c++}else{if(c>0){fen+=c}color=get_output(i,j).charAt(0);if(color=="w"){fpiece=get_output(i,j).charAt(1).toUpperCase()}else{fpiece=get_output(i,j).charAt(1)}fen+=fpiece;c=0}}if(c>0){fen+=c}if(j!=7){fen+="/"}c=0}}function fix_resize(){locateboard();for(i=0;i<8;i++){for(j=0;j<8;j++){if(get_spot(i,j)!=""){tobj=document.getElementById(get_spot(i,j));tobj.style.left=40*i+"px";tobj.style.top=40*j+"px"}}}calculateboard()}function onboard(a){ox=a.offsetLeft;oy=a.offsetTop;return(ox>=0&&ox<300&&oy>=0&&oy<300)}function getcol(a){ox=a.offsetLeft;return Math.floor(ox/40)}function getrow(a){oy=a.offsetTop;return Math.floor(oy/40)}function movemouse(a){if(isdrag){scrolly=nn6?window.pageYOffset:document.body.scrollTop;scrollx=nn6?window.pageXOffset:document.body.scrollLeft;if(!nn6){scrolly=document.documentElement.scrollTop;scrollx=document.documentElement.scrollLeft}mx=nn6?a.clientX:event.clientX;my=nn6?a.clientY:event.clientY;my+=scrolly;mx+=scrollx;if(mx>boardx&&mx<boardx+320&&my>boardy&&my<=boardy+320){fixx=-boardx+mx-20;fixy=-boardy+my-20;row=0;col=0;if(fixx<0){col=0}else{modx=fixx%40;if(modx<20){col=Math.floor(fixx/40)}else{col=Math.floor(fixx/40)+1}}if(fixy<0){row=0}else{mody=fixy%40;if(mody<20){row=Math.floor(fixy/40)}else{row=Math.floor(fixy/40)+1}}dobj.style.left=(col*40)+"px";dobj.style.top=(row*40)+"px";if(col==0){spot="a"}else{if(col==1){spot="b"}else{if(col==2){spot="c"}else{if(col==3){spot="d"}else{if(col==4){spot="e"}else{if(col==5){spot="f"}else{if(col==6){spot="g"}else{if(col==7){spot="h"}}}}}}}}spot+=(7-row+1)}else{dobj.style.left=mx-20-boardx+"px";dobj.style.top=my-20-boardy+"px"}return false}}function selectmouse(b){var d=nn6?b.target:event.srcElement;var a="HTML";if(ie7){a="HTML"}while(d.tagName!=a&&d.className!="dg"){d=nn6?d.parentNode:d.parentElement}var c=d.id;if(d.className=="dg"&&waiting==0){piece=d.alt.charAt(1);color=d.alt.charAt(0);if(color=="b"){return false}isdrag=true;dobj=d;x=nn6?b.clientX:event.clientX;y=nn6?b.clientY:event.clientY;scrolly=nn6?window.pageYOffset:document.body.scrollTop;scrollx=nn6?window.pageXOffset:document.body.scrollLeft;if(!nn6){scrolly=document.documentElement.scrollTop;scrollx=document.documentElement.scrollLeft}y+=scrolly;x+=scrollx;fixx=-boardx+x-20;fixy=-boardy+y-20;row=0;col=0;if(fixx<0){col=0}else{modx=fixx%40;if(modx<20){col=Math.floor(fixx/40)}else{col=Math.floor(fixx/40)+1}}if(fixy<0){row=0}else{mody=fixy%40;if(mody<20){row=Math.floor(fixy/40)}else{row=Math.floor(fixy/40)+1}}if(col==0){spot="a"}else{if(col==1){spot="b"}else{if(col==2){spot="c"}else{if(col==3){spot="d"}else{if(col==4){spot="e"}else{if(col==5){spot="f"}else{if(col==6){spot="g"}else{if(col==7){spot="h"}}}}}}}}spot+=(7-row+1);start_spot=spot;document.onmousemove=movemouse;return false}}function killdrag(e){if(isdrag==false){return false}isdrag=false;if(spot==""||piece==""){return false}curmove=piece.toUpperCase()+start_spot+spot;if(spot.charAt(1)=="8"&&piece=="p"){curmove=start_spot+spot+"=Q"}curpos=fen;var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if(!xmlhttp&&typeof XMLHttpRequest!="undefined"){try{xmlhttp=new XMLHttpRequest()}catch(e){xmlhttp=false}}if(!xmlhttp&&window.createRequest){try{xmlhttp=window.createRequest()}catch(e){xmlhttp=false}}xmlhttp.open("GET","http://www.chessvideos.tv/gp/?f="+curpos+"&m="+curmove+"&c="+move_count,true);xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4){newfen=xmlhttp.responseText;waiting=0;error=0;set_waiting();if(newfen=="ERROR"){setstatus("<b>Error, bad move</b>");error=1;init(initPos)}else{if(newfen=="STALEMATE"){setstatus("<b>Stalemate!  Its a draw</b>")}else{if(newfen=="DRAW"){setstatus("<b>Not enough material to mate!  Its a draw</b>")}else{if(newfen=="CHECKMATE"){setstatus("<b>Checkmate!  You win</b>")}else{if(newfen.length>4&&newfen.substring(0,4)=="LOSE"){init(newfen.substring(4));setstatus("<b>Checkmate! You lose</b>")}else{if(same_count>=3){setstatus("<b>Drawn by repetition</b>")}else{if((move_count%2)==1&&newfen==fen_back_odd){same_count++;init(newfen);move_count++;set_waiting()}else{if((move_count%2)==0&&newfen==fen_back_even){same_count++;init(newfen);move_count++;set_waiting()}else{init(newfen);if((move_count%2)==0){fen_back_even=newfen}else{fen_back_odd=newfen}same_count=0;move_count++;set_waiting()}}}}}}}}}};waiting=1;set_waiting();xmlhttp.send(null)}function setstatus(a){nn6?document.getElementById("status").innerHTML=a:document.all.status.innerHTML=a}function set_waiting(){if(waiting==1){setstatus("Thinking...")}else{setstatus("Moves so Far: "+move_count)}}window.onresize=fix_resize;document.onmousedown=selectmouse;document.onmouseup=killdrag;

