<!DOCTYPE HTML>
<html lang="la">
<head>
	<title>Index generalis Civis Romani et Scriptoris Latini – Index nominum</title>
	<meta name="author" content="Bernd Platzdasch"><!-- 23.8.2020 -->
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="css/main.css">
	<link rel="stylesheet" type="text/css" href="css/idx.css">
	<script src="js/store.js" type="text/javascript"></script>
	<style type="text/css">
		#title_add { font-size: smaller; color: gray; font-weight: normal; }
		td.list-style { width: 1.1em; }
		span.label { color: darkgreen; font-weight: normal; }
	</style>
	<?php
		$PHP_SELF = $_SERVER ["PHP_SELF"];
		$bu = (isset($_POST ["bu"])) ? $_POST ["bu"] : "✳";
		//
		$sel = (isset($_POST ["sel"])) ? $_POST ["sel"] : 1;
		$bl = (isset($_POST ["bl"])) ? $_POST ["bl"] : "";
		$anf = (isset($_POST ["anf"])) ? $_POST ["anf"] : "";
		$zur = (isset($_POST ["zur"])) ? $_POST ["zur"] : "";
		$vor = (isset($_POST ["vor"])) ? $_POST ["vor"] : "";
		$end = (isset($_POST ["end"])) ? $_POST ["end"] : "";
		//
		if (($bl == "l◄") AND ($anf != "")) { $sel = 1; }
		if (($bl == "►") AND ($vor != "")) { $sel = $vor; }
		if (($bl == "◄") AND ($zur != "")) { $sel = $zur; }
		if (($bl == "►l") AND ($end != "")) { $sel = $end; }
		//
		require_once $_SERVER['DOCUMENT_ROOT'] . '/db_connection.php';
		//
		$sql_bu = "(Name LIKE '$bu%')";
		if ($bu == "✳") { $sql_bu = "(Name LIKE '%')"; }
		$sql_select = "SELECT * FROM cr_sl_namen WHERE" . $sql_bu . "AND (Name NOT LIKE '%/%') ORDER BY Name";
		$sql_result = $db->query($sql_select);
		$insgesamt = $sql_result->num_rows;
		switch ($insgesamt) {
			case 0:
				$treffer = "ne unum quidem nomen";
				break;
			case 1:
				$treffer = "1 nomen";
				break;
			default:
				$treffer = $insgesamt . " nomina";
		}
	?>
	<script type="text/javascript">
		var optionen = new Array();
		function createDatenseiten() {
			var insgesamt = "<?php echo $insgesamt ?>" ;
			if (insgesamt > 10) var range = 20;
			var rest = insgesamt % range;
			var divi = insgesamt / range;
			if (rest == 0) {
				var ganz = insgesamt / range;
			} else {
				var ganz = vorkomma(divi);
				ganz = ganz - 0; // wieder in Zahl umwandeln
			}
				for (var i=1; i<=ganz; i++) {
				var wert1 = ((i * range) - range) + 1;
				optionen.push(wert1);
				var wert2 = (i * range);
				optionen.push(wert2);
			}
			if (rest != 0) {
				wert1 = (ganz * range) + 1;
				optionen.push(wert1);
				wert2 = (ganz * range) + rest;
				optionen.push(wert2);
			}
			var laenge = optionen.length;
			for (var i=0; i < laenge; i++) {
				var newOpt = document.createElement("option");
				var f1 = optionen[i];
				var f2 = optionen[i+1];
				f1 != f2 ? txt = f1 + "-" + f2 : txt = f1;
				var newOpt = document.createElement("option");
				var newTxt = document.createTextNode(txt);
				newOpt.setAttribute("value", optionen[i]);
				document.form1.sel.appendChild(newOpt);
				newOpt.appendChild(newTxt);
				i++;
			}
			for (var i=0; i < laenge; i++) {
				var newOpt = document.createElement("option");
				var f1 = optionen[i];
				var f2 = optionen[i+1];
				f1 != f2 ? txt = f1 + "-" + f2 : txt = f1;
				var newTxt = document.createTextNode(txt);
				newOpt.setAttribute("value", optionen[i]);
				document.form2.sel.appendChild(newOpt);
				newOpt.appendChild(newTxt);
				i++;
			}
			for (i = 0; i < optionen.length; i++) {
				if(optionen[i] == "<?php echo $sel ?>") {
					break;
				}
			}
			if (i > 0) {
				document.form1.sel[i/2].selected = "true";
				document.form2.sel[i/2].selected = "true";
			}
		}
		function vorkomma(z) {
			var s = z.toString();
			var p = s.indexOf(".");
			return s.substr(0, p);
		}
	</script>
</head>
<body onload="store_mnu();">
	<?php
		include('header.inc.php');
	?>
	<div class="border">
		<div id="main">
		<h1>Index nominum <span id="title_add">auctorum et interpretum</span></h1>
			<table class="abc">
				<tr>
					<td class='abc'>
						<form action="<?php echo $PHP_SELF ?>" method="post">
							<?php
								echo "<input class='abc' type='submit' name='bu' value='✳'>";
								foreach (range('A', 'Y') as $char) {
									echo "<input class='abc' type='submit' name='bu' value=$char>";
								}
								echo "<input class='abc' id='Z' type='submit' name='bu' value='Z'>";
							?>
						</form>
					</td>
				</tr>
			</table>
			<p id="treffer"><?php echo "&#160; " . $bu ?> = <?php echo $treffer ?></p>
			<form name="form1" style="visibility: hidden;" action="<?php echo $PHP_SELF ?>" method="post">
					<input class="inp_nav" type="submit" name="bl" value="l◄">
					<input class="inp_nav" type="submit" name="bl" value="◄">
					<input class="inp_nav" type="submit" name="bl" value="►">
					<input class="inp_nav" type="submit" name="bl" value="►l">
					<input type="hidden" name="bu" value="<?php echo $bu ?>">
					<input type="hidden" name="anf">
					<input type="hidden" name="zur">
					<input type="hidden" name="vor">
					<input type="hidden" name="end">
					<select name="sel" onChange="document.form1.submit()"></select>
			</form>
			<p>&#160;</p>
 			<?php
				function vorname_nachname_arr ($value,$key) {
					// nur bei Arrays
					$pos = strpos($value, ",");
					if ($pos != false) {
						$cognomen = substr($value, 0, $pos);
						$praenomen = substr($value, $pos+2);
						$value = $praenomen . " " . $cognomen;
					}
				}
				function vorname_nachname_str ($value) {
					// nur bei Strings
					$pos = strpos($value, ",");
					if ($pos != false) {
						$cognomen = substr($value, 0, $pos);
						$praenomen = substr($value, $pos+2);
						$value = $praenomen . " " . $cognomen;
					}
					return $value;
				}
				function nachname_str ($value) {
					// nur einzelner String
					$pos = strpos($value, ",");
					if ($pos != false) {
						$value = substr($value, 0, $pos);
					}
					return $value;
				}
				$anz_angezeigte = 0;
				if ($insgesamt > 0) {
					if ($insgesamt <= 20) { $range = 0; }
					if ($insgesamt > 10) { $range = 20; }
					$sel = $sel-1;
					if ($insgesamt > 10) { $sql_limit = "LIMIT $sel, $range"; } else { $sql_limit =""; }
					$sql_select = "$sql_select $sql_limit";
					$sql_result = $db->query($sql_select);
					$anz_angezeigte = $sql_result->num_rows;
					$zaehler = $sel;
					echo "<table id='daten'>";
					while ($row = $sql_result->fetch_object()) {
						$zaehler++;
						// angesetzter Name:
						$erster_name = $row->ersterName;
						$nachname = nachname_str($erster_name);
						echo "<tr>
								<td class='zaehler' >" . $zaehler . ".</td>
								<td colspan='2'><b>" . $erster_name ."</b></td>
							</tr>";
						if ($erster_name != $row->Name ) {
							$alle_namen = $row->Name;
							$alle_namen = explode(" = ", $alle_namen);
							array_walk($alle_namen,"vorname_nachname_arr");
							// erstes Element des Arrays entfernen:
							// $erstes_element = array_shift($alle_namen);
							sort($alle_namen);
							$alle_namen = implode(" = ", $alle_namen);
							// $alle_namen = " = " . $alle_namen;
							echo "<tr>
									<td class='zaehler'>&#160;</td>
									<td  class='list-style' style='vertical-align:top;'>–&#160;</td>
									<td ><span class='label'>Nomina in «Cive Romano/Scriptore Latino»:</span> ". $alle_namen . "</td>
								</tr>";
						}
						// eigentlicher Name:
						$originalname = $row->Originalname;
// 						if (($originalname != "" ) and ($originalname != $erster_name)) {
						if ($row->Originalname != "" ) {
						$originalname = vorname_nachname_str($originalname);
						echo 	"<tr>
									<td class='zaehler'>&#160;</td>
									<td class='list-style'>–&#160;</td>
									<td><span class='label'>Nomen genuinum: </span>" . $originalname . "</td>
								</tr>";
						}
						// externer Link mit Infos über den Namensträger:
						$extLink = trim($row->extLink);
						if ($extLink != "" ) {
							$extLink = "<a target= '_blank' href='" . $extLink . "'>Scitu digna</a>";
							echo "<tr>
									<td class='zaehler'>&#160;</td>
									<td class='list-style'>–&#160;</td>
									<td>" . $extLink . "</td>
								</tr>";
						}
						//
						$erster_name = urlencode($erster_name);
						if ($row->symbolae != 0) {
							echo "<tr>
									<td class='zaehler'>&#160;</td>
									<td class='list-style'>–&#160;</td>
									<td><a target= '_blank' href='symbolae.php?auctor=$erster_name'>Symbolae: $row->symbolae</a></td>
								</tr>";
						}
						if ($row->epist_mis != 0) {
							echo "<tr>
									<td class='zaehler'>&#160;</td>
									<td class='list-style'>–&#160;</td>
									<td><a target= '_blank' href='symbolae.php?auctor=$erster_name&genlit=Epistula'>Epistulae missae: $row->epist_mis</a></td>
								</tr>";
						}
						if ($row->epist_acc != 0) {
							
							echo "<tr>
									<td class='zaehler'>&#160;</td>
									<td class='list-style'>–&#160;</td>
									<td><a target= '_blank' href='symbolae.php?titulus=$nachname&genlit=Epistula'>Epistulae acceptae: $row->epist_acc</a></td>
								</tr>";
						}
						if ($row->interpretes != 0) {
							echo "<tr>
									<td class='zaehler'>&#160;</td>
									<td class='list-style'>–&#160;</td>
									<td><a target= '_blank' href='versiones.php?aucV=$erster_name'>Interpres: $row->interpretes</a></td>
								</tr>";
						}
						if ($row->versiones != 0) {
							echo "<tr>
									<td class='zaehler'>&#160;</td>
									<td class='list-style'>–&#160;</td>
									<td><a target= '_blank' href='versiones.php?aucO=$erster_name'>Versiones: $row->versiones</a></td>
								</tr>";
						}
						if ($row->diplo_acc != 0) {
							$vor_nach = vorname_nachname_str($row->ersterName);
							$vor_nach = urlencode($vor_nach);
							echo "<tr>
									<td class='zaehler'>&#160;</td>
									<td class='list-style'>–&#160;</td>
									<td><a target= '_blank' href='idx_dip.php?$vor_nach'>Diplomata accepta: $row->diplo_acc</a></td>
								</tr>";
						}
					}
					echo "</table>";
				}
				$db->close();
			?>
			<p>&#160;</p>
			<form name="form2" style="visibility: hidden;" action="<?php echo $PHP_SELF ?>" method="post">
					<input class="inp_nav" type="submit" name="bl" value="l◄">
					<input class="inp_nav" type="submit" name="bl" value="◄">
					<input class="inp_nav" type="submit" name="bl" value="►">
					<input class="inp_nav" type="submit" name="bl" value="►l">
					<input type="hidden" name="bu" value="<?php echo $bu ?>">
					<input type="hidden" name="anf">
					<input type="hidden" name="zur">
					<input type="hidden" name="vor">
					<input type="hidden" name="end">
					<select name="sel" onChange="document.form2.submit()"></select>
			</form>
			<?php
				if ($anz_angezeigte > 5) {
					echo "<p id='sursum'><a href='javascript:window.scrollTo(0, 0);'>sursum</a></p>";
				}
			?>
			<p>&#160;</p>
			<script type="text/javascript">
				createDatenseiten();
			</script>
			<script type="text/javascript">
				document.form1.anf.value = 0;
				document.form2.anf.value = 0;
				// nicht über Ende hinaus beim Vorblättern:
				var end = <?php echo $sel ?> + <?php echo $range ?> + 1;
				if (end > optionen[optionen.length-2]) { end = 1; }
				document.form1.vor.value = end ;
				document.form2.vor.value = end ;
				// nicht über Anfang hinaus beim Zurückblättern:
				var zur = <?php echo $sel ?> - <?php echo $range ?> + 1;
				if (zur < 0) { zur = 1; }
				document.form1.zur.value = zur;
				document.form2.zur.value = zur;
				// damit "Alle" ausgeschlossen bleibt:
				document.form1.end.value = optionen[optionen.length-2];
				document.form2.end.value = optionen[optionen.length-2];
				if ( "<?php echo $insgesamt ?>" > 20) {
					document.form1.style.visibility="visible";
				}
				if(("<?php echo $insgesamt ?>" > 20) && ("<?php echo $anz_angezeigte ?>" > 10)) {
					document.form2.style.visibility="visible";
				}
				if(("<?php echo $insgesamt ?>" <= 20) && ("<?php echo $anz_angezeigte ?>" > 10)) {
					document.getElementById("sursum").style.paddingTop = "0";
					document.form2.style.height = "0px";
				}
			</script>
		</div>
	</div>
	<?php include('footer.inc.php');?>
</body>
</html>
