    <strong>Pr&auml;mienvergabe:</strong> Buchen Sie eine der folgenden Pr&auml;mien ab.<br><br>
    <form name="praemien" action="<?=$_SERVER['REQUEST_URI']?>" method="POST" onsubmit="return checkpoints()">
      <input type="hidden" name="userId" value="<?=$userId?>" />
      <input type="hidden" name="state" value="submitted" />
      <input type="hidden" name="praemienid" value="0" />
      <input type="hidden" name="praemienhmnr" value="0" />
      <input type="hidden" name="submitted" value="1" />
      <fieldset class="fieldset"><legend>Pr&auml;mienvergabe</legend>
      <table>
        <?=formfield::show('datum','Datum','date',$datum?$datum:date('d.m.Y',time()));?>
        <tr>
          <td>Pr&auml;mie:</td>
          <td><select name="praemie" onChange="checkfamilienpraemie();fillPraemienValues();"></select></td>
        </tr>
<? if ($kinder) { ?>
        <tr id="familienpraemie">
          <td>
            <table>
              <tr><td><b>Kinder:</b> </td><td>&nbsp;</td></tr>
<? foreach($kinder as $kind) :
       $kind->konto = new konto($kind->userId);
       $kind->kontostand = $kind->konto->getSaldo();
 ?>
              <tr><td><?=$kind->vorname?></td>
              <td><?php
       if ($kind->konto->praemienlimit($praemienid)) : print "Praemienlimit"; else
       if ($kind->konto->geldpraemienlimit($praemienid)) : print "Geldpraemienlimit"; else ?>
<select id="valuekind[<?=$kind->userId?>]" name="valuekind[<?=$kind->userId?>]" onchange="checkfamilienvalues()"><?
endif; ?>
              </td></tr>
<!-- hello world -->
<? endforeach;
endif; ?>
              <tr><td>Wert: <div id="gesamtkinder">0</div></td><td>Ben&ouml;tigt: <div id="wertkinder">0</div></td></tr>
              <tr><td><b>Eltern:</b> </td><td>&nbsp;</td></tr>
<? foreach($partners as $partner) {

   $partner->konto = new konto($partner->userId);
   $partner->kontostand = $partner->konto->getSaldo();
   if (!$partner->konto->praemienlimit($praemienid) && ! $partner->konto->geldpraemienlimit($praemienid)) {
 ?>
              <tr><td><?=$partner->vorname?></td>
              <td><select id="valuepartner[<?=$partner->userId?>]" name="valuepartner[<?=$partner->userId?>]" onchange="checkfamilienvalues()">
              </td></tr>
<? }} ?>

              <tr><td>Wert: <div id="gesamtpartner">0</div></td><td>Ben&ouml;tigt: <div id="wertpartner">0</div></td></tr>
            </table>
          </td>
        </tr>
<? } ?>
        <tr>
          <td colspan="2"><br>
            Oder tragen Sie hier manuell eine Pr&auml;mie ein.<br><br>
          </td>
        </tr>
        <tr>
          <td>Beschreibung:</td>
          <td>
            <textarea name="description" class="inputtext"></textarea>
          </td>
        </tr>
        <tr>
          <td>MINUS-Punkte:&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td><input type="text" name="points" size="5" /></td>
        </tr>
        <tr>
          <td colspan="2">&nbsp;</td>
        <tr>
          <td>&nbsp;</td>
          <td><input type="submit" value="Eintragen"></td>
        </tr>
      </table>
      </fieldset>
    </form>
    <hr>
    <br>

    <script language="JavaScript">
        praemienliste = new Array()
        praemiendescriptions = new Array()
        praemienpoints = new Array()
        praemienpointskinder = new Array()
        praemienids = new Array()
        praemienhmnrs= new Array()
        praemientypes = new Array()
<?
       $i=1;
       echo "<!-- Saldo: " . $client->konto->getSaldo() . " -->\n";
       foreach($praemien as $praemie)
{
    $pointsrequired = $praemie->punkte;
    $show = true;
    if ($praemie->familienpraemie && count($kinder) > 0) {
        $pointsrequired = ($praemie->zusaetze[0]->anzeige+0);
        $pointsrequiredkinder = ($praemie->zusaetze[1]->anzeige+0);
        $show = false;
        $kinderpoints = 0;
        foreach ($kinder as $kind) {
            $kinduser = new user($kind->versnummer,0,0,$kind->rechtskreis);
            $kindkonto = new konto($kinduser->userId);
            if ($kindkonto->hasminbestellpunkte() && ! $kindkonto->praemienlimit($praemie->id))
            {
                $kinderpoints+=$kindkonto->getSaldo();
            }
        }
        if ($kinderpoints >= ($praemie->zusaetze[1]->anzeige +0)) {
            $show = true;
        } 
    } else {
        /* Zusatz(U-)praemien sollen nicht geprueft werden */
        /* Ansonsten muss Praemienlimit (n-mal pro Jahr) und Mindespunkte geprueft werden */
         if ( $praemie->praemientyp != 'U'
                 && ( ! $client->konto->hasminbestellpunkte() 
                     || $client->konto->praemienlimit($praemie->id)
                     || $client->konto->geldpraemienlimit($praemie->id)
                    )
            )
         {
            $show = false;
        }
    }
    $praemie->description = str_replace("\n",'\n', str_replace("\r",'',$praemie->description));

 
    if ((($kinder && $praemie->familienpraemie) || ($pointsrequired <= $client->konto->getSaldo())) && $show) {
       $praemie->name = str_replace("\n",'\n', str_replace("\r",'',$praemie->name));
        $praemie->beschreibung = str_replace("\n",'\n', str_replace("\r",'',$praemie->beschreibung));
        if (count($praemie->zusaetze)>0 & ! $praemie->familienpraemie)
        {
            foreach ($praemie->zusaetze as $artikel)
            {
                print "        praemienliste[$i] = \"" .  str_replace('"','\\"',$praemie->name);
                if ($artikel->anzeige) print  " - " . str_replace('"','\\"',$artikel->anzeige);
                print "\"\n";
                print "        praemienpoints[$i] = '$pointsrequired'\n";
                print "        praemientypes[$i] = \"" . $praemie->praemientyp . "\"\n";
                print "        praemiendescriptions[$i] = \"" .  str_replace('"','\\"',str_replace('>','&raquo;',$praemie->beschreibung)) .  "\"\n";
                print "        praemienids[$i] = '$artikel->praemienid'\n";
                print "        praemienhmnrs[$i] = '$artikel->hmnr'\n";
                $i++;
            }
        } else {
            print "        praemienliste[$i] = \"" .  str_replace('"','\\"',$praemie->name).  "\"\n";
            print "        praemientypes[$i] = \"" . $praemie->praemientyp . "\"\n";
            if ($praemie->familienpraemie) {
                print "        praemienpointskinder[$i] = '$pointsrequiredkinder'\n";
            }

            $praemie->beschreibung = str_replace('"','\\"',str_replace("\n",'\n', str_replace("\r",'',$praemie->beschreibung)));
            print "        praemiendescriptions[$i] = \"" .  str_replace('>','&raquo;',$praemie->beschreibung) .  "\"\n";

            print "        praemienpoints[$i] = '$pointsrequired'\n";
            print "        praemienids[$i] = '$praemie->id'\n";
            print "        praemienhmnrs[$i] = '" . $praemie->zusaetze[0]->hmnr. "'\n";
            $i++;
        }
    }
}
?>
        praemie=document.forms.praemien.praemie
        praemie.length=0
        newEntry = new Option('','',false,true);
        praemie.options[0]=newEntry;
        for (i=1; i< praemienliste.length; i++)
        {
            bezeichner = praemienliste[i] + ' ' + praemientypes[i];
            if (praemienhmnrs[i]) bezeichner += ' (Nr ' + praemienhmnrs[i] + ')';
            newEntry = new Option(bezeichner, praemienliste[i],false,true)
            praemie.options[i]=newEntry
        }
        praemie.selectedIndex = 0;

        function fillPraemienValues()
        {
            i = document.forms.praemien.praemie.selectedIndex
            if (i==0) {
              return;
            }
            document.forms.praemien.description.value = "Bestellung der Prämie: " + praemienliste[i];
            document.forms.praemien.points.value = praemienpoints[i]
            document.forms.praemien.praemienid.value = praemienids[i]
            document.forms.praemien.praemienhmnr.value = praemienhmnrs[i]
        }
        fillPraemienValues();
        $(document).ready(function() {
          $('#familienpraemie').hide();
        });
            kinder = Array()
<? for($i=0; $i< count($kinder); $i++)
                { ?>
                    kinder[<?=$i?>]=Array();
                    kinder[<?=$i?>]['id']=<?=$kinder[$i]->userId?>         
                    <?$konto = new konto($kinder[$i]->userId)?>;
                    kinder[<?=$i?>]['saldo']=<?=$konto->getSaldo()?>         
<? } ?>
            partners = Array()
<? for($i=0; $i< count($partners); $i++)
                { ?>
                    partners[<?=$i?>]=Array();
                    partners[<?=$i?>]['id']=<?=$partners[$i]->userId?>         

                    <?$konto = new konto($partners[$i]->userId)?>;
                    partners[<?=$i?>]['saldo']=<?=$konto->getSaldo()?>         
<? } ?>

        function checkfamilienvalues()
        {
            i = document.forms.praemien.praemie.selectedIndex
            value = praemienpoints[i]
            document.forms.praemien.points.value = praemienpoints[i]
            kinderval=0;
            i = document.forms.praemien.praemie.selectedIndex
            for (x=0; x < kinder.length; x++) {
                kind = document.getElementById('valuekind[' + kinder[x]['id'] + ']');

                maxvalue = Math.min(praemienpointskinder[i],kinder[x]['saldo'])
                maxvalue = Math.min(praemienpointskinder[i]-kinderval,maxvalue)

                selectedIndex = kind.selectedIndex

                for (j =kind.options.length; j >= 0;j--)
                {
                    kind.options[j]=null;
                }
                for (j = 0; j <= maxvalue/50; j++) {
                    newEntry = new Option(j*50,j*50,false,true)
                        kind.options[j] = newEntry;
                }

                kind.selectedIndex=Math.min(selectedIndex,kind.options.length-1) 
                kinderval = Math.round((kind.options[kind.selectedIndex].value)) + Math.round(kinderval)

            }
            gesamtkinderdiv = document.getElementById('gesamtkinder');
            gesamtkinderdiv.innerHTML = kinderval
            
            partnerval=0;
            for (x=0; x < partners.length; x++) {
                partner = document.getElementById('valuepartner[' + partners[x]['id'] + ']');
                maxvalue = Math.min(praemienpoints[i],partners[x]['saldo'])
                maxvalue = Math.min(praemienpoints[i]-partnerval,maxvalue)
                selectedIndex = partner.selectedIndex
                for (j =partner.options.length; j >= 0;j--)
                {
                    partner.options[j]=null;
                }
                for (j = 0; j <= maxvalue/50; j++) {
                    newEntry = new Option(j*50,j*50,false,true)
                        partner.options[j] = newEntry;
                }
                partner.selectedIndex=Math.min(selectedIndex,partner.options.length-1) 
                partnerval = Math.round((partner.options[partner.selectedIndex].value)) + Math.round(partnerval)
            }
            gesamtpartnerdiv = document.getElementById('gesamtpartner');
            gesamtpartnerdiv.innerHTML = partnerval
        }

        function checkfamilienpraemie()
        {
           praemie=document.forms.praemien.praemie;
           text=praemie.options[praemie.selectedIndex].text;
           value=praemie.options[praemie.selectedIndex].value;
           if (text.indexOf('Familien-') == 0) {
               $('#familienpraemie').show();
               i = document.forms.praemien.praemie.selectedIndex
               wertkinderdiv = document.getElementById('wertkinder');
               wertkinderdiv.innerHTML = praemienpointskinder[i]
               i = document.forms.praemien.praemie.selectedIndex
               wertpartnerdiv = document.getElementById('wertpartner');
               wertpartnerdiv.innerHTML = praemienpoints[i];
               for (x=0; x < kinder.length; x++) {
                   kind = document.getElementById('valuekind[' + kinder[x]['id'] + ']');
                   for (j=kind.options.length; j >= 0;j--)
                   {
                       kind.options[j]=null;
                   }
                   for (j = 0; j <= Math.min(praemienpointskinder[i],kinder[x]['saldo'])/50; j++) {
                       newEntry = new Option(j*50,j*50,false,true)
                       kind.options[j] = newEntry;
                       kind.selectedIndex=0
                   }
               }
               for (x=0; x < partners.length; x++) {
                   name = 'valuepartner[' + partners[x]['id'] + ']';
                   partner = document.getElementById('valuepartner[' + partners[x]['id'] + ']');
                   for (j = partner.options.length; j >= 0;j--)
                   {
                       partner.options[j]=null;
                   }
                   for (j = 0; j <= Math.min(praemienpoints[i],partners[x]['saldo'])/50; j++) {
                       newEntry = new Option(j*50,j*50,false,true)
                       partner.options[j] = newEntry;
                       partner.selectedIndex=0
                   }
               }
           } else {
               $('#familienpraemie').hide();
           }
        }

        function checkpoints()
        {
           praemie=document.forms.praemien.praemie;
           text=praemie.options[praemie.selectedIndex].text;
           if (text.indexOf('Familien-') == 0) {
               i = document.forms.praemien.praemie.selectedIndex
               value = praemienpoints[i]
               kinderval=0;
               i = document.forms.praemien.praemie.selectedIndex
               for (x=0; x < kinder.length; x++) {
                 kind = document.getElementById('valuekind[' + kinder[x]['id'] + ']');
                 kinderval = Math.round((kind.options[kind.selectedIndex].value)) + Math.round(kinderval)
               }
               partnerval=0;
               for (x=0; x < partners.length; x++) {
                 partner = document.getElementById('valuepartner[' + partners[x]['id'] + ']');
                 partnerval = Math.round((partner.options[partner.selectedIndex].value)) + Math.round(partnerval)
               }
               if (kinderval != praemienpointskinder[i] || partnerval != praemienpoints[i]) 
               {
                 alert('Es wurden nicht genuegend Punkte vergeben.')
                 return false;
               }
           }
           return true;
        }
    </script>
