function rsvp_foundAttendee(&$output, &$text)
{
    global $wpdb;
    if (is_numeric($_POST['attendeeID']) && $_POST['attendeeID'] > 0) {
        $attendee = $wpdb->get_row($wpdb->prepare("SELECT id, firstName, lastName, rsvpStatus \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM " . ATTENDEES_TABLE . " \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE id = %d", $_POST['attendeeID']));
        if ($attendee != null) {
            $output = RSVP_START_CONTAINER;
            if (strtolower($attendee->rsvpStatus) == "noresponse") {
                $output .= RSVP_START_PARA . __("Hi", 'rsvp-plugin') . " " . htmlspecialchars(stripslashes($attendee->firstName . " " . $attendee->lastName)) . "!" . RSVP_END_PARA;
                if (trim(get_option(OPTION_WELCOME_TEXT)) != "") {
                    $output .= RSVP_START_PARA . trim(get_option(OPTION_WELCOME_TEXT)) . RSVP_END_PARA;
                } else {
                    $output .= RSVP_START_PARA . __("There are a few more questions we need to ask you if you could please fill them out below to finish up the RSVP process.", 'rsvp-plugin') . RSVP_END_PARA;
                }
                $output .= rsvp_frontend_main_form($attendee->id);
            } else {
                $output .= rsvp_frontend_prompt_to_edit($attendee);
            }
            return rsvp_handle_output($text, $output . RSVP_END_CONTAINER);
        }
        return rsvp_handle_output($text, rsvp_frontend_greeting());
    } else {
        return rsvp_handle_output($text, rsvp_frontend_greeting());
    }
}
function rsvp_frontend_handler($text)
{
    global $wpdb;
    $passcodeOptionEnabled = get_option(OPTION_RSVP_PASSCODE) == "Y" ? true : false;
    //QUIT if the replacement string doesn't exist
    if (!strstr($text, RSVP_FRONTEND_TEXT_CHECK)) {
        return $text;
    }
    // See if we should allow people to RSVP, etc...
    $openDate = get_option(OPTION_OPENDATE);
    $closeDate = get_option(OPTION_DEADLINE);
    if (strtotime($openDate) !== false && strtotime($openDate) > time()) {
        return "<p>I am sorry but the ability to RSVP for our wedding won't open till <strong>" . date("m/d/Y", strtotime($openDate)) . "</strong></p>";
    }
    if (strtotime($closeDate) !== false && strtotime($closeDate) < time()) {
        return "<p>The deadline to RSVP for this wedding has passed, please contact the bride and groom to see if there is still a seat for you.</p>";
    }
    if (isset($_POST['rsvpStep'])) {
        $output = "";
        switch (strtolower($_POST['rsvpStep'])) {
            case "handlersvp":
                if (is_numeric($_POST['attendeeID']) && $_POST['attendeeID'] > 0) {
                    // update their information and what not....
                    if (strToUpper($_POST['mainRsvp']) == "Y") {
                        $rsvpStatus = "Yes";
                    } else {
                        $rsvpStatus = "No";
                    }
                    $attendeeID = $_POST['attendeeID'];
                    $wpdb->update(ATTENDEES_TABLE, array("rsvpDate" => date("Y-m-d"), "rsvpStatus" => $rsvpStatus, "note" => $_POST['rsvp_note'], "kidsMeal" => isset($_POST['mainKidsMeal']) && strToUpper($_POST['mainKidsMeal']) == "Y" ? "Y" : "N", "veggieMeal" => isset($_POST['mainVeggieMeal']) && strToUpper($_POST['mainVeggieMeal']) == "Y" ? "Y" : "N"), array("id" => $attendeeID), array("%s", "%s", "%s", "%s", "%s"), array("%d"));
                    rsvp_printQueryDebugInfo();
                    rsvp_handleAdditionalQuestions($attendeeID, "mainquestion");
                    $sql = "SELECT id FROM " . ATTENDEES_TABLE . " \n\t\t\t\t\t \tWHERE (id IN (SELECT attendeeID FROM " . ASSOCIATED_ATTENDEES_TABLE . " WHERE associatedAttendeeID = %d) \n\t\t\t\t\t\t\tOR id in (SELECT associatedAttendeeID FROM " . ASSOCIATED_ATTENDEES_TABLE . " WHERE attendeeID = %d)) \n\t\t\t\t\t\t\t AND rsvpStatus = 'NoResponse'";
                    $associations = $wpdb->get_results($wpdb->prepare($sql, $attendeeID, $attendeeID));
                    foreach ($associations as $a) {
                        if ($_POST['rsvpFor' . $a->id] == "Y") {
                            if ($_POST['attending' . $a->id] == "Y") {
                                $rsvpStatus = "Yes";
                            } else {
                                $rsvpStatus = "No";
                            }
                            $wpdb->update(ATTENDEES_TABLE, array("rsvpDate" => date("Y-m-d"), "rsvpStatus" => $rsvpStatus, "kidsMeal" => strToUpper(isset($_POST['attending' . $a->id . 'KidsMeal']) ? $_POST['attending' . $a->id . 'KidsMeal'] : "N") == "Y" ? "Y" : "N", "veggieMeal" => strToUpper(isset($_POST['attending' . $a->id . 'VeggieMeal']) ? $_POST['attending' . $a->id . 'VeggieMeal'] : "N") == "Y" ? "Y" : "N"), array("id" => $a->id), array("%s", "%s", "%s", "%s"), array("%d"));
                            rsvp_printQueryDebugInfo();
                            rsvp_handleAdditionalQuestions($a->id, $a->id . "question");
                        }
                    }
                    if (get_option(OPTION_HIDE_ADD_ADDITIONAL) != "Y") {
                        if (is_numeric($_POST['additionalRsvp']) && $_POST['additionalRsvp'] > 0) {
                            for ($i = 1; $i <= $_POST['additionalRsvp']; $i++) {
                                if ($i <= 3 && !empty($_POST['newAttending' . $i . 'FirstName']) && !empty($_POST['newAttending' . $i . 'LastName'])) {
                                    $wpdb->insert(ATTENDEES_TABLE, array("firstName" => trim($_POST['newAttending' . $i . 'FirstName']), "lastName" => trim($_POST['newAttending' . $i . 'LastName']), "rsvpDate" => date("Y-m-d"), "rsvpStatus" => $_POST['newAttending' . $i] == "Y" ? "Yes" : "No", "kidsMeal" => isset($_POST['newAttending' . $i . 'KidsMeal']) ? $_POST['newAttending' . $i . 'KidsMeal'] : "N", "veggieMeal" => isset($_POST['newAttending' . $i . 'VeggieMeal']) ? $_POST['newAttending' . $i . 'VeggieMeal'] : "N", "additionalAttendee" => "Y"), array('%s', '%s', '%s', '%s', '%s', '%s'));
                                    rsvp_printQueryDebugInfo();
                                    $newAid = $wpdb->insert_id;
                                    rsvp_handleAdditionalQuestions($newAid, $i . 'question');
                                    // Add associations for this new user
                                    $wpdb->insert(ASSOCIATED_ATTENDEES_TABLE, array("attendeeID" => $newAid, "associatedAttendeeID" => $attendeeID), array("%d", "%d"));
                                    rsvp_printQueryDebugInfo();
                                    $wpdb->query($wpdb->prepare("INSERT INTO " . ASSOCIATED_ATTENDEES_TABLE . "(attendeeID, associatedAttendeeID)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t SELECT " . $newAid . ", associatedAttendeeID \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM " . ASSOCIATED_ATTENDEES_TABLE . " \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE attendeeID = " . $attendeeID));
                                    rsvp_printQueryDebugInfo();
                                }
                            }
                        }
                    }
                    if (get_option(OPTION_NOTIFY_ON_RSVP) == "Y" && get_option(OPTION_NOTIFY_EMAIL) != "") {
                        $sql = "SELECT firstName, lastName, rsvpStatus FROM " . ATTENDEES_TABLE . " WHERE id= " . $attendeeID;
                        $attendee = $wpdb->get_results($sql);
                        if (count($attendee) > 0) {
                            $body = "Hello, \r\n\r\n";
                            $body .= stripslashes($attendee[0]->firstName) . " " . stripslashes($attendee[0]->lastName) . " has submitted their RSVP and has RSVP'd with '" . $attendee[0]->rsvpStatus . "'.";
                            wp_mail(get_option(OPTION_NOTIFY_EMAIL), "New RSVP Submission", $body);
                        }
                    }
                    return frontend_rsvp_thankyou();
                } else {
                    return rsvp_frontend_greeting();
                }
                break;
            case "editattendee":
                if (is_numeric($_POST['attendeeID']) && $_POST['attendeeID'] > 0) {
                    // Try to find the user.
                    $attendee = $wpdb->get_row($wpdb->prepare("SELECT id, firstName, lastName, rsvpStatus \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM " . ATTENDEES_TABLE . " \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE id = %d", $_POST['attendeeID']));
                    if ($attendee != null) {
                        $output .= "<div>\r\n";
                        $output .= "<p>Welcome back " . htmlentities($attendee->firstName . " " . $attendee->lastName) . "!</p>";
                        $output .= rsvp_frontend_main_form($attendee->id);
                        return $output . "</div>\r\n";
                    }
                }
                break;
            case "foundattendee":
                if (is_numeric($_POST['attendeeID']) && $_POST['attendeeID'] > 0) {
                    $attendee = $wpdb->get_row($wpdb->prepare("SELECT id, firstName, lastName, rsvpStatus \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM " . ATTENDEES_TABLE . " \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE id = %d", $_POST['attendeeID']));
                    if ($attendee != null) {
                        $output = "<div>\r\n";
                        if (strtolower($attendee->rsvpStatus) == "noresponse") {
                            $output .= "<p>Hi " . htmlentities(stripslashes(utf8_decode($attendee->firstName . " " . $attendee->lastName))) . "!</p>";
                            if (trim(get_option(OPTION_WELCOME_TEXT)) != "") {
                                $output .= "<p>" . trim(utf8_decode(get_option(OPTION_WELCOME_TEXT))) . "</p>";
                            } else {
                                $output .= "<p>There are a few more questions we need to ask you if you could please fill them out below to finish up the RSVP process.</p>";
                            }
                            $output .= rsvp_frontend_main_form($attendee->id);
                        } else {
                            $output .= rsvp_frontend_prompt_to_edit($attendee);
                        }
                        return $output . "</div>\r\n";
                    }
                    return rsvp_frontend_greeting();
                } else {
                    return rsvp_frontend_greeting();
                }
                break;
            case "find":
                $_SESSION['rsvpFirstName'] = $_POST['firstName'];
                $_SESSION['rsvpLastName'] = $_POST['lastName'];
                $passcode = "";
                if (isset($_POST['passcode'])) {
                    $passcode = $_POST['passcode'];
                    $_SESSION['rsvpPasscode'] = $_POST['passcode'];
                }
                $firstName = $_POST['firstName'];
                $lastName = $_POST['lastName'];
                if (strlen($_POST['firstName']) <= 1 || strlen($_POST['lastName']) <= 1) {
                    $output = "<p style=\"color:red\">A first and last name must be specified</p>\r\n";
                    $output .= rsvp_frontend_greeting();
                    return $output;
                }
                // Try to find the user.
                if ($passcodeOptionEnabled) {
                    $attendee = $wpdb->get_row($wpdb->prepare("SELECT id, firstName, lastName, rsvpStatus \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM " . ATTENDEES_TABLE . " \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE firstName = %s AND lastName = %s AND passcode = %s", $firstName, $lastName, $passcode));
                } else {
                    $attendee = $wpdb->get_row($wpdb->prepare("SELECT id, firstName, lastName, rsvpStatus \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM " . ATTENDEES_TABLE . " \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE firstName = %s AND lastName = %s", $firstName, $lastName));
                }
                if ($attendee != null) {
                    // hey we found something, we should move on and print out any associated users and let them rsvp
                    $output = "<div>\r\n";
                    if (strtolower($attendee->rsvpStatus) == "noresponse") {
                        $output .= "<p>Hi " . htmlentities(stripslashes(utf8_decode($attendee->firstName . " " . $attendee->lastName))) . "!</p>";
                        if (trim(get_option(OPTION_WELCOME_TEXT)) != "") {
                            $output .= "<p>" . trim(utf8_decode(get_option(OPTION_WELCOME_TEXT))) . "</p>";
                        } else {
                            $output .= "<p>There are a few more questions we need to ask you if you could please fill them out below to finish up the RSVP process.</p>";
                        }
                        $output .= rsvp_frontend_main_form($attendee->id);
                    } else {
                        $output .= rsvp_frontend_prompt_to_edit($attendee);
                    }
                    return $output . "</div>\r\n";
                }
                // We did not find anyone let's try and do a rough search
                $attendees = null;
                if (!$passcodeOptionEnabled) {
                    for ($i = 3; $i >= 1; $i--) {
                        $truncFirstName = rsvp_chomp_name($firstName, $i);
                        $attendees = $wpdb->get_results("SELECT id, firstName, lastName, rsvpStatus FROM " . ATTENDEES_TABLE . " \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE lastName = '" . mysql_real_escape_string($lastName) . "' AND firstName LIKE '" . mysql_real_escape_string($truncFirstName) . "%'");
                        if (count($attendees) > 0) {
                            $output = "<p><strong>We could not find an exact match but could any of the below entries be you?</strong></p>";
                            foreach ($attendees as $a) {
                                $output .= "<form method=\"post\">\r\n\n\t\t\t\t\t\t\t\t\t\t\t\t<input type=\"hidden\" name=\"rsvpStep\" value=\"foundattendee\" />\r\n\n\t\t\t\t\t\t\t\t\t\t\t\t<input type=\"hidden\" name=\"attendeeID\" value=\"" . $a->id . "\" />\r\n\n\t\t\t\t\t\t\t\t\t\t\t\t<p style=\"text-align:left;\">\r\n\n\t\t\t\t\t\t\t\t\t\t" . htmlentities(utf8_decode($a->firstName . " " . $a->lastName)) . " \n\t\t\t\t\t\t\t\t\t\t<input type=\"submit\" value=\"RSVP\" />\r\n\n\t\t\t\t\t\t\t\t\t\t</p>\r\n</form>\r\n";
                            }
                            return $output;
                        } else {
                            $i = strlen($truncFirstName);
                        }
                    }
                }
                return "<p><strong>We were unable to find anyone with a name of " . htmlentities(utf8_decode($firstName . " " . $lastName)) . "</strong></p>\r\n" . rsvp_frontend_greeting();
                break;
            case "newsearch":
            default:
                return rsvp_frontend_greeting();
                break;
        }
    } else {
        return rsvp_frontend_greeting();
    }
}