<?php
	include('/var/www/html/uclit/protected/common/login/check.php');
	include('/var/www/html/uclit/protected/common/css_login/css_head2.php');
	include('/var/www/html/uclit/protected/connections/db_pers.php');
	
	if(isset($_POST['lname']) && isset($_POST['fname']) && isset($_POST['department']))
	{
		
/*			@session_start(); 
		$suser = $_SERVER['PHP_AUTH_USER'];
		@session_register($suser); 
*/
		//format date 2003-05-06 13:20:59
		$datetimenow = date('Y-m-d')." ".strftime("%H:%M:%S");
	
		

		//connection to db server
		$db = mysql_select_db("uclit_db") or die ("Could not select uclit_db database.");
		
		$pid = $_POST['hpid'];
		$lname = ucfirst($_POST['lname']);
		$fname = ucfirst($_POST['fname']);
		$mname = strtoupper($_POST['mname']);
		if ($mname == '')
			$mname = '*';
		
		$dept_id = $_POST['department'];
		$title = $_POST['title'];
		$type = $_POST['type_id'];
				
		
				
		function error()
		{
			if (mysql_errno() == 1062)
			{
				if(mysql_error() != "Duplicate entry '' for key 2")
				die("<p><b>".mysql_error()."</b></p><font color=red>'Employee' already exist in the Personnel table.</font>");	
			}	
		}
		
		$sql_dept = "SELECT dept_id, dept_name, dept_head_id FROM departments WHERE dept_id = '$dept_id'";
		$dept_result = mysql_query($sql_dept, $uclit_dbConn) or die ("Unable to exercute query. <p>$sql_dept</p>");
		while ($var = mysql_fetch_array($dept_result)) 
		{
			$deptname = $var['dept_name'];
			$dept_head_id = $var['dept_head_id'];
		}
		
			$query_head_email = "SELECT e_mail FROM personnel WHERE personnel_id = '$dept_head_id'";
			$sql_result_email = mysql_query($query_head_email, $uclit_dbConn) or die("Couldn't execute query.<p>$query_head_dept_email</p>");
			$head_dept_email = mysql_result($sql_result_email, 0);
		
		$table_name = 'personnel';

		$sql_test = "SELECT * FROM personnel WHERE (last_name='$lname' and first_name='$fname' and dept_id='$dept_id')";
		
		//check if employee already exist in the personnel table
		$tResult = mysql_query($sql_test, $uclit_dbConn) or die ("Unable to exercute query. <p>$sql_test</p>");
		if( $row =@mysql_fetch_row($tResult) >= 1)
		{
			echo "Employee already exist in the Personnel table.";
		}
		else
		{
			
			$sql = "INSERT INTO personnel (personnel_id, last_name, first_name, middle_name, title, dept_id, type_id) VALUES ('', '$lname', '$fname', '$mname', '$title', '$dept_id', '$type')";
				
			if( !($result = mysql_query($sql, $uclit_dbConn)))
			{
			
				error(); //error while query sql statement
				exit;	
			}
			else 
			{
					
				//sql to statement to log
				$sql_useraccess = "INSERT INTO EquipmentUserAccessLog VALUES ('', '$suser', '$sql', '$datetimenow')";
				//log user access
				@mysql_query($sql_useraccess, $equipConn);
			
			echo "
			
				<h2><i>Add New Employee - Employee added</i></h2>	
				<table width=100% border='0' cellpadding='1' cellspacing='1'>
					<tr>
					  <td><b>Last Name:</b>&nbsp;&nbsp;$lname</td>
					  <td><b>First Name:</b>&nbsp;&nbsp;$fname</td>
					  <td><b>Middle Initial:</b>&nbsp;&nbsp;$mname</td>
					</tr>
					<tr>
					  <td colspan='3'>&nbsp;</td>
					</tr>
					<tr>
					  <td><b>Dept. Name:</b>&nbsp;&nbsp;$deptname</td>
					</tr>
					<tr>
					  <td colspan='3'>&nbsp;</td>
					</tr>
				</table>
				<p><a href='./show_addemployee.php'>Add another Staff Member</a>
			&nbsp;&nbsp;|&nbsp;&nbsp<a href='./show_allemployee.php'>List All Employee</a>
			</p>
			";

			$mail_to = "$head_dept_email";
			$mail_subj = "A new employee has been added in your department";
			$mail_text = "Department Head,
			$fname $mname $lname was added to the personnel database in department $deptname with 
			a title of $title at $datetimenow.  Please click on the following link to assign resources to the new employee.  http://ul.uc.edu/uclit/protected/depthead/show_new_employee.php?uaction=add&pid=$pid";
			$mail_header = "From: libhelp@uc.edu\r\n";
			$mail_header .= "X-Mailer: server\r\n";
			$mail_header .= "Reply To: libhelp@uc.edu\r\n";
			$sendmail_param = "libhelp@uc.edu";
			mail("$mail_to","$mail_subj","$mail_text","$mail_header","-f$sendmail_param");
			
			}
		}
	}//end if
	
	@mysql_close($uclit_dbConn);

 include('/var/www/html/uclit/protected/common/css_login/css_foot2.php'); ?> 