<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selectid") 
    $oc_id = $paramValue;  
  else if ($paramName == "addids") 
    $tn_ids = $paramValue;  
  else if ($paramName == "elements") 
    $elements = $paramValue;  
  else if ($paramName == "selvals") 
    $ttypes = $paramValue;  
  else if ($paramName == "inpvals") 
    $values = $paramValue;  
}

$element = explode (",", $elements);
$tn_id = explode (",", $tn_ids);
$ttype = explode (",", $ttypes);
$value = explode (",", $values);

$dal = new DAL();

for ($i=0; $i<count($oc_id); $i++) {
//print_r ("ele:$element[$i]");
  if ($element[$i] == "selectinput") {

    $tnj_id = hexdec(uniqid());

    $sql = "INSERT INTO telno " .
           "(value, telnotype) " .
           "VALUES ('$value[$i]', '$ttype[$i]') ";
    $tn_id = $dal->query($sql);
      
    $oct_id = hexdec(uniqid());

    $sql = "INSERT INTO organizationcontactelno " .
           "(id, organizationcontact_id, telno_id) " .
           "VALUES ($oct_id, $oc_id[$i], $tn_id[$i] ";
    $result = $dal->query($sql);
  }

  else if ($element[$i] == "select" && $ttype[$i] == -1) {
    $sql = "DELETE FROM organizationcontacttelno " .
           "WHERE id = $tn_id[$i] ";
    $result = $dal->query($sql);

    $sql = "DELETE FROM telno " .
           "WHERE id = $tn_id[$i] ";
    $result = $dal->query($sql);
  }
    
  else if ($element[$i] == "select") {
      $sql = "UPDATE telno " .
             "SET value='$value[$i]', telnotype='$ttype[$i]' " .
             "WHERE id = $tn_id ";
      $result = $dal->query($sql);
  }

}

echo json_encode($oc_id);

?>

