<h1>SignUp</h1>
<p>
<?php
if ( $_POST['Register'] )
{
	$username	= mssql_escape_string( $_POST['username'] );
	$password	= mssql_escape_string( $_POST['password'] );
	$password2	= mssql_escape_string( $_POST['password2'] );
	$email	= mssql_escape_string( $_POST['email'] );
	$email2	= mssql_escape_string( $_POST['email2'] );
	$age	= mssql_escape_string( $_POST['age'] );
	
	if ( strlen( $username ) > 25 || strlen( $username ) < 3 ) $errors[] = 'Username must be at least 3 characters long and no longer than 15 characters';
	if ( preg_match( '[^A-Za-z0-9]' , $username) ) $errors[] = 'Username must be alphanumeric';
	
	if ( strlen( $password ) > 15 || strlen( $password ) < 7 ) $errors[] = 'Password must be at least 7 characters long and no longer than 15 characters';
	if ( $password != $password2 ) $errors[] = 'Passwords do not match';

	if ( strlen( $email ) > 90 || strlen( $email ) < 5 ) $errors[] = 'Email must be at least 5 characters long and no longer than 90 characters';
	if ( strpos($email, '@') == false) $errors[] = 'You have entered an invalid email address';
	if ( $email != $email2 ) $errors[] = 'Emails do not match';
	
	if ( strlen( $age ) > 2 || strlen( $age ) < 1 ) $errors[] = 'Age must be higher than 0 and no larger than 2 digits.';
	if ( preg_match( '[^0-9]' , $age) ) $errors[] = 'Age may only contain numbers';
	
	
	if ( count( $errors ) == 0 )
	{
		$mssql_link = mssql_connect( '.\SQLEXPRESS', 'sa', 'Password' );
		if ( !$mssql_link )
		{
			showMessage('info', 'Registration is temporarily offline');
		}
		else
		{
			$checkUser = mssql_query( "SELECT * FROM Account..tUser WHERE sUserID = '$username';" );
			if ( mssql_num_rows( $checkUser ) == 0 )
			{
				$inserUser = mssql_query( "INSERT INTO Account..tUser (sUserID, sUserPW, sUserName, sUserIP, sEmail, nAge) VALUES ('$username', '$password', '$username', '" . $_SERVER['REMOTE_ADDR'] . "', '$email', '$age');" );
				$getUserNo = mssql_query("SELECT * FROM Account..tUser WHERE sUserID = '$username';" );
				
				if (mssql_num_rows($getUserNo) == 1)
				{
					while($row = mssql_fetch_assoc($getUserNo))
					{
						$inserMinor = mssql_query( "INSERT INTO Account..tMinorsPlaytime (nUserNo, bIsAdult) VALUES ('" . $row["nUserNo"] . "', 'True');");
					}
				}
				else
				{
					showMessage('error', 'Error in user ID.');
					die;
				}
				mssql_close( $mssql_link );
				
				if( $inserUser and $inserMinor)
				{
					showMessage('success', 'Account was created successfully, enjoy!');
				}
				else
				{
					showMessage('error', 'An error occurred during account creation.');
					die;
				}
			}
			else
			{
				showMessage( 'error', 'Such username is already in use' );
			}
		}
	}
	else
	{
		foreach ( $errors as $myError )
		{
			$errors22 = $myError.'<br />';
		}
		showMessage( 'error', $errors22 );
	}
}
else
{ ?>
<form method="post">
	<table width="400px">
		<tr>
			<td>Username</td>
			<td><input type="text" name="username" /></td>
		</tr>
		<tr>
			<td>Password</td>
			<td><input type="password" name="password" /></td>
		</tr>
		<tr>
			<td>Password Confirmation</td>
			<td><input type="password" name="password2" /></td>
		</tr>
		<tr>
			<td>Email Address</td>
			<td><input type="text" name="email" /></td>
		</tr>
		<tr>
		<td>Email Address Confirmation</td>
			<td><input type="text" name="email2" /></td>
		</tr>
		<tr>
			<td>Age</td>
			<td><input type="numbers" name="age" /></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" name="Register" value="Create Account" /></td>
		</tr>
	</table>
</form>
<?php 
}
?>