<?php
include("mysql_login.php");
echo "Echoing things";
echo $email = $_POST['emailadd'];
include("emailvarification.php");
query($email); //Query the user
generatepass($email); //Generate password
	function query($email)
	{	
		$query_email = mysql_query("SELECT * FROM `user` WHERE email='$email'") or die("querying email failed");
		echo $query_email_result = mysql_fetch_array($query_email);
		echo $email = $query_email_result['email'] . " Email <br>";
		echo $name = $query_email_result['name'] . " Name <br>";
		echo $username = $query_email_result['username'] . " Username <br>";
	}
	function generatepass($email)
	{
		list($split_mail, $domain) = split('@', $email);
		echo $split_mail . "Is new <br>";
		$rand = rand(100,800);
		$newpass = $split_mail.$rand;
		updatequery($email, $newpass); // Update Query
	}
	// Function update query will update the new password and process the e-mail
	function updatequery($email, $newpass)
	{
		echo "<br> We are in update query <br>";
		echo $email. "<br>". $newpass . "<br>";
		$query_password = mysql_query("UPDATE `user` SET password = md5('$newpass') WHERE email = '$email'") or die ("Updating password fail");
		sendemail($email, $newpass);
	}
	function sendemail($email, $newpass)
	{
		echo " I am in sending e-mail section";
		$subject = "<br>Your password has been reset<br>";
		$message = "This is an automatic message. Your password has been reset upon your request. Your new password is: $newpass ";
		$header = "Webmaster: UAA IEEE";
		$send_mail = mail($email, $subject, $message, NULL, $header);
		if(!$send_mail)
		{
			echo "<br>Error on sending e-amil";
		} else
		{
			echo "Your password has been delivered";
			echo "<br> Check your e-amil";
			echo "You will be redirected to main page";
			echo "<meta http-equiv = \"refresh\" content = \"1; URL=index.php\">";

		}
	}
?>