<?
if(!empty($auth_head))
   eval($auth_head);
else{
   echo 'You are not Authorized to use this page';
   exit;
}

if($_POST[but] == "Submit"){
   if(isset($_GET['act'])){
      $act = $_GET['act'];
   }
   else{
      unset($act);
      exit;
   }
   
   $data = $_POST;
   if(!empty($act) && $act != "add")
      $id = $data['ind'];
   $suf = ''; // compiles the SET statement
   foreach($data as $key => $value){
      if($value == "on"){
         $value = "Y";
      }
      //elseif(preg_match("/(start|end)/",$key) && empty($value))
         //$value = 'NULL';
      //   $data[$key] = fixdate($data[$key]);
      
      // ignore the submit button and the ind since we stored it in $id
      // and it shouldn't be in the SET statement
      elseif($key == "but" || $key == "ind")
         $value = '';
      else
         $value = addslashes($data[$key]);

      if(!empty($value))
         $suf .= $key . "='" . $value . "', ";
      if(empty($value) && $key != "ind" && $key != "but")
         $suf .= $key . "=NULL, ";
   } // foreach($data as $key => $value)

   $check = array('active'=>'on','hide'=>'on','nep'=>'on','so'=>'on','gb'=>'on','ssc'=>'on',
                'exec_committee'=>'on','ex_officio'=>'on','staff'=>'on','agency_contact'=>'on');

   //I need this so that I can set all the checkboxes that aren't checked to NULL (see below)
   $check = array_diff_assoc($check,$data);
   foreach($check as $key => $value){
      $suf .= $key . "=NULL, ";
   }
   
   $suf = substr($suf, 0, -2); //cuts the last ", " off

   $tbname  = TABLE_PERSONNEL;
   $primary = "id";
   $query   = "SELECT $primary FROM $tbname WHERE $primary='$id'";
   $result  = $db->sql_query($query);
   $num     = $db->sql_numrows($result);
   if(isset($act) && $act == "add"){
      if($num==0){
         $query  = "INSERT INTO $tbname SET $suf";
         //echo $query . "<br>";
         $result = $db->sql_query($query);
         if(!$result)
            $indication = "unknown error please try again";
         else
            $indication = "$title Added Successfully!";
      }
      else{
         $indication = "Contribution number $id Already Exists in the Database!
                        You must first delete the existing paper.";
      }
   }
   if(isset($act) && $act == "upd"){
      if($num == 1){
         $query  = "UPDATE $tbname SET $suf WHERE id='$id'";
         //echo $query;
         $result = $db->sql_query($query);
         if(!$result)
            $indication = "unknown error please try again";
         else
            $indication = "$title Updated Successfully!";
      }
      else{
         $indication = "Contribution number $id Does Not Exist in the Database, Please Add it First!";
      }
   }
   if(isset($act) && $act == "del"){
      if($num == 1){
         $query  = "DELETE FROM $tbname WHERE id=$id";
         $result = $db->sql_query($query);
         if(!$result)
            $indication = "unknown error please try again";
         else
            $indication = "Contact number $id Deleted Successfully!";
      }
      else{
         $indication = "Contact number $id Does Not Exist in the Database, Delete Failed!";
      }
   }
}
?>
