<?php
if(isset($_POST['step2'])) {
    $errorStr = formStep3();
    if($errorStr!=null){
	
	require_once('signup.php');
    }else{
		echo "Thank you for registering. If your browser does not automatically redirect you within 5 secounds click here:";
		?>
        <p>Link Goes Here</p>
<meta http-equiv="refresh" content="5; URL=index.php">
<?php
    }}
else {
    require_once('signup.php');
    $_SESSION["step2"] = 1;	}

function formStep3(){

$dbhandle = mssql_connect('xxxx\SQLExpress', 'xxxx', 'xxxx')
  or die("Couldn't connect to SQL Server on the server");

//select a database to work with
$selected = mssql_select_db('Account', $dbhandle)
  or die("Couldn't open the database"); 
  
    $errors = array();
    $errorStr = null;
	if(preg_match("/^[0-9a-zA-Z]{3,15}$/i", $_POST["username"])){
	$strSql="select * from tUser where sUserID='$_POST[username]'";
	$result = mssql_query($strSql) 
    or die('A error occured: ' . mysql_error());
// Get result count:
$Count = mssql_num_rows($result);
    if ($Count > 0)  $errors[] = "This Accountname already exists, please choose another one.";
	}
		if(preg_match("/^([1-zA-Z0-1@.\s]{1,255})$/i", $_POST["email"])){
	$strSql="select * from tUser where email='$_POST[email]'";
	$result = mssql_query($strSql) 
    or die('A error occured: ' . mysql_error());
	$Count = mssql_num_rows($result);
    if ($Count > 0)  $errors[] = "This email already exists, please choose another one.";
	}
    if(!preg_match("/^[0-9a-zA-Z]{3,15}$/i", $_POST["username"])) $errors[]="You are only alowed to use uppper and lower cases in English in your username.";
    if(strlen($_POST["username"])<3) $errors[] = "The length of your username can't be less than 3 characters";
    if(strlen($_POST["username"])>15) $errors[]= "The length of your username can't be more than 15 characters";
    if(!preg_match("/^[0-9a-zA-Z]{3,15}$/i", $_POST["password1"])) $errors[]="You can only use English Characters of Upper case and lower case in your password. Try again";
    if($_POST["username"]==$_POST["password1"]) $errors[]= "Accountname and password can't be the same!";
    if(strlen($_POST["password1"])<3) $errors[] = "The length of the password can't be less than 3 characters";
    if(strlen($_POST["password1"])>15) $errors[] = "The length of the password can't be more than 15 characters";
    if($_POST["password2"]!=$_POST["password1"]) $errors[] = "Passwords do not match";
	if(strlen($_POST["email"])>256) $errors[] = "The length of the email can't be more than 256 characters";
	if(strlen($_POST["email"])<5) $errors[] = "The length of the email can't be less than 5 characters";
	if(strlen($_POST["name"])>0) $errors[] = "Bot Detected.";
	 if(!preg_match("/^([1-zA-Z0-1@.\s]{1,255})$/i", $_POST["email"])) $errors[]="You are only alowed to use English letters and numbers in your email.";
	
    if(sizeof($errors)>0){
       $errorStr .= "<br><font>";
       $errorStr .= "You have the following error(s):";
       foreach($errors as $error)
               $errorStr .= "<li>$error</li>";
       $errorStr .= "</font><br><br>";
    }else{
	$userid= $_POST["username"];
    $passwd_user=$_POST["password1"];
	$email=$_POST["email"];
	$ip=$_SERVER['REMOTE_HOST'];

$regquery = "INSERT INTO tUser (sUserID, sUserPW, sUserName, email, sUserIP) VALUES ('$userid', '$passwd_user', '$userid', '$email', '$ip')";
	 $dk_account_result1=odbc_do($account_odbc,$regquery);
	 mssql_query($regquery);
     mssql_close($dbhandle);
    }
    return $errorStr;
}
?>
