<?php
   include ('include_fns.php');

  session_start();


  function ShowResults($title, $list, $form)
  {

    echo "<centre>";
    echo "<table class='jsearchtable shadetable' cellspacing = 0 cellpadding = 2 border = 0 >";

    ?>
      <style>

        .ui-widget-content2 {
	       border: 1px solid #aaaaaa;
   	       color: #222222;
        }
      </style>


      <script>

        var JSearchTable = function() {};

        JSearchTable.Setup = function() {
        var table = $('.jsearchtable');
         $('caption', table).addClass('ui-state-default');
         $('th', table).addClass('ui-state-default');
         $('td', table).toggleClass('ui-widget-content2');
        };

        $(function() {
          JSearchTable.Setup();
        });
      </script>

    <?php
    
    $actions = 0;
    $items = sizeof($list);

    if($items == 0)
    {
      echo '<tr>
             <td colspan = 3 align = center>There are no Items to display !</td>
           </tr>';
    }
    else
    { 

      if (($form == 'PATIENT')||($form == 'DIARYPATIENT'))
         echo "<tr>
                 <th colspan = 2>Account</th><th>First Name</th><th>Surname</th><th>Initials</th><th>Cell No</th><th>Birthday</th><th>ID No.</th><th>File Ref.</th><th>Select</th>
               </tr>";
      else
        echo "<tr><th>Key</th><th>Description</th><th>Select</th></tr>";

      // print each row 
      for($i = 0; $i<$items; $i++)
      {
          if($i%2) // background colors alternate
            $bgcolor = "'#ffffff'";
        else
          $bgcolor = "'#fafafa'";

        echo "<tr>";
        
        if (($form == 'PATIENT')||($form == 'DIARYPATIENT'))
        {
          for ($j = 2; $j < 9; $j++)
            if  (! $list[$i][$j])
              $list[$i][$j] = '&nbsp';

          echo "<td bgcolor = $bgcolor height = 30 id = 'key' name = 'key' class = 'key'>". $list[$i][0]."</td>";
          echo "<td bgcolor = $bgcolor height = 30 id = 'key2' name = 'key2' class = 'key2'>". $list[$i][1]."</td>";
          echo "<td bgcolor = $bgcolor height = 30 id = 'firstname' name = 'firstname' class = 'firstname'>". $list[$i][2]."</td>";
          echo "<td bgcolor = $bgcolor height = 30 id = 'surname' name = 'surname' class = 'surname'>". $list[$i][3]."</td>";

          for ($j = 4; $j < 9; $j++)
            echo "<td bgcolor = $bgcolor height = 30 class = 'desc'>". $list[$i][$j]."</td>";

          echo "<td height = 30 id = 'select' name = 'select' class = 'select'><button class='btnSelect'>Select</button></td>";

        }
        else
        {
          echo "<td bgcolor = $bgcolor height = 30 id = 'key' name = 'key' class = 'key'>". $list[$i][1]."</td>";
          echo "<td bgcolor = $bgcolor height = 30 id = 'desc' name = 'desc' class = 'desc'>". $list[$i][0]."</td>";
          echo "<td height = 30 id = 'select' name = 'select' class = 'select'><button class='btnSelect'>Select</button></td>";
        }
        ?>

        <script>
        

          $('.btnSelect').button({ icons: { primary: 'ui-icon-check'} }).click(function(){


             var SearchType = '<?php echo $form ?>';

             if ((SearchType == 'PATIENT')||(SearchType == 'DIARYPATIENT'))
             {
               var searchkey  = encodeURIComponent($(this).closest('tr').find('.key').text());
               $('#selectedkey').val(searchkey);

               var searchkey2 = encodeURIComponent($(this).closest('tr').find('.key2').text());
               $('#selectedkey2').val(searchkey2);

               var FirstName = encodeURIComponent($(this).closest('tr').find('.firstname').text());
               var Surname   = encodeURIComponent($(this).closest('tr').find('.surname').text());
               $('#selecteddesc').val(FirstName + ' '+ Surname);
             }
             else
             {
               var searchkey  = encodeURIComponent($(this).closest('tr').find('.key').text());
               var searchdesc = encodeURIComponent($(this).closest('tr').find('.desc').text());

               $('#selectedkey').val(searchkey);
               $('#selecteddesc').val(searchdesc);
             }


             var SearchDialog;
             
             SearchDialog = '#' + SearchType + 'searchscreen';

             $(SearchDialog).dialog( "close" );

          });
        </script>

        <?php
        
        echo "</tr>";
      }
      echo '</table>';
      echo '</centre>';

      echo '<input type="hidden" name="selectedkey"  id="selectedkey">';
      echo '<input type="hidden" name="selectedkey2" id="selectedkey2">';
      echo '<input type="hidden" name="selecteddesc" id="selecteddesc">';

    } 
 }
 
 
  function GetResults($DisplayFields, $Table, $Field, $Criteria, $Value, $OrderBy, $Form)
  {

    $list = array();  

    if($conn=db_connect())
    {
      $Value = strtoupper($Value);
      
      if (($Form == 'DIARYPATIENT') || ($Form == 'PATIENT'))
        $Table = 'PATIENT LEFT OUTER JOIN ACCOUNT ON ACCOUNT.ACCOUNT_KEY = PATIENT.ACCOUNT_KEY'; // TO CATER FOR CASENO ON ACCOUNT TABLE
     
      if ($Form == 'DIARYPATIENT') 
      {
        if ($Criteria == 'like') 
          $query = "select $DisplayFields from $Table where hide = 'F' and upper($Field) like '%$Value%' order by $OrderBy";
        else
          $query = "select $DisplayFields from $Table where hide = 'F' and upper($Field) $Criteria '$Value' order by $OrderBy";
        
      } 
      else
      { 
        if ($Criteria == 'like') 
          $query = "select $DisplayFields from $Table where upper($Field) like '%$Value%' order by $OrderBy";
        else
          $query = "select $DisplayFields from $Table where upper($Field) $Criteria '$Value' order by $OrderBy";
      }  

      $result = ibase_query($conn,$query);
      if(!$result)
      {
        echo '<p>Unable to get data from database.</p>';
        return false;
      }
      else
      {
          $numcol = ibase_num_fields($result);
          $i = 0;
         
          while ($row=ibase_fetch_row ($result))
          {
            $list[$i][0] = '';
                         
            if (($Form == 'PATIENT')||($Form == 'DIARYPATIENT'))
            {
                for($j=0; $j <$numcol; $j++)
                  $list[$i][$j] = $row[$j]; // assign display fields

            }
            else
            {
                $list[$i][1] =$row[0]; // assign key  

                for($j=1; $j <$numcol; $j++)
                  $list[$i][0] = $list[$i][0].' '.$row[$j]; // assign display fields

             }
            $i++;
          } 
      }

      ibase_close($conn);
    }
    return $list;
  } 


  if(!check_admin_user())
  {
   echo "You do not seem to be logged in.";
   exit;
  }
  

  $SearchDisplayFields = $_GET['DisplayFields'];
  $SearchTable         = $_GET['Table'];
  $SearchForm          = $_GET['Form'];
  $SearchFields        = $_GET['Fields'];
  $SearchCriteria      = $_GET['Criteria'];
  $SearchValue         = $_GET['Value'];
  $SearchOrderBy       = $_GET['OrderBy'];
  
  if ($SearchForm == 'DIARYPATIENT')
  {
    $_SESSION['DIARYPATIENTFields']   = $SearchFields;
    $_SESSION['DIARYPATIENTCriteria'] = $SearchCriteria;
    $_SESSION['DIARYPATIENTValue']    = $SearchValue;
    $_SESSION['DIARYPATIENTOrderBy']  = $SearchOrderBy;
  }
  else
  if ($SearchForm == 'PATIENT')
  {
    $_SESSION['PATIENTFields']   = $SearchFields;
    $_SESSION['PATIENTCriteria'] = $SearchCriteria;
    $_SESSION['PATIENTValue']    = $SearchValue;
    $_SESSION['PATIENTOrderBy']  = $SearchOrderBy;
  }
  else
  if ($SearchForm == 'MEDICALAID')
  {
    $_SESSION['MEDICALAIDFields']   = $SearchFields;
    $_SESSION['MEDICALAIDCriteria'] = $SearchCriteria;
    $_SESSION['MEDICALAIDValue']    = $SearchValue;
    $_SESSION['MEDICALAIDOrderBy']  = $SearchOrderBy;
  }
  else
  if ($SearchForm == 'TARIFFOPTOM')
  {
    $_SESSION['TARIFFOPTOMFields']   = $SearchFields;
    $_SESSION['TARIFFOPTOMCriteria'] = $SearchCriteria;
    $_SESSION['TARIFFOPTOMValue']    = $SearchValue;
    $_SESSION['TARIFFOPTOMOrderBy']  = $SearchOrderBy;
  }
  else
  if ($SearchForm == 'STOCKOPTOM')
  {
    $_SESSION['STOCKOPTOMFields']   = $SearchFields;
    $_SESSION['STOCKOPTOMCriteria'] = $SearchCriteria;
    $_SESSION['STOCKOPTOMValue']    = $SearchValue;
    $_SESSION['STOCKOPTOMOrderBy']  = $SearchOrderBy;
  }
  else
  if ($SearchForm == 'TARIFFTABLE')
  {
    $_SESSION['TARIFFTABLEFields']   = $SearchFields;
    $_SESSION['TARIFFTABLECriteria'] = $SearchCriteria;
    $_SESSION['TARIFFTABLEValue']    = $SearchValue;
    $_SESSION['TARIFFTABLEOrderBy']  = $SearchOrderBy;
  }
  else
  if ($SearchForm == 'STOCKMED')
  {
    $_SESSION['STOCKMEDFields']   = $SearchFields;
    $_SESSION['STOCKMEDCriteria'] = $SearchCriteria;
    $_SESSION['STOCKMEDValue']    = $SearchValue;
    $_SESSION['STOCKMEDOrderBy']  = $SearchOrderBy;
  }
  else
  if ($SearchForm == 'DIAG1')
  {
    $_SESSION['DIAG1Fields']   = $SearchFields;
    $_SESSION['DIAG1Criteria'] = $SearchCriteria;
    $_SESSION['DIAG1Value']    = $SearchValue;
    $_SESSION['DIAG1OrderBy']  = $SearchOrderBy;
  }
  else
  if ($SearchForm == 'DIAG2')
  {
    $_SESSION['DIAG2Fields']   = $SearchFields;
    $_SESSION['DIAG2Criteria'] = $SearchCriteria;
    $_SESSION['DIAG2Value']    = $SearchValue;
    $_SESSION['DIAG2OrderBy']  = $SearchOrderBy;
  }
  else
  if ($SearchForm == 'DIAG3')
  {
    $_SESSION['DIAG3Fields']   = $SearchFields;
    $_SESSION['DIAG3Criteria'] = $SearchCriteria;
    $_SESSION['DIAG3Value']    = $SearchValue;
    $_SESSION['DIAG3OrderBy']  = $SearchOrderBy;
  }
  else
  if ($SearchForm == 'DIAG4')
  {
    $_SESSION['DIAG4Fields']   = $SearchFields;
    $_SESSION['DIAG4Criteria'] = $SearchCriteria;
    $_SESSION['DIAG4Value']    = $SearchValue;
    $_SESSION['DIAG4OrderBy']  = $SearchOrderBy;
  }
  else
  if ($SearchForm == 'DIAG5')
  {
    $_SESSION['DIAG5Fields']   = $SearchFields;
    $_SESSION['DIAG5Criteria'] = $SearchCriteria;
    $_SESSION['DIAG5Value']    = $SearchValue;
    $_SESSION['DIAG5OrderBy']  = $SearchOrderBy;
  }

  $results = GetResults($SearchDisplayFields, $SearchTable, $SearchFields, $SearchCriteria, $SearchValue, $SearchOrderBy,
                                 $SearchForm); 
  
      
  ShowResults('Lookup', $results, $SearchForm);


?>  
