<?php
session_start();
include "../conn.php";
include "../functions.php";

checkAdmin(session_id());

$submit = $_POST["submit"];

if (isset($submit))
{
	$jobtitle = $_POST["jobtitle"];
	$jobdescrip = $_POST["jobdescrip"];
	$jobcatid = $_POST["jobcatid"];
	$enddate = $_POST["enddate"];
	
	$query = "insert into job(jobtitle, jobdescrip, enddate) values ('$jobtitle', '$jobdescrip', '$enddate')";
	mysql_query($query) or die (mysql_error());
		
	$query = "select last_insert_id() from job";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result))
	{
		$jobid = $row[0];
	}
		
	$count = count($jobcatid);
		
	for ($i = 0; $i < $count; $i++)
	{
		$query = "insert into job_cat values('$jobid', '$jobcatid[$i]')";
		mysql_query ($query) or die (mysql_error());
	}
		
	header ("location: job.php");
}
?>
<?php
$title = "Add Job Posting";
include "header.php";
?>

<center>

<form action=<?php echo $_SERVER["PHP_SELF"]; ?> method="post" name=form1>
<table width=80% border=0 cellpadding=5 cellspacing=1>
  <tr>
    <td align="right">Job Title:</td>
    <td align="left"><input type="text" name="jobtitle" value="<?php echo $jobtitle; ?>"></td>
  </tr>
  
  <tr>
    <td align="right" valign="top">Job Description:</td>
    <td align="left"><textarea name="jobdescrip" rows=3 cols=30 wrap="virtual"><?php echo $jobdescrip; ?></textarea></td>
  </tr>
  
  <tr>
    <td align="right" valign="top">
    Job Categories:
    </td>
    
    <td>
    	<?php
    	
    	$query = "select jobcatid, catname from jobcat";

	$result = mysql_query($query) or die(mysql_error());

	while ($row = mysql_fetch_array($result))
	{
		echo "<input type=checkbox name=jobcatid[] value=$row[0]>$row[1]<br>";
	}
	?>
    </td>
  </tr>
  
  <tr>
    <td align="right">End Date:</td>
    <?php 
    if (! isset($enddate))
    {
    	$year = date("Y");
    	$month = date("m");
    	$day = date("d");
    	
    	$enddate = date("Y-m-d", mktime(0,0,0,$month + 1, $day, $year));
    }
    ?>
    <td align="left"><input type="text" name="enddate" value="<?php echo $enddate; ?>" readonly onfocus='popUpCalendar(this, form1.enddate, "yyyy-mm-dd")'></td>
  </tr>
  
  <tr>
    <td align="center" colspan=2><input type="hidden" name="submit" value=1><input type="submit" value=" Add ">&nbsp;<input type="reset" value="Clear"></td>
  </tr>
</table>
</form>
</center>

<?php
include "footer.php";
?>