<?php
/*
	ini_set('display_errors',1);
	ini_set('display_startup_errors',1);
	error_reporting(-1);
*/
	include("connect.php");

	$status = 0;
	if (isset($_POST["ID"])) {
		$id = $_POST["ID"];
		if (ctype_space($id) || $id == '') {
			$status = 0;
		}
		else {
			$status = 1;
		}
	}

	$query = "SELECT userid, appNum FROM userinfo WHERE userid = ?";
	$stmt = $mysqli->prepare($query);
	$stmt->bind_param('s', $id);
	$stmt->execute();
	$stmt->bind_result($userid, $num);

	if ($stmt->fetch()) {
		$status = 2;
		$_SESSION["uid"] = $userid;
		$_SESSION["appnum"] = $num;
		$_SESSION["survey"] = 2;
		if (empty($_SESSION["appnum"])) {
			header("Location: http://serv.cusp.nyu.edu/projects/soundtagging/sorry.php");
			die();
		}
		else {
			header("Location: http://serv.cusp.nyu.edu/projects/soundtagging/thanks.php");
			die();
		}
	}
	else {
		$sucess = 1;
	}
?>

<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Urban Soundtagging</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-theme.min.css" rel="stylesheet">
    <link href="style.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js does not work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="container">

      <div class="starter-template section1">
        <h2>Welcome</h2>
        <p style="font-size:larger">Please click on the New Participant button if this is your first time, or enter your ID below to continue</p>
			<button class="btn btn-default" onclick="location.href='consent.php'">New Participant</button>
			<br>
			<form action = "index.php" method = "POST">
				<p style="font-size:larger">If you have an ID already:</p>
				<input type = "text" name = "ID" placeholder="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" size="42">
				<br>
				<button class="btn btn-default" type = "submit" value = "Enter">Enter</button>
			</form>
<?php
	if ($status == 1) {
		echo "<p style='color:red'>ID not found, please retry or create a new one</p>";
	}
?>
      </div>
      <div>
<strong>WARNING</strong>: Do not press the back button in your web browser or refresh any pages in the study after pressing the Enter button above. Pressing the back button or refreshing the study will invalidate all of your answers.
      </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>
