<html>
<head>
<title></title>
</head>
<BODY document.Form1.Username.focus();">

<?php

function display_form_info($username,$password)
{
// Form for changing the password
echo <<< Begin
        <p>
        <CENTER>
        <FORM ACTION="update_alumni.php" NAME="Form3" METHOD="POST">
        <CENTER>
	  <input name="Username" type="hidden" value="$username" size="30">
	  <input name="Password" type="hidden" value="$password" size="30">
        <P>
	  <INPUT TYPE="submit" VALUE="Fill Out Information">
	  <P>
        </FORM>
	  </CENTER>
Begin;
}

function display_form()
{
echo <<< Begin
		<H2 align="center">New Alumni</H2>
        	</center>
        	<p align="center">Please fill out all the fields below.</p>
        	<CENTER>
        	<FORM ACTION="{$_SERVER['PHP_SELF']}" NAME="Form1" METHOD="POST">
        	<CENTER>

		<TABLE BGCOLOR="lightgrey" BORDER="1" BORDERCOLOR="gray">
            
		<TR>
        	<TD align="right"><B>Username:</B></TD>
        	<TD><INPUT NAME="Username" SIZE="50"></TD>
       	</TR>

		
		<TR>
        	<TD align="right"><B>Password (at least 5 characters):</B></TD>
        	<TD><INPUT NAME="Password" SIZE="50" type="password"></TD>
       	</TR>
	
		<TR>
        	<TD align="right"><B>Retype Password:</B></TD>
        	<TD><INPUT NAME="Retype_Password" SIZE="50" type="password"></TD>
       	</TR>
		
		<TR>
        	<TD align="right"><B>First Name:</B></TD>
        	<TD><INPUT NAME="First_Name" SIZE="50"></TD>
       	</TR>

		<TR>
        	<TD align="right"><B>Last Name:</B></TD>
        	<TD><INPUT NAME="Last_Name" SIZE="50"></TD>
       	</TR>

		<TR>
        	<TD align="right"><B>Email:</B></TD>
        	<TD><INPUT NAME="Email" SIZE="50"></TD>
       	</TR>

		<TR>
        	<TD align="right"><B>Phone:</B></TD>
        	<TD><INPUT NAME="Phone" SIZE="50"></TD>
       	</TR>

		<TR>
        	<TD align="right"><B>Address:</B></TD>
        	<TD><INPUT NAME="Address" SIZE="50"></TD>
       	</TR>

		<TR>
        	<TD align="right"><B>Major:</B></TD>
        	<TD><INPUT NAME="Major" SIZE="50"></TD>
       	</TR>

		<TR>
        	<TD align="right"><B>Graduation Year:</B></TD>
        	<TD><INPUT NAME="Graduation_Year" SIZE="50"></TD>
       	</TR>

		<TR>
        	<TD align="right"><B>In which city you were born?</B></TD>
        	<TD><INPUT NAME="Answer" SIZE="50"></TD>
       	</TR>


       	<input name='submitted' value='TRUE' type='hidden'>
	
       	 </TABLE>

		<p></p>

        	<TABLE>
        	<TR>
        	<TD align="center"><INPUT TYPE="submit" VALUE="SUBMIT">
       	 </TD>
       	 <TD align="center">
       	 <INPUT TYPE="reset" VALUE="RESET">
        	</TD>
        	</TR>
       	 </TABLE>

       	 </FORM>
Begin;
}

function does_user_exist($username,&$k)
{
	@$d = mysql_connect('localhost','database_username','database_password');
	if (!d)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}
	if (empty($username))
	{
		echo "You can't leave the username empty.<br>\n";
		return FALSE;
	}
	@$select = mysql_select_db('alumni');
	if (!$select)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}

	$q = "select count(*) from users where username='$username'";
	@$r = mysql_query($q);
	if (!$r)
	{
		echo "Sorry can't process your information at this time.<BR>";
		@mysql_close($d);
		return FALSE;
	}

	@$row = mysql_fetch_row($r);
	$k = $row[0];

	@mysql_free_result($r);
	@mysql_close($d); 
	return TRUE;
}

function new_alumni($first_name,$last_name,$phone,$address,$email,$graduation_year,$major,$username,$password,$question,$answer,$retype_password,&$errors)
{	
	$errors = array();
	$s = array($first_name,$last_name,$phone,$address,$email,$graduation_year,$major,$username,$password,$question,$answer,$retype_password);
	foreach ($s as $v)
	{
		if (empty($v))
		{
			$errors[] = "You have to fill out all the fields.";
			return FALSE;
		}
	}
	if (strlen($password) < 5)
	{
		$errors[] = "The password must have at least 5 characters.";
		return FALSE;
	}
	if ($password != $retype_password)
	{
		$errors[] = "The passwords you entered are not the same.";
		return FALSE;
	}
	@$d = mysql_connect('localhost','database_username','database_password');
	if (!d)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}
	@$select = mysql_select_db('alumni');
	if (!$select)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}
		
	$q = "insert into users (username,password,question,answer,date,phone,address,email,major,
		graduation_year,first_name,last_name) values 
		('$username',sha1('$password'),'$question','$answer',now(),'$phone',
			'$address','$email','$major','$graduation_year','$first_name','$last_name')";
	@$r = mysql_query($q);
	if (!$r)
	{
		echo "Sorry can't process your information at this time.<BR>";
		@mysql_close($d);
		return FALSE;
	}


	@mysql_close($d); 
	return TRUE;
}

if (isset($_POST['submitted']))
{	
	$username = trim(stripslashes($_POST['Username']));
	$k = 0;
	$flag = does_user_exist($username,$k);
	if (!$flag)
		exit;
	if($k == 0)	
	{	
		$question = "In which city you were born?";
		$password = trim(stripslashes($_POST['Password']));
		$retype_password = trim(stripslashes($_POST['Retype_Password']));
		$answer = trim(stripslashes($_POST['Answer']));
		$phone = trim(stripslashes($_POST['Phone']));
		$address = trim(stripslashes($_POST['Address']));
		$email = trim(stripslashes($_POST['Email']));
		$graduation_year = trim(stripslashes($_POST['Graduation_Year']));
		$major = trim(stripslashes($_POST['Major']));
		$first_name = trim(stripslashes($_POST['First_Name']));
		$last_name = trim(stripslashes($_POST['Last_Name']));
		$flag = new_alumni($first_name,$last_name,$phone,$address,$email,$graduation_year,$major,$username,$password,$question,$answer,$retype_password,$errors);
		if (!$flag)
		{
			foreach ($errors as $error)
				echo "$error<br>\n";
		}
		else
		{
			echo "<h2 align='center'>You have been added to the list. Thank you.</h2>";
			//update_info($username,$password);
		}
	} // k = 0
	else
		echo "Choose a different username, because the username you chose already exists.<br>";
				
} // if (form submitted)
else
	display_form();

?>
</BODY></HTML>

