<?php
/*
This file access the value put by user who want to be a local member, validate, and store on database in all the values are valid. 
Otherwise it refresh to the same page and have user put the valid information. 
*/
require_once("includes/mysql_login.php");
$first_name = stripslashes($_POST['f_name']);
$first_name = mysql_real_escape_string($first_name);
$last_name = stripslashes($_POST['l_name']);
$last_name = mysql_real_escape_string($last_name);
$email = stripslashes($_POST['e_mail']);
$email = mysql_real_escape_string($email);
$major = stripslashes($_POST['select_major']);
$other_major = stripslashes($_POST['major_not_listed']);
$other_major = mysql_real_escape_string($other_major);
$minor = stripslashes($_POST['minor']);
$minor = mysql_real_escape_string($minor);
$college_year = stripslashes($_POST['college_year']);
$graduation_year = stripslashes($_POST['graduation']);
$graduation_year = mysql_real_escape_string($graduation_year);
$comment = stripslashes($_POST['comment']);
$comment = mysql_real_escape_string($comment);
$date_submitted = date('l dS \of F Y h:i:s A');
//###########################################
//Check the validity of the user_input
		/*function invalid_name_email_graduation_zip($f_name, $l_name, $e_mail, $graduation)
		{
		if(preg_match("/[^a-zA-Z]+$/s", $f_name))	//Only letters are valid
		return true;
		if($f_name == "")
		return true;
		if(preg_match("/[^a-zA-Z]+$/s", $l_name)) //Only letters are valid
		return true;
		if($l_name == "")
		return true;
		if(preg_match("/[^a-zA-Z0-9\@\_\.\-]+$/s", $e_mail)) //letters, dot(.), @, - and _ are valid
		return true;
		if($e_mail == "")
		return true;
		if(preg_match("/[^a-zA-Z0-9\-\(\)\ ]+$/s", $graduation)) //Only numbers from 0-9, -, (, ) are valid
		return true;
		if($graduation == "")
		return true;
		}
if(invalid_name_email_graduation_zip($first_name, $last_name, $email, $graduation_year))
{
	echo "Error: on your inputs<br> Your name, e-amil, and graduation year is required to process the form";
	echo "<html>";
	echo "<head>";
	echo "<meta http-equiv = \"refresh\" content = \"3; URL = memberApply.php\">";
	echo "</head>";
	echo "<body> Please put your valid invormation</body></html>";
	exit;
}  */
//#########################################
echo "Following information is received <br>";
echo "Thank You!<br>";
echo "Name: ". $first_name. " " . $last_name;
echo "<br>";
echo "Email: " . $email;
echo "<br>";
echo "Major: " . $major;
echo " Other:";
echo " ". $other_major;
echo "<br> Minor: " . $minor . "<br>";
echo "Year: " . $college_year; 
echo "<br>";
echo "Expected Graduation: ". $graduation_year. "<br>";
//echo "Address : " . $street_addr. "<br>"."". $city_addr. " , " . $state_addr. " ," . " " . $zip_addr;
//create table application
function application()
{
$query = 'CREATE TABLE `application`(`applicationid` int(5) auto_increment NOT NULL, `first_name` varchar(100) NOT NULL, `last_name` varchar(100) NOT NULL,
								`email` varchar(100) NOT NULL, `major` varchar(100) NOT NULL, `other_major` varchar(100), `minor` varchar(100), `college_year` varchar(100) NOT NULL,
									`graduation_year` varchar(30), `comment` longtext,
									`date_applied` varchar(45) NOT NULL,
									PRIMARY KEY(`applicationid`))';
$application_query = mysql_query($query) or die (" Fatal Error". mysql_error());					
}
//application();
// This function should not be called unless table need to rebuilt
$application_insert = "INSERT INTO `application` VALUES('$applicationid', '$first_name', '$last_name', '$email', '$major', '$other_major', '$minor', '$college_year', 
														'$graduation_year', '$comment', '$date_submitted')";
$application_insert_result = mysql_query($application_insert);
if(!$application_insert_result)
{
echo "Error". mysql_error();
echo "<br> Page Redirected";
echo "<meta http-equiv = \"refresh\" content = \"0; URL=index.php\">";
} else
{
echo "<br><h5>Your application is submitted</h5><br>";
echo "Page redirected to homepage";
echo "<meta http-equiv = \"refresh\" content = \"1; URL=index.php\">";				
}
/*
	Anything below is for blog account. User can create a blog account while applying for membership
	Get username, name, e-mail, and password only 
*/
$name = $first_name. "  " . $last_name;
//$email;
$username = $_POST['u_name'];
$password = $_POST['password'];
if(($username != NULL)&&($username != NULL))
{
$admin = 0;
echo "Name $name <br> Username <br> $username <br> $password <br> Email $email";
	$blog_account = "INSERT INTO `user` VALUES('$indentification', '$username', md5('$password'), '$email', '$name', '$admin', now())";
	$blog_account_query = mysql_query($blog_account);
	if(!$blog_account_query)
	{
		echo " <br> Error on querying database" . mysql_error();
		echo "<meta http-equiv = \"refresh\" content = \"0; URL=index.php\">";
	}
}
echo "<br>Page redirected<br>";
//echo "<meta http-equiv = \"refresh\" content = \"0; URL=index.php\">";
?>