<?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');

	{
		$db = mysql_select_db("uclit_db", $uclit_dbConn) or die ("Unable to select $dbequipt database.");
		$QueryDept = "SELECT dept_id, dept_name, building, dept_head_id FROM departments ORDER BY dept_name" ;
		
		$db2 = mysql_select_db("uclit_db", $uclit_dbConn) or die("Couldn't select $dbequipt database.");
	
		$dept_result = mysql_query($QueryDept, $uclit_dbConn) or die("Couldn't execute query. <p>$QueryDept</p>");
			
		$QueryType = "SELECT type_id, name FROM type ORDER BY name" ;
		$type_result = mysql_query($QueryType, $uclit_dbConn) or die("Couldn't execute query. <p>$QueryType</p>");
		
			
			
		if(!$_GET['pid'])
		{
			$pid = '';
			$lname = '';
			$fname = '';
			$mname = '';
			$dept_id = '';
			$mail_location = '';
			$email = '';
			$phone = '';
			$fax = '';
			$web_role = '';
		}
		else //Page is being called from edit or delete
		{
			$pid = $_GET['pid'];
			$uaction = $_GET['uaction'];

			$sql_personnel = "SELECT personnel.personnel_id,
								personnel.last_name,
								personnel.first_name,
								personnel.middle_name,
								personnel.dept_id,
								personnel.e_mail,
								personnel.phone,
								personnel.web_role,
								personnel.room,
								personnel.duties,
								personnel.duties_sec,
								personnel.removed,
								personnel.type_id,
								personnel.title,
								personnel.working_title,
								personnel.ID_Num,
								personnel.PAF,
								personnel.PAF_date,
								personnel.PAF_num,
								departments.dept_id,
								departments.dept_name,
								departments.building,
								departments.dept_head_id
							FROM departments
   							INNER JOIN personnel ON (departments.dept_id = personnel.dept_id) WHERE personnel_id='$pid'";
			
			$employee_result = @mysql_query($sql_personnel, $uclit_dbConn) or die ("Unable to execute query: <p>$sql_personnel</p>");
			
			while($row = @mysql_fetch_array($employee_result))
			{
				$pid = $row['personnel_id'];
				$lname = $row['last_name'];
				$fname = $row['first_name'];
				$mname = $row['middle_name'];
				$dept_id = $row['dept_id'];
				$email = $row['e_mail'];
				$phone = $row['phone'];
				$web_role = $row['web_role'];
				$type_id = $row['type_id'];
				$title = $row['title'];
				$working = $row['working_title'];
				$id_num = $row['ID_Num'];
				$PAF = $row['PAF'];
				$PAF_date = $row['PAF_date'];
				$PAF_number = $row['PAF_num'];
				$bldg = $row['building'];
				$dept_head_id = $row['dept_head_id'];
			}
			
		}	
	
		if(!isset($uaction))
		{
			$h3 = 'Personnel: Add New Staff Member';
		    $goto = 'process_employee_add.php';
		    $buttonvalue = 'Add Employee';
		}
		elseif ($uaction == 'edit')
		{
			$h3 = "Update Record[ '$pid' ] from Personnel";
			$goto = 'process_employeeud.php';  //send edited record to database
			$buttonvalue = 'Update Employee';
		}
		elseif ($uaction == 'delete')
		{
			$h3 = "Delete Current Record[ '$pid' ] from Personnel?";
			$goto = 'process_employeeud.php'; //send deleted record to database
			$buttonvalue = 'Delete Employee';
		}	
	
	}
    
	
    echo" <h2>$h3</h2>

	<font color='red'>*</font><font size='-1'>- Indicates required fields</font>
    <form action='$goto' method='post' name='employee_form' onSubmit='return validator();'>
	<table width='400' height='260'border='0' cellspacing='1' cellpadding='1'>
	
	  <tr>
	    <td align='right'>Last Name:&nbsp;</td>
	    <td><input name='lname' type='text' value='$lname'><font color='red'>*</font></td>

	  </tr>
	  <tr>
	    <td align='right'>First Name:&nbsp;</td>
	    <td><input name='fname' type='text' value='$fname'><font color='red'>*</font></td>
	  </tr>
	  <tr>
	    <td align='right'>Middle Name Initial:&nbsp;</td>
	    <td><input name='mname' type='text' value='$mname' size='2' maxlength='1'></td>";
?>
	  </tr>
      
      <tr>
	    <td align='right'>Employee Type:&nbsp;</td>
	    <td><select name='type_id'>
			  <option value=''>Select ...</option> 
<?php
	while($row = @mysql_fetch_array($type_result))
	{
		$ftype_id = $row['type_id'];
		$type_name = $row['name'];
	
		if ($type_id == $ftype_id)
		{	echo "$type_id == $ftype_id";
			echo "<option value='$ftype_id' selected>$type_name</option>";
		}
		else 
		{
			echo "<option value='$ftype_id'>$type_name</option>";
		}

	}
			  
?>			  
	        </select><font color="red">*</font></td>
	  </tr>
      
      
      
      
      
      
	  <tr>
	    <td align='right'>Department ID:&nbsp;</td>
	    <td><select name='department'>
			  <option value=''>Select ...</option> 
<?php
	while($row = @mysql_fetch_array($dept_result))
	{
		$fdept_id = $row['dept_id'];
		$dept_name = $row['dept_name'];
	
		if ($dept_id == $fdept_id)
		{	echo "$dept_id == $fdept_id";
			echo "<option value='$fdept_id' selected>$dept_name</option>";
		}
		else 
		{
			echo "<option value='$fdept_id'>$dept_name</option>";
		}

	}
			  
?>			  
	        </select><font color="red">*</font></td>
	  </tr>
	  
	  </tr>
	  <tr>
<?php
	
		$sql_supervisor = "SELECT personnel.personnel_id,
								personnel.last_name,
								personnel.first_name,
								departments.dept_id,
								departments.dept_name,
								departments.building,
								departments.dept_head_id
							FROM departments
							INNER JOIN personnel ON (departments.dept_id = personnel.dept_id) WHERE personnel_id='$dept_head_id'";
	  
	  	$supervisor_result = @mysql_query($sql_supervisor, $uclit_dbConn) or die ("Unable to exercute query: <p>$sql_personnel</p>");
			
		while($row = @mysql_fetch_array($supervisor_result))
		{
			$pid_sup = $row['personnel_id'];
			$lname_sup = $row['last_name'];
			$fname_sup = $row['first_name'];
			$mname_sup = $row['middle_name'];
			$dept_id_sup = $row['dept_id'];
			$bldg_sup = $row['building'];
			$dept_head_id = $row['dept_head_id'];
		}
		
	if ($uaction == 'edit')
	{
		echo"
	    <td width='33%' align='right'>Supervisor:&nbsp;</td>
	    <td width='67%'><input name='supervisor' type='text' value='$lname_sup, $fname_sup' disabled></td>

	  </tr>
	   <tr>
	    <td width='33%' align='right'>Building:&nbsp;</td>
	    <td width='67%'><input name='building' type='text' value='$bldg' disabled></td>
	  </tr>";

	}
	echo"
	  <tr>
	    <td width='33%' align='right'>Official Title:&nbsp;</td>
	    <td width='67%'><input name='title' size = '50' type='text' value='$title'></td>
	  </tr>
        <tr>
	    <td width='33%' align='right'>Working Title:&nbsp;</td>
	    <td width='67%'><input name='working' size = '50' type='text' value='$working'></td>
	  </tr>
	  <tr>
	    <td width='33%' align='right'>University ID Number:&nbsp;</td>
	    <td width='67%'><input name='id_num' type='text' value='$id_num'></td>
	  </tr>";
  
	if ($uaction == 'edit')
	{ 
		echo"
	    <td width='40%' align='right'>Personnel ID:&nbsp;</td>
	    <td width='60%'><input name='pid' type='text' value='$pid' disabled></td>
		
	  <tr>
	  	<td width='20%' align='right' >PAF Received:&nbsp;</td>
	    <td align=left>
	 	<INPUT type='checkbox' name='PAF_sel' value='1'";

		if($PAF == '1')
		{
 			echo" CHECKED>"; 
		}
	 	else
	 	{
			echo">";
		}
		echo"
		<td width='33%' align='right'>PAF Received Date:&nbsp;</td>
	    <td width='33%'><input name='pafdate' type='text' value='$PAF_date'></td>
	    <td width='33%' align='right'>PAF Number:&nbsp;</td>
	    <td width='33%'><input name='pafnum' type='text' value='$PAF_number'></td>
	  </tr> 
	  <tr>
	    <td align='right'>E-mail:&nbsp;</td>
	    <td><input name='email' type='text' value='$email' size='35' maxlength='30'></td>
	  </tr>
	  <tr>
	    <td align='right'>Telephone #:&nbsp;</td>
	    <td><input name='phone' type='text' value='$phone' size='20' maxlength='15'></td>
	  </tr>
	  <tr>
	    <td align='right'>Web Role:&nbsp;</td>
	    <td><!--<input name='web_role' type='text' value='$web_role' size='20' maxlength='15'>-->
			<select name='web_role'> ";

		if ($web_role != '' && isset($uaction))
		{
			if (web_role == '*')
				echo "<option value='$web_role' selected>None</option>";
			else 
				echo "<option value='$web_role' selected>$web_role</option>";
		}
?>
		   	<option value="*">None</option>
		   	<option value="Contributor">Contributor</option>
		   	<option value="Developer">Developer</option>
		   	<option value="Editor">Editor</option>
	     	</select>
        	</td>
<?php
	}
		
	echo"
		<td><input name='hpid' type='hidden' value='$pid'></td>
	    <td><input name='uaction' type='hidden' value='$uaction'></td>
	  </tr>
	  <tr>
	    <td>&nbsp;</td>
	    <td><input name='submit' type='submit' value='$buttonvalue'></td>";
?>
	  </tr>
	  <tr>
	    <td colspan="2"></td>
	  </tr>
	  
	</table>
	
	</form>
	<tr>
	    <td colspan="2" align="center">
		</td>
	  </tr>
	
	<script language='javascript'>

	function validator()
	{
		var noErrors;
		var msgError = 'Please enter data in the following fields\\n';
		var obj = document.employee_form;
		
		if (obj.lname.value != "" && obj.fname.value != "" && obj.department.value != "")
		{
			return (true);
		}
		else //
		{
			//required fields is empty
			noErrors = false;
	
			if(obj.lname.value == "")
			{
				msgError += '"Last Name" field\\n';
				obj.lname.focus();
			}
			if(obj.fname.value == "")
			{
				msgError += '"First Name" field\\n';
				if(obj.lname.value != "")
					obj.fname.focus();
			}
			if(obj.department.value == "")
			{	
				msgError += '"Department" list\\n';
				if(obj.fname.value != "")
					obj.department.focus();
			}
			if(obj.type_id.value == "")
			{	
				msgError += '"Employee Type" list\\n';
				if(obj.fname.value != "")
					obj.type_id.focus();
			}
	
			//errors found
			if(!noErrors)
			{
				alert(msgError);
				return (noErrors);
			}
		}
	}//end function
	</script>
	
<?php include('/var/www/html/uclit/protected/common/css_login/css_foot2.php'); ?> 