<?php
include "../conn.php";

$confirm = $_POST["confirm"];

if ($confirm == 1)
{
	$jobid = $_POST["jobid"];
	$jobtitle = $_POST["jobtitle"];
	$jobdescrip = $_POST["jobdescrip"];
	$jobcatid = $_POST["jobcatid"];
	$enddate = $_POST["enddate"];
	
	$query = "update job set jobtitle='$jobtitle', jobdescrip='$jobdescrip', enddate='$enddate' where jobid='$jobid'";
	mysql_query($query) or die (mysql_error());
	
	$query = "delete from job_cat where jobid='$jobid'";
	mysql_query($query) or die (mysql_error());
	
	$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");
}

$jobid = $_POST["jobid"];

$title = "Modify Job Posting";
include "header.php";

$query = "select jobtitle, jobdescrip, enddate from job where jobid='$jobid'";
$result = mysql_query($query) or die (mysql_error());

while ($row = mysql_fetch_array($result))
{
	$jobtitle = $row[0];
	$jobdescrip = $row[1];
	$enddate = $row[2];
}
?>

<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=40 wrap="virtual"><?php echo $jobdescrip; ?></textarea></td>
  </tr>
  
  <tr>
    <td align="right" valign="top">
    Job Category:
    </td>
    
    <td>
    	<?php
    	
    	$query = "select jobcatid, catname from jobcat";

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

	while ($row = mysql_fetch_array($result))
	{
		$set = 0;
		
		$query = "select jobcatid from job_cat where jobid='$jobid'";
		$catresult = mysql_query($query) or die (mysql_error());
		
		while ($catrow = mysql_fetch_array($catresult))
		{
			if ($row[0] == $catrow[0])
			{
				echo "<input type=checkbox name=jobcatid[] value=$row[0] checked>$row[1]<br>";
				$set = 1;
				break;
			}
		}
		if ($set == 0)
		{
			echo "<input type=checkbox name=jobcatid[] value=$row[0]>$row[1]<br>";
		}
	}
	?>
    </td>
  </tr>
  
  <tr>
    <td align="right">End Date:</td>

    <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="jobid" value="<?php echo $jobid; ?>"><input type="hidden" name="confirm" value=1><input type="submit" value="Modify">&nbsp;<input type="reset" value="Clear"></td>
  </tr>
</table>
</form>
</center>
<?php
include "footer.php";
?>