<? include $DOCUMENT_ROOT."/zzzfilelist.inc"; ?>
<? if (!$dont_inc_general) { include Inc("functions_general.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>
<body>
<center>
<font size=+2><b>Middle-Earth Tool for Overcoming Opposition</b></font><br>
</center>

<script language="JavaScript">
function recalc(p) {
	var sumprec, sumrev, sumcost, balance, sumnext;
	var theForm = document.forms["calc"];
	var turn, turn_n;

	for (turn=1; turn<=3; turn++) {
		sumprec=document.getElementById("gold-"+turn+"-"+p).innerHTML*1;
		sumrev = theForm.elements["towntaxes-"+turn+"-"+p].value*1+theForm.elements["goldprod-"+turn+"-"+p].value*1+theForm.elements["marketsell-"+turn+"-"+p].value*1+theForm.elements["receivedgifts-"+turn+"-"+p].value*1+theForm.elements["otherrevenues-"+turn+"-"+p].value*1;
		document.getElementById("revenues-"+turn+"-"+p).innerHTML="<b>"+sumrev;
		sumcost = theForm.elements["costarmies-"+turn+"-"+p].value*1+theForm.elements["costtowns-"+turn+"-"+p].value*1+theForm.elements["costheroes-"+turn+"-"+p].value*1+theForm.elements["marketbuy-"+turn+"-"+p].value*1+theForm.elements["sentgifts-"+turn+"-"+p].value*1+theForm.elements["othercosts-"+turn+"-"+p].value*1;
		document.getElementById("costs-"+turn+"-"+p).innerHTML="<b>"+sumcost;
		balance=sumrev-sumcost;
		document.getElementById("balance-"+turn+"-"+p).innerHTML="<b>"+balance;
		sumnext=sumprec+balance;
		turn_n=turn+1;
		document.getElementById("gold-"+turn_n+"-"+p).innerHTML=sumnext;
	}
}
</script>

<hr>
<?
	if (!$goingtologin) { include Inc("testlogin.php");	}
?>
<? $where="showstock"; include Inc("checkgameaccess.inc.php"); ?>
<?
	$filenb_touse=0;
	for ($nbf=1; $nbf<=$filenb; $nbf++) {
		if (strpos($filename[$nbf], "eutral.csv")!=false) {
			$filenb_touse++;
			$filename_touse[$filenb_touse]=$filename[$nbf];
			$filedir_touse[$filenb_touse]=$filedir[$nbf];
	 	}
		if ((strpos($filedir[$nbf], "turn")!=false) && (strpos($filename[$nbf], ".csv")!=false)) {
			$fileword=str_replace(" ","-",$filename[$nbf]);
			$fileword=str_replace(".","-",$fileword);
			$fileword = explode("-", $fileword);
			if (($fileword[0]=="Game") && ($fileword[1]==$game)) {
				$filenb_touse++;
				$filename_touse[$filenb_touse]=$filename[$nbf];
				$filedir_touse[$filenb_touse]=$filedir[$nbf];
				$fileturn_touse[$filenb_touse]=$fileword[2];
				$filepeople_touse[$filenb_touse]=$fileword[3];
	 		}
	 	}
#		echo $filename[$nbf];
	}
	
	$turntodisplaymax=0;
	for ($i=1; $i<=25; $i++) {
		$turnmosttodate[$i]=0;
		$turnunderdisplay[$i]=-1;
		for ($nbf=1; $nbf<=$filenb_touse; $nbf++) {
			if ($filepeople_touse[$nbf]==$i) {
				if ($fileturn_touse[$nbf]>$turnmosttodate[$i]) {
					$turnmosttodate[$i]=$fileturn_touse[$nbf];
					if ($turnmosttodate[$i]>$turntodisplaymax) {
						$turntodisplaymax=$turnmosttodate[$i];
					}
				}
			}
		}
	}

	for ($nbf=1; $nbf<=$filenb; $nbf++) {
		$done=false;
		if ($fileturn_touse[$nbf]==$turntodisplaymax) {
			$done=true;
		}
		if ($done) {
			$fn=$DOCUMENT_ROOT.$filedir_touse[$nbf].'/'.$filename_touse[$nbf];
			$fp = fopen ($fn, "r");
			if (filesize($fn)>0) { $content = fread($fp, filesize($fn)); }
			fclose($fp);
			Decrunch($content);

			for ($i=1; $i<=$lin; $i++) {
				switch ($cell[$i][1]) {
					case "people" :
						$peoplenb=$cell[$i][2];
					break;
					case "town" :
						$nbtowns++;
						$town[$nbtowns] = new Town;
						$town[$nbtowns]->people=$peoplenb;
						$town[$nbtowns]->name=$cell[$i][2];
						$town[$nbtowns]->capital=$cell[$i][3];
						$town[$nbtowns]->hex=$cell[$i][4];
						$town[$nbtowns]->climate=$cell[$i][5];
						$town[$nbtowns]->size=$cell[$i][6];
						$town[$nbtowns]->fortif=$cell[$i][7];
						$town[$nbtowns]->loyalty=$cell[$i][8];
						$town[$nbtowns]->dock=$cell[$i][9];
						$town[$nbtowns]->hidden=$cell[$i][10];
						$town[$nbtowns]->sieged=$cell[$i][11];
						for ($sf2=1; $sf2<=8; $sf2++) {
							$town[$nbtowns]->localprod[$sf2]=$cell[$i][11+$sf2];
						}
					break;
					case "market" :
						for ($sf=1; $sf<=21; $sf++) {
							$market[$sf]=$cell[$i][$sf+1];
						}
					break;
					case "stock" :
						for ($sf=1; $sf<=20; $sf++) {
							$stock[$peoplenb][$sf]=$cell[$i][$sf+1];
						}
					break;
				}
			}
		}
	}
	
#	echo "<a href='showstock.inc.php?game=$game&show=balance' style='color:black;cursor:pointer'><font size=2>".$loc[$loctag[6]+16][$lang]."</a> -";
#	echo "<a href='showstock.inc.php?game=$game&show=store' style='color:black;cursor:pointer'><font size=2>".$loc[$loctag[6]+17][$lang]."</a>";

	$nbupdtodate=0;
	echo "<b><font size=+1>".$loc[$loctag[6]+16][$lang]."</font></b><br>";
	echo "<table align=center border=1 cellspacing=0>";
	echo "<form name='calc'>";
	# people
	echo "<tr align=center>";
	echo "<td width=90></td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			echo "<td width=90><b>".$loc[$loctag[1]+$i][$lang]."</td>";
			$nbupdtodate++;
		}
	}
	echo "</tr>\n";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			if ($stock[$i][6]!="") { $data[$i]=true; }
		}
	}
	for ($turn=1; $turn<=3; $turn++) {
	# next turn
	echo "<tr align=center bgcolor=#C0C0FF>";
	echo "<td colspan=".($nbupdtodate+1)."><i>".$loc[$loctag[5]+11][$lang]." ".($turntodisplaymax+$turn)."</td>";
	echo "</tr>\n";
	# gold in cash
	echo "<tr align=center bgcolor=#C0FFC0>";
	echo "<td>".$loc[$loctag[6]+1][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			echo "<td><div id=gold-$turn-$i>";
			if ($data[$i]) {
			if ($turn==1) {
				$goldcash[$i]=$stock[$i][6];
			} else {
				$s=$stock[$i][1]+$stock[$i][2]+$stock[$i][3];
				$s=$stock[$i][5]-$s;
				$goldcash[$i]+=$s;
			}
			echo $goldcash[$i];
			}
			echo "</div></td>";
		}
	}
	echo "</tr>";

	# taxes	
	for ($i=1; $i<=25; $i++) {
		$towntaxes[$i]=0;
	}
	for ($j=2; $j<=5; $j++) {
		for ($i=1; $i<=25; $i++) {
			if ($turnmosttodate[$i]==$turntodisplaymax) {
				for ($t=1; $t<=$nbtowns; $t++) {
					if (($town[$t]->people==$i) && ($town[$t]->size==$j)) {
						$towntaxes[$i]+=2500*($j-1)*(int)($stock[$i][4])/100;
					}
				}
			}
		}
	}
	echo "<tr align=center bgcolor=#C0FFC0>";
	echo "<td>+".$loc[$loctag[6]+7][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			echo "<td>"; if ($towntaxes[$i]!=0) { echo "<input align=center type='text' name='towntaxes-$turn-$i' value=".$towntaxes[$i]." size=8 onChange='recalc($i);'>"; } echo "</td>";
		}
	}
	echo "</tr>\n";
	# gold production
	for ($i=1; $i<=25; $i++) {
		$towngold[$i]=0;
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			for ($t=1; $t<=$nbtowns; $t++) {
				if (($town[$t]->people==$i) && ($town[$t]->sieged==1)) {
					$towngold[$i]+=$town[$t]->localprod[8];
				}
			}
		}
	}
	echo "<tr align=center bgcolor=#C0FFC0>";
	echo "<td>+".$loc[$loctag[6]+18][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			echo "<td>"; if ($data[$i]) { echo "<input align=center type='text' name='goldprod-$turn-$i' value=".$towngold[$i]." size=8 onChange='recalc($i);'>"; } echo "</td>";
		}
	}
	echo "</tr>\n";

	# market selling
	echo "<tr align=center bgcolor=#C0FFC0>";
	echo "<td>+".$loc[$loctag[6]+19][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
		echo "<td>"; if ($data[$i]) { echo "<input align=center type='text' name='marketsell-$turn-$i' value=0 size=8 onChange='recalc($i);'>"; } echo "</td>";
		}
	}
	echo "</tr>\n";
	# received gifts
	echo "<tr align=center bgcolor=#C0FFC0>";
	echo "<td>+".$loc[$loctag[6]+20][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			echo "<td>"; if ($data[$i]) { echo "<input align=center type='text' name='receivedgifts-$turn-$i' value=0 size=8 onChange='recalc($i);'>"; } echo "</td>";
		}
	}
	echo "</tr>\n";
	# other
	echo "<tr align=center bgcolor=#C0FFC0>";
	echo "<td>+".$loc[$loctag[6]+21][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			echo "<td>"; if ($data[$i]) { echo "<input align=center type='text' name='otherrevenues-$turn-$i' value=0 size=8 onChange='recalc($i);'>"; } echo "</td>";
		}
	}
	echo "</tr>\n";
	
	# revenues total
	echo "<tr align=center bgcolor=#C0FFC0>";
	echo "<td><b>=".$loc[$loctag[6]+6][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			echo "<td><b><div id=revenues-$turn-$i>".$stock[$i][5]."</div></td>";
		}
	}
	echo "</tr>\n";
	
	# armies
	echo "<tr align=center bgcolor=#FFC0C0>";
	echo "<td>-".$loc[$loctag[6]+3][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			if ($stock[$i][1]=="") { $stock[$i][1]=0; }
			echo "<td>"; if ($data[$i]) { echo "<input align=center type='text' name='costarmies-$turn-$i' value=".$stock[$i][1]." size=8 onChange='recalc($i);'>"; } echo "</td>";
		}
	}
	echo "</tr>\n";
	# towns
	echo "<tr align=center bgcolor=#FFC0C0>";
	echo "<td>-".$loc[$loctag[6]+4][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			if ($stock[$i][2]=="") { $stock[$i][2]=0; }
			echo "<td>"; if ($data[$i]) { echo "<input align=center type='text' name='costtowns-$turn-$i' value=".$stock[$i][2]." size=8 onChange='recalc($i);'>"; } echo "</td>";
		}
	}
	echo "</tr>\n";
	# heroes
	echo "<tr align=center bgcolor=#FFC0C0>";
	echo "<td>-".$loc[$loctag[6]+5][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			if ($stock[$i][3]=="") { $stock[$i][3]=0; }
			echo "<td>"; if ($data[$i]) { echo "<input align=center type='text' name='costheroes-$turn-$i' value=".$stock[$i][3]." size=8 onChange='recalc($i);'>"; } echo "</td>";
		}
	}
	echo "</tr>\n";

	# market buying
	echo "<tr align=center bgcolor=#FFC0C0>";
	echo "<td>-".$loc[$loctag[6]+23][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
		echo "<td>"; if ($data[$i]) { echo "<input align=center type='text' name='marketbuy-$turn-$i' value=0 size=8 onChange='recalc($i);'>"; } echo "</td>";
		}
	}
	echo "</tr>\n";
	# sent gifts
	echo "<tr align=center bgcolor=#FFC0C0>";
	echo "<td>-".$loc[$loctag[6]+20][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
		echo "<td>"; if ($data[$i]) { echo "<input align=center type='text' name='sentgifts-$turn-$i' value=0 size=8 onChange='recalc($i);'>"; } echo "</td>";
		}
	}
	echo "</tr>\n";
	# other
	echo "<tr align=center bgcolor=#FFC0C0>";
	echo "<td>-".$loc[$loctag[6]+21][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
		echo "<td>"; if ($data[$i]) { echo "<input align=center type='text' name='othercosts-$turn-$i' value=0 size=8 onChange='recalc($i);'>"; } echo "</td>";
		}
	}
	echo "</tr>\n";
	
	# costs total
	echo "<tr align=center bgcolor=#FFC0C0>";
	echo "<td><b>=".$loc[$loctag[6]+22][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			$s=$stock[$i][1]+$stock[$i][2]+$stock[$i][3];
			echo "<td>"; if ($data[$i]) { echo "<b><div id=costs-$turn-$i>".$s."</div>"; } echo "</td>";
		}
	}
	echo "</tr>\n";

	# balance
	echo "<tr align=center bgcolor=#FFC0FF>";
	echo "<td><i>".$loc[$loctag[6]+8][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			$s=$stock[$i][1]+$stock[$i][2]+$stock[$i][3];
			$s=$stock[$i][5]-$s;
			echo "<td>"; if ($data[$i]) { echo"<b><div id=balance-$turn-$i>".$s."</div>"; } echo "</td>";
		}
	}
	echo "</tr>\n";
	}

	# next turn +1
	echo "<tr align=center bgcolor=#C0C0FF>";
	echo "<td colspan=".($nbupdtodate+1).">".$loc[$loctag[5]+11][$lang]." ".($turntodisplaymax+$turn)."</td>";
	echo "</tr>\n";
	# gold in cash
	echo "<tr align=center bgcolor=#C0FFC0>";
	echo "<td>".$loc[$loctag[6]+1][$lang]."</td>";
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			$s=$stock[$i][1]+$stock[$i][2]+$stock[$i][3];
			$s=$stock[$i][5]-$s;
			$goldcash[$i]+=$s;
			echo "<td>"; if ($data[$i]) { echo "<b><div id=gold-$turn-$i>".$goldcash[$i]."</div>"; } echo "</td>";
		}
	}
	echo "</tr>\n";

	echo "</form>";
	echo "</table>";
	echo "<br>\n";
	
	echo "<b><font size=+1>".$loc[$loctag[6]+17][$lang]."</font></b><br>";
	echo "<table align=center border=1 cellspacing=0>";
	for ($j=1; $j<=7; $j++) {
		echo "<tr align=center bgcolor=#C0C0FF>";
		echo "<td width=90>".$loc[$loctag[6]+8+$j][$lang]."</td>";
		for ($i=1; $i<=25; $i++) {
			if ($turnmosttodate[$i]==$turntodisplaymax) {
				echo "<td width=90>".($stock[$i][$j+6]+$stock[$i][$j+13]);
				$s=($stock[$i][$j+6]+$stock[$i][$j+13])*$market[$j+14];
				if ($s!=0) { echo "<br><font size=2><i>".$s; }
				echo "</td>";
			}
		}
		echo "</tr>\n";
	}
	echo "</table>";
?>