<div class="module">Teilnehmer suchen</div>
<?
$searchfields = array('versnummer', 'rechtskreis', 'anrede', 'titel', 'name', 'vorname', 'strasse', 'plz', 'ort','land', 'geburtsdatum', 'email','deleted');
if (!isset($state)) {
  $state = '';
}
if ($state == "submitted" || $state == "submitdoublettes" || $state == 'submitdoublekvnrneu' || $state == 'submitmissingkvnrneu' || $state == 'submitsearchrausfaller' )
{
    if ($state == "submitted") {
        if ($_SESSION['searchuser'] && (!$sortfield))
        {
            /* wenn Session gesetzt ist */
            foreach ($searchfields as $searchfield)
            {
                $$searchfield = $_SESSION[$searchfield];
            }
            $from = $_SESSION['from'];
            $sortfield = $_SESSION['sortfield'];
            $dir = $_SESSION['dir'];
        }

        if (!isset($from) or $from == '') ($from = 0);
        /* create user template to match */
        $matchUser = new user();
        if (isset($u18)) {
            $matchUser->u18 = $u18;
        }

        foreach ($searchfields as $searchfield)
        {
            if (isset($$searchfield)) {
            $matchUser->$searchfield = $$searchfield;
            }
        }
#        print_r($matchUser);

        /* Trage matchdaten fuer spaetere Verwendung in die Session ein */

        foreach ($searchfields as $searchfield) {
            if (isset($$searchfield)) {
              $_SESSION[$searchfield] = $$searchfield;
            } else {
              $_SESSION[$searchfield] = '';
            }
        }

        /* get matching users */
        if (!isset($sortfield)) {
          $sortfield = '';
        }
        if (!isset($dir)) {
          $dir = '';
        }
        $users = $matchUser->getUsers($sortfield, $dir,1);
    } 
    if ($state == 'submitdoublettes')
    {
        $doublettes = checkdoublettes($sortfield, $dir);
        $filename =exportdoublettes();
        $users = array();
        foreach ($doublettes as $doublette)
        {
            array_push($users, $doublette['userId']);
        }
        if (!$from or $from == '') ($from = 0);
    }

    if ($state == 'submitdoublekvnrneu')
    {
        $lookupuser = new user();
        $doublettes = $lookupuser->fetchdoublekvnrneu();
//        $filename =exportdoublettes();
        $users = array();
        foreach ($doublettes as $doublette)
        {
            array_push($users, $doublette->userId);
        }
        if (!$from or $from == '') ($from = 0);
    }

    if ($state == 'submitmissingkvnrneu')
    {
        $lookupuser = new user();
        $doublettes = $lookupuser->fetchmissingkvnrneu();
//        $filename =exportdoublettes();
        $users = array();
        foreach ($doublettes as $doublette)
        {
            array_push($users, $doublette->userId);
        }
        if (!$from or $from == '') ($from = 0);
    }

    if ($state == 'submitsearchrausfaller')
    {
        $lookupuser = new user();
        $doublettes = $lookupuser->fetchmissingmitglieder();
//        $filename =exportdoublettes();
        $users = array();
        foreach ($doublettes as $doublette)
        {
            array_push($users, $doublette->userId);
        }
        if (!$from or $from == '') ($from = 0);
    }

    if (isset($from)) {
        $_SESSION['from'] = $from;
    }
    if (isset($sortfield)) {
        $_SESSION['sortfield'] = $sortfield;
    }
    if (isset($dir)) {
        $_SESSION['dir'] = $dir;
    }


    if (count($users) == 0)
    { 
            echo "Kann Teilnehmer nicht finden<br>\n";
            $state="";
    } else {

       if (!$sortfield) $sortfield = "erstellungsdatum";

#       if (count($users) < $maxLinesPerTable) {
#           $maxLinesPerTable = count($users);
#       }

       $to = $from + $maxLinesPerTable;
       if (count($users) < $to) { $to = count($users); }

?>
<? 
if (isset($filename)) {
/* Downloadfeld anzeigen */ ?>
 <form name="filename" action="#" method="POST" onsubmit="return submitdownload()"  >
   <input type="hidden" name="module" value="<?=$module?>" />
   <input type="hidden" name="action" value="<?=$action?>" />
   <input type="hidden" name="state" value="download" />
   <fieldset class="fieldset"><legend>CSV-Datei herunterladen</legend>
   <table>
     <tr>
       <td>Dateiname</td>
       <td><input class="inputgen" size="40" type="text" name="downloadfilename" value="<?= $filename?>" /></td>
     </tr>
     <tr>
       <td>&nbsp;</td>
       <td><input class="submit" title="Laden sie hiermit eine Datei mit dem Suchergebnis als Excel-Datei herunter" type="submit" name="submit" value="Herunterladen" /></td>
   </table>
   </fieldset>
 </form>
<? /*---------------------------------*/ ?>
     <script language="JavaScript">
      document.forms.filename.downloadfilename.focus();
       function submitdownload()
       {
           downloadfile = document.forms.filename.downloadfilename.value;
           newhref='downloadcsv.php?downloadfile=' + downloadfile ;

           document.location.href=newhref;
           return false;
       }
    </script>
<?
}
?>
     <script language="JavaScript">
       function formsubmit(name)
       {
    /* first change sorting order */
           if (document.forms.usertable.sortfield.value=='<?=$sortfield?>')
           {
               if (document.forms.usertable.dir.value == 'ASC')
               {
                   document.forms.usertable.dir.value = 'DESC';
               } else {
                   document.forms.usertable.dir.value = 'ASC';
               }
           }

           document.forms.usertable.sortfield.value=name;
           document.forms.usertable.submit();
       }

       function formstart() {
           document.forms.usertable.from.value='0';
           document.forms.usertable.submit();
       }

       function formback(start) {
           document.forms.usertable.from.value=start;
           document.forms.usertable.submit();
       }

       function formforward(start) {
           document.forms.usertable.from.value=start;
           document.forms.usertable.submit();
       }


     </script>
     <form name="usertable" action="<?=$_SERVER['REQUEST_URI']?>" action="POST" width="650">
    <input type="hidden" name="module" value="<?=$module?>" />
       <input type="hidden" name="action" value="<?=$action?>" />
       <input type="hidden" name="state" value="<?=$state?>" />
<?
    foreach ($searchfields as $searchfield)
    {
?>
       <input type="hidden" name="<?=$searchfield?>" value="<?=$$searchfield?>" />
<?
    }
?>
       <input type="hidden" name="sortfield" value="<?=$sortfield?>" />
       <input type="hidden" name="dir" value="<?=$dir?>" />
       <input type="hidden" name="from" value="<?=$from?>" />
       <p>
       Ergebnisse: <?=$from +1?> bis <?=$from + (count($users)<$maxLinesPerTable)?count($users):$maxLinesPerTable?> von <strong><?=count($users)?></strong>, sortiert nach: <?=$sortfield?><br>
       <? if ($from >= 1) { ?><a href="javascript:formstart()">&lt;&lt;</a> | <?  } else { echo "&lt;&lt | ";} ?>
       <? if ($from >= 1) { ?><a href="javascript:formback(<?=$from - $maxLinesPerTable?>)">&lt;</a> | <?  } else { echo "&lt | ";} ?>
       <? if ($from < (count($users) - $maxLinesPerTable -1)) { ?><a href="javascript:formforward(<?=$from + $maxLinesPerTable?>)">&gt;</a><?  } else { echo "&gt | ";} ?>
       <? if ($from < count($users) - $maxLinesPerTable) { ?> | <a href="javascript:formforward(<?=round(count($users)/$maxLinesPerTable - 0.5)*$maxLinesPerTable?>)">&gt;&gt;</a><?  } else { echo "&gt&gt  ";} ?> 
       </p>
       <script language="JavaScript">
         var background;
         function highlight(id)
         {
             row = document.getElementById(id);
             background = row.style.backgroundColor;
             row.style.backgroundColor = '#ffffff';
             row.style.cursor = 'hand';
         }
         function lowlight(id)
         {
             row = document.getElementById(id);
             row.style.backgroundColor = background;

         }
       </script>
       
       <table border="0" cellspacing="1" cellpadding="3" style="border: 1px solid black">
         <tr bgcolor='#dddddd'>
           <td width="42"><a href="javascript:formsubmit('anrede')">Anrede&nbsp;Titel</a></td>
           <td width="120"><a href="javascript:formsubmit('name')">Name</a></td>
           <td width="200"><a href="javascript:formsubmit('plz')">Adresse</a></td>
<!--           <td width="30" onClick="javascript:formsubmit('hausnummer')">Nr</td> -->
<!--
           <td width="50"><a href="javascript:formsubmit('plz')">Plz</a></td>
           <td width="120"><a href="javascript:formsubmit('ort')">Ort</a></td>
           <td width="120"><a href="javascript:formsubmit('land')">Land</a></td>
-->
           <td><a href="javascript:formsubmit('email')">E-Mail</a></td>
           <td width="100"><a href="javascript:formsubmit('versnummer')">Vers.Nr.</a></td>
           <td width="100"><a href="javascript:formsubmit('versnummerneu')">Vers.Nr.Neu</a></td>
<!--
           <td width="100"><a href="javascript:formsubmit('rechtskreis')">Rechtskreis</a></td>
-->
           <td>&nbsp;</td>
         </tr>
<?
       $color[0] = "#eeeeee";
       $color[1] = "#f2f2f2";
       $colnr=0;
       for ($i = $from; $i < $to; $i++)
       {
           if (!isset($deleted)) {
               $deleted = 0;
           }
           $user = new user('',$users[$i],$deleted);
           $colnr=1-$colnr;
           $mycolor = $color[$colnr];
           if ((isset($userId)?$userId:0) == $user->userId)
           {
               $mycolor = '#fff2f2';
           }
?>
         <tr bgcolor="<?=$mycolor?>" onMouseOver="highlight('zeile<?=$i?>')" onMouseOut="lowlight('zeile<?=$i?>')" id="zeile<?=$i?>">
           <td><?=$user->anrede?>&nbsp;<?=$user->titel?></td> 
           <td ><?=$user->vorname?>&nbsp;<?=$user->name?></td>
           <td ><?=str_replace(' ','&nbsp;', $user->strasse . " " . $user->hausnummer)?><br/>
<!--           <td><?=$user->hausnummer?></td> -->
           <?=$user->plz?>&nbsp;<?=$user->ort?><br/>
           <?=$user->land?></td>
           <td ><?=$user->email?></td> 
           <td ><?=$user->versnummer?> <?=$user->rechtskreis?></td>
           <td ><?=$user->versnummerneu?></td>
           <td>
             <table>
               <tr>
                 <td>
                   <a href="<?=$_SERVER['PHP_SELF']?>?module=<?=$module?>&action=edit&userId=<?=$user->userId?>&deleted=<?=$deleted?>"><img src="/grafik/button_edit.png" border="0" alt="Editieren" /></a>
                 </td>
                 <td>
                   <a href="mailto:<?=$user->email?>"><img src="/grafik/button-mail.gif" border="0" alt="EMail" /></a>
                 </td>
                 <td>
                   <a href="<?=$_SERVER['PHP_SELF']?>?module=<?=$module?>&action=addpoints&userId=<?=$user->userId?>&deleted=<?=$deleted?>"><img src="/grafik/button_points.png" border="0" alt="Punkte vergeben" /></a>
                 </td>
                 <? if ($loggedIn->role == 'superuser') { ?>
                 <td>
                   <a href="<?=$_SERVER['PHP_SELF']?>?module=<?=$module?>&action=delete&userId=<?=$user->userId?>&deleted=<?=$deleted?>"><img src="/grafik/button_drop.png" border="0" alt="Teilnehmer loeschen" /></a>
                 </td>
                 <? } ?>
               <tr>
             </table>
           </td>
         </tr>
<?
       }
?>
</table>
</form>
<?
    }
}

if ($state!="submitted" && $state!="submitdoublettes" && $state != 'submitdoublekvnrneu' && $state != 'submitmissingkvnrneu' && $state != 'submitsearchrausfaller')
{
    $_SESSION['searchuser'] = false;
?>

 <br>
 <form name="suchen" action="<?=$_SERVER['REQUEST_URI']?>" method="POST">
    <input type="hidden" name="module" value="<?=$module?>" />
    <input type="hidden" name="action" value="<?=$action?>" />
    <input type="hidden" name="state" value="submitted" />
    <fieldset class="fieldset"><legend>Pers. Daten</legend>
    <table>
      <tr>
        <td>Versnr.(alt/neu)</td><td><input type="text" id="versnummer" name="versnummer" />
        <script type="text/javascript">
          $("#versnummer").autocomplete('ajax/autocomplete.php?table=users&field=versnummer');
        </script>
        </td>
      </tr>
      <tr>
    <td>Rechtskreis</td><td><input type="radio" name="rechtskreis"
    value="West" /> West <input type="radio" name="rechtskreis" value="Ost" /> Ost</td>
      </tr>
      <tr>
        <td>Anrede</td><td><input type="text" name="anrede" /></td>
      </tr>
      <!--
      <tr>
        <td>Titel</td><td><input type="text" name="titel" /></td>
      </tr>
      -->
      <tr>
        <td>Name</td><td><input type="text" id="name" name="name" /></td>
        <script type="text/javascript">
          $("#name").autocomplete('ajax/autocomplete_name.php');
        </script>

      </tr>
      <tr>
        <td>Vorname</td><td><input type="text" name="vorname" /></td>
      </tr>
      <tr>
        <td>Stra&szlig;e</td><td><input type="text" name="strasse" value="<?=(isset($strasse))?$strasse:''?>" /></td>
      </tr>
      <tr>
        <td>PLZ</td><td><input type="text" id="plz" name="plz" value="<?=isset($plz)?$plz:''?>" /></td>
        <script type="text/javascript">
          $("#plz").autocomplete('ajax/autocomplete_plz.php');
          $("#plz").keypress(function() { getcity($("#ort"),$(this).val())} );
          function getcity(obj,plz) {
              if (plz.length==5) {
              url = 'http://localhost:8888/healthmiles/admin/ajax/get_city_by_plz.php?q=' + plz;
              $.ajax({
                  url: url,
                  success: function(data) {
                      obj.val(data);
                  }
              });
              }
          }
        </script>
      </tr>
      <tr>
        <td>Ort</td><td><input type="text" id="ort" name="ort" /></td>
        <script type="text/javascript">
          $("#plz").autocomplete('ajax/autocomplete_ort.php');
        </script>
      </tr>

      <tr>
        <td>Land</td><td>
        <select name="land"><option value=""></option><? foreach ($eucountries as $iso=>$country) { ?>
            <option value="<?=$country?>" <?=((isset($land)?$land:'')==$country)?'selected="selected"':''?>><?=$country?></option>
<? } ?></td>
      </tr>

      <tr>
        <td>Geburtsdatum</td>
          <td><input type="text" name="geburtsdatumtag" value="<?=isset($geburtsdatumtag)?$geburtsdatumtag:''?>" size="2" maxlength="2" /> .
          <input type="text" name="geburtsdatummonat" value="<?=isset($geburtsdatummonat)?$geburtsdatummonat:''?>" size="2" maxlength="2" /> .
          <input type="text" name="geburtsdatumjahr" value="<?=isset($$geburtsdatumjahr)?$geburtsdatumjahr:''?>" size="4" maxlength="4" />
        </td>
      </tr>

      <tr>
        <td>E-Mail</td><td><input type="text" name="email" /></td>
      </tr>
      <tr>
        <td>Gel&ouml;scht</td><td><input type="checkbox" value="1" name="deleted" /></td>
      </tr>
      <tr>
        <td>&nbsp;</td><td><input type="radio" value="yes" name="u18" 
        <?=isset($u18) && ($u18 == 'yes')?'selected="selected"':'' ?> />U18
          <input type="radio" value="no" name="u18" 
          <?=isset($u18) && ($u18 == 'no')?'selected="selected"':'' ?>
          /> Erw.
              <input type="radio" value="both" name="u18" 
          <?=!isset($u18) || ($u18 == 'both')?'selected="selected"':'' ?>
              /> Beide

       </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><input type="submit" value="suchen"></td>
      </tr>
    </table>
    </fieldset>
  </form>
  <script language="JavaScript">
    document.forms.suchen.versnummer.focus();
  </script>
  <form name="doppeltesuchen" action="<?=$_SERVER['REQUEST_URI']?>" method="POST">
    <input type="hidden" name="module" value="user" />
    <input type="hidden" name="action" value="search" />
    <input type="hidden" name="state" value="submitdoublettes" />
    <fieldset class="fieldset"><legend>Doubletten suchen</legend>
    <input type="submit" value="Doubletten suchen" />
    </fieldset>
  </form>

  <form name="doppeltekvneusuchen" action="<?=$_SERVER['REQUEST_URI']?>" method="POST">
    <input type="hidden" name="module" value="user" />
    <input type="hidden" name="action" value="search" />
    <input type="hidden" name="state" value="submitdoublekvnrneu" />
    <fieldset class="fieldset"><legend>Doppelte KVNr. suchen</legend>
    <input type="submit" value="Doppelte KVNr. suchen" />
    </fieldset>
  </form>

  <form name="fehlendekvnrneusuchen" action="<?=$_SERVER['REQUEST_URI']?>" method="POST">
    <input type="hidden" name="module" value="user" />
    <input type="hidden" name="action" value="search" />
    <input type="hidden" name="state" value="submitmissingkvnrneu" />
    <fieldset class="fieldset"><legend>Fehlende neue KVNr.</legend>
    <input type="submit" value="Fehlende neue KVNr." />
    </fieldset>
  </form>

  <form name="fehlendemitgliedersuchen" action="<?=$_SERVER['REQUEST_URI']?>" method="POST">
    <input type="hidden" name="module" value="user" />
    <input type="hidden" name="action" value="search" />
    <input type="hidden" name="state" value="submitsearchrausfaller" />
    <fieldset class="fieldset"><legend>Rausfaller suchen</legend>
    <input type="submit" value="Rausfaller suchen" />
    </fieldset>
  </form>

<?
}
?>
