<?php
include ("../config.php");
if (((($_GET['load'] == "help") or ($_GET['load'] == "chat") or ($_GET['load'] == "login") or ($_GET['load'] == "logout")))) {
if (($_GET['load'] == "help") && ($_GET['id'] == "list3")) {
eval (" ?>" . admin_header() . " <?php ");
}
} else {
eval (" ?>" . admin_header() . " <?php ");
echo '<SCRIPT LANGUAGE="JavaScript">var checkflag = "false";function check(field) {if (checkflag == "false") {for (i = 0; i < field.length; i++) {field[i].checked = true;}checkflag = "true";return "Uncheck All"; }else {for (i = 0; i < field.length; i++) {field[i].checked = false; }checkflag = "false";return "Check All"; }}</script>';
}
if ($ipbancheck3 == "0") {if ($numv == "0"){
	if ($warn == $naum) {
	echo "You Do Not Have Access to the Admin Panel";
}
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

$from = (($page * $setting[max_results]) - $setting[max_results]);

if (((($userlevel == "2") or ($userlevel == "3") or ($userlevel == "4") or ($userlevel == "5")))) {
	echo "Sorry $username, but you do not have permission to manage ads. You are only a $level.";
}
echo '<SCRIPT LANGUAGE="JavaScript">var checkflag = "false";function check(field) {if (checkflag == "false") {for (i = 0; i < field.length; i++) {field[i].checked = true;}checkflag = "true";return "Uncheck All"; }else {for (i = 0; i < field.length; i++) {field[i].checked = false; }checkflag = "false";return "Check All"; }}</script>';
//if ($_GET['load'] == "games") {
$admin_page = "yes";
echo "<center><a href='games.php?load=games&view=quickadd'><b>Quick Add</b></a> | <a href='games.php?load=games&view=add'>Add Games</a> | <a href='games.php?load=games&view=add&add=types'>Add Game Types</a> | <a href='games.php?load=games'>Manage Games</a> | <a href='games.php?load=games&view=types'>Manage Game Types</a></center><br><br>";

echo '<script language="javascript">
function awindow(towhere, newwinname, properties) {
window.open(towhere,newwinname,properties);
}
</script>';

$sqlsa = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."permissions WHERE username = '".$_COOKIE[$pre."username"]."' AND games = 'yes'"));

if ($sqlsa == "1") {
if ($_GET['view'] == "search") {

	echo "<title>OneCMS - www.insanevisions.com/onecms > Games > Search</title><form action='games.php?load=games&view=search' method='get'><input type='hidden' name='view' value='search'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td>Search for game</td><td><input type='text' name='search'></td><td><input type='submit' name='Submit' value='Search'></td></tr></table></form>";

	echo "<form action='games.php?load=games&view=manage' name='form1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td><b>Name</b></td><td><b><b>Edit</b></td><td><b>Delete</b></td></tr><center><div align=\"center\">";

	$query="SELECT * FROM ".$pre."games WHERE name LIKE '%" . $_GET['search'] . "%' ORDER BY `id` DESC LIMIT $from, $setting[max_results]";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		$id = $row[id];
		$name = $row[name];

    	echo "<tr><td><a href='".$gamepart1.$row[id].fthrough($name, "").$gamepart2."'>".$name."</a></td><td><input type=\"checkbox\" name=\"id[]\" value=\"$id\"></td><td><input type=\"checkbox\" name=\"delete[]\" value=\"$id\"></td></tr>";
    }

echo "<tr><td><div align='right'><input type='submit' value='Submit'></td><td><input type=button value='Check All' onClick='this.value=check(this.form)'></td><td><a href=\"games.php?load=games&view=add\">Add Games</a></td><td><a href='games.php?load=games&view=add&add=types'>Add Types</a></td></tr></form></table><br><br>";

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."games WHERE name LIKE '%" . $_GET['search'] . "%'"),0);

$total_pages = ceil($total_results / $setting[max_results]);
if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"games.php?load=games&view=search&search=".$_GET['search']."&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"games.php?load=games&view=search&search=".$_GET['search']."&page=$i\">$i</a>&nbsp;";if (($i/25) == (int)($i/25)) {echo "<br>";}
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"games.php?load=games&view=search&search=".$_GET['search']."&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if ($_GET['view'] == "") {
	echo "<title>OneCMS - www.insanevisions.com/onecms > Games > List</title><form action='games.php?load=games&view=search' method='get'><input type='hidden' name='view' value='search'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td>Search for game</td><td><input type='text' name='search'></td><td><input type='submit' name='Submit' value='Search'></td></tr></table></form>";

	echo "<table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><form action='games.php?load=games&view=manage' name='form1' method='post'><tr><td><b>Name</b></td><td><b><b>Edit</b></td><td><b>Delete</b></td></tr><center><div align=\"center\">";

	if ((($userlevel == "3") or ($userlevel == "4") or ($userlevel == "5"))) {

	$query="SELECT * FROM ".$pre."games WHERE username = '".$_COOKIE[$pre."username"]."' ORDER BY `id` DESC LIMIT $from, $setting[max_results]";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		$id = "$row[id]";
		$name2 = "$row[name]";
		$name = stripslashes($name2);

    	echo "<tr><td><a href='".$gamepart1.$row[id].fthrough($row[name], "").$gamepart2."' target='popup'>$name</a></td><td><input type=\"checkbox\" name=\"id[]\" value=\"$id\"></td><td><input type=\"checkbox\" name=\"delete[]\" value=\"$id\"></td></tr>";
    }
			} else {

$query="SELECT * FROM ".$pre."games ORDER BY `id` DESC LIMIT $from, $setting[max_results]";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		$id = "$row[id]";
		$name2 = "$row[name]";
		$name = stripslashes($name2);

    	echo "<tr><td><a href='".$gamepart1.$row[id].fthrough($row[name], "").$gamepart2."' target='popup'>$name</a></td><td><input type=\"checkbox\" name=\"id[]\" value=\"$id\"></td><td><input type=\"checkbox\" name=\"delete[]\" value=\"$id\"></td></tr>";
    }
			}

echo "<tr><td><div align='right'><input type='submit' name='submit' value='Submit'></td><td><input type=button value='Check All' onClick='this.value=check(this.form)'></td><td><a href=\"games.php?load=games&view=add\">Add Games</a></td><td><a href='games.php?load=games&view=add&add=types'>Add Types</a></td></tr></form></table><br><br>";


$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."games"),0);

$total_pages = ceil($total_results / $setting[max_results]);
if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"games.php?load=games&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"games.php?load=games&page=$i\">$i</a>&nbsp;";if (($i/25) == (int)($i/25)) {echo "<br>";}
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"games.php?load=games&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if ($_GET['view'] == "quickadd") {

echo "<form action=\"games.php?load=games&view=quickadd\" method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td>How many games to quick add?</td><td><input type='text' name='search'></td><td><input type='submit' name='addd' value='Submit'></td></tr></table></form>";

if ($_POST['search']) {
echo "<form action='games.php?load=games&view=add2' name='form1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\">";
echo "<input type=\"hidden\" name=\"s\" value='".$_POST['search']."'>";

    for($i = 0; $i < $_POST['search']; $i = $i+1) {
	echo "<tr><td><b><center>Game #".$i."</b></center></td></tr><tr><td>Name</td><td><input type=\"text\" name='name_".$i."'></td></tr>";
	}

	echo "<tr><td><input type=\"submit\" value=\"Add\"></td></tr></form></table>";
}
}

if (($_GET['view'] == "add") && ($_GET['add'] == "")) {
echo '<link rel="stylesheet" href="../a_images/js/themes/system.css" /><script type="text/javascript" src="../a_images/js/src/utils.js"></script><script type="text/javascript" src="../a_images/js/src/calendar.js"></script><script type="text/javascript" src="../a_images/js/src/calendar-setup.js"></script><script type="text/javascript" src="../a_images/js/lang/calendar-en.js"></script>';

echo "<form action=\"games.php?load=games&view=add\" method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td>How many games to add?</td><td><input type='text' name='search'></td><td><input type='submit' name='addd' value='Submit'></td></tr></table></form>";

if ($_POST['search']) {
echo "<form action='games.php?load=games&view=add2' name='form1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\">";
echo "<input type=\"hidden\" name=\"s\" value='".$_POST['search']."'>";

    for($i = 0; $i < $_POST['search']; $i = $i+1) {
	echo "<tr><td><b><center>Game #".$i."</b></center></td></tr><tr><td>Name</td><td><input type=\"text\" name='name_".$i."'></td></tr><tr><td>System</td><td><select name='system_".$i."'><option value=''>-------</option>";

$sql = mysql_query("SELECT * FROM ".$pre."systems ORDER BY `name` ASC");
while ($r = mysql_fetch_array($sql)) {
	echo "<option value='".$r[id]."'>".$r[name]."</option>";
}
echo "</select></td></tr><tr><td>Gallery</td><td><select name='album_".$i."'><option value=''>-------</option>";

$sql = mysql_query("SELECT * FROM ".$pre."albums ORDER BY `name` ASC");
while ($r = mysql_fetch_array($sql)) {
	echo "<option value='".$r[id]."'>".$r[name]."</option>";
}
echo "</select></td><td>New Gallery</td><td><input type='text' name='album2_".$i."' size='10'></td></tr><tr><td>Publisher</td><td><select name='pub_".$i."'><option value=''>-------</option>";
	$query = mysql_query("SELECT * FROM ".$pre."pr ORDER BY `name` ASC") or die(mysql_error());
	while($row = mysql_fetch_array($query)) {
		echo "<option value=\"$row[name]\">$row[name]</option>";
	}
		echo "</select></td><td>New Publisher</td><td><input type='text' name='pub2_".$i."'></td></tr>";

		echo "<tr><td>Developer</td><td><select name='dev_".$i."'><option value=''>-------</option>";
	$query = mysql_query("SELECT * FROM ".$pre."pr ORDER BY `name` ASC") or die(mysql_error());
	while($row = mysql_fetch_array($query)) {
		echo "<option value=\"$row[name]\">$row[name]</option>";
	}
		echo "</select></td><td>New Developer</td><td><input type='text' name='dev2_".$i."'></td></tr>";

		echo "<tr><td>Genre</td><td><select name='genre_".$i."'><option value=''>-------</option>";
	$query = mysql_query("SELECT * FROM ".$pre."game WHERE type = 'genre'") or die(mysql_error());
	while($row = mysql_fetch_array($query)) {
		echo "<option value=\"$row[name]\">$row[name]</option>";
	}
		echo "</select></td></tr>";

		echo "<tr><td>ESRB Rating</td><td><select name='esrb_".$i."'><option value=''>-------</option>";
	$query = mysql_query("SELECT * FROM ".$pre."game WHERE type = 'esrb'") or die(mysql_error());
	while($row = mysql_fetch_array($query)) {
		echo "<option value=\"$row[name]\">$row[name]</option>";
	}
		echo "</select></td></tr>";

	$query = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = 'games'") or die(mysql_error());
	while($row = mysql_fetch_array($query)) {
		$thename = "$row[name]";
		$type = "$row[type]";

		if ($type == "textarea") {
			echo "<tr><td>".$thename."</td><td><textarea name='".$thename.$i."' cols=\"45\" rows=\"12\"></textarea></td></tr>";
		}
		if ($type == "textfield") {
			echo "<tr><td>".$thename."</td><td><input type=\"text\" name='".$thename.$i."'></td></tr>";
		}
	}

	echo "<tr><td>Release Date</td><td><input type='text' name='release_".$i."' id='release_".$i."' size='10'> <img src='../a_images/calendar.gif' border='0' id='date_".$i."' onmouseover=\"this.style.cursor='pointer'\" onmouseout=\"this.style.cursor='default'\"><script type='text/javascript'>var cal = new Zapatec.Calendar.setup({inputField : 'release_".$i."',ifFormat : '%m/%d/%Y',button : 'date_".$i."',showsTime : false});</script></td></tr><tr><td>Description</td><td><textarea name='des_".$i."' cols='30' rows='10'>".$des."</textarea></td></tr>";
	}
	echo "<tr><td><input type=\"submit\" value=\"Add\"></td></tr></form></table>";
}
}

if (($_GET['view'] == "add") && ($_GET['add'] == "types")) {
$sql = mysql_query("SELECT * FROM ".$pre."permissions WHERE username = '".$_COOKIE[$pre."username"]."' AND games = 'yes'");
$num = mysql_num_rows($sql);

if ($num == "0") {
echo "Sorry, but you do not have permission to add games.";
} else {
		echo "<form action=\"games.php?load=games&view=add&add=types\" method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td>How many types to add?</td><td><input type='text' name='search'></td><td><input type='submit' name='addd' value='Submit'></td></tr></table></form>";

		echo "<form action='games.php?load=games&view=add2&add=types' name='form1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\">";

    if ($_POST['search']) {

echo "<input type=\"hidden\" name=\"s\" value='".$_POST['search']."'>";

    for($i = 0; $i < $_POST['search']; $i = $i+1) {
	echo "<tr><td><b><center>Type #".$i."</b></center></td></tr><tr><td>Name</td><td><input type=\"text\" name='name_".$i."'></td></tr><tr><td>Type</td><td><select name='type_".$i."'>";
	echo "<option value='genre'>Genre</option><option value='esrb'>ESRB Rating</option></select></td></tr>";
	}
	echo "<tr><td><input type=\"submit\" value=\"Add\"></td></tr>";
	}
	echo "</form></table>";
}
}
if (($_GET['view'] == "add2") && ($_GET['add'] == "types")) {

   for($i = 0; $i < $_POST['s']; $i = $i+1) {
   if ($_POST["name_$i"]) {
   $r = mysql_query("INSERT INTO ".$pre."game VALUES (null, '".$_POST["name_$i"]."', '".$_POST["type_$i"]."')") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$r."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
   } else {
   echo $i." - no name entered<br>";
   }
   }
if ($r == TRUE) {
echo re_direct("1500", "games.php?load=games");
echo "The type(s) have been created. <a href=\"games.php?load=games&view=types\">Return to Manage Types Home</a>";
}
}

if (($_GET['view'] == "add2") && ($_GET['add'] == "")) {

   for($i = 0; $i < $_POST['s']; $i = $i+1) {
   if ($_POST["name_$i"] == "") {
   echo $i." - no game name entered<br>";
   } else {
   if (($_POST["album_$i"]) && ($_POST["album2_$i"])) {
   echo "You selected an album <b>and</b> added a gallery, please go back and do one of them";
   } else {
   $_POST["des_$i"] = addslashes($_POST["des_$i"]);
   $_POST["name_$i"] = addslashes($_POST["name_$i"]);

   $edit2 = "INSERT INTO ".$pre."games VALUES (null, '".addslashes($_POST["name_$i"])."', '0', '".$_COOKIE['username']."'";

   if (($_POST["pub_$i"] == "") && ($_POST["pub2_$i"] == "")) {
   $edit2 .= ", ''";
   }
   
   if (($_POST["pub_$i"]) && ($_POST["pub2_$i"] == "")) {
   $edit2 .= ", '".$_POST["pub_$i"]."'";
   }

   if (((($_POST["pub_$i"] == "") && ($_POST["pub2_$i"]) or ($_POST["pub_$i"]) && ($_POST["pub2_$i"])))) {
   $pubadd = mysql_query("INSERT INTO ".$pre."pr VALUES (null, '".$_POST["pub2_$i"]."', '', '', '', '', '', '')");

   $edit2 .= ", '".$_POST["pub2_$i"]."'";
   }

   if (($_POST["dev_$i"] == "") && ($_POST["dev2_$i"] == "")) {
   $edit2 .= ", ''";
   }
   
   if (($_POST["dev_$i"]) && ($_POST["dev2_$i"] == "")) {
   $edit2 .= ", '".$_POST["dev_$i"]."'";
   }

   if (((($_POST["dev_$i"] == "") && ($_POST["dev2_$i"]) or ($_POST["dev_$i"]) && ($_POST["dev2_$i"])))) {
   $devadd = mysql_query("INSERT INTO ".$pre."pr VALUES (null, '".$_POST["dev2_$i"]."', '', '', '', '', '', '')");

   $edit2 .= ", '".$_POST["dev2_$i"]."'";
   }

   if ($_POST["release_$i"]) {
   $tdate = explode("/", $_POST["release_$i"]);
   $date = date("U", mktime(0, 0, 0, $tdate[0], $tdate[1], $tdate[2]));
   }

   $edit2 .= ", '".$_POST["genre_$i"]."', '".$date."'";
   
   $edit2 .= ", '".$_POST["esrb_$i"]."', '".$_POST["boxart_$i"]."', '".$_POST["des_$i"]."', '".$_POST["skin_$i"]."', '".$_POST["system_$i"]."'";

   if (($_POST["album_$i"] == "") && ($_POST["album2_$i"] == "")) {
   $edit2 .= ", ''";
   }
   
   if (($_POST["album_$i"]) && ($_POST["album2_$i"] == "")) {
   $edit2 .= ", '".$_POST["album_$i"]."'";
   }

   if (($_POST["album_$i"] == "") && ($_POST["album2_$i"])) {
   $galleryadd = mysql_query("INSERT INTO ".$pre."albums VALUES (null, '".$_POST["album2_$i"]."')");

   $gfetch = @mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."albums WHERE name = '".$_POST["album2_$i"]."'"));

   $edit2 .= ", '".$gfetch[0]."'";
   }
$edit2 .= ")";
$r = mysql_query($edit2) or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$edit2."<br><b>Error:</b> (".mysql_errno().")".mysql_error());

$fetch = @mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."games WHERE name = '".$_POST["name_$i"]."' AND username = '".$_COOKIE['username']."' AND publisher = '".$_POST["pub_$i"]."' AND developer = '".$_POST["dev_$i"]."' AND genre = '".$_POST["genre_$i"]."' ORDER BY `id` DESC"));

$query = mysql_query("SELECT * FROM ".$pre."fielddata WHERE cat = 'games' ORDER BY `id` DESC") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
$name = "$row[name]";

if ($_POST["$name$i"]) {
mysql_query("INSERT INTO ".$pre."fields VALUES ('null', '".$name."', '".addslashes($_POST["$name$i"])."', '".$fetch[0]."', 'games')") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> N/A<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
}

}
}
}
if ($r == TRUE) {
echo re_direct("1500", "games.php?load=games");
echo "The game(s) have been created. <a href=\"games.php?load=games\">Manage Games</a>";
}
}

if ((($_GET['view'] == "manage") && ($_POST['delete'] == "") && ($_GET['edit'] == ""))) {
echo "<form action='games.php?load=games&view=manage&edit=2' name='form1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\">";

    while (list(, $i) = each ($_POST['id'])) {
	$query="SELECT * FROM ".$pre."games WHERE id = '$i'";
	$result=mysql_query($query);
	while($row2 = mysql_fetch_array($result)) {
		$name2 = "$row2[name]";
		$name = htmlspecialchars(stripslashes($name2));
		$dev = "$row2[developer]";
		$pub = "$row2[publisher]";
		$genre = "$row2[genre]";
		$esrb = "$row2[esrb]";
		$box = "$row2[boxart]";
		$des2 = "$row2[des]";
		$des = stripslashes($des2);
		$system = "$row2[system]";
		$album = "$row2[album]";
		$boxart = "$row2[boxart]";

		$t1 = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."albums WHERE id = '".$album."'"));
		$t2 = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."systems WHERE id = '".$system."'"));

	echo "<input type='hidden' name='id[]' value='".$i."'><tr><td><b><center>Game #".$i."</b></center></td></tr><tr><td>Name</td><td><input type=\"text\" name='name_".$i."' value='".$name."'></td></tr><tr><td>System</td><td><select name='system_".$i."'><option value='".$system."' selected>-- ".$t2[0]." --</option><option value=''>-------</option>";

$sql = mysql_query("SELECT * FROM ".$pre."systems ORDER BY `name` ASC");
while ($r = mysql_fetch_array($sql)) {
	echo "<option value='".$r[id]."'>".$r[name]."</option>";
}
echo "</select></td></tr><tr><td>Gallery</td><td><select name='album_".$i."'><option value=''>-------</option><option value='".$album."' selected>-- ".$t1[0]." --</option>";

$sql = mysql_query("SELECT * FROM ".$pre."albums ORDER BY `name` ASC");
while ($r = mysql_fetch_array($sql)) {
	echo "<option value='".$r[id]."'>".$r[name]."</option>";
}
echo "</select></td></tr><tr><td>Publisher</td><td><select name='pub_".$i."'><option value='".$pub."' selected>-- ".$pub." --</option>";
	$query = mysql_query("SELECT * FROM ".$pre."pr ORDER BY `name` ASC") or die(mysql_error());
	while($row = mysql_fetch_array($query)) {
		echo "<option value=\"$row[name]\">$row[name]</option>";
	}
		echo "</select></td></tr>";

		echo "<tr><td>Developer</td><td><select name='dev_".$i."'><option value='".$dev."' selected>-- ".$dev." --</option>";
	$query = mysql_query("SELECT * FROM ".$pre."pr ORDER BY `name` ASC") or die(mysql_error());
	while($row = mysql_fetch_array($query)) {
		echo "<option value=\"$row[name]\">$row[name]</option>";
		}
		echo "</select></td></tr>";

		echo "<tr><td>Genre</td><td><select name='genre_".$i."'><option value='".$genre."' selected>-- ".$genre." --</option>";
	$query = mysql_query("SELECT * FROM ".$pre."game WHERE type = 'genre'") or die(mysql_error());
	while($row = mysql_fetch_array($query)) {
		echo "<option value=\"$row[name]\">$row[name]</option>";
	}
		echo "</select></td></tr>";

		echo "<tr><td>ESRB Rating</td><td><select name='esrb_".$i."'><option value='".$esrb."' selected>-- ".$esrb." --</option>";
	$query = mysql_query("SELECT * FROM ".$pre."game WHERE type = 'esrb'") or die(mysql_error());
	while($row = mysql_fetch_array($query)) {
		echo "<option value=\"$row[name]\">$row[name]</option>";
	}
		echo "</select></td></tr>";
		
		echo "<tr><td>Boxart</td><td><input type='text' name='boxart_".$i."' value='".$boxart."'></td>";
		
	echo "<tr><td>Description</td><td><textarea name='des_".$i."' cols='30' rows='10'>".$des."</textarea></td></tr>";

		$query = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = 'games'") or die(mysql_error());
	while($row = mysql_fetch_array($query)) {
		$thename = "$row[name]";
		$type = "$row[type]";

		$dataa = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE name = '".$thename."' AND id2 = '".$row2[id]."' AND cat = 'games'"));
		$data = stripslashes($dataa[0]);

		if ($type == "textarea") {
			echo "<tr><td>".$thename."</td><td><textarea name='".$thename.$i."' cols=\"45\" rows=\"12\">".$data."</textarea></td></tr>";
		}
		if ($type == "textfield") {
			echo "<tr><td>".$thename."</td><td><input type=\"text\" name='".$thename.$i."' value=\"$data\"></td></tr>";
		}
	}
    $date = date("m/d/Y", $row2[release]);

	echo '<link rel="stylesheet" href="../a_images/js/themes/system.css" /><script type="text/javascript" src="../a_images/js/src/utils.js"></script><script type="text/javascript" src="../a_images/js/src/calendar.js"></script><script type="text/javascript" src="../a_images/js/src/calendar-setup.js"></script><script type="text/javascript" src="../a_images/js/lang/calendar-en.js"></script>';

	echo "<tr><td>Release Date</td><td><input type='text' name='release_".$i."' id='release_".$i."' value='".$date."' size='10'> <img src='../a_images/calendar.gif' border='0' id='date_".$i."' onmouseover=\"this.style.cursor='pointer'\" onmouseout=\"this.style.cursor='default'\"><script type='text/javascript'>var cal = new Zapatec.Calendar.setup({inputField : 'release_".$i."',ifFormat : '%m/%d/%Y',button : 'date_".$i."',showsTime : false});</script></td></tr><tr><td>Select this if your are keeping the same date</td><td><input type='checkbox' name='check_".$i."' value='yep' checked></tr><tr><tr><td>Skin</td><td><select name='skin_".$i."'><option value=''>-------</option>";

    $sql = mysql_query("SELECT * FROM ".$pre."skins ORDER BY `id` DESC");
	while($r = mysql_fetch_array($sql)) {
		if ($r[id] == $row2[skin]) {
		echo "<option value='".$r[id]."' selected>-- ".$r[name]." --</option>";
		} else {
		echo "<option value='".$r[id]."'>".$r[name]."</option>";
		}
	}
	echo "</select></td></tr>";
	}
	}

	echo "<tr><td><input type=\"submit\" value=\"Modify\"></td></tr></form></table>";
	}
}

if (($_GET['view'] == "manage") && ($_GET['edit'] == "2")) {
   while (list(, $i) = each ($_POST['id'])) {
   $_POST["des_$i"] = addslashes($_POST["des_$i"]);
	$edit2 = "UPDATE ".$pre."games SET name = '".addslashes($_POST["name_$i"])."', username = '".$_COOKIE[$pre."username"]."', publisher = '".$_POST["pub_$i"]."', developer = '".$_POST["dev_$i"]."', genre = '".$_POST["genre_$i"]."'";
	
   if ($_POST["check_$i"] == "") {
   $tdate = explode("/", addslashes($_POST["release_$i"]));
   $date = date("U", mktime(0, 0, 0, $tdate[0], $tdate[1], $tdate[2]));
   $edit2 .= ", release = '".$date."'";
   }
   
   $edit2 .= ", esrb = '".$_POST["esrb_$i"]."', boxart = '".$_POST["boxart_$i"]."', des = '".$_POST["des_$i"]."', skin = '".$_POST["skin_$i"]."', album = '".$_POST["album_$i"]."', system = '".$_POST["system_$i"]."' WHERE id = '".$i."'";

   $r = mysql_query($edit2) or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$edit2."<br><b>Error:</b> (".mysql_errno().")".mysql_error());

$querya2 = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = 'games' ORDER BY `id` DESC") or die(mysql_error());
while($ra = mysql_fetch_array($querya2)) {
$name = "$ra[name]";

$firstcount = @mysql_num_rows(mysql_query("SELECT * FROM ".$pre."fielddata WHERE id2 = '".$i."' AND cat = 'games' AND name = '".$name."'"));
if ($firstcount == "0") {
mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, '".$name."', '".addslashes($_POST["$name$i"])."', '".$i."', 'games')");
} else {
mysql_query("UPDATE ".$pre."fielddata SET data = '".addslashes($_POST["$name$i"])."' WHERE id2 = '".$i."' AND cat = 'games' AND name = '".$name."'");
}
}
}
if ($r == TRUE) {
echo re_direct("1500", "games.php?load=games");
echo "The game(s) have been updated. <a href=\"games.php?load=games\">Return to Manage Games Home</a>";
}
}

if ($_GET['view'] == "types") {

			echo "<title>{sitename}OneCMS - www.insanevisions.com/onecms > Game Type Manager</title>";

			echo "<table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><form action='games.php?load=games&view=types1' name='form1' method='post'><tr><td><b>Name</b></td><td><b>Type</b></td><td><b><b>Edit</b></td><td><b>Delete</b></td></tr><center><div align=\"center\">";

	if ((($userlevel == "3") or ($userlevel == "4") or ($userlevel == "5"))) {

	echo "Hey, you can't edit types!";

	} else {

$query="SELECT * FROM ".$pre."game ORDER BY `id` DESC LIMIT $from, $setting[max_results]";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		$id = "$row[id]";
		$name2 = "$row[name]";
		$type = "$row[type]";
		$name = stripslashes($name2);
    	echo "<tr><td>$name</td><td>$type</td><td><input type=\"checkbox\" name=\"id[]\" value=\"$id\"></td><td><input type=\"checkbox\" name=\"delete[]\" value=\"$id\"></td></tr>";
    }
			}

echo "<tr><td><div align='right'><input type='submit' name='submit' value='Submit'></td><td><input type=button value='Check All' onClick='this.value=check(this.form)'></td><td><a href=\"games.php?load=games&view=add\">Add Games</a></td><td><a href='games.php?load=games&view=add&add=types'>Add Types</a></td></tr></form></table><br><br>";


$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."game"),0);

$total_pages = ceil($total_results / $setting[max_results]);
if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"games.php?load=games&view=types&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"games.php?load=games&view=types&page=$i\">$i</a>&nbsp;";if (($i/25) == (int)($i/25)) {echo "<br>";}
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"games.php?load=games&view=types&page=$next\">Next>></a>";
}
echo "</center>";
}
}

	if ((($_GET['view'] == "types1") && ($_POST['delete'] == "") && ($_GET['edit'] == ""))) {

	echo "<form action='games.php?load=games&view=types1&edit=2' name='form1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\">";

    while (list(, $i) = each ($_POST['id'])) {
	$query="SELECT * FROM ".$pre."game WHERE id = '$i'";
	$result=mysql_query($query);
	while($row2 = mysql_fetch_array($result)) {
		$name = "$row2[name]";
		$type = "$row2[type]";

		echo "<input type='hidden' name='id[]' value='".$i."'><tr><td><center><b>Type #".$i."</b></center></td></tr><tr><td>Name</td><td><input type='text' name='name_".$i."' value=\"$name\"></td></tr><tr><td>Type</td><td><select name='type_".$i."'><option value=\"$type\" selected>-- $type --</option><option value='esrb'>ESRB Rating</option><option value='genre'>Genre</option></select></td></tr>";
	}
	}
		echo "<tr><td><input type=\"submit\" value=\"Modify\"></td></tr></form></table>";
	}

	if (($_GET['view'] == "types1") && ($_GET['edit'] == "2")) {

   while (list(, $i) = each ($_POST['id'])) {
   $r = mysql_query("UPDATE ".$pre."game SET name = '".$_POST["name_$i"]."', type = '".$_POST["type_$i"]."' WHERE id = '".$i."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$r."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
   }
if ($r == TRUE) {
echo re_direct("1500", "games.php?load=games");
echo "The type(s) have been updated. <a href=\"games.php?load=games&view=types\">Return to Manage Types Home</a>";
}
}

if (($_GET['view'] == "types1") && ($_POST['id'] == "")) {
echo '<SCRIPT LANGUAGE="JavaScript">
var agree=confirm("Confirm Deletion?");
if (agree)
document.write("");
else
history.go(-1);
// End -->
</SCRIPT>';

while (list(, $val) = each ($_POST['delete'])) {
$delete = mysql_query("DELETE FROM ".$pre."game WHERE id = '$val'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$delete."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
if ($delete == TRUE) {
echo re_direct("1500", "games.php?load=games");
echo "The game type(s) have been deleted. <a href=\"games.php?load=games&view=types\">Return to Manage Types Home</a>";
}
}

if (($_GET['view'] == "manage") && ($_POST['id'] == "")) {
echo '<SCRIPT LANGUAGE="JavaScript">
var agree=confirm("Confirm Deletion?");
if (agree)
document.write("");
else
history.go(-1);
// End -->
</SCRIPT>';

while (list(, $val) = each ($_POST['delete'])) {
$delete = mysql_query("DELETE FROM ".$pre."games WHERE id = '$val'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$delete."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
$delete2 = mysql_query("DELETE FROM ".$pre."fielddata WHERE id = '$val' AND cat = 'games'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$delete2."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
if (($delete == TRUE) && ($delete2 == TRUE)) {
echo re_direct("1500", "games.php?load=games");
echo "The game(s) have been deleted. <a href=\"games.php?load=games\">Return to Manage Games Home</a>";
}
}
}
//}

} else {
	echo "Sorry, but you do not have permission to this page.";
}
//}
if (($_GET['load'] == "help") or ($_GET['load'] == "chat")) {
if (($_GET['load'] == "help") && ($_GET['id'] == "list3")) {
eval (" ?>" . admin_footer() . " <?php ");
}
} else {
eval (" ?>" . admin_footer() . " <?php ");
}
?>