//Source for MYLIST.CGI
var x,r;
var ShowPopups=false;

r=Math.random()*10000;
username=document.A.a.value;

document.write("<title>Game List for "+username+"</title>\n");
document.write("<div id='GamePopup' style='position:absolute;'></div>");
document.write("<center>");
document.write("<br><a href='mylist.cgi?r="+r+"'>Refresh</a>&nbsp;");
document.write("<a href=\"index.shtml\">Home</a><br>");
document.write("<a href='mylist.cgi?limit=all'>Show All Games</a>");

document.write("<br><font size=4>It's your move in the following games:</font><br>\n");
document.write("<font size=1>(Click the table headers to sort the columns)</font><br>\n");

games=document.A.g.value.split(",");
players=document.A.p.value.split(",");
times=document.A.t.value.split(",");
boards=document.A.b.value.split(",");
moves=document.A.moves.value.split(",");
notes=document.A.notes.value.split(":");

document.write("<div style='color:#990000'>View:");
document.write("<span class=option onclick='ShowTable();'>List</span>&nbsp;&nbsp;");
document.write("<span class=option onclick='ShowDiagrams();'>Diagrams</span>");
document.write("</div>");

document.write("<div id='display'></div>");
ShowTable();

document.write("<br>");

if(x==0){
	document.write("<br><b>It's not your move in any more games.</b><br>");
}

document.write("<a href='mylist.cgi?r="+r+"'>Refresh</a>&nbsp;");
document.write("<a href=\"index.shtml\">Home</a></center></body>");
document.write("<script src='sorttable.js'></script>");

function ShowSmallBoard(board,white,black,timeleft){
   var BoardHtml="";
   var c,x,t;

   BoardHtml+="<table style='display:inline' cellspacing=0 cellpadding=0><tr><td align=center>";
   BoardHtml+=black+"<br>";

   for(var x=0;x<64;x++){
      var t=board.charAt(x);

      if(t == "."){t="sq";}
      if(t == "P"){t="wp";}
      if(t == "R"){t="wr";}
      if(t == "N"){t="wn";}
      if(t == "B"){t="wb";}
      if(t == "K"){t="wk";}
      if(t == "Q"){t="wq";}
      if(t == "p"){t="bp";}
      if(t == "r"){t="br";}
      if(t == "n"){t="bn";}
      if(t == "b"){t="bb";}
      if(t == "q"){t="bq";}
      if(t == "k"){t="bk";}

      c='w';
      if(Math.floor(x/8)%2==0){
         if(x%2==1){
            c="b";
         }
      } else {
         if(x%2==0){
            c="b";
         }
      }
      t=c+t;
      BoardHtml+="<img border=0 height=16 width=16 src='images/wood/s"+t+".gif'>";

      if(x%8==7 && x!=0){
         BoardHtml+="<br>";
      }
   }
   BoardHtml+=white;
   BoardHtml+="</td></tr></table>";
   return BoardHtml;
}

function ShowDiagrams(){
   var content="";
	for(var i=0;i<boards.length;i++){
      var p=players[i].split(" vs. ");
      content+="<div style='display: inline; margin:10px'>";
      content+="<div style='cursor:pointer; display: inline;' onclick='LoadGame(\""+games[i]+"\");'>";
		content+=ShowSmallBoard(boards[i],p[0],p[1],parsetime(times[i]));
      content+="</div></div>";
	}
   document.getElementById("display").innerHTML=content;
}

function LoadGame(id){
   document.location="viewgame.cgi?p1="+id;
}

function ShowTable(){
   var content="";
	content+="<div id='TableView'><table class='sortable' border=1 id=maintable cellpadding=0 cellspacing=0>\n";
	content+="<tr><th>Players</th><th>Move #</th><th>Time Remaining</th><th>Notes</th><th></th></tr>";
	for(x=0;x<games.length && games[0]!="";x++){
		content+="<tr>";
		content+="<td><a href=\"viewgame.cgi?p1="+games[x]+"&r="+r+"\">";
		content+=players[x]+"</a>";
		content+="<td>"+moves[x]+"</td>";
		content+="<td>";
		content+=parsetime(times[x]);
		content+="<td>"+notes[x]+"</td>";
		content+="<td><div id='board"+x+"'></div></td>";
		content+="</tr>\n";
	}
	content+="</table>\n";
	content+="</div>";
   document.getElementById("display").innerHTML=content;
}

function setOption(c){
	
	document.getElementById('nodiagram').style.backgroundColor='';
	document.getElementById('popup').style.backgroundColor='';
	document.getElementById('inline').style.backgroundColor='';
	document.getElementById('nodiagram').style.color='#990000';
	document.getElementById('popup').style.color='#990000';
	document.getElementById('inline').style.color='#990000';

	if(c.id!='inline'){
		for(var i=0;i<boards.length;i++){
			var e=document.getElementById('board'+i);
			e.innerHTML="";
		}
	}

	if(c.id=="nodiagram"){
		ShowPopups=false;
		document.getElementById('nodiagram').style.backgroundColor='';
	} else if(c.id=="popup"){
		ShowPopups=true;
	} else {
		ShowPopups=false;
		for(var i=0;i<boards.length;i++){
			var e=document.getElementById('board'+i);
			e.innerHTML=ShowSmallBoard(boards[i]);
		}
	}

	c.style.backgroundColor='#990000';
	c.style.color='#ffffff';
}

function parsetime(time){
   var d,h,m,s;

   d=Math.floor(time/86400);
   time=time-d*86400;
   h=Math.floor(time/3600);
   time-=h*3600;
   m=Math.floor(time/60);
   time-=m*60;
   s=time;

   d=""+d;
	if(d.length==2){
		d="0"+d;
	} else if(d.length==1){
		d="00"+d;
	}

	h=""+h;
	if(h.length==1){
		h="0"+h;
	}

   return d+" days "+h+" hours "+m+" min "+s+" sec";
}

