<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');
 
foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id") 
    $ad_id = $paramValue; 
  else if ($paramName == "address_id") 
    $address_id = $paramValue; 
  else if ($paramName == "organizationcontact_id") 
    $oc_id = $paramValue; 
  else if ($paramName == "suite") 
    $suite = $paramValue; 
  else if ($paramName == "addresstype_id") 
    $addresstype_id = $paramValue; 
  else if ($paramName == "careof") 
    $careof = $paramValue; 
  else if ($paramName == "pobox") 
    $pobox = $paramValue; 
  else if ($paramName == "station") 
    $station = $paramValue; 
  else if ($paramName == "streetno") 
    $streetno = $paramValue; 
  else if ($paramName == "streetname") 
    $streetname = $paramValue; 
  else if ($paramName == "streetype") 
    $streetype = $paramValue; 
  else if ($paramName == "streetdir") 
    $streetdir = $paramValue; 
  else if ($paramName == "ruralroute") 
    $ruralroute = $paramValue; 
  else if ($paramName == "city") 
    $city = $paramValue; 
  else if ($paramName == "province") 
    $province = $paramValue; 
  else if ($paramName == "postcode") 
    $postcode = $paramValue; 
  else if ($paramName == "country") 
    $country = $paramValue; 
}

$today = date("Y-m-d");
$dal = new DAL();



if ($ad_id == 0) {
    
  $ad_id = hexdec(uniqid());

  $sql = "INSERT INTO address " .
         "(id, department, suite, careof, pobox, " .
         "station, streetno, streetname, streetype, streetdir, " .
         "ruralroute, city, province, postcode, country, note) " .
         "VALUES " .
         "($ad_id, '$department', '$suite', '$careof', '$pobox', " .
         "'$station', '$streetno', '$streetname', '$streetype', '$streetdir', " .
         "'$ruralroute', '$city', '$province', '$postcode', '$country', '$note') ";
  $result = $dal->query($sql);

  $oca_id = hexdec(uniqid());

  $sql = "INSERT INTO organizationcontactaddress " .
         "(id, organizationcontact_id, address_id, addresstype_id) " .
         "VALUES " .
         "($oca_id, $oc_id, $ad_id, $addresstype_id) ";
  $result = $dal->query($sql);
}

else {

  $sql = "UPDATE address SET " .
         "suite='$suite', careof='$careof', " .
         "pobox='$pobox', station='$station', streetno='$streetno', " .
         "streetname='$streetname', streetype='$streetype', streetdir='$streetdir', " .
         "ruralroute='$ruralroute', city='$city', province='$province', " .
         "postcode='$postcode', country='$country', note='$note' " .
         "WHERE id = $ad_id ";
  $result = $dal->query($sql);
}  

echo json_encode($ad_id);

?>

