<?

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

$Error = "";

$RegID = request("RegID");

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

	// Delete records
	SqlExecute("DELETE FROM student WHERE paymentid = " . $RegID);

	// Set data
	$StudentCount = request("hdnStudentCount");
	
	for( $index = 1; $index < $StudentCount + 1; $index++ )
	{
			
		$Name = trim(request("txtName" . $index));
		$UserName = trim(request("txtUserName" . $index));
		$Password = trim(request("txtPassword" . $index));
		$Age = trim(request("txtAge" . $index));
		//$Difficulty = trim(request("selDifficulty" . $index));

		// Validate data
		if( $Name == "" || $UserName == "" || $Password == "" || $Age == "" ) 
		{
			$Error .= "Please complete the data.  All fields are required<BR>";
			break;
		}
		
		if( SelectScalar("SELECT COUNT(*) FROM student WHERE username = '" . $UserName . "' AND site = '" . $gblSiteName . "'") > 0 )
		{
			$Error .= "The username '" . $UserName . "' is already taken.  Please choose a different username.<BR>";
			break;		
		}

		// Update lesson record
		$Record = new PhpSqlRow;

		$Record->SetNameValue( "id", CreateId12() );
		$Record->SetNameValue( "active", 0 );
		$Record->SetNameValue( "site", $gblSiteName );		
		$Record->SetNameValue( "paymentid", $RegID );
		$Record->SetNameValue( "name", $Name );
		$Record->SetNameValue( "username", $UserName );
		$Record->SetNameValue( "password", $Password );		
		$Record->SetNameValue( "age", $Age );
		//$Record->SetNameValue( "difficulty", $Difficulty );		

		InsertRow($Record, "student");
	
	}
	
	if( $Error == "" )
	{	
		header("Location: verify.php?RegID=" . $RegID);
		exit;
	}
	else
	{
		SqlExecute("DELETE FROM student WHERE paymentid = " . $RegID);
	}
	
}
else
{
	$Record = SelectSingleRow("SELECT students FROM payment WHERE id = " . $RegID);
	$StudentCount = $Record->GetValue("students");
}

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

// Begin Template
$tp = new PhpTemplate;
for( $tp->StartFromFile( "templates/register_students.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("STUDENT-COUNT") ) echo $StudentCount;
	
	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 = " . $RegID;
		$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->NextAndEchoOutput(); ) 
			{
				
				if( $tpElem->IsEscape("INDEX") ) echo $RowIndex;
				else if( $tpElem->IsEscape("NAME") ) echo $Record->GetValue("name");
				else if( $tpElem->IsEscape("USERNAME") ) echo $Record->GetValue("username");
				else if( $tpElem->IsEscape("PASSWORD") ) echo $Record->GetValue("password");
				else if( $tpElem->IsEscape("AGE") ) echo $Record->GetValue("age");
				/*else if( $tpElem->IsEscape("DIFFICULTIES") )
				{
					$tpElem2 = new PhpTemplate;						
					for( $index = 0; $index < count($gblDifficulties); $index++ )
					{
						for( $tpElem2->Start( $tpElem->GetEscapeArgument() ); $tpElem2->NextAndEchoOutput(); ) 
						{	
							if( $tpElem2->IsEscape("DIFFICULTY") ) echo $gblDifficulties[$index];
							else if( $tpElem2->IsEscape("IF-SELECTED") )
							{
								if( $gblDifficulties[$index] == $Record->GetValue("difficulty") ) 
									echo $tpElem2->GetEscapeArgument();
							}
						}			
					}								
				}*/
	
			}

		}
		
		if( $RowIndex < $StudentCount )
		{
			$RowIndex++;
			for( ; $RowIndex < $StudentCount + 1; $RowIndex++ )
			{
				$tpElem = new PhpTemplate;				
				for( $tpElem->Start( $tp->GetEscapeArgument() ); $tpElem->NextAndEchoOutput(); ) 
				{
					if( $tpElem->IsEscape("INDEX") ) echo $RowIndex;
					else if( $tpElem->IsEscape("NAME") ) echo request("txtName" . $RowIndex);
					else if( $tpElem->IsEscape("USERNAME") ) echo request("txtUserName" . $RowIndex);
					else if( $tpElem->IsEscape("PASSWORD") ) echo request("txtPassword" . $RowIndex);
					else if( $tpElem->IsEscape("AGE") ) echo request("txtAge" . $RowIndex);
					/*else if( $tpElem->IsEscape("DIFFICULTIES") )
					{
						$tpElem2 = new PhpTemplate;						
						for( $index = 0; $index < count($gblDifficulties); $index++ )
						{
							for( $tpElem2->Start( $tpElem->GetEscapeArgument() ); $tpElem2->NextAndEchoOutput(); ) 
							{	
								if( $tpElem2->IsEscape("DIFFICULTY") ) echo $gblDifficulties[$index];
								else if( $tpElem2->IsEscape("IF-SELECTED") )
								{
									if( $gblDifficulties[$index] == request("selDifficulty" . $RowIndex) ) 
										echo $tpElem2->GetEscapeArgument();
								}
							}			
						}								
					}*/
				}			
			}
		}
		
		
	}
			
}

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

?>
