<?php
$GLOBALS['testmode'] = 'on';
/*  super treasure hunt game.  this page submits to itslef and posts 'action' */
include "sthsessioninit.inc";
include "sthcommongamefunctions.inc";


if (isset($_POST['gamenum']))
{
	// someone loaded the STH game from the check-a-ticket screen
	$gamenum = $_POST['gamenum'];	
	
}
if ($gamenum == "")
{
	$gamenum = "none";
}


if (isset($_POST['actionnow']))
{
	$actionnow = $_POST['actionnow'];
}
if ($actionnow == "")
{
	$actionnow = "noSubmit";
}


if ($actionnow == "submitLoadGameForm" && $gamenum != "none")

{	
	// load this saved game
	$load_game_query = mysql_query("SELECT game_state, game_name, date_created, pdf_filename FROM `sth_games` WHERE `sth_game_number` = '$gamenum' LIMIT 1");
		$sth_game_object = mysql_fetch_object($load_game_query);
		$num_rows = mysql_num_rows($load_game_query);  // for testing
		if ($num_rows == 0) {
			print "Database returns 0 rows for this sth_game_number";
			
		}
		else {			
			$_SESSION['sth_game_num'] = $gamenum;
			
			$_SESSION['sth_game_state'] = $sth_game_object->game_state;
			$_SESSION['sth_game_name'] = $sth_game_object->game_name;
			$_SESSION['sth_pdf_filename'] = $sth_game_object->pdf_filename;
			$_SESSION['sth_save_date'] = $sth_game_object->date_created;
			//$_SESSION['sth_ticket_num'] = $sth_game_object->ticket_number;
		}
}






print "<h1>Print Your Super Treasure Hunt Birthday Party Game<br /></h1>";

$game_state = $_SESSION['sth_game_state'];

if ($game_state == "printready") {

	$ticket_num = $_SESSION['sth_ticket_num'];
	$game_num =	$_SESSION['sth_game_num'];
	$game_name = $_SESSION['sth_game_name'];
	$pdf_filename = $_SESSION['sth_pdf_filename'];
	$save_date = $_SESSION['sth_save_date'];
	
	print "<h2><font size = '4'>Your Treasure Hunt Game is Ready to Print</font></h2>";
	//print "<div>game state:".$game_state."</div>";
	//print "<div>game num:".$game_num."</div>";		
	print '<ul>';
	print '<li class="c_med" style="color: darkred;"><strong>Ticket Number: </strong>'.$ticket_num.'</li>';	
	print '<li class="c_med" style="color: darkred;"><strong>Game Name: </strong>'.$game_name.'</li>';	
	print '<li class="c_med" style="color: darkred;"><strong>Created On: </strong>'.$save_date.'</li>';	
	print '</ul>';
	$pdf_link = "/gamepdf/".$pdf_filename;
	print '<p style="font-size: 15pt;"><a href = "'.$pdf_link.'">Click here to open your print-ready Super Treasure Hunt .pdf file</a></p>';
	print '<div class="c_med">To download the game file directly, right-click and select "save link as".</div>';
	print '<p class="c_med2_bold">Have a Great Super Treasure Hunt Party!</p>';
	
	print '<div class="c_med">To load a different saved game for printing, go to the <a href = "/check-ticket">Check a Ticket</a> page.</div>';
}


if ($game_state == "new") {

	//$ticket_num = $_SESSION['sth_ticket_num'];
	//$game_num =	$_SESSION['sth_game_num'];
	//$game_name = $_SESSION['sth_game_name'];
	//$pdf_filename = $_SESSION['sth_pdf_filename'];
	
	
	print "<h2>There is no game created or loaded.</h2>";
	print '<ul>';
	print '<li class="c_med2">Create a new Super Treasure Hunt game: <a class="sth_text_link" href = "/sth/sthcreate">New STH Game</a></li>';
	print '<li class="c_med2">Load a saved Super Treasure Hunt game: <a class="sth_text_link" href = "/check-ticket">Check a Ticket</a></li>';
	print '</ul>';
}

if ($game_state == "gameready") {

	$game_name = $_SESSION['sth_game_name'];
	$save_date = $_SESSION['sth_save_date'];
	print "<h2>Your Super Treasure Hunt game is ready, but not paid for.</h2>";
	print '<ul>';
	//print '<li class="c_med" style="color: darkred;"><strong>Ticket Number: </strong>'.$ticket_num.'</li>';	
	print '<li class="c_med" style="color: darkred;"><strong>Game Name: </strong>'.$game_name.'</li>';	
	print '<li class="c_med" style="color: darkred;"><strong>Created On: </strong>'.$save_date.'</li>';	
	print '</ul>';
	print '<p class="c_med_bold">This game must be paid with a Super Treasure Hunt ticket, to allow printing.</p>';
	print "<p><a href = '/sth/sthcreate'>I already have a ticket to pay for this Super Treasure Hunt game</a></p>";	
	print "<p><a href = '/buy-or-gift'>Visit the online store to purchase a Super Treasure Hunt game ticket</a></p>";	
	
}

if ($game_state != "gameready" && $game_state != "new" && $game_state != "printready" ) {

	$game_name = $_SESSION['sth_game_name'];
	$save_date = $_SESSION['sth_save_date'];
	print "<h2>It appears that you started to create a game, but did not complete it.</h2>";

	print "<p><a href = '/sth/sthcreate'>Finish creating your Super Treasure Hunt game</a></p>";	
	print "<p><a href = '/sth/sthdetails'>Help me understand the Super Treasure Hunt game</a></p>";	
}


?>
