<? 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>
<link href="<? echo IncL('style_global.css'); ?>" rel="stylesheet" type="text/css">
</head>
<script language="JavaScript">
function showenemy() {
	for (x=1; x<=190; x++) {
		if (document.getElementById('showenemy-'+x)) {
			document.getElementById('showenemy-'+x).style.display='block';
		}
	}
}
function showallied() {
	for (x=1; x<=190; x++) {
		if (document.getElementById('showallied-'+x)) {
			document.getElementById('showallied-'+x).style.display='block';
		}
	}
}
function showlostsecondary() {
	for (x=1; x<=190; x++) {
		if (document.getElementById('showlostsecondary-'+x)) {
			document.getElementById('showlostsecondary-'+x).style.display='block';
		}
	}
}
function showall() {
	showenemy();
	showallied();
	showlostsecondary();
}
function showsummary() {
	document.getElementById('showsummary').style.display='block';
}
</script>
<body>
<center>
<font size=+2><b>Middle-Earth Tool for Overcoming Opposition</b></font><br>
</center>
<hr>
<?
	if (!$goingtologin) { include Inc("testlogin.php");	}
?>
<? $where="showmagic"; include Inc("checkgameaccess.inc.php"); ?>
<? $game_checked=$game; include Inc("loaddata.inc.php"); ?>
<?
	$fn=Inc("artefacts.csv");
	$fp = fopen ($fn, "r");
	$content = fread($fp, filesize($fn));
	fclose($fp);
	Decrunch($content);
	$artefactdata=$cell;
	$artlin=$lin;
	$fn=Inc("spells.csv");
	$fp = fopen ($fn, "r");
	$content = fread($fp, filesize($fn));
	fclose($fp);
	Decrunch($content);
	$spell=$cell;
	$spelin=$lin;
	
	if ($show=="") { $show="artefacts"; }
	echo "<center>";
	echo "<a href='?show=artefacts&game=$game_checked' style='color:black'>";
	if ($show=="artefacts") { echo "<b>"; }
	echo $loc[$loctag[9]+34][$lang]."</b></a> - ";
	echo "<a href='?show=spells&game=$game_checked' style='color:black'>";
	if ($show=="spells") { echo "<b>"; }
	echo $loc[$loctag[9]+57][$lang]."</b></a>";
	echo "<br><br>";

	# sides
	for ($i=21; $i<=25; $i++) {
		if (($NationSide[$i-20]!=0) && ($NationSideTurn[$i-20]<=$turntodisplaymax)) {
			$Siding[$i]=$NationSide[$i-20];
		}
	}

	if ($show!="spells") {
		
		echo "<font size=-1><a onclick='showenemy();' style='color:black;text-decoration:underline;cursor:pointer'>".$loc[$loctag[5]+19][$lang]."</a>";
		echo " - <a onclick='showallied();' style='color:black;text-decoration:underline;cursor:pointer'>".$loc[$loctag[5]+20][$lang]."</a>";
		echo " - <a onclick='showlostsecondary();' style='color:black;text-decoration:underline;cursor:pointer'>".$loc[$loctag[5]+21][$lang]."</a>";
		echo " - <a onclick='showall();' style='color:black;text-decoration:underline;cursor:pointer'>".$loc[$loctag[5]+22][$lang]."</a><br>";
		echo "<a onclick='showsummary();' style='color:black;text-decoration:underline;cursor:pointer'>".$loc[$loctag[5]+24][$lang]."</a>";

		for ($i=1; $i<=$nbheroes; $i++) {
			if ($hero[$i]->artefactlist!="") {
				$spe=explode("#", $hero[$i]->artefactlist);
				for ($a=0; $a<=count($spe)-1; $a++) {
					$artefact_hero[$spe[$a]]=$i;
					$artefact_hero_turninfo[$spe[$a]]=$hero[$i]->turninfo;
				}
			}
		}
		for ($i=1; $i<=$nbartefact; $i++) {
			$num=$artefact[$i]->num;
			$artefact_hex[$num]=$artefact[$i]->hex;
			$artefact_hex_turninfo[$num]=$artefact[$i]->turninfo;
		}

		for ($i=1; $i<=190; $i++) {
			$pos=strpos($artefactname[$i][$lang], "+");
			$pow=0;
			if ($pos!=0) {
				$pow=substr($artefactname[$i][$lang], $pos+1, 2);
				$type=substr($artefactname[$i][$lang], $pos-2, 2);
				if (($type=="Ma") && ($pow<20)) { $pow=0; }
			} else {
				$pos1=strpos($artefactname[$i][$lang], "(")+1;
				$pos2=strpos($artefactname[$i][$lang], ")");
				$pow=(substr($artefactname[$i][$lang], $pos1, $pos2-$pos1)*1);
				if ($pow<2000) { $pow=0; }
			}
			if ($pow!=0) {
				$artefact_level[$i]=1;
			} else {
				$artefact_level[$i]=2;
			}
		}
		$artefact_level[41]=1;
		for ($i=1; $i<=190; $i++) {
			if ($artefact_hero[$i]!=0) {
				if ($Siding[$hero[$artefact_hero[$i]]->people]==$game_side[$game]) {
					$artefact_summ[1][$artefact_level[$i]]++;
				} else {
					$artefact_summ[2][$artefact_level[$i]]++;
				}
			} else {
				$artefact_summ[3][$artefact_level[$i]]++;
			}
		}

		echo "<div id='showsummary' style='display:none'>\n";
		echo "<br>";
		echo "<table border=1><tr><td></td><td width=100 align=center><font size=-1><b>".$loc[$loctag[5]+2][$lang]."</td><td width=100 align=center><font size=-1><b>".$loc[$loctag[5]+3][$lang]."</td><td width=100 align=center><font size=-1><b>".$loc[$loctag[5]+18][$lang]."</td></tr>";
		for ($i=1; $i<=2; $i++) {
			echo "<tr><td><font size=-1><b>".$loc[$loctag[5]+24+$i][$lang]."</td>";
			for ($j=1; $j<=3; $j++) {
				$diff=$artefact_summ[$j][$i]-$artefact_summ_start[$j][$i];
				if ($diff>0) { $diff="+".$diff; }
				echo "<td align=center><font size=-1>".$artefact_summ[$j][$i]." ($diff)</td>";
			}
			echo "</tr>";
		}
		echo "</table>";
		echo "</div>";
		echo "<br><br>";

		$w1=40; $w2=200; $w3=90; $w3b=170; $w4=40; $w5=30; $w6=70;
		echo "<table align=center border=1 cellspacing=0>";
		echo "<tr align=center>";
		echo "<td width=$w1><font size=-1><b>".$loc[$loctag[5]+17][$lang]."</td>";
		echo "<td width=$w2><font size=-1><b>".$loc[$loctag[9]+34][$lang]."</td>";
		echo "<td width=$w3><font size=-1><b>".$loc[$loctag[5]+5][$lang]."</td>";
		echo "<td width=$w3b><font size=-1><b>".$loc[$loctag[4]+2][$lang]."</td>";
		echo "<td width=$w4><font size=-1><b>".$loc[$loctag[4]+15][$lang]."</td>";
		echo "<td width=$w5><font size=-1><b>".$loc[$loctag[5]+11][$lang]."</td>";
		echo "<td width=$w6></td></tr>";
		echo "</table>";
		for ($i=1; $i<=190; $i++) {
			
			# type of artefact
			if ($artefact_hero[$i]!=0) {
				if ($Siding[$hero[$artefact_hero[$i]]->people]!=$game_side[$game]) {
					echo "<div id='showenemy-$i' style='display:none'>\n";
				} else {
					echo "<div id='showallied-$i' style='display:none'>\n";
				}
			} else {
				if ($artefact_level[$i]==1) {
					echo "<div id='showlostmain-$i' style='display:block'>\n";
				} else {
					echo "<div id='showlostsecondary-$i' style='display:none'>\n";
				}
			}
			
			# mini-form
			echo "<table align=center border=1 cellspacing=0>";
			echo "<form action='showmagic_enter.inc.php' method='post'>";
			echo "<input type='hidden' name='num' value='$i'>";
			echo "<input type='hidden' name='game_checked' value='$game'>";
			echo "<input type='hidden' name='action' value='update'>";
			
			# display type of artefact
			echo "<tr>";
			echo "<td width=$w1><font size=-1>";
			if ($artefact_hero[$i]!=0) {
				if ($Siding[$hero[$artefact_hero[$i]]->people]!=$game_side[$game]) {
					echo "<font color=red>".$loc[$loctag[5]+3][$lang];
				} else {
					echo "<font color=green>".$loc[$loctag[5]+2][$lang];
				}
			} else {
				if ($artefact_level[$i]!=1) { echo "<i>"; }
				echo "<font color=grey>".$loc[$loctag[5]+18][$lang];
			}
			
			# name
			echo "</td><td width=$w2><font size=-1>";
			$pos=strpos($artefactname[$i][$lang], "[");
			if ($pos!=0) {	echo substr($artefactname[$i][$lang], 0, $pos); } else { echo $artefactname[$i][$lang]; }
			
			# hero name if any
			echo "</td><td width=$w3><font size=-1>";
			if ($artefact_hero[$i]!=0) {
				$display=$hero[$artefact_hero[$i]]->name;
				if ($Siding[$hero[$artefact_hero[$i]]->people]!=$game_side[$game]) {
					echo "<input type='text' name='name' size=15 value='$display' class='petit'>\n";
				} else {
					echo $display;
				}
			} else {
				echo "<input type='text' name='name' size=15 class='petit'>";
			}
			
			# hero people if any
			echo "</td><td width=$w3b><font size=-1>";
			$display=$hero[$artefact_hero[$i]]->people;
			if ($Siding[$display]!=$game_side[$game]) {
				echo "<select name='people' class='petit'>";
				echo "<option>";
				for ($p=1 ;$p<=28; $p++) {
					echo "<option value='$p'"; 
					if ($Siding[$display]==$game_side[$game]) { echo " disabled"; } 
					if ($p==$display) { echo " selected"; } 
					echo ">".$loc[$loctag[1]+$p][$lang];
				}
				echo "</select>";
			} else {
				echo $loc[$loctag[1]+$display][$lang];
			}
			
			# hex
			echo "</td><td width=$w4><font size=-1>";
			if ($artefact_hex[$i]!=0) {
				$display=$artefact_hex[$i];
				echo "<input type='text' name='hex' size=5 value='$display' class='petit'>";
			} else {
				if ($artefact_hero[$i]!=0) {
					$display=$hero[$artefact_hero[$i]]->hex;
					if ($Siding[$hero[$artefact_hero[$i]]->people]!=$game_side[$game]) {
						echo "<input type='text' name='hex' size=5 value='$display' class='petit'>";
					} else {
						echo $display;
					}
				} else {
					echo "<input type='text' name='hex' size=5 class='petit'>";
				}
			}
			
			# turn of info
			echo "</td><td width=$w5><font size=-1>";
			if ($artefact_hex[$i]!=0) {
				$display=$artefact_hex_turninfo[$i];
				echo "<input type='text' name='turn' size=3 value='$display' class='petit'>";
			} else {
				if ($artefact_hero[$i]!=0) {
					$display=$hero[$artefact_hero[$i]]->turninfo;
					if ($Siding[$hero[$artefact_hero[$i]]->people]!=$game_side[$game]) {
						echo "<input type='text' name='turn' size=3 value='$display' class='petit'>";
					} else {
						echo $display;
					}
				} else {
					echo "<input type='text' name='turn' size=3 class='petit'>";
				}
			}
			echo "</td><td width=$w6>";
			echo "<input type='submit' class='petit' value='".$loc[$loctag[5]+23][$lang]."'>";
			echo "</td></form></tr></table>";
			echo "</div>\n";
		}
	}
	if ($show=="spells") {
		echo "<table align=center border=0 cellspacing=0><tr><td width=50%>";
		echo "<table border=1 cellspacing=0>";
		for ($i=1; $i<=$nbheroes; $i++) {
	#		if (($hero[$i]->artefactlist!="") || ($hero[$i]->spelllist!="")) {
			if (($hero[$i]->spelllist!="") && ($hero[$i]->turninfo>=$turntodisplay-1)) {
				echo "<tr>";
				echo "<td><font size=-1>".$hero[$i]->name;
				echo "<br>".$loc[$loctag[2]+24][$lang]." ".$hero[$i]->turninfo;
				echo "<br>(".$loc[$loctag[1]+$hero[$i]->people][$lang].")</td>";
	/*			echo "<td>";
				if ($hero[$i]->artefactlist!="") {
					$art=explode("#", $hero[$i]->artefactlist);
					for ($a=0; $a<=count($art); $a++) {
						$ty=strpos($artefact[$art[$a]][$lang], "[");
						echo substr($artefact[$art[$a]][$lang],0,$ty)."<br>";
					}
				}
				echo "</td>";*/
				echo "<td><font size=-1>";
				if ($hero[$i]->spelllist!="") {
					$spe=explode("#", $hero[$i]->spelllist);
					for ($a=0; $a<=count($spe); $a++) {
						$spe2=explode(":", $spe[$a]);
						for ($j=1; $j<=$spelin; $j++) {
							if ($spell[$j][1]==$spe2[0]) {
								echo "#".$spell[$j][1]." ".$spell[$j][$lang+1]." (".$spe2[1].")<br>";
							}
						}
					}
				}
				echo "</td>";
				echo "</tr>";
			}
		}
		echo "</table>";
		echo "</td><td>";
		echo "<table border=1 cellspacing=0>";
		for ($s=1; $s<=$spelin; $s++) {
			$found=false;
			for ($i=1; $i<=$nbheroes; $i++) {
			if (($hero[$i]->spelllist!="") && ($hero[$i]->turninfo>=$turntodisplay-1)) {
					$spe=explode("#", $hero[$i]->spelllist);
					for ($a=0; $a<=count($spe); $a++) {
						$spe2=explode(":", $spe[$a]);
						if ($spell[$s][1]==$spe2[0]) {
							if (!$found) {
								echo "<tr>";
								echo "<td width=50%><font size=-1>";
								echo "#".$spell[$s][1]." ".$spell[$s][$lang+1]."<br>";
								echo "</td>";
								echo "<td><font size=-1>";
								$found=true;
							}
							echo $hero[$i]->name." (".$loc[$loctag[1]+$hero[$i]->people][$lang].") : ".$spe2[1]."<br>";
						}
					}
				}
			}
			if ($found) {
				echo "</td></tr>";
			}
		}
		echo "</table>";
		echo "</td></tr></table>";
	}
?>