<?php

  include ('include_fns.php');

  session_start();

  $action       = $_GET['action'];
  $empty        = $_GET['id'];
  $patienterror = $_GET['error'];
 
    
  if(!check_admin_user())
  {
   echo "You do not seem to be logged in.";
   exit;
  }
  
  function display_search_form($Table, $Fields, $OrderBy, $DisplayFields,$Form, $Show)
  {
    ?>
    <center>
    <table border = 0 cellpadding = 6 cellspacing = 0 width = 100% class="shadetable">

     <tr>
     <td>
     <div id="toolbar" class="ui-widget-header ui-corner-all">
        <table width = 100%>
          <tr>
            <td align="left" width = "40">Search Field:</td>
            <td align="left" width = "30">Criteria:</td>
            <td align="left" width = 100%>Value:</td>
            <td align="left" width = "30">Order By:</td>
          </tr>

          <tr>
            <td width = "40">


        <select size="1" name= "Fields" id ="Fields" class="roundedit">

          <?php

            for($i = 0; $i<(sizeof($Fields)); $i++)
            {
               if ($_SESSION[$Form.'Fields'] == $Fields[$i][0])
                   echo '<option selected value="'.$Fields[$i][0].'">'.$Fields[$i][1].'</option>';
               else
                   echo '<option value="'.$Fields[$i][0].'">'.$Fields[$i][1].'</option>';
            }
          ?>

        </select>
        </td>

        <td width = "30">

        <select size="1" name= "Criteria" id="Criteria" class="roundedit">

          <?php
            if ($_SESSION[$Form.'Criteria'] == '=')
            {
              echo '<option selected value = "=">is equal to</option>';
              echo '<option value = "starting with">starts with</option>';
              echo '<option value = "like">contains</option>';
           }
            else
            if ($_SESSION[$Form.'Criteria'] == 'starting with')
            {
              echo '<option value = "=">is equal to</option>';
              echo '<option selected value = "starting with">starts with</option>';
              echo '<option value = "like">contains</option>';
            }
            else
            if ($_SESSION[$Form.'Criteria'] == 'like')
            {
              echo '<option value = "=">is equal to</option>';
              echo '<option value = "starting with">starts with</option>';
              echo '<option selected value = "like">contains</option>';
            }
            else
            {
              echo '<option value = "=">is equal to</option>';
              echo '<option value = "starting with">starts with</option>';
              echo '<option value = "like">contains</option>';
            }

          ?>

        </select></td>


     <td width = 100%><input type="text" name= "Value" id="Value" class="roundedit" style='width:100%' value = <?php echo '"'.$_SESSION[$Form.'Value'].'"' ?> ></td>

     <td width = "30">
        <select size="1" name= "OrderBy" id="OrderBy" class="roundedit">

        <?php
        for($i = 0; $i<(sizeof($OrderBy)); $i++)
        {
          if ($_SESSION[$Form.'OrderBy'] == $OrderBy[$i][0])
            echo '<option selected value="'.$OrderBy[$i][0].'">'.$OrderBy[$i][1].'</option>';
          else
            echo '<option value="'.$OrderBy[$i][0].'">'.$OrderBy[$i][1].'</option>';
        }
        ?>
        </select>
      </td>

      <td>
        <button id="btnSearchScreen">Lookup</button>

        <script>

          $('#btnSearchScreen').button({ icons: { primary: 'ui-icon-search'} }).click(function(){

           var Fields        = $('#Fields option:selected').val();
           var Criteria      = encodeURIComponent($('#Criteria option:selected').val());
           var Value         = $('#Value').val();
           var OrderBy       = $('#OrderBy option:selected').val();
           var Form          = $('#Form').val();
           var DisplayFields = $('#DisplayFields').val();
           var Table         = $('#Table').val();
           
           $.blockUI({ message: null });
           $('#divSearchScreen').html('');
           $("#divSearchScreen").load("searchresults.php?Form=" + Form + "&DisplayFields="+encodeURIComponent(DisplayFields) + "&Table="+ Table + "&Fields=" + Fields + "&Criteria=" + Criteria + "&Value=" + encodeURIComponent(Value) + "&OrderBy=" + OrderBy);
           setTimeout($.unblockUI, 2000);

          });


          $(document).keypress(function(e)
          {
            if (e.which == 13)
             {
               var Fields        = $('#Fields option:selected').val();
               var Criteria      = encodeURIComponent($('#Criteria option:selected').val());
               var Value         = $('#Value').val();
               var OrderBy       = $('#OrderBy option:selected').val();
               var Form          = $('#Form').val();
               var DisplayFields = $('#DisplayFields').val();
               var Table         = $('#Table').val();

               $.blockUI({ message: null });

               $('#divSearchScreen').html('');
               $("#divSearchScreen").load("searchresults.php?Form=" + Form + "&DisplayFields="+encodeURIComponent(DisplayFields) + "&Table="+ Table + "&Fields=" + Fields + "&Criteria=" + Criteria + "&Value=" + encodeURIComponent(Value) + "&OrderBy=" + OrderBy);
               setTimeout($.unblockUI, 2000);

             }
          });

        </script>

      </td>
      </tr>

   

      <input type="hidden" name="DisplayFields" id="DisplayFields" value = '<?php echo $DisplayFields ?>'>
      <input type="hidden" name="Table" id = "Table" value = '<?php echo $Table ?>'>
      <input type="hidden" name="Form" id = "Form" value = '<?php echo $Form ?>'>
      
      </table>
   </div>
   </td>
   </tr>
   </table>
   </center>
    <p></p>
    <div id="divSearchScreen">
  <?php
}


    switch ($action)
    {
      case 'patient':
      {
        $PatientFields = array();
        $PatientFields[0][0] = 'Surname';
        $PatientFields[0][1] = 'Surname';
        $PatientFields[1][0] = 'Account_Key';
        $PatientFields[1][1] = 'Account';
        $PatientFields[2][0] = 'Firstname';
        $PatientFields[2][1] = 'First Name';
        $PatientFields[3][0] = 'IDNumber';
        $PatientFields[3][1] = 'ID Number';
        $PatientFields[4][0] = 'CaseNo';
        $PatientFields[4][1] = 'Case No.';
        


        $PatientOrderBy = array();
        $PatientOrderBy[0][0] = 'Surname';
        $PatientOrderBy[0][1] = 'Surname';
        $PatientOrderBy[1][0] = 'Account_Key';
        $PatientOrderBy[1][1] = 'Account';
        $PatientOrderBy[2][0] = 'Firstname';
        $PatientOrderBy[2][1] = 'First Name';
        $PatientOrderBy[3][0] = 'IDNumber';
        $PatientOrderBy[3][1] = 'ID Number';
        $PatientOrderBy[4][0] = 'CaseNo';
        $PatientOrderBy[4][1] = 'Case No.';
        
        $PatientDisplayFields = 'Account_Key, Patient_key, FirstName, Surname, Initials, Cell, Birthday, IDNumber, CaseNo';

        if ($empty == 'empty') 
          echo '<b> No records found ! </b>';

        display_search_form('Patient', $PatientFields, $PatientOrderBy, $PatientDisplayFields, 'PATIENT', 'Patient');

        unset($PatientFields);
        unset($PatientOrderBy);

        break;
      }
      case 'diarypatient':
      {
        $DiaryPatientFields = array();
        $DiaryPatientFields[0][0] = 'Surname';
        $DiaryPatientFields[0][1] = 'Surname';
        $DiaryPatientFields[1][0] = 'Account_Key';
        $DiaryPatientFields[1][1] = 'Account';
        $DiaryPatientFields[2][0] = 'Firstname';
        $DiaryPatientFields[2][1] = 'First Name';
        $DiaryPatientFields[3][0] = 'IDNumber';
        $DiaryPatientFields[3][1] = 'ID Number';
        $DiaryPatientFields[4][0] = 'CaseNo';
        $DiaryPatientFields[4][1] = 'Case No.';



        $DiaryPatientOrderBy = array();
        $DiaryPatientOrderBy[0][0] = 'Surname';
        $DiaryPatientOrderBy[0][1] = 'Surname';
        $DiaryPatientOrderBy[1][0] = 'Account_Key';
        $DiaryPatientOrderBy[1][1] = 'Account';
        $DiaryPatientOrderBy[2][0] = 'Firstname';
        $DiaryPatientOrderBy[2][1] = 'First Name';
        $DiaryPatientOrderBy[3][0] = 'IDNumber';
        $DiaryPatientOrderBy[3][1] = 'ID Number';
        $DiaryPatientOrderBy[4][0] = 'CaseNo';
        $DiaryPatientOrderBy[4][1] = 'Case No.';
        


        $DiaryPatientDisplayFields = 'Account_Key, Patient_key, FirstName, Surname, Initials, Cell, Birthday, IDNumber, CaseNo';

        if ($empty == 'empty')
          echo '<b> No records found ! </b>';

        display_search_form('Patient', $DiaryPatientFields, $DiaryPatientOrderBy, $DiaryPatientDisplayFields, 'DIARYPATIENT', 'Patient');

        unset($DiaryPatientFields);
        unset($DiaryPatientOrderBy);

        break;
      }
      case 'medicalaid':
      {
        $MedicalAidFields = array();
        $MedicalAidFields[0][0] = 'MedicalAid_Key';
        $MedicalAidFields[0][1] = 'Code';
        $MedicalAidFields[1][0] = 'Name';
        $MedicalAidFields[1][1] = 'Name';


        $MedicalAidOrderBy = array();
        $MedicalAidOrderBy[0][0] = 'MedicalAid_Key';
        $MedicalAidOrderBy[0][1] = 'Code';
        $MedicalAidOrderBy[1][0] = 'Name';
        $MedicalAidOrderBy[1][1] = 'Name';
        
        $MedicalAidDisplayFields = 'MedicalAid_Key, Name';

        if ($empty == 'empty') 
          echo '<b> No records found ! </b>';

        display_search_form('MedicalAid', $MedicalAidFields, $MedicalAidOrderBy, $MedicalAidDisplayFields, 'MEDICALAID', 'Medical Aid');

        unset($MedicalAidFields);
        unset($MedicalAidOrderBy);  

        break;
      }
      case 'tariff':
      {
        $TariffFields = array();
        $TariffFields[0][0] = 'TariffOptom_Key';
        $TariffFields[0][1] = 'Code';
        $TariffFields[1][0] = 'English';
        $TariffFields[1][1] = 'English';
        $TariffFields[2][0] = 'Afrikaans';
        $TariffFields[2][1] = 'Afrikaans';


        $TariffOrderBy = array();
        $TariffOrderBy[0][0] = 'TariffOptom_Key';
        $TariffOrderBy[0][1] = 'Code';
        $TariffOrderBy[1][0] = 'English';
        $TariffOrderBy[1][1] = 'English';
        $TariffOrderBy[2][0] = 'Afrikaans';
        $TariffOrderBy[2][1] = 'Afrikaans';
        
        $TariffDisplayFields = 'TariffOptom_Key, English';

        if ($empty == 'empty') 
          echo '<b> No records found ! </b>';

        display_search_form('TariffOptom', $TariffFields, $TariffOrderBy, $TariffDisplayFields, 'TARIFFOPTOM', 'Tariff');
          
        unset($TariffFields);
        unset($TariffOrderBy);  
        break;
      }
      case 'stock':
      {
        $StockFields = array();
        $StockFields[0][0] = 'StockOptom_Key';
        $StockFields[0][1] = 'Code';
        $StockFields[1][0] = 'StockOptom_Desc';
        $StockFields[1][1] = 'Description';


        $StockOrderBy = array();
        $StockOrderBy[0][0] = 'StockOptom_Key';
        $StockOrderBy[0][1] = 'Code';
        $StockOrderBy[1][0] = 'StockOptom_Desc';
        $StockOrderBy[1][1] = 'Description';
        
        $StockDisplayFields = 'StockOptom_Key, StockOptom_Desc';

        if ($empty == 'empty') 
          echo '<b> No records found ! </b>';

        display_search_form('StockOptom', $StockFields, $StockOrderBy, $StockDisplayFields, 'STOCKOPTOM', 'Stock');

        unset($StockFields);
        unset($StockOrderBy);  

        break;
      }
      case 'tarifftable':
      {
        $TariffFields = array();
        $TariffFields[0][0] = 'TariffTable_Key';
        $TariffFields[0][1] = 'Code';
        $TariffFields[1][0] = 'English';
        $TariffFields[1][1] = 'English';
        $TariffFields[2][0] = 'Afrikaans';
        $TariffFields[2][1] = 'Afrikaans';


        $TariffOrderBy = array();
        $TariffOrderBy[0][0] = 'TariffTable_Key';
        $TariffOrderBy[0][1] = 'Code';
        $TariffOrderBy[1][0] = 'English';
        $TariffOrderBy[1][1] = 'English';
        $TariffOrderBy[2][0] = 'Afrikaans';
        $TariffOrderBy[2][1] = 'Afrikaans';
        
        $TariffDisplayFields = 'TariffTable_Key, English';

        if ($empty == 'empty') 
          echo '<b> No records found ! </b>';

        display_search_form('TariffTable', $TariffFields, $TariffOrderBy, $TariffDisplayFields, 'TARIFFTABLE', 'Tariff');
          
        unset($TariffFields);
        unset($TariffOrderBy);  
        break;
      }
      case 'stockmed':
      {
        $StockFields = array();
        $StockFields[0][0] = 'StockMed_Key';
        $StockFields[0][1] = 'Code';
        $StockFields[1][0] = 'English';
        $StockFields[1][1] = 'Description';


        $StockOrderBy = array();
        $StockOrderBy[0][0] = 'StockMed_Key';
        $StockOrderBy[0][1] = 'Code';
        $StockOrderBy[1][0] = 'English';
        $StockOrderBy[1][1] = 'Description';
        
        $StockDisplayFields = 'StockMed_Key, English';

        if ($empty == 'empty') 
          echo '<b> No records found ! </b>';

        display_search_form('StockMed', $StockFields, $StockOrderBy, $StockDisplayFields, 'STOCKMED', 'Stock');

        unset($StockFields);
        unset($StockOrderBy);  

        break;
      }
      case 'diag1': 
      case 'diag2':
      case 'diag3':
      case 'diag4':
      case 'diag5':
      {
        $DiagnosisFields = array();
        $DiagnosisFields[0][0] = 'Code';
        $DiagnosisFields[0][1] = 'Code';
        $DiagnosisFields[1][0] = 'Description';
        $DiagnosisFields[1][1] = 'Description';


        $DiagnosisOrderBy = array();
        $DiagnosisOrderBy[0][0] = 'Code';
        $DiagnosisOrderBy[0][1] = 'Code';
        $DiagnosisOrderBy[1][0] = 'Description';
        $DiagnosisOrderBy[1][1] = 'Description';

        $DiagnosisDisplayFields = 'Code, Description';

        if ($empty == 'empty') 
          echo '<b> No records found ! </b>';

        display_search_form('Items', $DiagnosisFields, $DiagnosisOrderBy, $DiagnosisDisplayFields, strtoupper($action), 'Diagnosis');

        unset($DiagnosisFields);
        unset($DiagnosisOrderBy);  

        break;
      }

    }

?> 
