<?php 
	ini_set('display_errors', 1);
	require 'db.php';
	$name = $_POST['name'];
	$grade = $_POST['grade'];
	$school = $_POST['school'];
	$email = $_POST['email'];
	//$league = $_POST['division'];
	//$size = $_POST['size'];
	if (checkEmail($email)) {
		try {
			$SQL = "INSERT INTO people (name, grade, school, email) VALUES (:name, :grade, :school, :email)";
			$logon = $pdo -> prepare($SQL);
			$logon->bindParam(':name', $name);
			$logon->bindParam(':grade', $grade);
			$logon->bindParam(':school', $school);
			$logon->bindParam(':email', $email);
			$logon->execute();
		} catch (Exception $e) {
			if ($e->errorInfo[1] == 1062) {
		      echo "Netfangið er núþegar í gagnagrunninum";
		   } else {
		      echo "Eitthvað fór úrskeiðis, ekki tókst að skrá þig" . $e;
		   }
		}
	}
	else{
		echo "Tölvupóstfangið var eitthvað vitlaust";
	}
	function checkEmail($a)
	{
		if (strpos($a, '@')) {
			$attPos = strpos($a, '@');
			$dotPos = strpos($a, '.');
			if ($attPos < $dotPos) {
				return true;
			}
			else{
				return false;
			}
		}
		else{
			return false;
		}
	}
?>