<?php
include_once("kurs/datenbankanbindung.php");		// f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php
#require("func_check_vertiefungsbereich.php");	// Prüft, ob doppelte Vertiefungsbereiche abgegeben werden.
require_once("func_pflichtfelder.php");		// Prüft, ob Pflichtfelder ausgefüllt wurden
require_once("func_vertiefungsbereich.php");		// Prüft, ob vertiefungsbereich gewählt wurde, wenn Stellen erfasst sind.

$db = dbconnect();
$ansp = $db->query("SELECT wert1, wert2 FROM parameter WHERE pid='2'");
$uhd = $ansp->fetch_array();
$ansprechpart = "$uhd[wert1]"; // Ansprechpartner bei Probleme
$helpmail = "$uhd[wert2]";     // Kontaktmailadresse bei Problemen

if ($_COOKIE["uid1"] == ""){ include("kurs/anmeldefehler_subdir.php"); exit;} //Wenn man nicht angemeldet ist soll man nichts ausw&auml;hlen k&ouml;nnen
$uid1=$_COOKIE["uid1"];

include("kurs/sta/stan_useronline_write.php");     // F&uuml;r Statistik, wieviele User online sind



// &Uuml;Berpr&uuml;fung, ob die Anmeldefrist &uuml;berschritten ist
$heute  = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));  // heutiges Datum
$heute_format = date("YmdHis", $heute);  // ISO-8601 Format wie in der Datenbank: Formatierung f&uuml;r Vergleiche erforderlich

$res = $db->query("SELECT Jahrgang FROM stud WHERE uid='$uid1'");
$stud_jahr = $res->fetch_array();

$anm_schluss = $db->query("SELECT DATE_Format(anmeldebeginn, '%Y%m%d%H%i%s')AS beginndatum, DATE_Format(anmeldebeginn, '%d.%m.%Y um %H:%i:%s Uhr')AS beginnform, DATE_Format(anmeldeschluss, '%Y%m%d%H%i%s')AS enddatum
                              FROM stan_frist
                             WHERE jahrgang=\"$stud_jahr[Jahrgang]\"");
$row_schluss = $anm_schluss->fetch_array(); // Herausfinden des Beginn- Schlussdatums


$a = strtotime ($heute_format); //Umrechnung des Fristgrenztages in eine Zahl
$b = strtotime ($row_schluss[enddatum]); // Umrechnung des Enddatums in eine Zahl
$c = strtotime ($row_schluss[beginndatum]); // Umrechnung des Beginndatums in eine Zahl

if ($a < $c){
echo "

<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Anmeldungen noch nicht m&ouml;glich!</title>
</head>
<body>
<table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"550\" border=\"0\">
                <tr>
                    <td width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\"></td>
                    <td width=\"95%\" background=\"../images/box_top.gif\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <span class=\"sh\">Anmeldungen noch nicht m&ouml;glich!</span><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td colspan=\"2\"><br>
                    <p align=\"center\"><b>Antr&auml;ge können erst ab $row_schluss[beginnform] bearbeitet werden!</b>
                    </p>
                    <p align=\"center\"><a href=\"hauptframe.php\">
                    <img border=\"0\" src=\"../images/zurueck.gif\" width=\"77\" height=\"22\"></a><br>
                    </p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p><br>
                    </p>
                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\" colspan=\"2\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
        </div>
        </td>
    </tr>
</table>
</body>
</html>";
exit;
} // Ende &Uuml;berpr&uuml;fung der Zeit&uuml;berschreitung
/*
if ($a >= $b){
echo "
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Anmeldezeit vertstrichen</title>
</head>
<body>
<table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"550\" border=\"0\">
                <tr>
                    <td width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\"></td>
                    <td width=\"95%\" background=\"../images/box_top.gif\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <span class=\"sh\">Anmeldezeit vertstrichen</span><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td colspan=\"2\"><br>
                    <p align=\"center\">Antr&auml;ge können nicht mehr bearbeitet werden!<br>Es sind nur noch <a href=\"antrag_aend.php\">Änderungsanträge</a> möglich
                    </p>
                    <p align=\"center\"><a href=\"hauptframe.php\">
                    <img border=\"0\" src=\"../images/zurueck.gif\" width=\"77\" height=\"22\"></a><br>
                    </p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p><br>
                    </p>
                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\" colspan=\"2\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
        </div>
        </td>
    </tr>
</table>
</body>
</html>";
exit;
} // Ende &Uuml;berpr&uuml;fung der Zeit&uuml;berschreitung
*/

# Wenn Erstanträge abgesendet wurden, dürfen keine Stellen mehr vorgemerkt werden
$res2 = $db->query("SELECT distinct status
                      FROM stan_antrag
                     WHERE uid='$uid1'
                     LIMIT 1");
$stud_status = $res2->fetch_array();

if($stud_status[status] != '0'){

echo "

<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Anmeldungen noch nicht m&ouml;glich!</title>
</head>
<body>
<table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"550\" border=\"0\">
                <tr>
                    <td width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\"></td>
                    <td width=\"95%\" background=\"../images/box_top.gif\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <span class=\"sh\">Anmeldungen nicht m&ouml;glich!</span><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td colspan=\"2\"><br>
                    <p align=\"center\"><b>Die Wahl der AG-Standorte ist erst nach dem Speichern<br>
                    der Erstanträge möglich</b>
                    </p>
                    <p align=\"center\"><a href=\"hauptframe.php\">
                    <img border=\"0\" src=\"../images/zurueck.gif\" width=\"77\" height=\"22\"></a><br>
                    </p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p><br>
                    </p>
                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\" colspan=\"2\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
        </div>
        </td>
    </tr>
</table>
</body>
</html>";
exit;
}
#################



if (isset($_GET['action']))
   $action = $_GET['action'];
else
   $action = "";

switch($action){
default:

echo "

<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Hinweis</title>
</head>
<body>
<table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"550\" border=\"0\">
                <tr>
                    <td width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\"></td>
                    <td width=\"95%\" background=\"../images/box_top.gif\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <span class=\"sh\">Hinweis</span><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td colspan=\"2\"><br>
                    <p align=\"center\"><b>Sie können pro Vertiefungsbereich zwei AG-Standorte wählen.<br>
                    Danach können Sie der Stelle keine anderen Prioritäten mehr zuweisen.</b>                    </p>
                        <p align=\"center\"><a href=".$_SERVER['PHP_SELF'] ."?action=bearbeiten0>
                        <img border=\"0\" src=\"../images/ok_button.gif\"></a><br>

                    </p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p><br>
                    </p>
                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\" colspan=\"2\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
        </div>
        </td>
    </tr>
</table>
</body>
</html>";

break;
case "bearbeiten0":

echo "

<html>

<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Anträge bearbeiten</title>


<style type=\"text/css\">
  select{
	width:200px;
  }
</style>

</head>

<body>


<table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" border=\"0\">
                <tr>
                    <td width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_top.gif\" valign=\"top\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <font class=\"hd\">AG-Standorte wählen</font><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td>";


                    $db = dbconnect();

                    $query = "SELECT distinct vert_bereich
                                FROM stan_antrag t1
                               WHERE t1.status = (
                                                  SELECT MAX(t2.status)
                                                    FROM stan_antrag t2
                                                   WHERE t1.aendid = t2.aendid
                                                     AND t2.status !='2' #hinzugefügt am 10.06.2009, weil Status 2 angezeigt wurde
                                                  )
                                 AND t1.uid = '$uid1'
                                 AND (t1.zuweisung is NULL OR t1.zuweisung='Z')
                               ORDER BY vert_bereich
                             ";

                    $result = $db->query ($query)
                       or die ("Cannot execute query");

                        while ($row = $result->fetch_array()){


				echo"
                                <table width='100%' bgcolor=#6487DC>
                                <tr>
                                  <td width='60%' style='border-style: none; border-width: medium' height='0'>
                                  <font color='#FFFFFF'><b>Vertiefungsbereich:
                                  ";
                                        $db = dbconnect();


                                          $query1 = $db->query ("SELECT vbid, bezeichnung
                                                                    FROM stan_vertiefungsbereich
                                                                   WHERE vbid = '$row[vert_bereich]'");

                                          $row1 = $query1->fetch_array();


                                          echo"
                                          $row1[bezeichnung]
                                          </b></font>
                                  </td>
                                </tr>
                                </table>
                                ";
                             $query2 = "SELECT t1.saaid
					     , t1.dst_id
					     , date_format(t1.beginn, '%d.%m.%Y') beginn
					     , date_format(t1.beginn, '%Y%m%d') beginn_form
					     , date_format(t1.ende, '%d.%m.%Y') ende
					     , t1.bearbeitung
					  FROM stan_antrag t1
					 WHERE t1.status = (
					                    SELECT MAX(t2.status)
					                      FROM stan_antrag t2
					                     WHERE t1.aendid = t2.aendid
					                       AND t2.status !='2' #hinzugefügt am 10.06.2009, weil Status 2 angezeigt wurde
					                   )
                                            AND t1.uid = '$uid1'
                                            AND vert_bereich = '$row[vert_bereich]'
                                            AND (t1.zuweisung is NULL OR t1.zuweisung='Z')
                                            #AND (zuweisung != 'G' or zuweisung is null)
                                            #AND (zuweisung != 'GV' or zuweisung is null)
                                           ORDER BY beginn_form";

                              $result2 = $db->query ($query2)
                                 or die ("Cannot execute query");

                             while ($row3 = $result2->fetch_array()){

                                $res4 = $db->query("SELECT bez, ort FROM stan_dienststellen WHERE dst_id='$row3[dst_id]'");
                                $row4 = $res4->fetch_array();
                                if($row4[bez] != ''){
                                  $dst_bez = $row4[bez]." ".$row4[name];
                                  $ort = $row4[ort];
                                }else{
                                  $res5 = $db->query("SELECT bez, ort FROM stan_dienststellen_tmp WHERE dst_id='$row3[dst_id]'");
                                  $row5 = $res5->fetch_array();
                                  $dst_bez = $row5[bez]." ".$row5[name];
                                  $ort = $row5[ort];
                                }

                               echo "
                               <form action="; echo $_SERVER['PHP_SELF'] . "?action=bearbeiten"; echo" method=\"POST\">

                               <table border=\"0\" cellpadding=\"4\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#E7E8ED\" width=\"100%\" id=\"AutoNumber1\">
                               <tr>
                                 <td width=\"40%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                                   $dst_bez ($row3[beginn]-$row3[ende])
                                 </td>
                                 <td valign=\"middle\" width=\"30%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                                 ";
                                 $db = dbconnect();
                                 $res6 = $db->query("SELECT saaid, wagsid FROM stan_ag_standort_wunsch WHERE uid='$uid1' AND saaid = '$row3[saaid]' AND prio='1'");
                                 $row6 = $res6->fetch_array();

                                 # Nur einmal ein Standort abspeichern
                                 if($row6[wagsid] ==''){
                                   $db = dbconnect();


                                    $query1 = "SELECT agsid, standort
                                                 FROM stan_ag_standort a, stan_standort b
                                                WHERE jahr='$stud_jahr[Jahrgang]'
                                                  AND vbid = '$row[vert_bereich]'
                                                  AND a.sid=b.sid
                                                ORDER BY standort ASC
                                              ";

                                    $result1 = $db->query ($query1)
                                       or die ("Cannot execute query");
                                    echo "<select size=\"1\" name=\"$row3[saaid]_1\">
                                    <option value='x1'>Priorität 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>
                                    ";
                                    $nr=0;
                                    while ($row1 = $result1->fetch_array()){

                                      $res6 = $db->query("SELECT saaid, wagsid FROM stan_ag_standort_wunsch WHERE uid='$uid1' AND saaid = '$row3[saaid]' AND prio='1'");
                                      $row6 = $res6->fetch_array();


                                      if($nr==0){
                                        $nr=1;
                                        echo"<option value='8888'"; if($row6[wagsid] == '8888'){ echo " selected "; } echo">keine (Praxisstelle außerhalb BW)</option>";
                                      }

                                       echo "<option value='$row1[agsid]'"; if($row1[agsid] == $row6[wagsid]){ echo " selected "; } echo">$row1[standort]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>\n";
                                    }
                                    echo "</select>";
                                    #Wenn noch nicht alle Standorte gewählt, dann Abschickenbutton anzeigen
                                    $button_send='X';
                                 }else{

                                      $res6b = $db->query("SELECT saaid,wagsid, standort
                                                              FROM stan_standort a, stan_ag_standort b, stan_ag_standort_wunsch c
                                                             WHERE a.sid = b.sid
                                                               AND c.wagsid=b.agsid
                                                               AND b.agsid='$row6[wagsid]'
                                                         ");

                                      $row6b = $res6b->fetch_array();

                                      if($row6[wagsid]== '8888'){
                                       echo"<input type='hidden' name='$row6[saaid]_1' value='exists1'>";
                                       echo "Stelle außerhalb BW";
                                      }else{
                                       echo"<input type='hidden' name='$row3[saaid]_1' value='exists1'>";
                                       echo "Priorität 1: $row6b[standort]";
                                      }
                                 }
                                   echo"
                                 </td>
                                 <td width=\"30%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                                 ";
                                 $db = dbconnect();
                                 $res6 = $db->query("SELECT saaid, wagsid FROM stan_ag_standort_wunsch WHERE uid='$uid1' AND saaid = '$row3[saaid]' AND prio='2'");
                                 $row6 = $res6->fetch_array();
#echo "SELECT wagsid FROM stan_ag_standort_wunsch WHERE uid='$uid1' AND saaid = '$row3[saaid]' AND prio='2'";
                                 # Nur einmal ein Standort abspeichern
                                 if($row6[wagsid] ==''){
                                   $db = dbconnect();


                                      $query1 = "SELECT agsid, standort
                                                  FROM stan_ag_standort a, stan_standort b
                                                 WHERE jahr='$stud_jahr[Jahrgang]'
                                                   AND vbid = '$row[vert_bereich]'
                                                   AND a.sid=b.sid
                                                 ORDER BY standort ASC
                                               ";

                                      $result1 = $db->query ($query1)
                                         or die ("Cannot execute query");

                                      echo "<select size=\"1\" name=\"$row3[saaid]_2\">
                                      <option value='x2'>Priorität 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>
                                      ";
                                      $nr=0;
                                      while ($row1 = $result1->fetch_array()){

                                       $res7 = $db->query("SELECT wagsid FROM stan_ag_standort_wunsch WHERE uid='$uid1' AND saaid = '$row3[saaid]' AND prio='2'");
                                       $row7 = $res7->fetch_array();


                                       if($nr==0){
                                         $nr=1;
                                         echo"<option value='9999'"; if($row7[wagsid] == '9999'){ echo " selected "; } echo">keine (Praxisstelle außerhalb BW)</option>";
                                       }
                                        echo "<option value='$row1[agsid]'"; if($row1[agsid] == $row7[wagsid]){ echo " selected "; } echo">$row1[standort]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>\n";
                                      }
                                      echo "</select>";

                                      #Wenn noch nicht alle Standorte gewählt, dann Abschickenbutton anzeigen
                                      $button_send='X';
                                 }else{
                                      $res6b = $db->query("SELECT saaid,wagsid, standort
                                                              FROM stan_standort a, stan_ag_standort b, stan_ag_standort_wunsch c
                                                             WHERE a.sid = b.sid
                                                               AND c.wagsid=b.agsid
                                                               AND b.agsid='$row6[wagsid]'
                                                         ");

                                      $row6b = $res6b->fetch_array();

                                      if($row6[wagsid]== '9999'){
                                       echo"<input type='hidden' name='$row6[saaid]_2' value='exists2'>";
                                       echo "Stelle außerhalb BW";
                                      }else{
                                       echo"<input type='hidden' name='$row3[saaid]_2' value='exists2'>";
                                       echo "Priorität 2: $row6b[standort]";
                                      }
                                 }
                                   echo"
                                 </td>
                               </tr>
                               </table>";

                             }

                        }
                    echo "

                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
            <br>";
            if(isset($button_send)){
	     echo"<input type=\"submit\" value=\"Abschicken\" name=\"B1\" size=\"20\"></p>";
	    }
	     echo"
             </form>
        </div>
        </td>
    </tr>
</table>

</body>

</html>";

break;
case "bearbeiten":

$db = dbconnect();
$result8 = $db->query("SELECT distinct t1.saaid
                             , concat(saaid,'_1') prio1
                             , concat(saaid,'_2') prio2
                          FROM stan_antrag t1
                         WHERE t1.status = (
                                            SELECT MAX(t2.status)
                                              FROM stan_antrag t2
                                             WHERE t1.aendid = t2.aendid
                                               AND t2.status !='2' #hinzugefügt am 10.06.2009, weil Status 2 angezeigt wurde
                                           )
                           AND t1.uid = '$uid1'
                           AND (t1.zuweisung is NULL OR t1.zuweisung='Z')
                      ");

while ($row8 = $result8->fetch_array()){

      $saaid= $row8[saaid];
      $prio1= $_POST[$row8[prio1]];
      $prio2= $_POST[$row8[prio2]];

if($prio1 == $prio2){
 echo"

<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Anmeldungen noch nicht m&ouml;glich!</title>
</head>
<body>
<table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"550\" border=\"0\">
                <tr>
                    <td width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\"></td>
                    <td width=\"95%\" background=\"../images/box_top.gif\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <span class=\"sh\">Gleiche Prioritäten</span><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td colspan=\"2\">
                    <br>

                    <p align=\"center\"><b>Sie haben bei einer (oder mehreren) Stelle(n) gleiche AG-Standorte angegeben</b>
                    </p>
                        <p align=\"center\"><a href=\"javascript:history.back()\">
                        <img border=\"0\" src=\"../images/zurueck.gif\" width=\"77\" height=\"22\"></a><br>
                    </p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p><br>
                    </p>
                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\" colspan=\"2\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
        </div>
        </td>
    </tr>
</table>
</body>
</html>
 ";
 exit;
 }


 if(($prio1 != 'x1' AND $prio2 =='x2')or($prio1 == 'x1' AND $prio2 !='x2')){
  echo"

 <html>
 <head>
 <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
 <link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
 <title>Anmeldungen noch nicht m&ouml;glich!</title>
 </head>
 <body>
 <table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
     <tr>
         <td>
         <div align=\"center\">
             <table cellspacing=\"0\" cellpadding=\"0\" width=\"550\" border=\"0\">
                 <tr>
                     <td width=\"10\" background=\"../images/box_e1.gif\">
                     <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                     <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\"></td>
                     <td width=\"95%\" background=\"../images/box_top.gif\">
                     <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                     <span class=\"sh\">Falsche Prioritäten</span><br>
                     <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                     <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                     <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                 </tr>
                 <tr>
                     <td width=\"1%\" background=\"../images/box_l.gif\">
                     <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                     <td colspan=\"2\"><br>
                     <p align=\"center\"><b>Sie dürfen nicht nur 1 Priorität setzen</b>
                     <br>
                     </p>
                         <p align=\"center\"><a href=\"javascript:history.back()\">
                         <img border=\"0\" src=\"../images/zurueck.gif\" width=\"77\" height=\"22\"></a><br>
                     </p>
                     <p></p>
                     <p></p>
                     <p></p>
                     <p></p>
                     <p><br>
                     </p>
                     </td>
                     <td width=\"1%\" background=\"../images/box_r.gif\">
                     <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                 </tr>
                 <tr>
                     <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                     <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                     <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\" colspan=\"2\">
                     <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                     <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                     <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                 </tr>
             </table>
         </div>
         </td>
     </tr>
 </table>
 </body>
 </html>
  ";
  exit;
 }

      $db = dbconnect();
      if($prio1 != 'x1' AND $prio1 != 'exists1'){
        # Löschung falls jemand F5 drückt. Dann wäre der Datensatz doppelt drin      
        $sql = $db->query ("DELETE FROM stan_ag_standort_wunsch 
                                  WHERE uid='$uid1'
                                    AND saaid='$saaid'
                                    AND prio='1'");

        $sql = $db->query ("INSERT INTO stan_ag_standort_wunsch (uid, saaid, wagsid, prio)
                             VALUES ('$uid1', '$saaid', '$prio1' ,1)"); // hier entspricht die uid den namen des Dropdownfeldes in der die neue Nr des PSN steht
      }

      if($prio2 != 'x2' AND $prio2 != 'exists2'){
        # Löschung falls jemand F5 drückt. Dann wäre der Datensatz doppelt drin      
        $sql = $db->query ("DELETE FROM stan_ag_standort_wunsch 
                                  WHERE uid='$uid1'
                                    AND saaid='$saaid'
                                    AND prio='2'");

        $sql = $db->query ("INSERT INTO stan_ag_standort_wunsch (uid, saaid, wagsid, prio)
                             VALUES ('$uid1', '$saaid', '$prio2' ,2)"); // hier entspricht die uid den namen des Dropdownfeldes in der die neue Nr des PSN steht
      }
}

 echo"
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Anmeldungen noch nicht m&ouml;glich!</title>
</head>
<body>
<table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"550\" border=\"0\">
                <tr>
                    <td width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\"></td>
                    <td width=\"95%\" background=\"../images/box_top.gif\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <span class=\"sh\">AG-Standorte erfasst</span><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td colspan=\"2\"><br>
                    <p align=\"center\"><b>Die Standorte wurden erfasst!</b>
                    </p>
                        <p align=\"center\"><a href=\"hauptframe.php\">
                        <img border=\"0\" src=\"../images/zurueck.gif\" width=\"77\" height=\"22\"></a><br>
                    </p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p><br>
                    </p>
                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\" colspan=\"2\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
        </div>
        </td>
    </tr>
</table>
</body>
</html>";


}
?>