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

  session_start();

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

    ?>


    <script>

      var JTable = function() {};

      JTable.Setup = function() {
        var table = $('.jtable');
  //       $('caption', table).addClass('ui-state-default');
         $('th', table).addClass('ui-state-default');
         $('td', table).toggleClass('ui-widget-content');
      };

      $(function() {
        JTable.Setup();
      });
    </script>
    
    <?php
    
     if (sizeof($results) > 0) 
     { 
 /*      echo "<table class='jtable shadetable leftmargined' cellspacing = 0 cellpadding = 2 id = 'diarytable' name = 'diarytable'>";
     //echo "<tr>";
     //echo "  <th colspan = 4 bgcolor='#5B69A6'>Statements</th>";
    // echo "</tr>";

         echo "<tr><th>Member No.</th><th>Surname</th><th>Initials</th><th>First Name</th><th>Birthday</th><th>ID Number</th><th>Sex</th><th>Dep No.</th><th>Medical Aid</th><th>Join Date</th><th>Phone 1</th><th>Phone 2</th></tr>";


     while ($row=ibase_fetch_row ($results))
       {
         echo "<tr>";
         echo "<td width = '100' align ='left'>".$row[0]."</td>"; // memberno
         echo "<td width = '100' align ='left'>".$row[1]."</td>"; // surname
         echo "<td width = '50' align ='left'>".$row[2]."</td>"; // init
         echo "<td width = '100' align ='left'>".$row[3]."</td>"; // first name
         echo "<td width = '60' align ='left'>".substr($row[4],0,10) ."</td>";  // birthday
         echo "<td width = '80' align ='left'>".$row[5]."</td>";  // idno
         echo "<td width = '60' align ='left'>".$row[6]."</td>";  // sex
         echo "<td width = '60' align ='left'>".$row[7]."</td>";  // depno
         echo "<td width = '120' align ='left'>".$row[8]."</td>";  // medical aid
         echo "<td width = '60' align ='left'>".substr($row[9],0,10)."</td>";  // join date
         echo "<td width = '60' align ='left'>".$row[10]."</td>"; // phone 1
         echo "<td width = '60' align ='left'>".$row[11]."</td>"; // phone 2

         echo "</tr>";
       } 

       echo "</table>";
*/

       $checkben  = 'F';
       $patxcount = -1;

       if ($results[0][0] != '')    
       {
            $benefits = ExecuteQuery("SELECT RET_BENEFITS FROM SP_BENEFITSCHECK('".$results[0][0]."');");
       

            if ($benefits[0][0] == 'T') 
            {
              echo "<b>This Account has benefits available !</b>";
              echo "</p>";
            }
            else
            {
              echo "<b>Please contact the Isoleso Call Centre to verify benefits for this patient !</b>";
              echo "</p>";
            }  
      
          echo "<b><u>Patient Details</u></b>";
          echo "</p>";
       }

       for($i = 0; $i< sizeof($results); $i++)
       {
       
        echo '<table class="jtable shadetable" style = "margin-left:5px"  width "100%"  border="0" align="middle">';

        echo '<tr>';
        echo ' <th width="120" align="left" scope="row">Membership No.</td>';
        echo ' <td width = 200>'.$results[$i][0].'</td>';
        echo '</tr>';
          
        echo '<tr>';
        echo ' <th align="left" scope="row">Surname</td>';
        echo ' <td>'.$results[$i][1].'</td>';
        echo '</tr>';
        
        echo '<tr>';
        echo ' <th  align="left" scope="row">Initials</td>';
        echo ' <td>'.$results[$i][2].'</td>';
        echo '</tr>';

        echo '<tr>';
        echo ' <th  align="left" scope="row">First Name</td>';
        echo ' <td>'.$results[$i][3].'</td>';
        echo '</tr>';

        echo '<tr>';
        echo ' <th  align="left" scope="row">Birthday</td>';
        echo ' <td>'.substr($results[$i][4], 0, 10).'</td>';
        echo '</tr>';

        echo '<tr>';
        echo ' <th  align="left" scope="row">ID No.</td>';
        echo ' <td>'.$results[$i][5].'</td>';
        echo '</tr>';

        echo '<tr>';
        echo ' <th  align="left" scope="row">Sex</td>';
        echo ' <td >'.$results[$i][6].'</td>';
        echo '</tr>';

        echo '<tr>';
        echo ' <th  align="left" scope="row">Dependent No.</td>';
        echo ' <td>'.$results[$i][7].'</td>';
        echo '</tr>';

        echo '<tr>';
        echo ' <th  align="left" scope="row">Medical Aid</td>';
        echo ' <td>'.$results[$i][8].'</td>';
        echo '</tr>';

        echo '<tr>';
        echo ' <th  align="left" scope="row">Join Date</td>';
        echo ' <td>'.substr($results[$i][9], 0, 10).'</td>';
        echo '</tr>';

        echo '<tr>';
        echo ' <th  align="left" scope="row">Phone 1</td>';
        echo ' <td>'.$results[$i][10].'</td>';
        echo '</tr>';

        echo '<tr>';
        echo ' <th  align="left" scope="row">Phone 2</td>';
        echo ' <td>'.$results[$i][11].'</td>';
        echo '</tr>';

        
        echo '</table>';
        echo '</p>';
       } 


     }  

  }
  
  function GetResults($surname, $idnumber, $membershipno)
  {
      $query = "SELECT NUMBER, SURNAME, INITIALS, FIRSTNAMES, BIRTHDAY, IDNUMBER, SEX, DEPNUM, MEDAID, JOINDATE, PHONE1, PHONE2
                FROM PATIENT
                WHERE SURNAME = '$surname'"; 

      if ($idnumber != '')
       $query .= " AND IDNUMBER = '$idnumber'";
     
      if ($membershipno != '')
       $query .= " AND NUMBER = '$membershipno'";

      $query .= " ORDER BY DEPNUM";

      $result = ExecuteQuery($query);
    
      return $result;
    
  }


    $surname      = $_GET['surname'];
    $idnumber     = $_GET['idnumber'];
    $membershipno = $_GET['membershipno'];
  
    $results = GetResults($surname, $idnumber, $membershipno);


    Show_Benefits($results);
  

?>
