<? include $DOCUMENT_ROOT."/zzzfilelist.inc"; ?>
<? include Inc("metootop.inc.php"); ?>
<? $where="adminpage"; include Inc("checkgameaccess.inc.php"); ?>
<?
	if ($adminuser) {
?>
<center>
<? include Inc("check_insertedturns.inc.php"); ?>
<?

	if ($actionadmin!="") {
		echo "<br>";
		$savegamelist=false;
		if ($actionadmin=="changeadminpass") {
			if ($newadminpass=="") {
				echo "<font class='rougeboldpadd'>Enter a password<br></font>";
			} else {
				echo "<font class='orangeBOLD'>Admin password is now : $newadminpass</font><br>";
				
				$savefile=$newadminpass;
				$fn=Inc("access.csv");
				$fp = fopen ($fn, "w");
				fwrite($fp, $savefile);
				fclose($fp);
			}
		}
		if ($actionadmin=="newgame") {
			if (($gamename=="") || ($gamepass=="") || ($gamenumber=="")) {
				echo "<font class='rougeboldpadd'>Enter a name, password and number for the new game<br></font>";
			} else {
				echo "<font class='orangeBOLD'>Creating game $gamename (n°$gamenumber) with password $gamepass</font><br>";
				
				$gamenb++;
				$game_nb[$gamenb]=$gamenb;
				$game_name[$gamenb]=$gamename;
				$game_pass[$gamenb]=$gamepass;
				$game_mode[$gamenb]=1;
				$game_check[$gamenb]=$gamenumber;
				$game_side[$gamenb]=$gameside;
				$place=strrpos($gamephoto, "/");
				$dirphoto=substr($gamephoto, 0, $place+1);
				$namephoto=substr($gamephoto, $place+1, strlen($gamephoto)-$place-1-4);
				copy($DOCUMENT_ROOT.$dirphoto.$namephoto.".jpg", $DOCUMENT_ROOT.$dirphoto."game".$gamenb.".jpg");
				copy($DOCUMENT_ROOT.$dirphoto.$namephoto."_no.jpg", $DOCUMENT_ROOT.$dirphoto."game".$gamenb."_no.jpg");
			}
			$savegamelist=true;
		}
		if ($actionadmin=="changepass") {
			if ($newpass=="") {
				echo "<font class='rougeboldpadd'>Enter a password<br></font>";
			} else {
				echo "<font class='orangeBOLD'>Password for game $actionongame is now : $newpass</font><br>";
				
				$game_pass[$actionongame]=$newpass;
			}
			$savegamelist=true;
		}
		if ($actionadmin=="changemode") {
			$game_mode[$actionongame]=$newmode;
			echo "<font class='orangeBOLD'>Moderator for game $actionongame is now : ".$usercell[$game_mode[$actionongame]][1]."</font><br>";
			$savegamelist=true;
		}
		if ($actionadmin=="closegame") {
			$game_status[$actionongame]="closed";
			echo "<font class='orangeBOLD'>Game $actionongame is now closed</font><br>";
			$savegamelist=true;
		}
		if ($actionadmin=="opengame") {
			$game_status[$actionongame]="";
			echo "<font class='orangeBOLD'>Game $actionongame is now open</font><br>";
			$savegamelist=true;
		}

		if ($actionadmin=="changeoptions") {
			if ($rememberpast!="on") {
				$game_past[$actionongame]="1";
				echo "<font class='orangeBOLD'>Game $actionongame is now starting over at each turn</font><br>";
			} else {
				$game_past[$actionongame]="0";
				echo "<font class='orangeBOLD'>Game $actionongame is now remembering past data</font><br>";
			}
			$savegamelist=true;
		}

		if ($savegamelist) {
			$savefile="";
			for ($i=1; $i<=$gamenb; $i++) {
				$savefile.=$game_nb[$i].";".$game_name[$i].";".$game_pass[$i].";";
				$savefile.=$game_mode[$i].";".$game_check[$i].";".$game_side[$i].";".$game_status[$i].";".$game_past[$i].";";
				$savefile.=chr(10);
			}
	
			$fn=Inc("gamelist.csv");
			$fp = fopen ($fn, "w");
			fwrite($fp, $savefile);
			fclose($fp);
		}
	}
	
	echo "<br>";
	echo "<font class='jaunePALEpetit'><a onclick=\"document.getElementById('modifyadminpass').style.display='block'\" style='text-decoration:underline;cursor:pointer'>Modify admin password</a></font>";
	echo "<div id='modifyadminpass' style='display:none'>";
	echo "<table border=1 cellpadding=5><tr>";
	echo "<form action='adminpage.php' method='post'>";
	echo "<input type='hidden' name='actionadmin' value='changeadminpass'>";
	echo "<td width=150><center>";
	echo "<font class='jaunePALEpetit'>Admin pass <input type='text' name='newadminpass' size=10><br>";
	echo "<input type='submit' value='".$loc[$loctag[9]+15][$lang]."'>";
	echo "</td></form></tr></table>";
	echo "</div>";

	echo "<br>";
	echo "<font class='jaunePALEpetit'><a onclick=\"document.getElementById('newgame').style.display='block'\" style='text-decoration:underline;cursor:pointer'>Create new game</a></font>";
	echo "<div id='newgame' style='display:none'>";
	echo "<table border=1 cellpadding=5><tr>";
	echo "<form name='addone' action='adminpage.php' method='post'>";
	echo "<input type='hidden' name='actionadmin' value='newgame'>";
	echo "<td><center>";
	echo "<font class='jaunePALEpetit'>Game name <input type='text' name='gamename' size=20><br><br>";
	echo "<font class='jaunePALEpetit'>Game password <input type='text' name='gamepass' size=10><br><br>";
	echo "<font class='jaunePALEpetit'>Game CR number <input type='text' name='gamenumber' size=4><br><br>";
	echo "<font class='jaunePALEpetit'>Game side ";
	echo "<select name='gameside'>";
	echo "<option value='1'>".$loc[$loctag[1]+26][$lang];
	echo "<option value='2'>".$loc[$loctag[1]+27][$lang];
	echo "</select><br><br>";
	echo "<input type='hidden' name='gamephoto' value=''>";
	echo "<a onclick='javascript:openWindow(\"choosepic.inc.php?photorep=pics&formopener=addone\",\"560\",\"600\")' style='cursor:pointer'>";
	echo "<img src=".IncL('choose_default.gif')." name='photo_img_addone' border=0>";
	echo "</a><br>";
	echo "<input type='submit' value='".$loc[$loctag[3]+41][$lang]."'>";
	echo "</td></form></tr></table>";
	echo "</div>";

	echo "<br><br>";
	echo "<table>";
	for ($n=1; $n<=$gamenb; $n++) {
		if ($n!=1) {
			echo "<tr height=20><td>&nbsp;</td></tr>";
		}
		echo "<tr align=center valign=middle>";
		echo "<td rowspan=2>";
			echo "<font color=#FFFF00>";
			if ($game_status[$n]=="closed") {
				echo "<font class='rougeboldpadd'>game closed</font><br>";
				echo "<img src='".DirL('picsdir.txt')."game".$n."_no.jpg' border=0><br>";
			} else {
				echo "<img src='".DirL('picsdir.txt')."game".$n.".jpg' border=0><br>";
			}
			$display=$loc[$loctag[2]+6][$lang]." ".$game_nb[$n];
			if ($game_name[$n]!="") { $display.=" : <b>".$game_name[$n]."</b>"; }
			$display=str_replace(" ","&nbsp;",$display);
			echo $display;
		echo "</td>";
		echo "<td width=30>&nbsp;</td>";
		echo "<td width=150><font class='jaunePALEpetit'>";
			$onefound=false;
			$mustfound="access".$n;
			for ($u=1; $u<=$userlin; $u++) {
				$alreadyin=false;
				for ($u2=7; $u2<=$usercol; $u2++) {
					if (($usercell[$u][$u2]==$mustfound) && (!$alreadyin)) {
						if ($onefound) {
							echo ", ";
						}
						echo $usercell[$u][1];
						$alreadyin=true;
						$onefound=true;
					}
				}
			}
		echo "</td>";
		echo "<td width=30>&nbsp;</td>";
		echo "<td>";
			echo "<table>";
			echo "<tr align=center>";
			echo "<td><font class='jaunePALEpetit'><b>turn</td>";
			for ($t=max($gameturnmin[$n], $gameturnmax[$n]-10); $t<=$gameturnmax[$n]; $t++) {
				echo "<td><font class='jaunePALEpetit'>".$t."</td>";
			}
			echo "</tr>";
			echo "<tr align=center>";
			echo "<td><font class='jaunePALEpetit'><b>CRs</td>";
			for ($t=max($gameturnmin[$n], $gameturnmax[$n]-10); $t<=$gameturnmax[$n]; $t++) {
				echo "<td>";
				echo "<font class='jaunePALEpetit'><b>".$gameturn[$n][$t];
				echo "</td>";
			}
			echo "</tr>";
			echo "</table>";
			
			
		echo "</td>";
		echo "</tr>";

		echo "<tr align=center>";
		echo "<td colspan=4>";
			echo "<table>";
			echo "<tr align=center valign=middle>";
			echo "<td>";
			echo "<font color=#FFFF00 size=-1>Game password = ".$game_pass[$n]."<br>";
	
			echo "<font class='jaunePALEpetit'><a onclick=\"document.getElementById('modifypass-$n').style.display='block'\" style='text-decoration:underline;cursor:pointer'>Modify password</a></font>";
			echo "<div id='modifypass-$n' style='display:none'>";
			echo "<table><tr>";
			echo "<form action='adminpage.php' method='post'>";
			echo "<input type='hidden' name='actionongame' value='$n'>";
			echo "<input type='hidden' name='access' value='ok'>";
			echo "<input type='hidden' name='actionadmin' value='changepass'>";
			echo "<td width=100><center>";
			echo "<font class='jaunePALEpetit'>Pass <input type='text' name='newpass' size=10><br>";
			echo "<input type='submit' value='".$loc[$loctag[9]+15][$lang]."'>";
			echo "</td></form></tr></table>";
			echo "</div>";
	
			echo "</td>";
			echo "<td width=30>&nbsp;</td>";
			echo "<td>";
			echo "<font color=#FFFF00 size=-1>Moderator = ".$usercell[$game_mode[$n]][1]."<br>";
	
			echo "<font class='jaunePALEpetit'><a onclick=\"document.getElementById('modifymode-$n').style.display='block'\" style='text-decoration:underline;cursor:pointer'>Change moderator</a></font>";
			echo "<div id='modifymode-$n' style='display:none'>";
			echo "<table><tr>";
			echo "<form action='adminpage.php' method='post'>";
			echo "<input type='hidden' name='actionongame' value='$n'>";
			echo "<input type='hidden' name='access' value='ok'>";
			echo "<input type='hidden' name='actionadmin' value='changemode'>";
			echo "<td width=100><center>";
			echo "<font class='jaunePALEpetit'>Moderator ";
			echo "<select name='newmode'>";
			for ($p=1; $p<=$userlin; $p++) {
				echo "<option value='$p'";
				if ($p==$game_mode[$n]) { echo " selected"; }
				echo ">".$usercell[$p][1];
				echo " (".$usercell[$p][3]." ".$usercell[$p][4].")";
			}
			echo "</select>";
			echo "<input type='submit' value='".$loc[$loctag[9]+15][$lang]."'>";
			echo "</td></form></tr></table>";
			echo "</div>";
	
			echo "</td>";
			echo "<td width=30>&nbsp;</td>";
			echo "<td>";

			echo "<font class='jaunePALEpetit'><a onclick=\"document.getElementById('changeoptions-$n').style.display='block'\" style='text-decoration:underline;cursor:pointer'>Game options</a></font>";
			echo "<div id='changeoptions-$n' style='display:none'>";
			echo "<table><tr>";
			echo "<form action='adminpage.php' method='post'>";
			echo "<input type='hidden' name='actionongame' value='$n'>";
			echo "<input type='hidden' name='access' value='ok'>";
			echo "<input type='hidden' name='actionadmin' value='changeoptions'>";
			echo "<td width=100><center><font class='jaunePALEpetit'>";
			echo "<input type=checkbox name='rememberpast'";
			if ($game_past[$n]!=1) { echo " checked"; }
			echo ">Remember past turns<br>";
			echo "<input type='submit' value='".$loc[$loctag[9]+15][$lang]."'>";
			echo "</td></form></tr></table>";
			echo "</div>";

	
			echo "</td>";
			echo "<td width=30>&nbsp;</td>";
			echo "<td>";
	
			if ($game_status[$n]=="closed") {
				echo "<font class='jaunePALEpetit'><a onclick=\"document.getElementById('opengame-$n').style.display='block'\" style='text-decoration:underline;cursor:pointer'>Open the game</a></font>";
				echo "<div id='opengame-$n' style='display:none'>";
				echo "<table><tr>";
				echo "<form action='adminpage.php' method='post'>";
				echo "<input type='hidden' name='actionongame' value='$n'>";
				echo "<input type='hidden' name='access' value='ok'>";
				echo "<input type='hidden' name='actionadmin' value='opengame'>";
				echo "<td width=100><center>";
				echo "<input type='submit' value='".$loc[$loctag[9]+15][$lang]."'>";
				echo "</td></form></tr></table>";
				echo "</div>";
			} else {
				echo "<font class='jaunePALEpetit'><a onclick=\"document.getElementById('closegame-$n').style.display='block'\" style='text-decoration:underline;cursor:pointer'>Close the game</a></font>";
				echo "<div id='closegame-$n' style='display:none'>";
				echo "<table><tr>";
				echo "<form action='adminpage.php' method='post'>";
				echo "<input type='hidden' name='actionongame' value='$n'>";
				echo "<input type='hidden' name='access' value='ok'>";
				echo "<input type='hidden' name='actionadmin' value='closegame'>";
				echo "<td width=100><center>";
				echo "<input type='submit' value='".$loc[$loctag[9]+15][$lang]."'>";
				echo "</td></form></tr></table>";
				echo "</div>";
			}
	
			echo "</td>";
			echo "</tr>";
			echo "</table>";
		echo "</td>";
		echo "</tr>";
	}
	echo "</table>";
?>
<?
	}
?>