<?php
require_once('./includes/core.php');
require_once('./includes/session.php');
if(isset($_GET['id'])) {
   $jobs_id = $_GET['id'];
   $open_spots_array = mysql_query("SELECT * FROM jobs_ranks WHERE id = '$jobs_id'");
   $open_spots = mysql_fetch_array($open_spots_array);
   if($open_spots['open_spots'] > 0){
      $user_open_spots_array = mysql_query("SELECT * FROM users WHERE name = '$user->name'");
      $user_open_spots = mysql_fetch_array($user_open_spots_array);
      mysql_query("UPDATE users SET corp_id = '$open_spots[corp_id]', job_rank = '$open_spots[job_rank]', secure_id = '$open_spots[id]' WHERE name = '$user->name'");
      mysql_query("UPDATE jobs_ranks SET open_spots = open_spots - 1 WHERE corp_id = '$open_spots[corp_id]' && job_rank = '$open_spots[job_rank]' && id = '$open_spots[id]'");
      if($user_open_spots['corp_id'] != 1 && $user_open_spots['manager_rank'] != 1) {
         mysql_query("UPDATE jobs_ranks SET open_spots = open_spots + 1 WHERE corp_id = '$user_open_spots[corp_id]' && job_rank = '$user_open_spots[job_rank]' && id = '$user_open_spots[secure_id]'");
         header("Location: jobs");
      }else{
         header("Location: jobs");
      }
   }else{
      header("Location: jobs");
   }
}else{
   header("Location: jobs");
}
?>