function rsvp_admin_guestlist()
{
    global $wpdb;
    if (get_option("rsvp_db_version") != RSVP_DB_VERSION) {
        rsvp_database_setup();
    }
    rsvp_install_passcode_field();
    if (count($_POST) > 0 && $_POST['rsvp-bulk-action'] == "delete" && (is_array($_POST['attendee']) && count($_POST['attendee']) > 0)) {
        foreach ($_POST['attendee'] as $attendee) {
            if (is_numeric($attendee) && $attendee > 0) {
                $wpdb->query($wpdb->prepare("DELETE FROM " . ASSOCIATED_ATTENDEES_TABLE . " WHERE attendeeID = %d OR associatedAttendeeID = %d", $attendee, $attendee));
                $wpdb->query($wpdb->prepare("DELETE FROM " . ATTENDEES_TABLE . " WHERE id = %d", $attendee));
            }
        }
    }
    $sql = "SELECT id, firstName, lastName, rsvpStatus, note, kidsMeal, additionalAttendee, veggieMeal, personalGreeting, passcode, email FROM " . ATTENDEES_TABLE;
    $orderBy = " lastName, firstName";
    if (isset($_GET['sort'])) {
        if (strToLower($_GET['sort']) == "rsvpstatus") {
            $orderBy = " rsvpStatus " . (strtolower($_GET['sortDirection']) == "desc" ? "DESC" : "ASC") . ", " . $orderBy;
        } else {
            if (strToLower($_GET['sort']) == "attendee") {
                $direction = strtolower($_GET['sortDirection']) == "desc" ? "DESC" : "ASC";
                $orderBy = " lastName {$direction}, firstName {$direction}";
            } else {
                if (strToLower($_GET['sort']) == "kidsmeal") {
                    $orderBy = " kidsMeal " . (strtolower($_GET['sortDirection']) == "desc" ? "DESC" : "ASC") . ", " . $orderBy;
                } else {
                    if (strToLower($_GET['sort']) == "additional") {
                        $orderBy = " additionalAttendee " . (strtolower($_GET['sortDirection']) == "desc" ? "DESC" : "ASC") . ", " . $orderBy;
                    } else {
                        if (strToLower($_GET['sort']) == "vegetarian") {
                            $orderBy = " veggieMeal " . (strtolower($_GET['sortDirection']) == "desc" ? "DESC" : "ASC") . ", " . $orderBy;
                        }
                    }
                }
            }
        }
    }
    $sql .= " ORDER BY " . $orderBy;
    $attendees = $wpdb->get_results($sql);
    $sort = "";
    $sortDirection = "asc";
    if (isset($_GET['sort'])) {
        $sort = $_GET['sort'];
    }
    if (isset($_GET['sortDirection'])) {
        $sortDirection = $_GET['sortDirection'];
    }
    ?>
		<script type="text/javascript" language="javascript">
			jQuery(document).ready(function() {
				jQuery("#cb").click(function() {
					if(jQuery("#cb").attr("checked")) {
						jQuery("input[name='attendee[]']").attr("checked", "checked");
					} else {
						jQuery("input[name='attendee[]']").removeAttr("checked");
					}
				});
			});
		</script>
		<div class="wrap">	
			<div id="icon-edit" class="icon32"><br /></div>	
			<h2>List of current attendees</h2>
			<form method="post" id="rsvp-form" enctype="multipart/form-data">
				<input type="hidden" id="rsvp-bulk-action" name="rsvp-bulk-action" />
				<input type="hidden" id="sortValue" name="sortValue" value="<?php 
    echo htmlentities($sort, ENT_QUOTES);
    ?>
" />
				<input type="hidden" name="exportSortDirection" value="<?php 
    echo htmlentities($sortDirection, ENT_QUOTES);
    ?>
" />
				<div class="tablenav">
					<div class="alignleft actions">
						<select id="rsvp-action-top" name="action">
							<option value="" selected="selected"><?php 
    _e('Bulk Actions', 'rsvp');
    ?>
</option>
							<option value="delete"><?php 
    _e('Delete', 'rsvp');
    ?>
</option>
						</select>
						<input type="submit" value="<?php 
    _e('Apply', 'rsvp');
    ?>
" name="doaction" id="doaction" class="button-secondary action" onclick="document.getElementById('rsvp-bulk-action').value = document.getElementById('rsvp-action-top').value;" />
						<input type="submit" value="<?php 
    _e('Export Attendees', 'rsvp');
    ?>
" name="exportButton" id="exportButton" class="button-secondary action" onclick="document.getElementById('rsvp-bulk-action').value = 'export';" />
					</div>
					<?php 
    $yesResults = $wpdb->get_results("SELECT COUNT(*) AS yesCount FROM " . ATTENDEES_TABLE . " WHERE rsvpStatus = 'Yes'");
    $noResults = $wpdb->get_results("SELECT COUNT(*) AS noCount FROM " . ATTENDEES_TABLE . " WHERE rsvpStatus = 'No'");
    $noResponseResults = $wpdb->get_results("SELECT COUNT(*) AS noResponseCount FROM " . ATTENDEES_TABLE . " WHERE rsvpStatus = 'NoResponse'");
    $kidsMeals = $wpdb->get_results("SELECT COUNT(*) AS kidsMealCount FROM " . ATTENDEES_TABLE . " WHERE kidsMeal = 'Y'");
    $veggieMeals = $wpdb->get_results("SELECT COUNT(*) AS veggieMealCount FROM " . ATTENDEES_TABLE . " WHERE veggieMeal = 'Y'");
    ?>
					<div class="alignright">RSVP Count -  
						Yes: <strong><?php 
    echo $yesResults[0]->yesCount;
    ?>
</strong> &nbsp; &nbsp;  &nbsp; &nbsp; 
						No: <strong><?php 
    echo $noResults[0]->noCount;
    ?>
</strong> &nbsp; &nbsp;  &nbsp; &nbsp; 
						No Response: <strong><?php 
    echo $noResponseResults[0]->noResponseCount;
    ?>
</strong> &nbsp; &nbsp;  &nbsp; &nbsp; 
						Kids Meals: <strong><?php 
    echo $kidsMeals[0]->kidsMealCount;
    ?>
</strong> &nbsp; &nbsp;  &nbsp; &nbsp; 
						Veggie Meals: <strong><?php 
    echo $veggieMeals[0]->veggieMealCount;
    ?>
</strong>
					</div>
					<div class="clear"></div>
				</div>
			<table class="widefat post fixed" cellspacing="0">
				<thead>
					<tr>
						<th scope="col" class="manage-column column-cb check-column" style=""><input type="checkbox" id="cb" /></th>
						<th scope="col" id="attendeeName" class="manage-column column-title" style="">Attendee<br />
							<a href="admin.php?page=rsvp-top-level&amp;sort=attendee&amp;sortDirection=asc">
								<img src="<?php 
    echo plugins_url();
    ?>
/rsvp/uparrow<?php 
    echo $sort == "attendee" && $sortDirection == "asc" ? "_selected" : "";
    ?>
.gif" width="11" height="9" 
									alt="Sort Ascending Attendee Status" title="Sort Ascending Attendee Status" border="0"></a> &nbsp;
							<a href="admin.php?page=rsvp-top-level&amp;sort=attendee&amp;sortDirection=desc">
								<img src="<?php 
    echo plugins_url();
    ?>
/rsvp/downarrow<?php 
    echo $sort == "attendee" && $sortDirection == "desc" ? "_selected" : "";
    ?>
.gif" width="11" height="9" 
									alt="Sort Descending Attendee Status" title="Sort Descending Attendee Status" border="0"></a>
						</th>			
            <th scope="col" id="rsvpEmail" class="manage-column column-title">Email</th>
						<th scope="col" id="rsvpStatus" class="manage-column column-title" style="">RSVP Status<br />
							<a href="admin.php?page=rsvp-top-level&amp;sort=rsvpStatus&amp;sortDirection=asc">
								<img src="<?php 
    echo plugins_url();
    ?>
/rsvp/uparrow<?php 
    echo $sort == "rsvpStatus" && $sortDirection == "asc" ? "_selected" : "";
    ?>
.gif" width="11" height="9" 
									alt="Sort Ascending RSVP Status" title="Sort Ascending RSVP Status" border="0"></a> &nbsp;
							<a href="admin.php?page=rsvp-top-level&amp;sort=rsvpStatus&amp;sortDirection=desc">
								<img src="<?php 
    echo plugins_url();
    ?>
/rsvp/downarrow<?php 
    echo $sort == "rsvpStatus" && $sortDirection == "desc" ? "_selected" : "";
    ?>
.gif" width="11" height="9" 
									alt="Sort Descending RSVP Status" title="Sort Descending RSVP Status" border="0"></a>
						</th>
						<?php 
    if (get_option(OPTION_HIDE_KIDS_MEAL) != "Y") {
        ?>
						<th scope="col" id="kidsMeal" class="manage-column column-title" style="">Kids Meal<br />
								<a href="admin.php?page=rsvp-top-level&amp;sort=kidsMeal&amp;sortDirection=asc">
									<img src="<?php 
        echo plugins_url();
        ?>
/rsvp/uparrow<?php 
        echo $sort == "kidsMeal" && $sortDirection == "asc" ? "_selected" : "";
        ?>
.gif" width="11" height="9" 
										alt="Sort Ascending Kids Meal Status" title="Sort Ascending Kids Meal Status" border="0"></a> &nbsp;
								<a href="admin.php?page=rsvp-top-level&amp;sort=kidsMeal&amp;sortDirection=desc">
									<img src="<?php 
        echo plugins_url();
        ?>
/rsvp/downarrow<?php 
        echo $sort == "kidsMeal" && $sortDirection == "desc" ? "_selected" : "";
        ?>
.gif" width="11" height="9" 
										alt="Sort Descending Kids Meal Status" title="Sort Descending Kids Meal Status" border="0"></a>
						</th>
						<?php 
    }
    ?>
						<th scope="col" id="additionalAttendee" class="manage-column column-title" style="">Additional Attendee <br />
									<a href="admin.php?page=rsvp-top-level&amp;sort=additional&amp;sortDirection=asc">
										<img src="<?php 
    echo plugins_url();
    ?>
/rsvp/uparrow<?php 
    echo $sort == "additional" && $sortDirection == "asc" ? "_selected" : "";
    ?>
.gif" width="11" height="9" 
											alt="Sort Ascending Additional Attendees Status" title="Sort Ascending Additional Attendees Status" border="0"></a> &nbsp;
									<a href="admin.php?page=rsvp-top-level&amp;sort=additional&amp;sortDirection=desc">
										<img src="<?php 
    echo plugins_url();
    ?>
/rsvp/downarrow<?php 
    echo $sort == "additional" && $sortDirection == "desc" ? "_selected" : "";
    ?>
.gif" width="11" height="9" 
											alt="Sort Descending Additional Attendees Status" title="Sort Descending Additional Atttendees Status" border="0"></a>
						</th>
						<?php 
    if (get_option(OPTION_HIDE_VEGGIE) != "Y") {
        ?>
						<th scope="col" id="veggieMeal" class="manage-column column-title" style="">Vegetarian <br />
										<a href="admin.php?page=rsvp-top-level&amp;sort=vegetarian&amp;sortDirection=asc">
											<img src="<?php 
        echo plugins_url();
        ?>
/rsvp/uparrow<?php 
        echo $sort == "vegetarian" && $sortDirection == "asc" ? "_selected" : "";
        ?>
.gif" width="11" height="9" 
												alt="Sort Ascending Vegetarian Status" title="Sort Ascending Vegetarian Status" border="0"></a> &nbsp;
										<a href="admin.php?page=rsvp-top-level&amp;sort=vegetarian&amp;sortDirection=desc">
											<img src="<?php 
        echo plugins_url();
        ?>
/rsvp/downarrow<?php 
        echo $sort == "vegetarian" && $sortDirection == "desc" ? "_selected" : "";
        ?>
.gif" width="11" height="9" 
												alt="Sort Descending Vegetarian Status" title="Sort Descending Vegetarian Status" border="0"></a>
						</th>
						<?php 
    }
    ?>
						<th scope="col" id="customMessage" class="manage-column column-title" style="">Custom Message</th>
						<th scope="col" id="note" class="manage-column column-title" style="">Note</th>
						<?php 
    if (rsvp_require_passcode()) {
        ?>
							<th scope="col" id="passcode" class="manage-column column-title" style="">Passcode</th>
						<?php 
    }
    ?>
						<?php 
    $qRs = $wpdb->get_results("SELECT id, question FROM " . QUESTIONS_TABLE . " ORDER BY sortOrder, id");
    if (count($qRs) > 0) {
        foreach ($qRs as $q) {
            ?>
							<th scope="col" class="manage-column -column-title"><?php 
            echo htmlspecialchars(stripslashes($q->question));
            ?>
</th>
						<?php 
        }
    }
    ?>
						<th scope="col" id="associatedAttendees" class="manage-column column-title" style="">Associated Attendees</th>
					</tr>
				</thead>
			</table>
			<div style="overflow: auto;height: 450px;">
				<table class="widefat post fixed" cellspacing="0">
				<?php 
    $i = 0;
    foreach ($attendees as $attendee) {
        ?>
						<tr class="<?php 
        echo $i % 2 == 0 ? "alternate" : "";
        ?>
 author-self">
							<th scope="row" class="check-column"><input type="checkbox" name="attendee[]" value="<?php 
        echo $attendee->id;
        ?>
" /></th>						
							<td>
								<a href="<?php 
        echo get_option("siteurl");
        ?>
/wp-admin/admin.php?page=rsvp-admin-guest&amp;id=<?php 
        echo $attendee->id;
        ?>
"><?php 
        echo htmlspecialchars(stripslashes($attendee->firstName) . " " . stripslashes($attendee->lastName));
        ?>
</a>
							</td>
              <td><?php 
        echo htmlspecialchars(stripslashes($attendee->email));
        ?>
</td>
							<td><?php 
        echo $attendee->rsvpStatus;
        ?>
</td>
							<?php 
        if (get_option(OPTION_HIDE_KIDS_MEAL) != "Y") {
            ?>
							<td><?php 
            if ($attendee->rsvpStatus == "NoResponse") {
                echo "--";
            } else {
                echo $attendee->kidsMeal == "Y" ? "Yes" : "No";
            }
            ?>
</td>
								<?php 
        }
        ?>
							<td><?php 
        if ($attendee->rsvpStatus == "NoResponse") {
            echo "--";
        } else {
            echo $attendee->additionalAttendee == "Y" ? "Yes" : "No";
        }
        ?>
</td>
							<?php 
        if (get_option(OPTION_HIDE_VEGGIE) != "Y") {
            ?>
							<td><?php 
            if ($attendee->rsvpStatus == "NoResponse") {
                echo "--";
            } else {
                echo $attendee->veggieMeal == "Y" ? "Yes" : "No";
            }
            ?>
</td>
							<?php 
        }
        ?>
							<td><?php 
        echo nl2br(stripslashes(trim($attendee->personalGreeting)));
        ?>
</td>
							<td><?php 
        echo nl2br(stripslashes(trim($attendee->note)));
        ?>
</td>
							<?php 
        if (rsvp_require_passcode()) {
            ?>
								<td><?php 
            echo $attendee->passcode;
            ?>
</td>
							<?php 
        }
        $sql = "SELECT question, answer FROM " . QUESTIONS_TABLE . " q \n\t\t\t\t\t\t\t\t\tLEFT JOIN " . ATTENDEE_ANSWERS . " ans ON q.id = ans.questionID AND ans.attendeeID = %d \n\t\t\t\t\t\t\t\t\tORDER BY q.sortOrder, q.id";
        $aRs = $wpdb->get_results($wpdb->prepare($sql, $attendee->id));
        if (count($aRs) > 0) {
            foreach ($aRs as $a) {
                ?>
									<td><?php 
                echo htmlspecialchars(stripslashes($a->answer));
                ?>
</td>
							<?php 
            }
        }
        ?>
							<td>
							<?php 
        $sql = "SELECT firstName, lastName FROM " . ATTENDEES_TABLE . " \n\t\t\t\t\t\t\t\t \tWHERE id IN (SELECT attendeeID FROM " . ASSOCIATED_ATTENDEES_TABLE . " WHERE associatedAttendeeID = %d) \n\t\t\t\t\t\t\t\t\t\tOR id in (SELECT associatedAttendeeID FROM " . ASSOCIATED_ATTENDEES_TABLE . " WHERE attendeeID = %d)";
        $associations = $wpdb->get_results($wpdb->prepare($sql, $attendee->id, $attendee->id));
        foreach ($associations as $a) {
            echo htmlspecialchars(stripslashes($a->firstName . " " . $a->lastName)) . "<br />";
        }
        ?>
							</td>
						</tr>
					<?php 
        $i++;
    }
    ?>
				</table>
			</div>
			</form>
		</div>
	<?php 
}
    $wpdb->insert($table, array("questionType" => "dropdown", "friendlyName" => "Drop Down"), array('%s', '%s'));
    $wpdb->insert($table, array("questionType" => "radio", "friendlyName" => "Radio"), array('%s', '%s'));
} else {
    if ((int) $installed_ver < 6) {
        $wpdb->insert($table, array("questionType" => "radio", "friendlyName" => "Radio"), array('%s', '%s'));
    }
}
$table = $wpdb->prefix . "rsvpCustomQuestionAnswers";
if ($wpdb->get_var("SHOW TABLES LIKE '{$table}'") != $table) {
    $sql = "CREATE TABLE {$table} (\n\t\t`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t`questionID` INT NOT NULL, \n\t\t`answer` MEDIUMTEXT NOT NULL\n\t\t);";
    $wpdb->query($sql);
}
$table = $wpdb->prefix . "attendeeAnswers";
if ($wpdb->get_var("SHOW TABLES LIKE '{$table}'") != $table) {
    $sql = "CREATE TABLE {$table} (\n\t\t`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t`questionID` INT NOT NULL, \n\t\t`answer` MEDIUMTEXT NOT NULL, \n\t\t`attendeeID` INT NOT NULL \n\t\t);";
    $wpdb->query($sql);
}
$table = $wpdb->prefix . "rsvpCustomQuestionAttendees";
if ($wpdb->get_var("SHOW TABLES LIKE '{$table}'") != $table) {
    $sql = "CREATE TABLE {$table} (\n\t\t`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t`questionID` INT NOT NULL ,\n\t\t`attendeeID` INT NOT NULL\n\t\t);";
    $wpdb->query($sql);
}
if ((int) $installed_ver < 5) {
    $table = QUESTIONS_TABLE;
    $sql = "ALTER TABLE `{$table}` ADD `permissionLevel` ENUM( 'public', 'private' ) NOT NULL DEFAULT 'public';";
    $wpdb->query($sql);
}
if ((int) $installed_ver < 9) {
    rsvp_install_passcode_field();
}
update_option("rsvp_db_version", RSVP_DB_VERSION);