<? include $DOCUMENT_ROOT."/zzzfilelist.inc"; ?>
<? if (!$dont_inc_general) { include Inc("functions_general.inc.php"); } ?>
<? include Inc("functions_images.inc.php"); ?>
<? include Inc("metoo.inc.php"); ?>
<? include Inc("metoo_loc.inc.php"); ?>
<html>
<head><title>MeToo : Middle-Earth Tool for Overcoming Opposition</title></head>
<?
	if (!$goingtologin) { include Inc("testlogin.php");	}
?>
<? $where="inputmoves"; include Inc("checkgameaccess.inc.php"); ?>
<?
	if ($nb_lin=="") { $nb_lin=10; }
	$nb_turnahead=4;
	$current_turn=0;

	$fnsave="Move ".$user."-".$game.".csv";
	$fnsave = $basedir.$metoobasedir.'turn/moves/'.$fnsave;

	if ($action=="input") {
		$lastmoveused=0;
		for ($j=1; $j<=$nb_lin; $j++) {
			for ($i=1; $i<=$nb_turnahead+1; $i++) {
				if ($move_turn[$j][$i]!=0) {
					$lastmoveused=$j;
				}
			}
		}
		$savefile="";
		for ($j=1; $j<=$lastmoveused; $j++) {
			$savefile.=$move_player[$j].";".$move_type[$j].";".$move_people[$j].";".$move_details[$j].";";
			for ($i=1; $i<=$nb_turnahead+1; $i++) {
				$savefile.=$move_turn[$j][$i].";";
			}
			$savefile.=chr(10);
		}
#		echo $savefile."<br>";

		$fp = fopen ($fnsave, "w");
		fwrite($fp, $savefile);
		fclose($fp);
		$saved=true;
	}
	
	include Inc("map_moves_2.inc.php");

	$fnsave="Move ".$user."-".$game.".csv";
	$fnsave = $basedir.$metoobasedir.'turn/moves/'.$fnsave;
	if (file_exists($fnsave)) {
		$fp = fopen ($fnsave, "r");
		$movelist = fread($fp, filesize($fnsave));
		fclose($fp);
		Decrunch($movelist);
		$movefound=true;
#		echo "found<br>".$movelist."<br>";
		
		for ($j=1; $j<=$lin; $j++) {
			$move_player[$j]=$cell[$j][1];
			$move_type[$j]=$cell[$j][2];
			$move_people[$j]=$cell[$j][3];
			$move_details[$j]=$cell[$j][4];
			for ($i=5; $i<=$col; $i++) {
				$move_turn[$j][$i-4]=$cell[$j][$i];
			}
		}
	}
	if ($lin>8) {
		$nb_lin=$lin+2;
	}
	if ($col>8) {
		$nb_turnahead=$col-6+2;
	}
?>
<center>
<form action="<? echo IncL("input_moves.inc.php"); ?>" method="post">
<input type='hidden' name='action' value='input'>
<input type='hidden' name='nb_lin' value='<? echo $nb_lin; ?>'>
<input type='hidden' name='game' value='<? echo $game; ?>'>
<table align=center>
<?
	echo "<tr align=center><td><b>".$loc[$loctag[5]+7][$lang]."</td>";
	echo "<td><b>".$loc[$loctag[5]+8][$lang]."</td>";
	echo "<td><b>".$loc[$loctag[5]+9][$lang]."</td>";
	echo "<td><b>".$loc[$loctag[5]+10][$lang]."</td>";
	for ($i=1; $i<=$nb_turnahead+1; $i++) {
		echo "<td><b>".$loc[$loctag[5]+11][$lang]." ".($current_turn+$i-1)."</td>";			
	}
	echo "</tr>\n";
	for ($j=1; $j<=$nb_lin; $j++) {
		echo "<tr>";
		echo "<td>";
		echo "<select name='move_player[$j]'>";
		for ($i=1 ;$i<=3; $i++) {
  			echo "<option value='$i'";
  			if (($movefound) && ($move_player[$j]==$i)) { echo " selected"; }
  			echo ">".$loc[$loctag[5]+$i][$lang];
  		}
		echo "</select>";
		echo "</td>";
		echo "<td>";
		echo "<select name='move_type[$j]'>";
		for ($i=1 ;$i<=3; $i++) {
  			echo "<option value='$i'";
  			if (($movefound) && ($move_type[$j]==$i)) { echo " selected"; }
  			echo ">".$loc[$loctag[5]+$i+3][$lang];
  		}
		echo "</select>";
		echo "</td>";
		echo "<td>";
		echo "<select name='move_people[$j]'>";
		for ($i=1 ;$i<=25; $i++) {
  			echo "<option value='$i'";
  			if (($movefound) && ($move_people[$j]==$i)) { echo " selected"; }
  			echo ">".$loc[$loctag[1]+$i][$lang];
  		}
		echo "</select>";
		echo "</td>";
		echo "<td>";
		echo "<input type='text' name='move_details[$j]' size=6";
		if (($movefound) && ($move_details[$j]!="")) { echo " value='".$move_details[$j]."'"; }
		echo ">";
		echo "</td>\n";			
		for ($i=1; $i<=$nb_turnahead+1; $i++) {
			echo "<td>";
			echo "<input type='text' name='move_turn[$j][$i]' size=4";
			if (($movefound) && ($move_turn[$j][$i]!="")) { echo " value='".$move_turn[$j][$i]."'"; }
			echo ">";
			echo "</td>";			
		}
		echo "</tr>\n";
	}
?>
</table>
</center>
<input value="<? echo $loc[$loctag[3]+41][$lang]; ?>" type="submit"><br>
<? if ($saved) { echo $loc[$loctag[2]+18][$lang]; }?>
</form>
<? include Inc("metoobottom.inc.php"); ?>