<?

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

$Error = "";

$RegID = request("RegID");
$bEdit = false;
if( $RegID != "" ) $bEdit = true;

$PricePerStudent = $gblPricePerStudent;

// Page submitted
if( request("SUBMIT") == "true" )
{

	// Set data
	$Students = request("txtStudents");
	$FirstName = trim(request("txtFirstName"));
	$LastName = trim(request("txtLastName"));
	$Address = trim(request("txtAddress"));
	$City = trim(request("txtCity"));
	$State = trim(request("txtState"));
	$Zip = trim(request("txtZip"));
	$Email = trim(request("txtEmail"));
	$Phone = trim(request("txtPhone"));
	$BookQuantity = trim(request("txtBooks"));
		
	// Validate data
	if( $Students == "" )
		$Error .= "Please enter the amount of students.<BR>";
	else if( !is_numeric($Students) )
		$Error .= "Please enter a valid student amount.<BR>";
	
	if( $FirstName == "" ) $Error .= "Please enter your first name.<BR>";
	if( $LastName == "" ) $Error .= "Please enter your last name.<BR>";
	if( $Address == "" ) $Error .= "Please enter your address.<BR>";
	if( $City == "" ) $Error .= "Please enter your city.<BR>";
	if( $State == "" ) $Error .= "Please enter your state.<BR>";
	if( $Zip == "" ) $Error .= "Please enter your zip code.<BR>";
	if( $Email == "" ) $Error .= "Please enter your e-mail address.<BR>";
	if( $Phone == "" ) $Error .= "Please enter your phone number.<BR>";
	
	if( $BookQuantity != "" && !is_numeric($BookQuantity) )
		$Error .= "Please enter a valid book quantity.<BR>";
	
	// Validate login
	if( $Error == "" )
	{
	
		// Update lesson record
		$Record = new PhpSqlRow;
		
		if( $bEdit )
			$Record->SetNameValue( "id", $RegID );

		$Record->SetNameValue( "orderid", CreateId12() );
		$Record->SetNameValue( "site", $gblSiteName );
		$Record->SetNameValue( "students", $Students );
		$Record->SetNameValue( "active", 0 );
		$Record->SetNameValue( "price", ($PricePerStudent * $Students) + ($BookQuantity * $gblPricePerBook) );		
		$Record->SetNameValue( "firstname", $FirstName );
		$Record->SetNameValue( "lastname", $LastName );
		$Record->SetNameValue( "address", $Address );
		$Record->SetNameValue( "city", $City );
		$Record->SetNameValue( "state", $State );
		$Record->SetNameValue( "zip", $Zip );
		$Record->SetNameValue( "email", $Email );
		$Record->SetNameValue( "phone", $Phone );		
		$Record->SetNameValue( "phone", $Phone );
		$Record->SetNameValue( "createdate", date("y-m-d h:i:s A") );
		
		if( $BookQuantity != "" )
			$Record->SetNameValue( "bookquantity", $BookQuantity );

		if( $bEdit )
			UpdateRow($Record, "payment", "id");
		else
			$RegID = InsertRow($Record, "payment");
					
		header("Location: register_students.php?RegID=" . $RegID);
		exit;			
	
	}	
		
}
else if( $bEdit )
{
	$Record = SelectSingleRow("SELECT * FROM payment WHERE id = " . $RegID);
	$Students = $Record->GetValue("students");
	$FirstName = $Record->GetValue("firstname");
	$LastName = $Record->GetValue("lastname");
	$Address = $Record->GetValue("address");
	$City = $Record->GetValue("city");
	$State = $Record->GetValue("state");
	$Zip = $Record->GetValue("zip");
	$Email = $Record->GetValue("email");
	$Phone = $Record->GetValue("phone");
	$BookQuantity = $Record->GetValue("bookquantity");
}

// Spit out the header
include "includes/header.php";

// Begin Template
$tp = new PhpTemplate;
for( $tp->StartFromFile( "templates/register.template" ); $tp->NextAndEchoOutput(); )
{
	CheckGlobalEscapes( $tp );
	
	if( $tp->IsEscape("IF-ERROR") )
	{
		if( $Error != "" )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("ERROR") ) echo $Error;	
	else if( $tp->IsEscape("STUDENTS") ) echo $Students;
	else if( $tp->IsEscape("FIRST-NAME") ) echo $FirstName;
	else if( $tp->IsEscape("LAST-NAME") ) echo $LastName;
	else if( $tp->IsEscape("ADDRESS") ) echo $Address;
	else if( $tp->IsEscape("CITY") ) echo $City;
	else if( $tp->IsEscape("STATE") ) echo $State;
	else if( $tp->IsEscape("ZIP") ) echo $Zip;
	else if( $tp->IsEscape("EMAIL") ) echo $Email;
	else if( $tp->IsEscape("PHONE") ) echo $Phone;
	else if( $tp->IsEscape("BOOK-PRICE") ) echo $gblPricePerBook;
	else if( $tp->IsEscape("BOOKS") ) echo $BookQuantity;	
}

// Spit out the footer
include "includes/footer.php";

?>
