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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "summaryid")
    $summaryId = $paramValue;
  if ($paramName == "selectid")
    $selectId = $paramValue;
  else if ($paramName == "field")
    $field = $paramValue;
}

$dal = new DAL();

if ($field == "fkyInstrumentGroup") {

  if ($summaryId > 0) {
    $sql = "SELECT 0 as id, 'Select' AS text " .
           "UNION ALL ";
  }    

  $dal = new DAL();
  $sql .= "SELECT id, name AS text " .
          "FROM instrumentgroup " .
          "WHERE $summaryId ";
  $result = $dal->query($sql);
}         

else if ($field == "fkyInstrumentClass") {
  $_SESSION["instrumentgroup"] = $summaryId;
  $_SESSION["instrumentclass"] = 0;
  $_SESSION["instrumenttype"] = 0;

  if ($summaryId > 0)  {
    $sql = "SELECT 0 as id, 'Select' AS text " .
           "UNION ALL ";
  }
  
  $dal = new DAL();
  $sql .= "SELECT id, name AS text " .
          "FROM instrumentclass " .
          "WHERE group_id = $summaryId ";
  $result = $dal->query($sql);
}         

else if ($field == "fkyInstrumentType") {

  $_SESSION["instrumentclass"] = $summaryId;
  $_SESSION["instrumenttype"] = 0;

  if ($summaryId > 0)  {
    $sql = "SELECT 0 as id, 'Select' AS text " .
             "UNION ALL ";
  }
  
  $dal = new DAL();
  $sql .= "SELECT id, name AS text " .
          "FROM instrumenttype " .
          "WHERE class_id = $summaryId " ;
  $result = $dal->query($sql);
}         

echo json_encode ($result);

?>

