<?

$EnforceSession = false;
include "includes/includes.php";

$PaymentStatus = request("payment_status");
if( $PaymentStatus != "Completed" ) { echo "failed."; exit; }

$OrderString = request("custom");
$OrderVariables = split("\|", $OrderString);

$OrderType = $OrderVariables[0];
$OrderNumber = $OrderVariables[1];
$OrderID = $OrderVariables[2];

if( $OrderType == "Registration" )
{

	$Record = SelectSingleRow("SELECT * FROM payment WHERE id = " . $OrderNumber . " AND orderid = '" . $OrderID . "'");
	$Students = $Record->GetValue("students");
	$Price = $Record->GetValue("price");
	$FirstName = $Record->GetValue("firstname");
	$LastName = $Record->GetValue("lastname");
	$Address = $Record->GetValue("address");
	$City = $Record->GetValue("city");
	$State = $Record->GetValue("state");
	$Zip = $Record->GetValue("zip");
	$Phone = $Record->GetValue("phone");
	$Email = $Record->GetValue("email");
	
	// Update order status
	SqlExecute("UPDATE payment SET active = 1 WHERE id = " . $OrderNumber . " AND orderid = '" . $OrderID . "'");
	SqlExecute("UPDATE student SET active = 1 WHERE paymentid = " . $OrderNumber);

	// Build e-mail body
	$EmailBody = "";

	$tp = new PhpTemplate;						
	for( $tp->StartFromFile( "templates/registration_email.template" ); $tp->Next(); ) 
	{

		$EmailBody .= $tp->GetOutput();
		$tp->ClearOutput();
		
		if( $tp->IsEscape("STUDENT-AMOUNT") ) $EmailBody .= $Students;
		else if( $tp->IsEscape("FIRST-NAME") ) $EmailBody .= $FirstName;
		else if( $tp->IsEscape("LAST-NAME") ) $EmailBody .= $LastName;
		else if( $tp->IsEscape("ADDRESS") ) $EmailBody .= $Address;
		else if( $tp->IsEscape("CITY") ) $EmailBody .= $City;
		else if( $tp->IsEscape("STATE") ) $EmailBody .= $State;
		else if( $tp->IsEscape("ZIP") ) $EmailBody .= $Zip;
		else if( $tp->IsEscape("PRICE") ) $EmailBody .= $Price;
		else if( $tp->IsEscape("PHONE") ) $EmailBody .= $Phone;
		else if( $tp->IsEscape("EMAIL") ) $EmailBody .= $Email;
		else if( $tp->IsEscape("STUDENTS") )
		{

			// Connect to database
			$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
			mysql_select_db($gblMySqlDb, $db);

			// Load questions / answers
			$RowIndex = 0;
			$query = "SELECT * FROM student WHERE paymentid = " . $OrderNumber;
			$result = mysql_query($query, $db);
			while($a = mysql_fetch_array($result))
			{				
				$Record = new PhpSqlRow;
				$Record->SetAsArray($a);
				$RowIndex++;

				$tpElem = new PhpTemplate;				
				for( $tpElem->Start( $tp->GetEscapeArgument() ); $tpElem->Next(); ) 
				{
					$EmailBody .= $tpElem->GetOutput();
					$tpElem->ClearOutput();

					if( $tpElem->IsEscape("NAME") ) $EmailBody .= $Record->GetValue("name");
					else if( $tpElem->IsEscape("USERNAME") ) $EmailBody .= $Record->GetValue("username");
					else if( $tpElem->IsEscape("PASSWORD") ) $EmailBody .= $Record->GetValue("password");
					else if( $tpElem->IsEscape("AGE") ) $EmailBody .= $Record->GetValue("age");

				}

			}
			
		}
		
	}
	
	// Email Registration
	mail( $gblContactEmail, "Registration", $EmailBody, "From: " . $gblContactEmail . "\r\nContent-type: text/html" );

	// Email customer
	mail($Email, "Registration", $EmailBody, "From: " . $gblContactEmail . "\r\nContent-type: text/html" );
	
	exit;
}

?>