Example #1
0
function XML_reg_start($parser, $name, $attr)
{
    global $discode, $catcode, $xDis, $distype, $bCombined;
    global $strBaseAthleteNotFound, $strBaseTeamNotFound, $strBaseRelayNotFound, $strLicenseNr;
    global $cfgDisciplineType, $cfgEventType, $strEventTypeSingleCombined, $strEventTypeClubCombined, $strDiscTypeTrack, $strDiscTypeTrackNoWind, $strDiscTypeRelay, $strDiscTypeDistance, $strErrNoSuchDisCode, $strNoSuchCategory;
    global $cfgCombinedDef, $cfgCombinedWO, $appnbr;
    global $cfgSVM, $relay_id, $relay_pos, $relay_round, $relay_xStart, $category, $team_type, $paid;
    global $arr_noCat, $cfgResDisc, $cfgSvmDiscFirst, $cfgSvmDiscLast;
    //
    // get approval number
    //
    if ($name == "MEETDATASET") {
        $appnbr = $attr['APPROVAL'];
        mysql_query("UPDATE meeting SET Nummer = '{$appnbr}' WHERE xMeeting = " . $_COOKIE['meeting_id']);
        $team_type = '';
    }
    //
    // get costs (entry_fee, entry_fee_reduction, penalty)
    //
    if ($name == "MEETDATASET") {
        $meet_fee_red = $attr['ENTRY_FEE_REDUCTION'];
        mysql_query("UPDATE meeting SET StartgeldReduktion = '{$meet_fee_red}' WHERE xMeeting = " . $_COOKIE['meeting_id']);
    }
    if ($name == "MEETDATASET") {
        $meet_fee = $attr['ENTRY_FEE'];
        mysql_query("UPDATE meeting SET Startgeld = '{$meet_fee}' WHERE xMeeting = " . $_COOKIE['meeting_id']);
    }
    if ($name == "MEETDATASET") {
        $meet_penalty = $attr['PENALTY'];
        mysql_query("UPDATE meeting SET Haftgeld = '{$meet_penalty}' WHERE xMeeting = " . $_COOKIE['meeting_id']);
        $_SESSION['meeting_infos']['Haftgeld'] = $meet_penalty;
    }
    //
    // start of discipline
    //
    if ($name == "DISCIPLINE") {
        $xDis = array();
        $discode = $attr['DISCODE'];
        $svmCategoryCode = $attr['SVMCATEGORYCODE'];
        if ($svmCategoryCode == '36_09') {
            $catcode = 'U12M';
        } else {
            $catcode = $attr['CATCODE'];
        }
        $disname = trim($attr['DISNAME']);
        // special name of discipline, ordinary disname + info (cold be user defined)
        $disinfo = trim($attr['DISINFO']);
        // special name of discipline, without ordinary disc-name (cold be user defined)
        $disspecial = $attr['DISSPECIAL'];
        $disfee = 0;
        $disfee = $attr['DISFEE'] / 100;
        $disid = $attr['DISID'];
        $type = $attr['TYPE'];
        $svmCatCode = 0;
        $xCatSvm = 0;
        if (isset($attr['SVMCATEGORYCODE'])) {
            $svmCatCode = $attr['SVMCATEGORYCODE'];
            // get svm xCat
            $res = mysql_query("SELECT xKategorie_svm FROM kategorie_svm WHERE Code = '{$svmCatCode}'");
            if (mysql_errno() > 0) {
                XML_db_error("23-" . mysql_errno() . ": " . mysql_error());
            }
            $row = mysql_fetch_row($res);
            $xCatSvm = $row[0];
        }
        $relay_id = 0;
        $relay_round = 0;
        // check discode and return if it doesn't exists
        $res = mysql_query("SELECT xDisziplin FROM disziplin_" . $_COOKIE['language'] . " WHERE Code = '{$discode}'");
        if (mysql_errno() == 0) {
            if (mysql_num_rows($res) == 0 && ($discode < $cfgResDisc && $discode > $cfgSvmDiscLast)) {
                echo "<p>{$strErrNoSuchDisCode} {$disname} ({$discode})</p>";
                return;
            }
            mysql_free_result($res);
        }
        // if this is a self specified discipline, check onlineid to differentiate
        $SQLdisSpecial = "";
        if ($disspecial == "y") {
            $SQLdisSpecial = " AND w.OnlineId = '{$disid}' ";
        } else {
            $disname = "";
            // do not fill the info-field with disname if there is no need
        }
        // if this is a discipline with info, check additonal the info to differentiate
        $SQLdisInfo = "";
        if (strlen($disinfo) != 0) {
            //$SQLdisInfo = " AND w.OnlineId = '$disid' ";
            $SQLdisInfo = " AND w.Info = '{$disinfo}' ";
            $disname = $disinfo;
        } else {
            $disname = "";
            // do not fill the info-field with disname if there is no need
        }
        // combined event
        if (isset($cfgCombinedDef[$discode])) {
            $bCombined = true;
            // check if this combined event exists
            $query = "SELECT * \r\n                           FROM\r\n                                wettkampf as w\r\n                                LEFT JOIN kategorie as k  ON (w.xKategorie = k.xKategorie)\r\n                                LEFT JOIN disziplin_" . $_COOKIE['language'] . " as d ON (w.xDisziplin = d.xDisziplin)\r\n                            WHERE  \r\n                                w.xMeeting = " . $_COOKIE['meeting_id'] . "\r\n                                AND k.Code = '{$catcode}'\r\n                                AND w.Mehrkampfcode = {$discode}";
            $res = mysql_query($query);
        } else {
            $bCombined = false;
            if ($discode >= $cfgSvmDiscFirst && $discode <= $cfgSvmDiscLast) {
                $query = "SELECT * \r\n                                   FROM\r\n                                        wettkampf as w\r\n                                        LEFT JOIN kategorie as k  ON ( w.xKategorie = k.xKategorie)\r\n                                        LEFT JOIN disziplin_" . $_COOKIE['language'] . " as d  ON (w.xDisziplin = d.xDisziplin)\r\n                                    WHERE  \r\n                                       w.xMeeting = " . $_COOKIE['meeting_id'] . "                                           \r\n                                       AND xKategorie_svm = " . $xCatSvm . "                            \r\n                                       AND w.Mehrkampfcode = 0\r\n                                       {$SQLdisSpecial} \r\n                                       {$SQLdisInfo} ";
                $res = mysql_query($query);
                while ($row_dis = mysql_fetch_assoc($res)) {
                    $xDis[] = $row_dis['xWettkampf'];
                }
            } else {
                // check if this discipline exists
                //    important: Mehrkampfcode has to be 0, else the query will also select
                //        the already defined combined events (if the same discipline)
                $query = "SELECT * \r\n                                   FROM\r\n                                        wettkampf as w\r\n                                        LEFT JOIN kategorie as k  ON ( w.xKategorie = k.xKategorie)\r\n                                        LEFT JOIN disziplin_" . $_COOKIE['language'] . " as d  ON (w.xDisziplin = d.xDisziplin)\r\n                                    WHERE  \r\n                                       w.xMeeting = " . $_COOKIE['meeting_id'] . "\r\n                                       AND k.Code = '{$catcode}'\r\n                                       AND d.Code = {$discode}     \r\n                                       AND xKategorie_svm = " . $xCatSvm . "                            \r\n                                       AND w.Mehrkampfcode = 0\r\n                                       {$SQLdisSpecial} \r\n                                       {$SQLdisInfo} ";
                $res = mysql_query($query);
            }
        }
        if (mysql_errno() > 0) {
            XML_db_error("1-" . mysql_errno() . ": " . mysql_error());
        } else {
            $res_catcode = mysql_query("SELECT xKategorie FROM kategorie WHERE Code = '{$catcode}'");
            if (mysql_errno() > 0) {
                XML_db_error("2-" . mysql_errno() . ": " . mysql_error());
            } else {
                $row_catcode = mysql_fetch_array($res_catcode);
                $category = $row_catcode[0];
                if ($bCombined) {
                    // create combined disciplines
                    if (mysql_num_rows($res) == 0) {
                        $_POST['combinedtype'] = $discode;
                        // needed by addCombinedEvent
                        $_POST['cat'] = $row_catcode[0];
                        // needed by addCombinedEvent
                        AA_meeting_addCombinedEvent($disfee, $_SESSION['meeting_infos']['Haftgeld'] / 100);
                        //$xDis = $cfgCombinedWO[$cfgCombinedDef[$discode]];
                    }
                }
            }
            if ($bCombined) {
                // create combined disciplines
                if (mysql_num_rows($res) == 0) {
                    // select again to get all generated xWettkampf ids
                    $query = "SELECT \r\n                                        w.xWettkampf \r\n                                    FROM\r\n                                        wettkampf as w\r\n                                        LEFT JOIN kategorie as k ON (w.xKategorie = k.xKategorie)\r\n                                        LEFT JOIN disziplin_" . $_COOKIE['language'] . " as d ON ( w.xDisziplin = d.xDisziplin)\r\n                                    WHERE  \r\n                                        w.xMeeting = " . $_COOKIE['meeting_id'] . "\r\n                                        AND    k.Code = '{$catcode}'\r\n                                        AND    w.Mehrkampfcode = {$discode}";
                    $res = mysql_query($query);
                    while ($row_dis = mysql_fetch_assoc($res)) {
                        $xDis[] = $row_dis['xWettkampf'];
                    }
                } else {
                    // combined event already exists, get existing disciplines
                    while ($row_dis = mysql_fetch_assoc($res)) {
                        $xDis[] = $row_dis['xWettkampf'];
                    }
                }
            } else {
                // create single disciplines
                if ($discode < $cfgResDisc || $discode > $cfgSvmDiscLast) {
                    if (mysql_num_rows($res) == 0) {
                        //discipline does not exist
                        // insert
                        //    ($disname will be empty if this is not a "special" discipline)
                        $sql = "INSERT INTO \r\n                                        wettkampf (xKategorie, xDisziplin, xMeeting\r\n                                            , Info, Haftgeld, Startgeld, OnlineId)\r\n                                    SELECT \r\n                                        k.xKategorie\r\n                                        , d.xDisziplin\r\n                                        , " . $_COOKIE['meeting_id'] . "\r\n                                        , '{$disname}'\r\n                                        , " . $_SESSION['meeting_infos']['Haftgeld'] / 100 . "\r\n                                        , '{$disfee}'\r\n                                        , '{$disid}'\r\n                                    FROM\r\n                                        disziplin_" . $_COOKIE['language'] . " as d\r\n                                        , (kategorie as k)\r\n                                    WHERE    d.Code = {$discode}\r\n                                    AND    k.Code = '{$catcode}'";
                        mysql_query($sql);
                        if (mysql_errno() > 0) {
                            XML_db_error("3-" . mysql_errno() . ": " . mysql_error());
                        } else {
                            $xDis[] = mysql_insert_id();
                        }
                    } else {
                        $row_dis = mysql_fetch_assoc($res);
                        $xDis[] = $row_dis['xWettkampf'];
                    }
                }
            }
        }
    }
    //
    // start of an athlete
    //
    if ($name == "ATHLETE" && (count($xDis) != 0 || $discode == $cfgResDisc || $discode >= $cfgSvmDiscFirst && $discode <= $cfgSvmDiscLast)) {
        $license = $attr['LICENSE'];
        if (isset($attr['PAID'])) {
            $paid = $attr['PAID'];
        }
        $relayDisc = AA_checkRelayDisc($discode);
        $effort = $attr['NOTIFICATIONEFFORT'];
        if ($relay_id > 0) {
            $relay_pos++;
        }
        $team_id = 0;
        $xTeam = 0;
        if (isset($attr['SVMTEAMID'])) {
            $team_id = $attr['SVMTEAMID'];
            $sql = "SELECT * FROM team WHERE xTeam = " . $team_id;
            $res = mysql_query($sql);
            if (mysql_num_rows($res) == 0) {
                return;
            }
        }
        if ($relay_id == 0 && $relayDisc) {
            // if event is a relay and the relay-id doesn't exist --> no announcement for the athletes
        } else {
            $athlete_id = 0;
            $sql2 = "SELECT TRIM(lastname) AS lastname, \r\n                            TRIM(firstname) AS firstname, \r\n                            substring(birth_date, 1,4) AS jahrgang, \r\n                            license, \r\n                            TRIM(sex) AS sex, \r\n                            nationality, \r\n                            birth_date, \r\n                            account_code, \r\n                            second_account_code,\r\n                            id_athlete ,\r\n                            account_info\r\n                       FROM base_athlete \r\n                      WHERE license = '" . $license . "';";
            $query2 = mysql_query($sql2);
            if ($query2 && mysql_num_rows($query2) == 1) {
                $row2 = mysql_fetch_assoc($query2);
                $club = $row2['account_code'];
                $club2 = $row2['second_account_code'];
                $vInfo = $row2['account_info'];
                $athlete_id = $row2['id_athlete'];
                $result2 = mysql_query("SELECT xVerein FROM verein WHERE xCode = '" . $club . "'");
                if (mysql_errno() > 0) {
                    XML_db_error("6-" . mysql_errno() . ": " . mysql_error());
                } else {
                    $rowClub1 = mysql_fetch_array($result2);
                    $club = $rowClub1[0];
                    if (!empty($club2)) {
                        $result2 = mysql_query("SELECT xVerein FROM verein WHERE xCode = '" . $club2 . "'");
                        if (mysql_errno() > 0) {
                            XML_db_error("7-" . mysql_errno() . ": " . mysql_error());
                            $club2 = 0;
                            // prevents from insert error in next statement
                        } else {
                            $rowClub2 = mysql_fetch_array($result2);
                            $club2 = $rowClub2[0];
                        }
                    } else {
                        $club2 = 0;
                    }
                }
                mysql_free_result($result2);
                // check if there are manual changes
                $sql4 = "SELECT Vorname, Name, xVerein, Manuell FROM athlet WHERE Lizenznummer = '" . $license . "'";
                $query4 = mysql_query($sql4);
                if (mysql_num_rows($query4) > 0) {
                    $row4 = mysql_fetch_assoc($query4);
                    if ($row4['Manuell'] == '0' || $GLOBALS['mode'] == 'overwrite') {
                        // overwrite the manual changes
                        $sql3 = "UPDATE athlet \r\n                            SET Name = '" . addslashes(trim($row2['lastname'])) . "', \r\n                                Vorname = '" . addslashes(trim($row2['firstname'])) . "', \r\n                                Jahrgang = '" . trim($row2['jahrgang']) . "', \r\n                                Geschlecht = '" . trim($row2['sex']) . "', \r\n                                Land = '" . trim($row2['nationality']) . "', \r\n                                Geburtstag = '" . trim($row2['birth_date']) . "', \r\n                                xVerein = '" . trim($club) . "', \r\n                                xVerein2 = '" . trim($club2) . "', \r\n                                Lizenznummer = '" . trim($license) . "',\r\n                                 Manuell = 0   \r\n                          WHERE (Lizenznummer = '" . trim($license) . "' \r\n                             OR (Name = '" . addslashes(trim($row2['lastname'])) . "' \r\n                            AND Vorname = '" . addslashes(trim($row2['firstname'])) . "' \r\n                            AND Jahrgang = '" . trim($row2['jahrgang']) . "' \r\n                            AND xVerein = '" . trim($club) . "'));";
                    } else {
                        switch ($row4['Manuell']) {
                            case 1:
                                $firstname = trim($row2['firstname']);
                                $name = trim($row4['Name']);
                                $verein = trim($club);
                                break;
                            case 2:
                                $firstname = $row4['Vorname'];
                                $name = trim($row2['lastname']);
                                $verein = trim($club);
                                break;
                            case 3:
                                $firstname = trim($row2['firstname']);
                                $name = trim($row2['lastname']);
                                $verein = $row4['xVerein'];
                                break;
                            case 4:
                                $firstname = $row4['Vorname'];
                                $name = $row4['Name'];
                                $verein = trim($club);
                                break;
                            case 5:
                                $firstname = trim($row2['firstname']);
                                $name = $row4['Name'];
                                $verein = $row4['xVerein'];
                                break;
                            case 6:
                                $firstname = $row4['Vorname'];
                                $name = trim($row2['lastname']);
                                $verein = $row4['xVerein'];
                                break;
                            case 7:
                                $firstname = $row4['Vorname'];
                                $name = $row4['Name'];
                                $verein = $row4['xVerein'];
                                break;
                            default:
                                break;
                        }
                        $sql3 = "UPDATE athlet \r\n                            SET Name = '" . addslashes($name) . "', \r\n                                Vorname = '" . addslashes($firstname) . "', \r\n                                Jahrgang = '" . trim($row2['jahrgang']) . "', \r\n                                Geschlecht = '" . trim($row2['sex']) . "', \r\n                                Land = '" . trim($row2['nationality']) . "', \r\n                                Geburtstag = '" . trim($row2['birth_date']) . "', \r\n                                xVerein = '" . $verein . "', \r\n                                xVerein2 = '" . trim($club2) . "', \r\n                                Lizenznummer = '" . trim($athlete['LICENSE']) . "'                           \r\n                          WHERE (Lizenznummer = '" . trim($athlete['LICENSE']) . "' \r\n                             OR (Name = '" . addslashes(trim($row2['lastname'])) . "' \r\n                            AND Vorname = '" . addslashes(trim($row2['firstname'])) . "' \r\n                            AND Jahrgang = '" . trim($row2['jahrgang']) . "' \r\n                            AND xVerein = '" . trim($club) . "'));";
                    }
                    $query3 = mysql_query($sql3);
                    if (mysql_errno() > 0) {
                        XML_db_error("4-" . mysql_errno() . ": " . mysql_error());
                    }
                }
            }
            // check if athlete is already in "athlet" table
            $result = mysql_query("SELECT xAthlet FROM athlet WHERE Lizenznummer = {$license}");
            if (mysql_errno() > 0) {
                XML_db_error("4-" . mysql_errno() . ": " . mysql_error());
            } else {
                //first copy athlete from base
                if (mysql_num_rows($result) == 0) {
                    $sql = "SELECT * FROM base_athlete\r\n                        WHERE license = {$license}";
                    $res = mysql_query($sql);
                    if (!$res) {
                        AA_printErrorMsg("5-" . mysql_errno() . ": " . mysql_error());
                    } else {
                        // check if athlete exists in base
                        if (mysql_num_rows($res) == 0) {
                            // athlete not found in base data
                            echo "<p>{$strBaseAthleteNotFound}: {$strLicenseNr} {$license}</p>\n";
                            $xAthlete = 0;
                        } else {
                            // get club id from club code
                            $row = mysql_fetch_assoc($res);
                            $club = $row['account_code'];
                            $club2 = $row['second_account_code'];
                            $athlete_id = $row['id_athlete'];
                            $result2 = mysql_query("SELECT xVerein FROM verein WHERE xCode = '" . $club . "'");
                            if (mysql_errno() > 0) {
                                XML_db_error("6-" . mysql_errno() . ": " . mysql_error());
                            } else {
                                $rowClub1 = mysql_fetch_array($result2);
                                $club = $rowClub1[0];
                                if (!empty($club2)) {
                                    $result2 = mysql_query("SELECT xVerein FROM verein WHERE xCode = '" . $club2 . "'");
                                    if (mysql_errno() > 0) {
                                        XML_db_error("7-" . mysql_errno() . ": " . mysql_error());
                                        $club2 = 0;
                                        // prevents from insert error in next statement
                                    } else {
                                        $rowClub2 = mysql_fetch_array($result2);
                                        $club2 = $rowClub2[0];
                                    }
                                }
                            }
                            mysql_free_result($result2);
                            // if club is valid
                            // insert athlete from base data
                            if (is_numeric($club)) {
                                $sql = "    INSERT IGNORE INTO athlet \r\n                                            (Name, Vorname, Jahrgang, \r\n                                            Lizenznummer, Geschlecht, Land, \r\n                                            Geburtstag, xVerein, xVerein2)\r\n                                        SELECT \r\n                                            TRIM(lastname), TRIM(firstname), substring(birth_date, 1,4), \r\n                                            license, TRIM(sex), nationality, \r\n                                            birth_date, '{$club}', '{$club2}'\r\n                                        FROM\r\n                                            base_athlete\r\n                                        WHERE\r\n                                            license = {$license}";
                                mysql_query($sql);
                                if (mysql_errno() > 0) {
                                    XML_db_error("8-" . mysql_errno() . ": " . mysql_error());
                                } else {
                                    $xAthlete = mysql_insert_id();
                                    if ($xAthlete == 0 || empty($xAthlete)) {
                                        echo "<p>++: {$strBaseAthleteNotFound}: {$strLicenseNr} {$license}</p>\n";
                                    }
                                }
                            }
                        }
                        // end athlete found
                    }
                } else {
                    // athlete already available
                    // check if athlete is still in base data (he could be deleted)
                    $sql = "SELECT * FROM base_athlete\r\n                        WHERE license = {$license}";
                    $res = mysql_query($sql);
                    if (mysql_errno() > 0) {
                        XML_db_error("5b-" . mysql_errno() . ": " . mysql_error());
                    } else {
                        if (mysql_num_rows($res) == 0) {
                            // athlete deleted
                            echo "<p>{$strBaseAthleteNotFound} (deleted): {$strLicenseNr} {$license}</p>\n";
                            $xAthlete = 0;
                        } else {
                            // athlete available, get id
                            $row = mysql_fetch_array($result);
                            $xAthlete = $row[0];
                        }
                    }
                }
                if ($xAthlete > 0) {
                    // check if already registered
                    $result = mysql_query("SELECT xAnmeldung, xTeam , xKategorie FROM anmeldung WHERE xAthlet = {$xAthlete} AND xMeeting = " . $_COOKIE['meeting_id'] . "");
                    if (mysql_errno() > 0) {
                        XML_db_error("9-" . mysql_errno() . ": " . mysql_error());
                    } else {
                        if (mysql_num_rows($result) == 0) {
                            // not yet registered
                            // get license category from base data
                            $result = mysql_query("    \r\n                                        SELECT k.xKategorie FROM\r\n                                            kategorie as k\r\n                                            , base_athlete as b\r\n                                        WHERE b.license = {$license}\r\n                                        AND k.Code = b.license_cat");
                            if (mysql_errno() > 0) {
                                XML_db_error("10-" . mysql_errno() . ": " . mysql_error());
                            } else {
                                if (mysql_num_rows($result) > 0) {
                                    $row = mysql_fetch_array($result);
                                    $xCat = $row[0];
                                    if ($xCat != '') {
                                        mysql_query("    INSERT INTO anmeldung SET\r\n                                                    Startnummer = 0\r\n                                                    , Bezahlt = '{$paid}'\r\n                                                    , xAthlet = {$xAthlete}\r\n                                                    , xMeeting = " . $_COOKIE['meeting_id'] . "\r\n                                                    , xKategorie = {$xCat}\r\n                                                    , Vereinsinfo = '{$vInfo}'\r\n                                                    , xTeam = " . $team_id);
                                        if (mysql_errno() > 0) {
                                            XML_db_error("11-" . mysql_errno() . ": " . mysql_error());
                                        } else {
                                            $xReg = mysql_insert_id();
                                        }
                                    } else {
                                        $result2 = mysql_query("SELECT license_cat \r\n                                                                  FROM base_athlete\r\n                                                                 WHERE license = {$license};");
                                        $row2 = mysql_fetch_array($result2);
                                        $license_cat = $row2[0];
                                        XML_db_error(str_replace('%cat%', $license_cat, $strNoSuchCategory));
                                    }
                                } else {
                                }
                            }
                        } else {
                            // registered
                            $row = mysql_fetch_array($result);
                            $xReg = $row[0];
                            $xTeam = $row[1];
                            $xCat = $row[2];
                            if ($xTeam != $team_id && $team_id > 0) {
                                if ($xTeam > 0) {
                                    if (!in_array($xTeam, $arr_noCat[$xAthlete])) {
                                        $arr_noCat[$xAthlete][] = $xTeam;
                                    }
                                }
                                if (!in_array($team_id, $arr_noCat[$xAthlete])) {
                                    $arr_noCat[$xAthlete][] = $team_id;
                                }
                            }
                        }
                        if ($bCombined) {
                            // effort are points, saved on registration
                            $sql = "SELECT notification_effort \r\n                                    FROM base_performance\r\n                                    WHERE id_athlete = {$athlete_id}\r\n                                    AND    discipline = {$discode}";
                            $res_effort = mysql_query($sql);
                            if (mysql_errno() > 0) {
                                XML_db_error("12-" . mysql_errno() . ": " . mysql_error());
                            } else {
                                if (mysql_num_rows($res_effort) > 0) {
                                    $row_effort = mysql_fetch_assoc($res_effort);
                                    $effort = $row_effort['notification_effort'];
                                    mysql_query("UPDATE anmeldung SET\r\n                                            BestleistungMK = '{$effort}'\r\n                                        WHERE\r\n                                            xAnmeldung = {$xReg}");
                                }
                            }
                        }
                        if ($xReg > 0) {
                            // check if athlete alredy starts for this discipline(s)
                            foreach ($xDis as $xDis1) {
                                // because we can get multiple disciplines (combined event),
                                // it is nessesary to determinate distype and discode for each discipline
                                // (catcode won't change)
                                $res_distype = mysql_query("\r\n                                            SELECT \r\n                                                d.Typ, d.Code \r\n                                            FROM \r\n                                                disziplin_" . $_COOKIE['language'] . " as d\r\n                                                LEFT JOIN wettkampf as w ON (w.xDisziplin = d.xDisziplin)\r\n                                            WHERE \r\n                                                w.xWettkampf = {$xDis1}");
                                if (mysql_errno() > 0) {
                                    XML_db_error("13-" . mysql_errno() . ": " . mysql_error());
                                } else {
                                    $row_distype = mysql_fetch_Array($res_distype);
                                    $distype = $row_distype[0];
                                    $temp_discode = $row_distype[1];
                                }
                                $result = mysql_query("SELECT xStart FROM start WHERE xAnmeldung = {$xReg} and xWettkampf = {$xDis1}");
                                if (mysql_errno() > 0) {
                                    XML_db_error("14-" . mysql_errno() . ": " . mysql_error());
                                } else {
                                    if (mysql_num_rows($result) == 0) {
                                        // not yet starting, add start
                                        $saison = $_SESSION['meeting_infos']['Saison'];
                                        if ($saison == '') {
                                            $saison = "O";
                                            //if no saison is set take outdoor
                                        }
                                        if (!$bCombined) {
                                            // check on notification effort.
                                            $res_effort = mysql_query("\r\n                                                    SELECT * FROM base_performance\r\n                                                    WHERE    id_athlete = {$athlete_id}\r\n                                                    AND    discipline = {$temp_discode}\r\n                                                    AND    category = '{$catcode}'");
                                            $sql = "SELECT * FROM base_performance\r\n                                                    WHERE    id_athlete = {$athlete_id}\r\n                                                    AND    discipline = {$temp_discode}\r\n                                                    AND    category = '{$catcode}'";
                                        } else {
                                            $sql = "SELECT\r\n                                                    base_performance.notification_effort\r\n                                                FROM\r\n                                                    athletica.base_performance\r\n                                                    INNER JOIN athletica.disziplin_" . $_COOKIE['language'] . " AS d \r\n                                                        ON (base_performance.discipline = d.Code)\r\n                                                    INNER JOIN athletica.wettkampf \r\n                                                        ON (d.xDisziplin = wettkampf.xDisziplin)\r\n                                                WHERE (base_performance.id_athlete ={$athlete_id}\r\n                                                    AND wettkampf.xWettkampf ={$xDis1}\r\n                                                    AND wettkampf.xMeeting =" . $_COOKIE['meeting_id'] . "\r\n                                                    AND base_performance.season ='I')";
                                            $res_effort = mysql_query($sql);
                                        }
                                        if (mysql_errno() > 0) {
                                            XML_db_error("15-" . mysql_errno() . ": " . mysql_error());
                                        } else {
                                            if (mysql_num_rows($res_effort) > 0) {
                                                $row_effort = mysql_fetch_assoc($res_effort);
                                                $effort = $row_effort['notification_effort'];
                                            }
                                            //
                                            // convert effort
                                            //
                                            if ($distype == $cfgDisciplineType[$strDiscTypeTrack] || $distype == $cfgDisciplineType[$strDiscTypeTrackNoWind] || $distype == $cfgDisciplineType[$strDiscTypeRelay] || $distype == $cfgDisciplineType[$strDiscTypeDistance]) {
                                                $pt = new PerformanceTime($effort);
                                                $perf = $pt->getPerformance();
                                            } else {
                                                $pa = new PerformanceAttempt($effort);
                                                $perf = $pa->getPerformance();
                                                //$perf = (ltrim($effort,"0"))*100;
                                            }
                                            if ($perf == NULL) {
                                                // invalid performance
                                                $perf = 0;
                                            }
                                            $sql = "INSERT INTO start SET\r\n                                                        xWettkampf = {$xDis1}\r\n                                                        , Bezahlt = '{$paid}'\r\n                                                        , xAnmeldung = {$xReg}\r\n                                                        , Bestleistung = '" . $perf . "' \r\n                                                        , BaseEffort = 'y'";
                                            $res = mysql_query($sql);
                                            if (mysql_errno() > 0) {
                                                XML_db_error("16-" . mysql_errno() . ": " . mysql_error());
                                            }
                                            if ($relay_id > 0) {
                                                $xStart = mysql_insert_id();
                                                $sql = "INSERT INTO staffelathlet SET\r\n                                                            xStaffelstart=" . $relay_xStart . " \r\n                                                            , xAthletenstart = " . $xStart . "\r\n                                                            , xRunde = " . $relay_round . "\r\n                                                            , Position = " . $relay_pos;
                                                $res = mysql_query($sql);
                                                if (mysql_errno() > 0) {
                                                    XML_db_error("16-" . mysql_errno() . ": " . mysql_error());
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            // enf foreach
                        }
                        // end xReg > 0
                    }
                }
                // end xAthlete > 0
            }
        }
    }
    //
    // start of a relay
    //
    if ($name == "RELAY" && count($xDis) != 0) {
        $id = $attr['ID'];
        $paid = $attr['PAID'];
        $notificationEffort = $attr['NOTIFICATIONEFFORT'];
        $teamID = $attr['TEAMID'];
        $relay_id = 0;
        $xDis1 = $xDis[0];
        $relay_pos = 0;
        // check if relay is already in table staffel
        $res = mysql_query("SELECT xStaffel FROM staffel WHERE xStaffel = {$id}");
        if (mysql_errno() > 0) {
            XML_db_error(mysql_errno() . ": " . mysql_error());
        } else {
            if (mysql_num_rows($res) == 0) {
                //
                // no, insert relay (get category first)
                //
                $result = mysql_query("    SELECT k.xKategorie, b.is_athletica_gen, b.relay_name, b.account_code FROM\r\n                                            kategorie as k\r\n                                            LEFT JOIN base_relay as b ON (k.Code = b.category)\r\n                                        WHERE \r\n                                                b.id_relay = '{$id}'");
                if (mysql_errno() > 0) {
                    XML_db_error(mysql_errno() . ": " . mysql_error());
                } else {
                    if (mysql_num_rows($result) > 0) {
                        $row = mysql_fetch_array($result);
                        $cat = $row[0];
                        if (empty($teamID)) {
                            $teamID = 0;
                        }
                        mysql_query("\r\n                            INSERT IGNORE INTO staffel\r\n                                (xStaffel, Name, xVerein\r\n                                , xMeeting, xKategorie, Athleticagen, xTeam)\r\n                            SELECT\r\n                                id_relay, relay_name, account_code\r\n                                , " . $_COOKIE['meeting_id'] . ", '{$cat}',is_athletica_gen , {$teamID}\r\n                            FROM\r\n                                base_relay\r\n                            WHERE\r\n                                id_relay = '{$id}'");
                        if (mysql_errno() > 0) {
                            XML_db_error(mysql_errno() . ": " . mysql_error());
                        } else {
                            $relay_id = $id;
                            // do not use mysql_insert_id() - it will return 0
                            // like this i will notice if the insert succeeded or not
                            if (mysql_affected_rows() == 0) {
                                echo "Fehler: Staffel ID {$id}";
                            }
                        }
                    } else {
                        $msg = str_replace('%id%', $id, $strBaseRelayNotFound);
                        echo "<p>{$msg}: </p>\n";
                    }
                }
            } else {
                //
                // yes, relay found
                //
                if (!empty($teamID)) {
                    $sql = "UPDATE staffel SET xTeam = " . $teamID . " WHERE xStaffel = " . $id;
                    mysql_query($sql);
                    if (mysql_errno() > 0) {
                        XML_db_error(mysql_errno() . ": " . mysql_error());
                    } else {
                        $relay_id = $id;
                        // do not use mysql_insert_id() - it will return 0
                    }
                } else {
                    $row = mysql_fetch_array($res);
                    $relay_id = $row[0];
                }
            }
            //
            // add start for discipline
            //
            if ($relay_id > 0) {
                $sql = "SELECT \r\n                                xStart, xRunde \r\n                        FROM \r\n                                start AS s\r\n                                LEFT JOIN runde AS r ON (r.xWettkampf = s.xWettkampf) \r\n                        WHERE \r\n                                s.xWettkampf = " . $xDis1 . " AND s.xStaffel = " . $relay_id;
                $result = mysql_query($sql);
                if (mysql_errno() > 0) {
                    XML_db_error(mysql_errno() . ": " . mysql_error());
                } else {
                    if (mysql_num_rows($result) == 0) {
                        // insert into table start
                        mysql_query("\r\n                            INSERT INTO start SET                                 \r\n                                Bezahlt = '" . $paid . "',\r\n                                xWettkampf = {$xDis1}\r\n                                , xStaffel = {$relay_id}");
                        if (mysql_errno() > 0) {
                            XML_db_error(mysql_errno() . ": " . mysql_error());
                        }
                        $relay_xStart = mysql_insert_id();
                        if ($team_type == "SVM") {
                            $sql = "SELECT \r\n                                            xRunde \r\n                                         FROM \r\n                                            start AS s\r\n                                            LEFT JOIN runde AS r ON (r.xWettkampf = s.xWettkampf) \r\n                                         WHERE \r\n                                            s.xWettkampf = " . $xDis1 . " AND s.xStaffel = " . $relay_id;
                            $res = mysql_query($sql);
                            if (mysql_errno() > 0) {
                                XML_db_error(mysql_errno() . ": " . mysql_error());
                            } else {
                                $row = mysql_fetch_row($res);
                                $relay_round = $row[0];
                            }
                        } else {
                            $sql = "SELECT \r\n                                        r.xRunde, w.xWettkampf , d.Code , d.Typ, m.DatumVon\r\n                                     FROM\r\n                                        wettkampf as w\r\n                                        LEFT JOIN runde as r On (w.xWettkampf = r.xWettkampf)\r\n                                        LEFT JOIN disziplin_" . $_COOKIE['language'] . " as d ON (d.xDisziplin = w.xDisziplin)\r\n                                        LEFT JOIN meeting as m ON (m.xMeeting = w.xMeeting)\r\n                                     WHERE\r\n                                        w.xKategorie = " . $category . "        \r\n                                        AND w.xMeeting = " . $_COOKIE['meeting_id'] . " \r\n                                     ORDER BY d.Anzeige";
                            $res = mysql_query($sql);
                            if (mysql_errno() > 0) {
                                XML_db_error(mysql_errno() . ": " . mysql_error());
                            } else {
                                $row = mysql_fetch_row($res);
                                // check if dummy round already exists
                                if ($relay_id > 0) {
                                    $event = $xDis1;
                                } else {
                                    $event = $row[1];
                                }
                                $sql = "SELECT * FROM runde WHERE xWettkampf =  " . $event;
                                $res = mysql_query($sql);
                                if (mysql_num_rows($res) == 0) {
                                    // add dummy round for relay
                                    $sql = "INSERT into runde SET\r\n                                                    Datum = '" . $row[4] . "'    \r\n                                                    , xRundentyp = 6 \r\n                                                    , xWettkampf = " . $event;
                                    $res = mysql_query($sql);
                                    if (mysql_errno() > 0) {
                                        XML_db_error(mysql_errno() . ": " . mysql_error());
                                    }
                                    $relay_round = mysql_insert_id();
                                }
                            }
                        }
                    } else {
                        // already entered
                        $row = mysql_fetch_array($result);
                        $relay_xStart = $row[0];
                        $relay_round = $row[1];
                        $sql = "UPDATE start SET \r\n                                Bezahlt = '" . $paid . "'\r\n                                WHERE xStart = " . $relay_xStart;
                        mysql_query($sql);
                        if (mysql_errno() > 0) {
                            XML_db_error(mysql_errno() . ": " . mysql_error());
                        }
                    }
                }
            }
        }
    }
    if ($name == "TEAM") {
        $id = $attr['TEAMID'];
        $team_type = $attr['TYPE'];
        $team_name = $attr['NAME'];
        $svm_cat = $attr['SVMCATEGORYCODE'];
        $team_id = 0;
        if ($team_type == "SVM") {
            $teamInBase = true;
            // check if team with same id is already in table team
            $sql = "SELECT xTeam FROM team WHERE xTeam = " . $id . " AND xMeeting = " . $_COOKIE['meeting_id'];
            $res = mysql_query($sql);
            if (mysql_errno() > 0) {
                XML_db_error(mysql_errno() . ": " . mysql_error());
            } else {
                if (mysql_num_rows($res) == 0) {
                    //
                    // no, insert relay (get category first)
                    //
                    $sql = "SELECT k.Code, b.is_athletica_gen, b.svm_name, b.account_code FROM\r\n                                                kategorie_svm as k\r\n                                                LEFT JOIN base_svm as b ON (k.Code = b.svm_category)\r\n                                            WHERE \r\n                                                    b.id_svm = '{$id}'";
                    $result = mysql_query($sql);
                    if (mysql_errno() > 0) {
                        XML_db_error(mysql_errno() . ": " . mysql_error());
                    } else {
                        if (mysql_num_rows($result) > 0) {
                            $row = mysql_fetch_array($result);
                            $svmCode = $row[0];
                            $cat = 0;
                            if (isset($cfgSVM[$svmCode . "_C"])) {
                                $cat_code = $cfgSVM[$svmCode . "_C"];
                            }
                            $sql = "SELECT xKategorie FROM kategorie WHERE Code = '" . $cat_code . "'";
                            $res = mysql_query($sql);
                            if (mysql_errno() > 0) {
                                XML_db_error(mysql_errno() . ": " . mysql_error());
                            }
                            $row_cat = mysql_fetch_row($res);
                            $cat = $row_cat[0];
                            // get svm cat
                            $sql = "SELECT xKategorie_svm FROM kategorie_svm WHERE Code = '" . $svm_cat . "'";
                            $res = mysql_query($sql);
                            if (mysql_errno() > 0) {
                                XML_db_error(mysql_errno() . ": " . mysql_error());
                            }
                            $row_cat_svm = mysql_fetch_row($res);
                            $xCat_svm = $row_cat_svm[0];
                            $sql = "SELECT xTeam FROM team WHERE xKategorie = " . $cat . " AND Name = '" . $team_name . "' AND xKategorie_svm = " . $xCat_svm . " AND xMeeting = " . $_COOKIE['meeting_id'];
                            $res = mysql_query($sql);
                            if (mysql_errno() > 0) {
                                XML_db_error(mysql_errno() . ": " . mysql_error());
                            }
                            if (mysql_num_rows($res) > 0) {
                                $row_team = mysql_fetch_row($res);
                                $sql = "UPDATE team\r\n                                                    SET \r\n                                                    xTeam =  " . $row[3] . ",\r\n                                                    Athleticagen = '" . $row[1] . "'\r\n                                              WHERE\r\n                                                    xTeam = " . $row_team[0];
                            } else {
                                $sql = "INSERT IGNORE INTO team\r\n                                                (xTeam, Name, Athleticagen, xMeeting,\r\n                                                  xVerein , xKategorie, xKategorie_svm )\r\n                                            SELECT\r\n                                                id_svm, svm_name, is_athletica_gen , " . $_COOKIE['meeting_id'] . ",\r\n                                                account_code ,  '{$cat}' , '{$xCat_svm}'\r\n                                               \r\n                                            FROM\r\n                                                base_svm\r\n                                            WHERE\r\n                                                id_svm = '{$id}'";
                            }
                            mysql_query($sql);
                            if (mysql_errno() > 0) {
                                XML_db_error(mysql_errno() . ": " . mysql_error());
                            } else {
                                $team_id = $id;
                                // do not use mysql_insert_id() - it will return 0
                                // like this i will notice if the insert succeeded or not
                                if (mysql_affected_rows() == 0) {
                                    echo "Fehler: Team ID {$id}";
                                }
                            }
                        } else {
                            // team not in base data
                            $msg = str_replace('%name%', $team_name, $strBaseTeamNotFound);
                            $msg = str_replace('%id%', $id, $msg);
                            echo "<p>{$msg}: </p>\n";
                            $teamInBase = false;
                        }
                    }
                } else {
                    //
                    // yes, team found
                    //
                    $row = mysql_fetch_array($res);
                    $team_id = $row[0];
                }
            }
        }
    }
    if ($name == "SVMCATCODE") {
        $svmCode = $attr['SVMCATEGORYCODE'];
        //
        //  create SVM disciplines
        $cat = 0;
        if (isset($cfgSVM[$svmCode . "_C"])) {
            $cat_code = $cfgSVM[$svmCode . "_C"];
        }
        $sql = "SELECT xKategorie FROM kategorie WHERE Code = '" . $cat_code . "'";
        $res = mysql_query($sql);
        if (mysql_errno() > 0) {
            XML_db_error(mysql_errno() . ": " . mysql_error());
        }
        $row_cat = mysql_fetch_row($res);
        $cat = $row_cat[0];
        $_POST['cat'] = $row_cat[0];
        $res = mysql_query("SELECT ks.xKategorie_svm FROM kategorie_svm AS ks WHERE ks.Code = '{$svmCode}'");
        $row = mysql_fetch_array($res);
        $_POST['svmcategory'] = $row[0];
        $_POST['svm'] = $row[0];
        $sql = "SELECT \r\n                        m.DatumVon\r\n                 FROM      \r\n                        meeting AS m\r\n                 WHERE      \r\n                        m.xMeeting = " . $_COOKIE['meeting_id'];
        $res = mysql_query($sql);
        $row = mysql_fetch_row($res);
        $_POST['date'] = $row[0];
        $_POST['wTyp'] = $cfgSVM[$svmCode . "_ET"];
        AA_meeting_addSVMEvent($_SESSION['meeting_infos']['Startgeld'] / 100, $_SESSION['meeting_infos']['Haftgeld'] / 100);
    }
}
                 } else {
                     // add new svm event
                     AA_Meeting_getEventType();
                     $_POST['date'] = $_SESSION['meeting_infos']['DatumVon'];
                     $_POST['svm'] = $_POST['svmcategory'];
                     AA_meeting_addSVMEvent($_SESSION['meeting_infos']['Startgeld'] / 100, $_SESSION['meeting_infos']['Haftgeld'] / 100);
                 }
                 mysql_query("UNLOCK TABLES");
                 if ($GLOBALS['AA_ERROR'] != '') {
                     AA_printErrorMsg($GLOBALS['AA_ERROR']);
                 }
             }
         }
     }
 } elseif ($_POST['arg'] == "add_combtype") {
     AA_meeting_addCombinedEvent($_SESSION['meeting_infos']['Startgeld'] / 100, $_SESSION['meeting_infos']['Haftgeld'] / 100);
 } elseif ($_POST['arg'] == "add_svmcat") {
     AA_Meeting_getEventType();
     $_POST['date'] = $_SESSION['meeting_infos']['DatumVon'];
     $_POST['svm'] = $_POST['svmcategory'];
     AA_meeting_addSVMEvent($_SESSION['meeting_infos']['Startgeld'] / 100, $_SESSION['meeting_infos']['Haftgeld'] / 100);
     if ($GLOBALS['AA_ERROR'] != '') {
         AA_printErrorMsg($GLOBALS['AA_ERROR']);
     }
 } elseif ($_POST['arg'] == "change_formula") {
     if (isset($_POST['nocat'])) {
         AA_meeting_changeFormula();
     } else {
         AA_meeting_changeCategory($_POST['item']);
     }
 } elseif ($_POST['arg'] == "new_discipline") {
function XML_regUKC($xml_simple, $ukc_meeting)
{
    global $arr_noCat, $discode, $catcode, $xDis, $distype, $cfgUKC_disc;
    $kidID = 0;
    $license = 0;
    $licensePaid = '';
    $announcement = '';
    $club = '';
    $firstname = '';
    $lastname = '';
    // check if clubs are in table verein
    foreach ($xml_simple->accounts->account as $account) {
        $club = $account->accountCode;
        $clubName = mysql_real_escape_string(str_replace("\r", "\n", trim(utf8_decode($account->accountName))));
        $clubNameShort = mysql_real_escape_string(str_replace("\r", "\n", trim(utf8_decode($account->accountShort))));
        if (empty($clubNameShort)) {
            $clubNameShort = $clubName;
        }
        $laenge = strlen($clubName);
        $laengeShort = strlen($clubNameShort);
        $result = mysql_query("SELECT xVerein FROM verein WHERE xCode = '" . $club . "'");
        if (mysql_errno() > 0) {
            AA_printErrorMsg("xml-19-" . mysql_errno() . ": " . mysql_error());
        } else {
            if (mysql_num_rows($result) == 0) {
                $sql = "INSERT INTO verein SET\r\n                                        Name = '" . $clubName . "',   \r\n                                        Sortierwert =  '" . $clubNameShort . "',   \r\n                                        xCode =  '" . $club . "'";
                $result = mysql_query($sql);
                if (mysql_errno() > 0) {
                    AA_printErrorMsg("xml-19-" . mysql_errno() . ": " . mysql_error());
                }
            }
        }
    }
    // announcement for each athlete
    foreach ($xml_simple->athletes->athlete as $athlete) {
        $kidID = 0;
        $license = 0;
        $licensePaid = '';
        $announcement = '';
        $club = '';
        $clubnr = '';
        $xDis = array();
        $xAthlete = 0;
        $xReg = 0;
        $street = '';
        $zipCode = 0;
        $city = '';
        $clubName = '';
        $email = '';
        $canton = '';
        $nationality = '';
        foreach ($athlete->attributes() as $attr => $value) {
            switch ($attr) {
                case 'license':
                    if ($value > 0) {
                        $license = $value;
                    }
                    break;
                case 'licensePaid':
                    if ($value == 1) {
                        $licensePaid = 'y';
                    } else {
                        $licensePaid = 'n';
                    }
                    break;
                case 'kidID':
                    if ($value > 0) {
                        $kidID = $value;
                    }
                    break;
                case 'announcement':
                    $announcement = $value;
                    break;
            }
        }
        $meetingDate = '';
        $discode = 0;
        $lastName = mysql_real_escape_string(str_replace("\r", "\n", trim(utf8_decode($athlete->lastName))));
        $firstName = mysql_real_escape_string(str_replace("\r", "\n", trim(utf8_decode($athlete->firstName))));
        $birthDate = $athlete->birthDate;
        $arr_birth = explode(".", $birthDate);
        $birthdate = $arr_birth[2] . "-" . $arr_birth[0] . "-" . $arr_birth[1];
        $birthYear = $arr_birth[2];
        if (!empty($athlete->accountCode)) {
            $club = $athlete->accountCode;
        }
        $sex = str_replace("\r", "\n", trim(utf8_decode($athlete->sex)));
        $nationality = str_replace("\r", "\n", trim(utf8_decode($athlete->nationality)));
        $street = mysql_real_escape_string(str_replace("\r", "\n", trim(utf8_decode($athlete->personalData->street))));
        $zipCode = str_replace("\r", "\n", trim(utf8_decode($athlete->personalData->zipCode)));
        $city = mysql_real_escape_string(str_replace("\r", "\n", trim(utf8_decode($athlete->personalData->city))));
        $clubName = mysql_real_escape_string(str_replace("\r", "\n", trim(utf8_decode($athlete->personalData->club))));
        $email = mysql_real_escape_string(str_replace("\r", "\n", trim(utf8_decode($athlete->personalData->email))));
        $canton = str_replace("\r", "\n", trim(utf8_decode($athlete->personalData->canton)));
        // get region
        $region = 0;
        if (!empty($canton)) {
            $sql_reg = "SELECT xRegion FROM region WHERE Anzeige = '" . $canton . "'";
            $res_reg = mysql_query($sql_reg);
            if (mysql_errno() > 0) {
                AA_printErrorMsg("xml-1-" . mysql_errno() . ": " . mysql_error());
            } else {
                if (mysql_num_rows($res_reg) == 1) {
                    $row = mysql_fetch_row($res_reg);
                    $region = $row[0];
                }
            }
        } else {
            $canton = 0;
        }
        if (empty($zipCode)) {
            $zipCode = 0;
        }
        // get category
        $currYear = date('Y');
        $age = $currYear - $birthYear;
        if ($ukc_meeting == 'y') {
            // normal UBS Kids Cup Meeting (only UBS Kids Cup)
            if ($age < 7) {
                $age = 7;
                //  minimum age
            }
            $sql_cat = "SELECT xKategorie, Code FROM kategorie AS k WHERE k.alterslimite = " . $age . " AND k.Geschlecht = '" . $sex . "' AND k.UKC = 'y' AND k.aktiv = 'y'";
        } else {
            //normal Meetring with UBS Kids Cup integration
            if ($age <= 7) {
                $age = 9;
                //  minimum age
            }
            if ($age % 2 == 0) {
                $age++;
            }
            $sql_cat = "SELECT xKategorie, Code FROM kategorie AS k WHERE k.alterslimite <= " . $age . " AND k.Geschlecht = '" . $sex . "' AND k.UKC = 'n' AND k.aktiv = 'y'";
        }
        $res_cat = mysql_query($sql_cat);
        if (mysql_errno() > 0) {
            AA_printErrorMsg("xml-1-" . mysql_errno() . ": " . mysql_error());
        } else {
            if (mysql_num_rows($res_cat) > 0) {
                $row_cat = mysql_fetch_row($res_cat);
                $cat = $row_cat[1];
                $xCat = $row_cat[0];
                $selection = "";
                if ($ukc_meeting == 'n') {
                    $selection = " AND (d.Code = " . $cfgUKC_disc[0] . " || d.Code = " . $cfgUKC_disc[1] . " || d.Code = " . $cfgUKC_disc[2] . ") ";
                }
                $sql = "SELECT \r\n                                    w.xWettkampf,\r\n                                    w.Mehrkampfcode,\r\n                                    k.xKategorie,\r\n                                    m.DatumVon   \r\n                                FROM\r\n                                    wettkampf as w\r\n                                    LEFT JOIN kategorie as k on (w.xKategorie = k.xKategorie )\r\n                                    LEFT JOIN disziplin_" . $_COOKIE['language'] . " as d on (w.xDisziplin = d.xDisziplin)\r\n                                    LEFT JOIN meeting AS m ON (m.xMeeting = w.xMeeting)  \r\n                                WHERE \r\n                                    w.xMeeting = " . $_COOKIE['meeting_id'] . "\r\n                                    {$selection}\r\n                                    AND k.Kurzname = '" . $cat . "'";
                $res = mysql_query($sql);
                if (mysql_num_rows($res) >= 1 && $ukc_meeting == 'y' || mysql_num_rows($res) == 3 && $ukc_meeting == 'n') {
                    while ($row_dis = mysql_fetch_assoc($res)) {
                        $xDis[] = $row_dis['xWettkampf'];
                        if ($row_dis['Mehrkampfcode'] > 0) {
                            $discode = $row_dis['Mehrkampfcode'];
                            // same for all combined disciplines
                        }
                        $catnr = $row_dis['xKategorie'];
                        $meetingDate = $row_dis['DatumVon'];
                    }
                } else {
                    if ($ukc_meeting == 'y') {
                        $_POST['combinedtype'] = 408;
                        $_POST['cat'] = $xCat;
                        AA_meeting_addCombinedEvent($_SESSION['meeting_infos']['Startgeld'] / 100, $_SESSION['meeting_infos']['Haftgeld'] / 100);
                    } else {
                        AA_meeting_addUkcEvent($xCat, $_SESSION['meeting_infos']['Startgeld'] / 100, $_SESSION['meeting_infos']['Haftgeld'] / 100);
                    }
                    $res = mysql_query($sql);
                    if (mysql_errno() > 0) {
                        AA_printErrorMsg("xml-1-" . mysql_errno() . ": " . mysql_error());
                    }
                    if (mysql_num_rows($res) > 0) {
                        while ($row_dis = mysql_fetch_assoc($res)) {
                            $xDis[] = $row_dis['xWettkampf'];
                            if ($row_dis['Mehrkampfcode'] > 0) {
                                $discode = $row_dis['Mehrkampfcode'];
                                // same for all combined disciplines
                            }
                            $meetingDate = $row_dis['DatumVon'];
                        }
                    }
                }
                if (!empty($club)) {
                    $result2 = mysql_query("SELECT xVerein FROM verein WHERE xCode = '" . $club . "'");
                    if (mysql_errno() > 0) {
                        AA_printErrorMsg("xml-19-" . mysql_errno() . ": " . mysql_error());
                    } else {
                        if (mysql_num_rows($result2) > 0) {
                            $rowClub1 = mysql_fetch_array($result2);
                            $clubnr = $rowClub1[0];
                        }
                    }
                } elseif (!empty($clubName)) {
                    // selfmade club
                    $result2 = mysql_query("SELECT xVerein FROM verein WHERE Name = '" . $clubName . "'");
                    if (mysql_errno() > 0) {
                        AA_printErrorMsg("xml-19-" . mysql_errno() . ": " . mysql_error());
                    } else {
                        if (mysql_num_rows($result2) > 0) {
                            $rowClub1 = mysql_fetch_array($result2);
                            $clubnr = $rowClub1[0];
                        } else {
                            $sql = "INSERT into verein SET\r\n                                                            Name = '" . $clubName . "',\r\n                                                            Sortierwert = '" . $clubName . "'";
                            mysql_query($sql);
                            $clubnr = mysql_insert_id();
                        }
                    }
                }
                if (empty($club) && empty($clubName) && $license == 0) {
                    $clubnr = 999999;
                }
                // if club is valid, insert athlete
                if (is_numeric($clubnr)) {
                    // if athlet exist
                    if ($license > 0) {
                        $sql = "SELECT * FROM athlet WHERE Lizenznummer= " . $license;
                    } else {
                        $sql = "SELECT * FROM athlet WHERE Name= '" . $lastName . "' AND Vorname = '" . $firstName . "' AND Geburtstag = '" . $birthdate . "'";
                    }
                    $res = mysql_query($sql);
                    if (mysql_errno() > 0) {
                        AA_printErrorMsg("xml-20-" . mysql_errno() . ": " . mysql_error());
                    } else {
                        $row = mysql_fetch_array($res);
                        if (mysql_num_rows($res) == 0) {
                            if ($license > 0) {
                                $licenseType = 1;
                            } else {
                                $licenseType = 3;
                            }
                            $sql = "INSERT IGNORE INTO athlet SET\r\n                                                                        Name = '" . $lastName . "',\r\n                                                                        Vorname = '" . $firstName . "',  \r\n                                                                        Jahrgang = '" . $birthYear . "', \r\n                                                                        Lizenznummer = " . $license . " , \r\n                                                                        Geschlecht = '" . $sex . "', \r\n                                                                        Land = '" . $nationality . "', \r\n                                                                        Geburtstag = '" . $birthdate . "',\r\n                                                                        Athleticagen = 'n',   \r\n                                                                        Bezahlt = '" . $licensePaid . "', \r\n                                                                        xVerein = '" . $clubnr . "',                                                                  \r\n                                                                        Lizenztyp = " . $licenseType . ",\r\n                                                                        xRegion = " . $region . ",   \r\n                                                                        Adresse = '" . $street . "',  \r\n                                                                        Plz = " . $zipCode . ",  \r\n                                                                        Ort = '" . $city . "',  \r\n                                                                        Email = '" . $email . "'";
                            mysql_query($sql);
                            if (mysql_errno() > 0) {
                                AA_printErrorMsg("xml-21-" . mysql_errno() . ": " . mysql_error());
                            } else {
                                $xAthlete = mysql_insert_id();
                            }
                        } else {
                            if ($license > 0) {
                                $sql = "UPDATE athlet SET                                                             \r\n                                                            Land = '" . $nationality . "',                                                                \r\n                                                            Bezahlt = '" . $licensePaid . "',      \r\n                                                            xRegion = " . $region . ",   \r\n                                                            Adresse = '" . $street . "',  \r\n                                                            Plz = " . $zipCode . ",  \r\n                                                            Ort = '" . $city . "',  \r\n                                                            Email = '" . $email . "'\r\n                                                            WHERE Lizenznummer= " . $license;
                            } else {
                                $sql = "UPDATE athlet SET  \r\n                                                            Land = '" . $nationality . "',                                                              \r\n                                                            Bezahlt = '" . $licensePaid . "',  \r\n                                                            xRegion = " . $region . ",   \r\n                                                            Adresse = '" . $street . "',  \r\n                                                            Plz = " . $zipCode . ",  \r\n                                                            Ort = '" . $city . "',  \r\n                                                            Email = '" . $email . "'    \r\n                                                            WHERE Name= '" . $lastName . "' AND Vorname = '" . $firstName . "' AND Geburtstag = '" . $birthdate . "'";
                            }
                            mysql_query($sql);
                            if (mysql_errno() > 0) {
                                AA_printErrorMsg("xml-22-" . mysql_errno() . ": " . mysql_error());
                            } else {
                                $xAthlete = $row[0];
                            }
                        }
                    }
                } else {
                    // club not found
                    if (empty($club)) {
                        if ($license > 0) {
                            if (!in_array($license, $arr_noCat)) {
                                $arr_noCat['athClub'][] = $license;
                            }
                        } else {
                            if (!in_array($kidID, $arr_noCat)) {
                                $arr_noCat['athClub'][] = $kidID;
                            }
                        }
                    } else {
                        if ($license > 0) {
                            if (!in_array($license, $arr_noCat)) {
                                $arr_noCat['club'][] = $club;
                            }
                        } else {
                            if (!in_array($kidID, $arr_noCat)) {
                                $arr_noCat['club'][] = $club;
                            }
                        }
                    }
                }
                if ($xAthlete > 0) {
                    // check if already registered
                    $result = mysql_query("SELECT xAnmeldung FROM anmeldung WHERE xAthlet = {$xAthlete} AND xMeeting = " . $_COOKIE['meeting_id'] . "");
                    if (mysql_errno() > 0) {
                        AA_printErrorMsg("xml-23-" . mysql_errno() . ": " . mysql_error());
                    } else {
                        if ($announcement == 1) {
                            if (mysql_num_rows($result) == 0) {
                                // not yet registered
                                if ($xCat != '') {
                                    mysql_query("INSERT INTO anmeldung SET\r\n                                                                                Startnummer = 0\r\n                                                                                , xAthlet = {$xAthlete}\r\n                                                                                , xMeeting = " . $_COOKIE['meeting_id'] . "\r\n                                                                                , xKategorie = {$xCat}\r\n                                                                                , KidID = {$kidID} \r\n                                                                                , Bezahlt = '" . $licensePaid . "'   \r\n                                                                                , Angemeldet = '{$announcement}'");
                                    if (mysql_errno() > 0) {
                                        AA_printErrorMsg("xml-24-" . mysql_errno() . ": " . mysql_error());
                                    } else {
                                        $xReg = mysql_insert_id();
                                    }
                                }
                            } else {
                                // registered
                                $row = mysql_fetch_array($result);
                                mysql_query("Update anmeldung SET\r\n                                                                                Startnummer = 0    \r\n                                                                                , xAthlet = {$xAthlete}\r\n                                                                                , xMeeting = " . $_COOKIE['meeting_id'] . "\r\n                                                                                , xKategorie = {$xCat}\r\n                                                                                , KidID = {$kidID} \r\n                                                                                , Bezahlt = '" . $licensePaid . "'   \r\n                                                                                , Angemeldet = '{$announcement}'   \r\n                                                                                WHERE xAnmeldung = {$row['0']}");
                                if (mysql_errno() > 0) {
                                    AA_printErrorMsg("xml-25-" . mysql_errno() . ": " . mysql_error());
                                } else {
                                    $xReg = $row[0];
                                }
                            }
                        }
                        if ($xReg > 0) {
                            // check if athlete alredy starts for this discipline(s)
                            foreach ($xDis as $xDis1) {
                                // because we can get multiple disciplines (combined event),
                                // it is nessesary to determinate distype and discode for each discipline
                                // (catcode won't change)
                                $res_distype = mysql_query("\r\n                                                                SELECT d.Typ, d.Code, d.Appellzeit, d.Stellzeit FROM \r\n                                                                        disziplin_" . $_COOKIE['language'] . " as d\r\n                                                                        LEFT JOIN wettkampf as w  ON (w.xDisziplin = d.xDisziplin)\r\n                                                                WHERE w.xWettkampf = {$xDis1}");
                                if (mysql_errno() > 0) {
                                    AA_printErrorMsg("xml-26-" . mysql_errno() . ": " . mysql_error());
                                } else {
                                    $row_distype = mysql_fetch_Array($res_distype);
                                    $distype = $row_distype[0];
                                    $temp_discode = $row_distype[1];
                                }
                                $result = mysql_query("SELECT xStart FROM start WHERE xAnmeldung = {$xReg} and xWettkampf = {$xDis1}");
                                if (mysql_errno() > 0) {
                                    AA_printErrorMsg("xml-27-" . mysql_errno() . ": " . mysql_error());
                                } else {
                                    if (mysql_num_rows($result) == 0) {
                                        // not yet starting, add start
                                        $saison = $_SESSION['meeting_infos']['Saison'];
                                        if ($saison == '') {
                                            $saison = "O";
                                            //if no saison is set take outdoor
                                        }
                                        mysql_query("INSERT INTO start SET\r\n                                                                                        xWettkampf = {$xDis1}                                                                               \r\n                                                                                        , xAnmeldung = {$xReg}\r\n                                                                                        ");
                                        if (mysql_errno() > 0) {
                                            AA_printErrorMsg("xml-28-" . mysql_errno() . ": " . mysql_error());
                                        }
                                    }
                                }
                            }
                        }
                        // end xReg > 0
                    }
                }
            } else {
                // not category UBS kids cup
                if (!in_array($birthYear, $arr_noCat)) {
                    $arr_noCat['cat'][] = $birthYear;
                }
            }
        }
        // end foreach athlete
    }
    // end foreach athlete
}