<?php 
 
include "sthcommongamefunctions.inc";

function print_error($newerrormsg)
{
	array_push($_SESSION['printerrorlist'], $newerrormsg);
	$_SESSION['printerrorcount']++;
	print "<div>New Print Error Msg: $newerrormsg</div>";
	return 1;
}

//<cfparam name = "form.order_number" default = "none">
$_SESSION['printerrorlist']  = array();
$_SESSION['printerrorcount'] = 0;

$sth_game_number = $_SESSION['sth_game_number'];


$load_game_query = mysql_query("SELECT * FROM `sth_games` WHERE `sth_game_number` = '$sth_game_number' 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_error("Database returns 0 rows for this sth_game_number");
}
else {
	$glyph_list = explode("|", $sth_game_object->glyph_list);
	$glype_level = explode("|", $sth_game_object->glyph_level);
	$glyph_location = explode("|", $sth_game_object->glyph_location);
	$clue_list = explode("|", $sth_game_object->clue_list);
	$clue_location = explode("|", $sth_game_object->clue_location);
	$clue_level = explode("|", $sth_game_object->clue_level);
	$key_location = explode("|", $sth_game_object->key_location);
	$glyph_jpg_list = explode("|", $sth_game_object->glyph_jpg_list);
	$sth_game_state = $sth_game_object->game_state;
}


$GlyphNumbersForThisClue = array();
/*
 * DB Table
 * game_state,
	glyph_list,
	glyph_level,
	glyph_location,
	clue_list,
	clue_level,
	clue_location,
	glyph_jpg_list,
	key_location,
	sth_game_number,
	ticket_number,
	ticket_id,
	date_created
 * 
 */

?>

	
<table width="100%" border="0">
  <tr>
    <td width="171" valign="top" ><font color="#660000" size="6"><strong><img src="content/sth/gameart/treasure_chest5.gif" width="171" height="141" align="left"></strong></font> 
    </td>
    <td valign="top">
<div align="left"> <font color="#660000" size="6"><strong>Super Treasure Hunt 
        Instructions</strong></font> 
        <div align="left"> 
          <table width="100%" border="0">
            
            <tr> 
              <td valign="top"><strong>1.</strong></td>
              <td valign="top"><strong>Print this page.</strong> You will need 
                paper and ink for 24 printed pages. Printing on colored paper 
                or cardstock is an optional bonus touch.</td>
            </tr>
            <tr> 
              <td valign="top"><strong>2.</strong></td>
              <td valign="top"><strong>Each printed pages contains all the Clues 
                to hide in one location.</strong> Just cut out the clues from 
                one page at a time and place them together into the specified 
                location. If you are in a hurry, you can hide the entire page (or just cut off the top).
				 Give the children the option to use scissors during the hunt.</td>
            </tr>
			<tr>
			<td valign="top"><strong>3.</strong></td>
              <td valign="top"><strong>
				For parties with more than 7 children</strong>, it is best to not cut apart the the clues for each location.
				Instead, hide the entire page in the specified location.  Split the group into teams (4-6 kids per team) and hide a 
				copy of the clues for each team.  So, with 2 teams, just print the Treasure Hunt out twice.
			</td></tr>			
         	<tr> 
              <td valign="top"><strong>4.</strong></td>
              <td valign="top"><strong>The last printed pages </strong>have the 
                first Clue the kids get to start the game (written in plain english) 
                and the final Congratulations Message which goes into the Treasure Chest.</td>
            </tr>
		 	<tr> 
              <td valign="top"><strong>5.</strong></td>
              <td valign="top"><strong>Lock the Treasure Chest</strong> with the 
                combination lock after you put in the treasure and also the Congratulations 
                Message.</td>
            </tr>
				<tr> 
              <td valign="top"><strong>6.</strong></td>
              <td valign="top">You can <strong>print from any internet computer</strong> 
                for the next 30 days You just need your order number, which was 
                emailed to you. </td>
            </tr>
			
		  </table>
          
        </div>
      </div></td>
</tr>
</table>
<p></p>
<div align="center"><font size="5"><em>Super Treasure Hunt Cheat Sheet - For your 
  Eyes Only</em></font></div>
<table width="100%">

	<?php 
	// <cfloop from="2" to="13" index="i">
	// <cfset Ctext = ListGetAt(Session.clue_list,i)>
	for ($i = 1; $i < 13; $i++)
	{
		$Ctext = $clue_list[$i];
		$result = GetGlyphNumbersFromClue($Ctext);
		print"<tr><td width='40%'><div align='right'>";
		foreach($GlyphNumbersForThisClue as $GnumX)
		{
			$GlyphArtSuffix =$glyph_jpg_list[$GnumX-1]; // possible GnumX is -1
			$art_source = "content/sth/gameart/SmallGlyphs/glyph".$GlyphArtSuffix.".jpg";
			print"<img src='$art_source'>";
			// print"-$art_source-";
		}
		print"</div></td>";
		print"<td valign='middle' width='60%'><div align='left'><font size='3'>";
		print"$Ctext";
		print"</font></div></td> </tr>";
	}
	?>
</table>
		
<h4 style="page-break-after: always"></h4>




<!--- fill up the locations --->
<?php 
for ($Lnum = 1; $Lnum <=10; $Lnum++)
{
	print"<h4 style='page-break-after: always'>";
	print"<table width='100%' border='0'>";	
	print"<tr><td width='171' valign='top' ><font color='#660000' size='6'><strong>";
	print"<img src='content/sth/gameart/treasure_chest5.gif' width='171' height='141' align='left'>";
	print"</strong></font></td>";	
	
	print"<td valign='top'><div align='left'>";
	print" <font size='6'>Location #$Lnum <strong><font size='3'>- Represented by this Clue:</font></strong></font>";
	$theclue = $clue_list[$Lnum-1];  // -1 was added for php port
	print"</div><font size='4'> <strong>$theclue</strong></font>"; 
	print"<p><strong>Cut out all of the Clues and Glyphs on this page (below) and hide them in 
            this location. </strong></p>";
  	print "</td></tr>";
	print "</table>";	
		
 	
    // <!--- put the clues into this location --->
    for ($i = 1; $i <= 14; $i++)
    {
    	$Clocation = $clue_location[$i-1];
    	if ($Clocation == $Lnum)
    	{
    		$Ctext = $clue_list[$i-1];
    		$Clevel = $clue_level[$i-1];
    		if ($Clevel == 0)
    		{
    			// clue in plain english
    			print"<table width='99%'><tr><td>";  //outside table
    			print"<table border='5' align='left'><tr><td>";
    			print"<font size='5'>$Ctext</font>";
    			print"</td></tr></table>";
    			print"</td></tr></table>";
    			
    		}
    		else
    		{
    			// <!--- clue must be translated to glyphs --->
    			print"<table width='99%'><tr><td>";  //outside table
    			print"<table border='5' align='left'><tr><td>";
    			$result = GetGlyphNumbersFromClue($Ctext);
    			
    			//  <!--- fills in Session.GlyphNumbersForThisClue --->
    			foreach ($GlyphNumbersForThisClue as $GnumX)
    			{
    				$GlyphArtSuffix = $glyph_jpg_list[$GnumX-1];
    				$glyphlink = "content/sth/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    				print "<img src='$glyphlink'>";
    				//print "<div>$glyphlink</div>";    				
    			}
    			    			
    			print"</td></tr></table>";
    			print"</td></tr></table>";
    			
    			
    		}
    	}    	
    }
	

	
    // <!--- Now put the glyphs into this location --->
	print "<table border='0' width='99%'><tr>";
    
	$column_count = 0;
	$size_of_list = count($glyph_list);
	for($i = 1; $i <= $size_of_list; $i++)
	{
		//print"*";
		$this_glyph_location = $glyph_location[$i-1];
		if ($this_glyph_location == $Lnum)
		{
			//print"+";
			if ($column_count == 3)
			{
				print "</tr><tr>";
				$column_count = 0;
			}
			$this_glyph_word = $glyph_list[$i-1];
			$this_glyph_level = $glyph_level[$i-1];
       		$GlyphArtSuffix = $glyph_jpg_list[$i-1];
       		
       		print "<td width = '33%'>";
          
		  	print "<table border='5' align='left'><tr><td>";
		  	if ( strlen($this_glyph_word) > 1 || (strlen($this_glyph_word) == 1 && ( 
		  		($this_glyph_word >= 'a' && $this_glyph_word <= 'z' ) ||
		  		($this_glyph_word >= 'A' && $this_glyph_word <= 'Z' ) ||
		  		($this_glyph_word >= '0' && $this_glyph_word <= '9' ) ) ) )
		  	{
		  		$glyphlink = "content/sth/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    			print "<div><img src='$glyphlink'><span class='glyphText'> = $this_glyph_word</span></div>";
		  		
		  	}
		  	else 
		  	{ // punctuation
		  		if ($this_glyph_word == "." || $this_glyph_word == ",")
		  		{
		  			$glyphlink = "content/sth/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    				print "<div><img src='$glyphlink'><span class='glyphText'><strong><font size = '6'> = $this_glyph_word</font></strong></span></div>";
		  		}
		  		else
		  		{   // some other punctuation
		  			$glyphlink = "content/sth/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    				print "<div><img src='$glyphlink'><span class='glyphText'><strong><font size = '4'> = $this_glyph_word</font></strong></span></div>";
		  		
		  		}
		  	}
			print "</td></tr></table>";
		  		
			print "</td>";
			$column_count++;
       		
       		
		}
	}
    
       	
     
	if ($column_count < 3)
	{
		for ($iii = $column_count; $iii <=3; $iii++)
		{
			if ($iii < 3)
			{
				print "<td></td>";
			}
		}
	}
		
	print "</tr></table>";
	
	
	// **** Place the Keys
	print "<table width='99%' border='0'><tr>";
	$column_count = 0;
	for ($i = 1; $i<=9; $i++)
	{
		$this_key_location = $key_location[$i-1];
		if ($this_key_location == $Lnum)
		{
			print "<td width='33%'>";
			print"<table align = 'left' border='5'><tr><td> ";
			$imglink = "content/sth/gameart/Keys/key".$i.".jpg";
    		print "<div align = 'center'><img src='$imglink'></div>";
    		print "</td></tr></table>";
    		print "</td>";
    		$column_count++;			
		}
	}
	
	if ($column_count < 3)
	{
		for ($iii = $column_count; $iii <=3; $iii++)
		{
			if ($iii < 3)
			{
				print "<td></td>";
			}
		}
	}
	
	print "</tr></table>";
	
	// </h4>
}
// <!--- done filling Locations --->

?>

<?php 
// <!--- fill up the l0 lockboxes --->
$BoxText = array("3","5","7","15","31","54","75","8","29","96");

for ($Bnum = 1; $Bnum <=10; $Bnum++)
{
	print "<h4 style='page-break-after: always'>";
	print "<table width='100%' border='0'><tr><td width='171' valign='top'>";
	print "<font color='#660000' size='6'><strong><img src='content/sth/gameart/treasure_chest5.gif' width='171' height='141' align='left'></strong></font>";

	print "</td> <td valign='top'><div align='left'> <font size='6'>";
	$boxtextnow = $BoxText[$Bnum-1];
    print "Lock Box #$boxtextnow</font></div>";
    
    if (strlen($boxtextnow) == 2)
    {
    	 print "<p><strong>A Lockbox is a small paper bag or envelope 
              with these locks taped to it.</strong></p>";
    }
    else 
    {
    	 print "<p><strong>A Lockbox is a small paper bag or envelope 
              with this lock taped to it.</strong></p>";
    }
     
    print "<p><strong>Cut out all of the Clues and Glyphs on this page (below) and put them in 
            this lockbox.  Put all of the lockboxes on a shelf where the kids can see them. 
            When the kids show you the correct keys, they may open the lockbox. </strong></p>";
			
    print "</td></tr></table>";
	



	print "<table width='100%' border='0'><tr><td>";
	print "<table align='left' border='5'><tr><td>";
	$imglink = "content/sth/gameart/LockBoxArt/LockBox".$Bnum.".jpg";
	print "<img src='$imglink'>";
	print "</td></tr></table>";

	print "</td></tr></table>";	
	

	
	//<!--- put the clues into this lockbox --->		
	for ($i = 1; $i <= 14; $i++)
	{
		
	
	
		$Clocation = $clue_location[$i-1];
		
		if ($Clocation == $Bnum+100)			
		{
			$Ctext = $clue_list[$i-1];
			$Clevel = $clue_level[$i-1];
			
			if ($Clevel != 0) // <!--- clue not in plain english --->
			{
				print "<table width='100%' border='0'><tr><td><table border='5' align='left'><tr><td>";
				$result = GetGlyphNumbersFromClue($Ctext);  //fills in $GlyphNumbersForThisClue
				foreach ($GlyphNumbersForThisClue as $GnumX)
				{
					$GlyphArtSuffix = $glyph_jpg_list[$GnumX-1];
					$imglink  = "content/sth/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
					print "<img src='$imglink'>";
				}
				
				// for testing ************************************************
				print "<font size='5'>$Ctext</font>";						
				
				print "</td></tr></table></td></tr></table>";
			}
			else
			{
				print "<table width='100%' border='0'><tr><td><table border='5' align='left'><tr><td>";
				print "<font size='5'>$Ctext</font>";						
				print "</td></tr></table></td></tr></table>";
			}
			
			
								
		}		// endif ($Clocation == $Bnum+100)
	
	}
		
		
	// <!--- Now put the glyphs into this location/lockbox --->
	
	print "<table border='0' width='99%'><tr>";
    $column_count = 0;
     
		
	$size_of_list = count($glyph_list);
	for($i = 1; $i <= $size_of_list; $i++)
	{
		//print"*";
		$this_glyph_location = $glyph_location[$i-1];
		if ($this_glyph_location == $Bnum+100)
		{
			//print"+";
			if ($column_count == 3)
			{
				print "</tr><tr>";
				$column_count = 0;
			}
			$this_glyph_word = $glyph_list[$i-1];
			$this_glyph_level = $glyph_level[$i-1];
       		$GlyphArtSuffix = $glyph_jpg_list[$i-1];
       		
       		print "<td width = '33%'>";          
		  	print "<table border='5' align='left'><tr><td>";
		  	if ( strlen($this_glyph_word) > 1 || (strlen($this_glyph_word) == 1 && ( 
		  		($this_glyph_word >= 'a' && $this_glyph_word <= 'z' ) ||
		  		($this_glyph_word >= 'A' && $this_glyph_word <= 'Z' ) ||
		  		($this_glyph_word >= '0' && $this_glyph_word <= '9' ) ) ) )
		  	{
		  		$glyphlink = "content/sth/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    			print "<div><img src='$glyphlink'><span class='glyphText'> = $this_glyph_word</span></div>";
		  		
		  	}
		  	else 
		  	{ // punctuation
		  		if ($this_glyph_word == "." || $this_glyph_word == ",")
		  		{
		  			$glyphlink = "content/sth/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    				print "<div><img src='$glyphlink'><span class='glyphText'><strong><font size = '6'> = $this_glyph_word</font></strong></span></div>";
		  		}
		  		else
		  		{   // some other punctuation
		  			$glyphlink = "content/sth/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    				print "<div><img src='$glyphlink'><span class='glyphText'><strong><font size = '4'> = $this_glyph_word</font></strong></span></div>";
		  		
		  		}
		  	}
			print "</td></tr></table>";
		  		
			print "</td>";
			$column_count++;       		
		}
	}       	
     
	if ($column_count < 3)
	{
		for ($iii = $column_count; $iii <=3; $iii++)
		{
			if ($iii < 3)
			{
				print "<td></td>";
			}
		}
	}
		
	print "</tr></table>";
	
	
	//<!--- current version does not have keys in lockboxes --->
	//  </h4>

}  // end for Bnum   <!--- done filling Lock Boxes --->


// <!--- the stuff that you start the game with --->
print "<h4 style='page-break-after: always'>";
print "<table width='100%' border='0'><tr><td width='171' valign='top'><font color='#660000' size='6'><strong>";
print "<img src='content/sth/gameart/treasure_chest5.gif' width='171' height='141' align='left'>";
print "</strong></font></td>";

print "<td valign='top'><div align='left'><font size='6'>";
print "Starting Clue and Message";
print "</font></div>"; 

print "<p><strong>Cut out and fold up this Greeing Card style Clue for to the kids to start 
            the game with.  This is all that they will need to get going.</strong></p>"; 
			
print "</tr></table>";
print "<p></p><p></p>";
	
print "<table border='5' align='center' width='100%'><tr align='center' valign='middle'>";
print "<td width='50%' height='350'><img src='content/sth/gameart/treasurehuntinvitation1.gif'></td>";
print "<td width='50%' height='350'><img src='content/sth/gameart/card_back.jpg' width='120' height='98'></td>";
print "</tr>";
print "<tr align='center' valign='middle'><td height='350'></td>";
print "<td headers='350'>";
	
$Lnum = 0;

// <!--- write the Quest introduction --->
for ($i = 1; $i <= 14; $i++)
{
	$Clocation = $clue_location[$i-1];
	if ($Clocation == $Lnum)
	{
		//  <!--- there should only be one match --->
        $Ctext = $clue_list[$i-1];
        $Clevel = $clue_level[$i-1];
        
        if ($Clevel == 0)
        {
        	// <!--- clue in plain english --->
              print "<p align='center'><strong><font size='4'>Hello Brave Adventurers</font></strong></p>";
              print "<p align='center'><strong>A magnificent treasure awaits you, if 
                you can get the treasure chest open.</strong></p>";
              print "<p align='center'><strong>To do that you will need to open the lock. 
                It will not be easy.</strong></p>";
              print "<div align='center'><strong>To begin, here is your first clue</strong></div>";
              print "<div align='center'><strong><font size='5'>$Ctext</font></strong></div>";
              print "<div></div>";
              print "<p align='center'><strong>Good Luck. You will need it! </strong></p>";
        }
          
	}
}     
print "</td></tr></table>";
		  
//	</h4>
	
//  <!--- print the final message / birthday card --->
print "<table width='100%' border='0'><tr><td valign='top'>";
print "<div align='center'><font size='6'>Congratulations Greeting Card</font></div>";
print "<div align='center'><strong>Cut out this card and fold it in half (<em>the 
        easy way</em>) twice, keeping the the chest artwork to the outsides. This 
        creates the final Congratulations Message card to place into the Treasure 
        Chest.</strong></div>"; 
			
print "</tr></table>";
print "<p></p>";

print "<table border='5' align='center' width='100%'>";
print "<tr align='center' valign='middle'>";
print "<td width='50%' height='350'><img src='content/sth/gameart/card_front.jpg'></td>";
print "<td width='50%' height='350'><img src='content/sth/gameart/card_back.jpg' width='120' height='98'></td>";
print "</tr>";

print "<tr align='center' valign='middle'>"; 
print "<td height='350'>";
//	<!--- put glyphs inside card left --->
	  $Ctext = $clue_list[13];
      // print "* $Ctext *"; 
      // <!--- clue must be translated to glyphs --->
		$result = GetGlyphNumbersFromClue($Ctext);
		$totalGs = count($GlyphNumbersForThisClue);
		                  
        print "<table align='center'><tr>";
		$counter1 = 0;

		for ($ZZdex = 1; $ZZdex <= $totalGs; $ZZdex++)
		{
			$try_value = 0;
			while ($try_value == 0)
			{
				$rand_pick = rand(1,$totalGs);
				$try_value = $GlyphNumbersForThisClue[$rand_pick-1];								
			}
			$GnumX = $try_value;
			$GlyphNumbersForThisClue[$rand_pick-1] = 0;
			
			print "<td>";
			// 	<!--- now display the glyph for the glyphs for each --->
			$GlyphArtSuffix = $glyph_jpg_list[$GnumX-1];
			$this_glyph_word = $glyph_list[$GnumX-1];
			$this_glyph_level = $glyph_level[$GnumX-1];
			
			$glyphlink = "content/sth/gameart/SmallGlyphs/glyph".$GlyphArtSuffix.".jpg";
    		print "<img src='$glyphlink'>";

    		
			if ( strlen($this_glyph_word) > 1 || (strlen($this_glyph_word) == 1 && ( 
		  		($this_glyph_word >= 'a' && $this_glyph_word <= 'z' ) ||
		  		($this_glyph_word >= 'A' && $this_glyph_word <= 'Z' ) ||
		  		($this_glyph_word >= '0' && $this_glyph_word <= '9' ) ) ) )
		  	{
		  		print "<span class='smallGlyphText'> = $this_glyph_word </span>";
		  	}
		  	else 
		  	{ // punctuation
		  		if ($this_glyph_word == "." || $this_glyph_word == ",")
		  		{
		  			print "<span class='smallGlyphText'> = <font size='5'>$this_glyph_word</font></span>";
		  		}
		  		else
		  		{   // some other punctuation
		  			print "<span class='smallGlyphText'> = <font size='3'>$this_glyph_word</font></span>";
		  		}
		  	}
    		
    		print "</td>";
    		$counter1++;
			if ($counter1 == 2)
			{
				print "</tr>";
				$counter1 = 0;
			}				
		}		
             
		if ($counter1 == 1)
		{
			print "<td></td></tr>";
		}
				
		print "</table>";
	
	print "</td>";
	
    print "<td headers='350'>";
    $i = 14;
	    
    $Ctext = $clue_list[$i-1];
    $Clevel = $clue_level[$i-1];
    
    //  <!--- clue must be translated to glyphs --->
    
    $result = GetGlyphNumbersFromClue($Ctext);

    //    <!--- fills in Session.GlyphNumbersForThisClue --->

    foreach ($GlyphNumbersForThisClue as $GnumX)
    {
    	$GlyphArtSuffix = $glyph_jpg_list[$GnumX-1];
    	$glyphlink = "content/sth/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    	print "<img src='$glyphlink'>";
    	
    }

    print "</td></tr></table>";

    
// </cfif> <!--- no real_errors  from above first table --->



if ($_SESSION['printerrorcount'] > 0)
{
	
	$errorcount = $_SESSION['printerrorcount'];
	print "<div>Errors Printing Game!!  Count: $errorcount </div>";
	foreach($_SESSION['printerrorlist'] as $errortext)
	print "<div>ERROR: $errortext</div>";
	
	//$_SESSION['sth_game_state'] = "gameerror";		
}
else
{
	print "<p>NO PRINT ERRORS !</p>";
	//$_SESSION['sth_game_state'] = "gameready";	
}

	?>	
		
		

