<?php

$email_text_order = "";
$email_text_TKGticket = "";
$email_text_STHticket = "";
$text_order = "";
$text_TKGticket = "";
$text_STHticket = "";
$theMessage = "";
$TKG_ticket_count = 0;
$STH_ticket_count = 0;

$order_query = mysql_query("SELECT *			
	    	FROM `tkgames_orders`
	    	WHERE `ID` = '$this_order_id' LIMIT 1");
$this_order_row = mysql_fetch_object($order_query);	
	    	
$this_order_total = $this_order_row->order_total;
$this_order_twoco_order_number = $this_order_row->twoco_order_number;
$this_order_email = $this_order_row->sold_to_email;
//print '<div>total: '.$this_order_total.'</div>';
//print '<div>this_order_twoco_order_number: '.$this_order_twoco_order_number.'</div>';

$order_items_query = mysql_query("SELECT *			
	    	FROM `tkgames_order_items`
	    	WHERE `orderID` = '$this_order_id'");
	    	
while($item_obj = mysql_fetch_object($order_items_query))  
{ 
	$this_item_catalog_id = $item_obj->catalogID;
	$this_item_unit_price = $item_obj->unit_price;
	$this_item_quantity = $item_obj->quantity;
	$this_item_description = $item_obj->description;
	
	//print '<div>this_item_catalog_id: '.$this_item_catalog_id.'</div>';
	//print '<div>this_item_quantity: '.$this_item_quantity.'</div>';
	
	
	// fetch this row from the tkgames_catalog table
	$this_catalog_row_query = mysql_query("SELECT *			
    	FROM `tkgames_catalog`
    	WHERE `ID` = '$this_item_catalog_id' LIMIT 1");
    	
    $this_catalog_row = mysql_fetch_object($this_catalog_row_query);	
	
    //print '<div>this_catalog_game_type: '.$this_catalog_row->ticket_game_type.'</div>';
    
   	// each loop is one order_item
    if ($this_catalog_row->ticket_game_type == 'sth' || $this_catalog_row->ticket_game_type == 'multi')
    {
    	$STH_ticket_count++;			    
    }
    elseif ($this_catalog_row->ticket_game_type == 'brain' || $this_catalog_row->ticket_game_type == 'multi')
    {
    	$TKG_ticket_count++;	
    }
    
	
	$space_buffer = 47 - strlen($this_item_description);
	$email_text_order1 = $email_text_order.$this_item_description;
	$email_text_order = str_pad($email_text_order1, $space_buffer);
	
	$space_buffer = 3 - strlen($this_item_quantity);
	$email_text_order1 = $email_text_order.$this_item_quantity." x";
	$email_text_order = str_pad($email_text_order1, $space_buffer);						  
	 
	$item_price = number_format($this_item_unit_price,2);
	$space_buffer = 6 - strlen($item_price);
	$email_text_order1 = $email_text_order.'$'.$item_price;
	$email_text_order = str_pad($email_text_order1, $space_buffer);	
	  
	$row_total1 = $this_item_quantity * $this_item_unit_price;
	$row_total = number_format($row_total1,2);			
	$email_text_order1 = $email_text_order.$row_total.chr(13).chr(10);
	$email_text_order = $email_text_order1;
	  
	$text_order = $text_order."<tr><td>".$this_item_description."</td>";
	$text_order = $text_order."<td align = 'center'>".$this_item_quantity."</td>";
	$text_order = $text_order."<td align = 'right'>$".$item_price."</td>";
	$text_order = $text_order."<td align = 'right'>$".$row_total."</td></tr>";
  
}

    //sprintf("$%2f", $row_total1);
    
    // prepare the tickets render text for this order_item_row


$these_tickets_query = mysql_query("SELECT *			
	    	FROM `tkgames_tickets`
	    	WHERE `orderID` = '$this_order_id'");
	    	
while($these_tickets = mysql_fetch_object($these_tickets_query))  
{ 	
	$t_ticket_type = $these_tickets->ticket_title;
	$final_ticket_number = $these_tickets->ticket_number;
	$t_ticket_image = '/artwork/'.$these_tickets->ticket_image;
	
  	$space_buffer = 40 - strlen($t_ticket_type);
	//$email_text_TKGticket1 = $email_text_TKGticket.$t_ticket_type;
	//$email_text_TKGticket2 = str_pad($email_text_TKGticket1, $space_buffer);
	//$email_text_TKGticket = $email_text_TKGticket2."Ticket Number: ".$final_ticket_number.chr(13).chr(10);
									  
	$text_TKGticket1 = $text_TKGticket.'<table width="470" class="ticket_box"><tr><td>';
	$text_TKGticket2 = $text_TKGticket1.'<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">';
	
	$text_TKGticket = $text_TKGticket2.'<tr><td><table align = "center">';											  
										  
	$text_TKGticket = $text_TKGticket.'<tr><td class = "ticket_title">';
	$text_TKGticket = $text_TKGticket.$t_ticket_type;
	$text_TKGticket = $text_TKGticket.'</td></tr>';
	$text_TKGticket = $text_TKGticket.'<tr><td class = "ticket_text">STH ';
	$text_TKGticket = $text_TKGticket.'<span class = "ticket_number">';
	$text_TKGticket = $text_TKGticket.$final_ticket_number;
	$text_TKGticket = $text_TKGticket.'</span></td></tr>';
	$text_TKGticket = $text_TKGticket.'<tr><td align = "right" class = "ticket_caption">';
	$text_TKGticket = $text_TKGticket.'Good for One Super Treasure Hunt Game';
	$text_TKGticket = $text_TKGticket.'</td></tr>';
	$text_TKGticket = $text_TKGticket.'</table></td>';
	if ($render_media == 'screen')
	{
		$text_TKGticket = $text_TKGticket.'<td align = "right"><img class = "ticket_image" src="'.$t_ticket_image.'" alt="TK Games Ticket"></td>';
	}
	$text_TKGticket = $text_TKGticket.'</tr></table>';
	$text_TKGticket = $text_TKGticket.'</td></tr></table>';

	
	if ($session->sth_game_state == "gameready")
	{
		// Use this ticket to pay for your new game
		$text_TKGticket = $text_TKGticket. '<div align = "center"><a class = "sth_text_links" href = "javascript: GoToSTH(\''.$final_ticket_number.'\')">Use this ticket to pay for your Super Treasure Hunt game</a></div>';
	}
	
	elseif ($session->sth_game_state == "printready")
	{
		// Use this ticket to create a new game
		$text_TKGticket = $text_TKGticket. '<div align = "center"><a class = "sth_text_links" href = "javascript: GoToSTH(\''.$final_ticket_number.'\')">Go back to your Super Treasure Hunt game</a></div>';
	}
	
	else
	{
		// gamestate = "new" || "" || "editgame" || "formerror"
		// Use this ticket to create a new STH game
		$text_TKGticket = $text_TKGticket. '<div align = "center"><a class = "sth_text_links" href = "javascript: GoToSTH(\''.$final_ticket_number.'\')">Use this ticket to create a new Super Treasure Hunt game now</a></div>';
	}
	
}
	// print "<div>Screen Text TKGticket:</div>";
	


							  
							  
// for email
$theMessage = $theMessage."The Order Number for your purchase is: ".$this_order_twoco_order_number."\n";
$theMessage = $theMessage."You purchased:\n";
$theMessage = $theMessage.$email_text_order."\n";
$theMessage = $theMessage."****   Order Total:                                           $";
$theMessage = $theMessage.$this_order_total."\n\n";
$theMessage = $theMessage."\n";
$theMessage = $theMessage."You are issued these GameX Tickets:\n";
if ($STH_ticket_count + $TKG_ticket_count > 0)
{
	$theMessage = $theMessage.$email_text_STHticket;
}
      
              
              
if ($STH_ticket_count > 0)
{
	$theMessage = $theMessage."\n\nSuper Treasure Hunt Instructions:\n";
	$theMessage = $theMessage."Game and more Information is at www.supertreasurehunt.com/TreasureHuntGame.cfm\n";
	$theMessage = $theMessage."Please read these soon, to get an understanding of how the game works.\n";
	                
	$theMessage = $theMessage."\nWhat you will need:\n";
	                
	$theMessage = $theMessage."* A 3-Number Combination Lock (or 3-Number Safe)\n";
	$theMessage = $theMessage."* A Treasure Chest of prizes to lock up\n";
	$theMessage = $theMessage."* 10 Envelopes, Paper Bags, or Smaller Chests\n";
	$theMessage = $theMessage."* Clues to 10 hiding places (please see the game form and example clues there)\n";
	$theMessage = $theMessage."* A work area for the kids, who might want paper, pencils, scissors\n\n";
	                
	$theMessage = $theMessage."When you fill out the game form (www.supertreasurehunt.com/gameform.cfm), your game is automatically saved.  After that, you can print the game clues as often as you need to from any computer.";
	$theMessage = $theMessage."  Simply go to www.SuperTreasureHunt.com and enter your STH Ticket Number on the home page to print the game instructions and clues.\n";
	$theMessage = $theMessage."If you have more than 5 kids, I recommend printing extra copies of the game and hiding 2 or more of each clue page together, for them to share.\n";
	$theMessage = $theMessage."\nHave a Great Super Treasure Hunt Party!\n";					                
}
              
              
if ($TKG_ticket_count >0)
{
	$theMessage = $theMessage."\n\nInstant Detective Game Instructions:\n";
	$theMessage = $theMessage."Game and more Information is at www.SuperTreasureHunt.com\n";
	$theMessage = $theMessage."This is also a link directly to the game.\n";
                
	$theMessage = $theMessage."\nAll you need is 5 to 20 players and an internet computer with a printer.  Just type in the names of the players, print, give each player his or her SECET CLUE SHEET, and begin playing Instant Detective Game.\n";
                
	$theMessage = $theMessage."\n\nYou'll print the game INSTRUCTIONS, the game CARDS, and EACH PLAYER'S SECRET CLUE SHEET.  Don't let anyone see the clue sheets, except the player who's name is at the top of each page!  Ideally, someone who is not playing can print the game for you.\n";	
                
	$theMessage = $theMessage."\nHave a Super Fun Instant Detective Game! - remember you can play more than once on each ticket.";	
}	
// done preparing various messages.
?>
