<?php
$submit = $_POST["submit"];

if ($submit == 1)
{
	$name = $_POST["name"];
	$email = $_POST["email"];
	$jobid = $_POST["jobid"];
	
	require("fileupload-class.php");
	$path = "cv/";
	$upload_file_name = "userfile";
	$acceptable_file_types = "application/msword|application/pdf";
	$default_extension = "";
	$mode = 2;
	$my_uploader = new uploader('en');
	$my_uploader->max_filesize(100000);
	
	if ($my_uploader->upload($upload_file_name, $acceptable_file_types, $default_extension)) 
	{
		$my_uploader->save_file($path, $mode);
	}
	if ($my_uploader->error) 
	{
		$err .= $my_uploader->error;
		echo $err;
	}
	else
	{
		$cvpath = $my_uploader->file['name'];
	}
	
	if (! $err)
	{
		include "../conn.php";
		
		$query = "insert into application(appdate, name, email, cvpath) values (now(), '$name', '$email', '$cvpath')";
		mysql_query($query) or die (mysql_error());
		
		$query = "select last_insert_id() from application";
		$result = mysql_query($query) or die (mysql_error());
		while ($row = mysql_fetch_array($result))
		{
			$appid = $row[0];
		}
		
		$query = "insert into job_application values ('$jobid', '$appid')";
		mysql_query($query) or die (mysql_error());
		
		echo "<html><head><title>Thank you</title></head><body><center><table border=0><tr><td align=center>Thank you, we will contact you soon if you are shortlisted.</td></tr><tr><td align=center><form action=javascript:window.close(); method=post><input type=submit value=close></td></form></tr></table></center></body></html>";
		exit;
	}
	
	echo "<html><head><title>Erro</title></head><body><center><table border=0><tr><td align=center>Error occured, please try again</td></tr><tr><td align=center><form action=javascript:window.close(); method=post><input type=submit value=close></td></form></tr></table></center></body></html>";
	exit;
}
?>
