<?php 
	mysql_connect("localhost", "test", "test") or die(mysql_error()); 
	mysql_select_db("thesis")or die(mysql_error());

	//require_once ('includes/config.inc.php');
	include ('includes/headr.php');
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> Sign up! </title>
  <link href="includes/grid.css" rel="stylesheet" type="text/css">
  <link href="includes/panels.css" rel="stylesheet" type="text/css">
  <link href="includes/main.css" rel="stylesheet" type="text/css">
  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="validation/jquery.validate.js"></script>
  <script type="text/javascript">
	$(document).ready(function(){
	$("#register").validate({
	rules: {
     username: "required",
	 email: {
         required: true,
         email: true,
     },
	 confemail: {
		 required: true,
		 equalTo: "#email",
		 },
	
	 pass: "required",
	 confpass: {
		 required: true,
		 equalTo: "#pass",
		 },
	
	},
	messages: {
		username: "Please type your name.",
		email: "Please enter a valid email address.",
		confemail: "Emails don't match!",
		pass: "Please enter a password",
		confpass: "Passwords don't match!",
		},

});
}); 
</script>
</head>

<?php 
	//after validating with jquery, escape the incoming data

	 //text search? redirect!
if (isset ($_GET['sitesearch'])){
	header('Location: home.php?sitesearch='.$_GET['sitesearch'].'');

}
	if (isset($_POST['submitted'])){
	//require_once (MYSQL);

    $username = mysqli_real_escape_string($dbc, $_POST['username']);
	$email = mysqli_real_escape_string($dbc, $_POST['email']);
	$confirm_email = mysqli_real_escape_string($dbc, $_POST['confemail']);
    $password = mysqli_real_escape_string($dbc, $_POST['pass']);
    $confirm_pass = mysqli_real_escape_string($dbc, $_POST['confpass']);		





	// Make sure the email address and the username are available:
		$q1 = "SELECT user_id FROM user WHERE email='$email'";
		$q2 = "SELECT user_id FROM user WHERE username='$username'";

		$r1 = mysqli_query ($dbc, $q1) or trigger_error("Query: $q1\n<br />MySQL Error: " . mysqli_error($dbc));
		$r2 = mysqli_query ($dbc, $q2) or trigger_error("Query: $q2\n<br />MySQL Error: " . mysqli_error($dbc));
		$flag1 = mysqli_num_rows($r1);
		$flag2 = mysqli_num_rows($r2);

		$email_match = ""; //error messages for registered emails and usernames
		$username_match = "";

		$success= ""; //message indicating whether the account was created successfully

		if ($flag1 == 0 && $flag2 == 0){ // No rows were retrieved, email and username are available. 
			
			
			//calculate age
	
			//Add the user to the database:
			$q = "INSERT INTO user (username, email, pass, avatar_id) VALUES ('$username','$email',SHA1('$password'), 0)";
			$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

			if (mysqli_affected_rows($dbc) == 1) { //One row affected, user successfully added
				$success= "Account was successfully created! Now <a href='login.php'>log in!</a>";
			}
			else {
				$success= "You could not be registered due to a system error. Please try again.";
			}

		}//end if
		
		else { //check for matches, set error messages values. Error messages will appear in the form

			if ($flag1 == 1){ //1 row retrieved, email match found
				$email_match = "That email address has already been registered.";
				}
			if ($flag2 == 1){ //1 row retrieved, username match found
				$username_match = "That username is already in use.";
				}
		} //end else
	
	mysqli_close($dbc);

	}

	?>
 <body>
 <div class="panel" id="registration">
 <h2>Join ArtRoom for free!</h2>
 <form action="registration.php" method="post" name="register" id="register"> 
 <table width=90% border="0" cellpadding="0" cellspacing="5">
 <tr> 
  <td height="22" align="right" valign="middle"><label for="username" class="label">Username </label></td>
  <td height="22" align="left" valign="middle"><input name="username" type="text" id="username"></td>
  </tr> 
  <tr>
  <td height="22" colspan="3" align="middle" valign="middle"><span class="error"><?php if (isset ($username_match)) echo $username_match;?></span></td>
  </tr> 
  <tr>
  <td height="22" align="right" valign="middle"><label for="email" class="label">Email Address </label></td>
  <td height="22" align="left" valign="middle"><input name="email" type="text" id="email"></td>
  </tr> 
  <tr>
  <td height="22" colspan="3" align="middle" valign="middle"><span class="error"><?php if (isset ($email_match)) echo $email_match;?></span></td>
  </tr> 
  <tr>
  <td height="22" align="right" valign="middle"><label for="confemail" class="label">Retype Email Address </label></td>
  <td height="22" align="left" valign="middle"><input name="confemail" type="text" id="confemail"></td>
  </tr> 
  <tr>
  <td height="22" align="right" valign="middle"><label for="pass" class="label">Password </label> </td>
  <td height="22" align="left" valign="middle"><input name="pass" type="password" id="pass"></td>
  </tr> 
  <tr>
  <td height="22" align="right" valign="middle"><label for="confpass" class="label">Confirm Password </label></td>
  <td height="22" align="left" valign="middle"><input name="confpass" type="password" id="confpass"></td>
  </tr> 
  <tr>
  <td height="22" colspan="3" align="middle" valign="middle"><span class="error"><?php if (isset ($success))echo $success;?></span></td>
  </tr> 
  <tr>
  <td height="22" colspan="3" align="middle" valign="middle"><input type="submit" name="submit" id="submit" value="Submit"></td>
  </tr> 
  <tr>
  <td height="22" colspan="3" align="middle" valign="middle"><input type="hidden" name="submitted" value="TRUE" /></td>
  </tr> 
  <tr>
  <td height="22" colspan="3" align="middle" valign="middle"><p>Already have an account? <a href="login.php">Login.</a></p></td>
  </tr>
  </table> 
 </form>
  </div>
 </body>
 <?php include ('includes/footer.php'); ?>
</html>
