<?php

	/* Grab the URL variable */
	$acode = $_GET["acode"];
	$doy = $_GET["doy"];
	
	/* Connect to database and grab the keys */
		
	$servername = "localhost";
	$username = "u9cwxhx7pzvxt";
	$password = "Maggie@2014SG$2014%%";
	$dbname = "phibase0_servequant";

	$conn = new mysqli($servername, $username, $password, $dbname);
	if ($conn->connect_error) {
		die("Connection failed: " . $conn->connect_error);
	} 


	$query = "SELECT `acode` FROM `acode`";

	$result = $conn->query($query);
	$num=$result->num_rows;	

	
	/* Set up a loop to check if requested key is found in database */
	$i=0;
	$validationkey = 0;

	$validation = "";


if ($result=mysqli_query($conn,$query))
{
	while ($obj=mysqli_fetch_object($result))
	{
		if ($acode == $obj->acode)
		{
			$validation = "Okay";
		}	
	}
	mysqli_free_result($result);
}	
	
	/* If a match was found in database, echo "VALID". Else echo "ERROR" */
	
	if($validation != "") {
	
 
		$result = $conn->query("SELECT * FROM acode WHERE acode='{$acode}' ") 
		or die(mysql_error()); 
		
		while($row = mysqli_fetch_object($result))
		{
			$email = $row->email;

			$code = 0;

			for ($i=0; $i<100; ++$i)
			  {
				$code =  $code + (999 - ord(substr($email,$i,1))) ;
			  }
		$code = (99999 * $doy) + $code;
		}
	}
	
	if($validation != "") 
	{
		echo $code ;
	} else 
	{
		echo 111111;
	}


?>