function rsvp_buildAdditionalQuestions($attendeeID, $prefix)
{
    global $wpdb, $rsvp_saved_form_vars;
    $output = "<div class=\"rsvpCustomQuestions\">";
    $sql = "SELECT q.id, q.question, questionType FROM " . QUESTIONS_TABLE . " q \n\t\t\t\t\tINNER JOIN " . QUESTION_TYPE_TABLE . " qt ON qt.id = q.questionTypeID \n\t\t\t\t\tWHERE q.permissionLevel = 'public' \n\t\t\t\t\t  OR (q.permissionLevel = 'private' AND q.id IN (SELECT questionID FROM " . QUESTION_ATTENDEES_TABLE . " WHERE attendeeID = {$attendeeID}))\n\t\t\t\t\tORDER BY q.sortOrder ";
    $questions = $wpdb->get_results($sql);
    if (count($questions) > 0) {
        foreach ($questions as $q) {
            $oldAnswer = rsvp_revtrievePreviousAnswer($attendeeID, $q->id);
            $output .= rsvp_BeginningFormField("", "") . RSVP_START_PARA . stripslashes($q->question) . RSVP_END_PARA;
            if ($q->questionType == QT_MULTI) {
                $oldAnswers = explode("||", $oldAnswer);
                $answers = $wpdb->get_results($wpdb->prepare("SELECT id, answer FROM " . QUESTION_ANSWERS_TABLE . " WHERE questionID = %d", $q->id));
                if (count($answers) > 0) {
                    $i = 0;
                    foreach ($answers as $a) {
                        $output .= rsvp_BeginningFormField("", "rsvpCheckboxCustomQ") . "<input type=\"checkbox\" name=\"" . $prefix . "question" . $q->id . "[]\" id=\"" . $prefix . "question" . $q->id . $a->id . "\" value=\"" . $a->id . "\" " . (in_array(stripslashes($a->answer), $oldAnswers) ? " checked=\"checked\"" : "") . " />" . "<label for=\"" . $prefix . "question" . $q->id . $a->id . "\">" . stripslashes($a->answer) . "</label>\r\n" . RSVP_END_FORM_FIELD;
                        $i++;
                    }
                    $output .= "<div class=\"rsvpClear\">&nbsp;</div>\r\n";
                }
            } else {
                if ($q->questionType == QT_DROP) {
                    //$oldAnswers = explode("||", $oldAnswer);
                    $output .= "<select name=\"" . $prefix . "question" . $q->id . "\" size=\"1\">\r\n" . "<option value=\"\">--</option>\r\n";
                    $answers = $wpdb->get_results($wpdb->prepare("SELECT id, answer FROM " . QUESTION_ANSWERS_TABLE . " WHERE questionID = %d", $q->id));
                    if (count($answers) > 0) {
                        foreach ($answers as $a) {
                            $output .= "<option value=\"" . $a->id . "\" " . (stripslashes($a->answer) == $oldAnswer ? " selected=\"selected\"" : "") . ">" . stripslashes($a->answer) . "</option>\r\n";
                        }
                    }
                    $output .= "</select>\r\n";
                } else {
                    if ($q->questionType == QT_LONG) {
                        $output .= "<textarea name=\"" . $prefix . "question" . $q->id . "\" rows=\"5\" cols=\"35\">" . htmlspecialchars($oldAnswer) . "</textarea>";
                    } else {
                        if ($q->questionType == QT_RADIO) {
                            //$oldAnswers = explode("||", $oldAnswer);
                            $answers = $wpdb->get_results($wpdb->prepare("SELECT id, answer FROM " . QUESTION_ANSWERS_TABLE . " WHERE questionID = %d", $q->id));
                            if (count($answers) > 0) {
                                $i = 0;
                                $output .= RSVP_START_PARA;
                                foreach ($answers as $a) {
                                    $output .= "<input type=\"radio\" name=\"" . $prefix . "question" . $q->id . "\" id=\"" . $prefix . "question" . $q->id . $a->id . "\" value=\"" . $a->id . "\" " . (stripslashes($a->answer) == $oldAnswer ? " checked=\"checked\"" : "") . " /> " . "<label for=\"" . $prefix . "question" . $q->id . $a->id . "\">" . stripslashes($a->answer) . "</label>\r\n";
                                    $i++;
                                }
                                $output .= RSVP_END_PARA;
                            }
                        } else {
                            // normal text input
                            $output .= "<input type=\"text\" name=\"" . $prefix . "question" . $q->id . "\" value=\"" . htmlspecialchars($oldAnswer) . "\" size=\"25\" />";
                        }
                    }
                }
            }
            $output .= RSVP_END_FORM_FIELD;
        }
    }
    return $output . "</div>";
}
function rsvp_buildAdditionalQuestions($attendeeID, $prefix)
{
    global $wpdb;
    $output = "";
    $sql = "SELECT q.id, q.question, questionType FROM " . QUESTIONS_TABLE . " q \n\t\t\t\t\tINNER JOIN " . QUESTION_TYPE_TABLE . " qt ON qt.id = q.questionTypeID \n\t\t\t\t\tWHERE q.permissionLevel = 'public' \n\t\t\t\t\t  OR (q.permissionLevel = 'private' AND q.id IN (SELECT questionID FROM " . QUESTION_ATTENDEES_TABLE . " WHERE attendeeID = {$attendeeID}))\n\t\t\t\t\tORDER BY q.sortOrder ";
    $questions = $wpdb->get_results($sql);
    if (count($questions) > 0) {
        foreach ($questions as $q) {
            $oldAnswer = rsvp_revtrievePreviousAnswer($attendeeID, $q->id);
            $output .= "<tr>\r\n" . "<td align=\"left\">" . stripslashes($q->question) . "</td>\r\n" . "<td align=\"left\">";
            if ($q->questionType == QT_MULTI) {
                $oldAnswers = explode(",", $oldAnswer);
                $output .= "</td></tr><tr><td colspan=\"2\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\r\n";
                $answers = $wpdb->get_results($wpdb->prepare("SELECT id, answer FROM " . QUESTION_ANSWERS_TABLE . " WHERE questionID = %d", $q->id));
                if (count($answers) > 0) {
                    $i = 0;
                    $output .= "<tr>\r\n";
                    foreach ($answers as $a) {
                        if ($i % 3 == 0) {
                            $output .= "</tr><tr>";
                        }
                        $output .= "<td><input type=\"checkbox\" name=\"" . $prefix . "question" . $q->id . "[]\" id=\"" . $prefix . "question" . $q->id . $a->id . "\" value=\"" . $a->id . "\" " . (in_array(stripslashes($a->answer), $oldAnswers) ? " checked=\"checked\"" : "") . " />" . "<label for=\"" . $prefix . "question" . $q->id . $a->id . "\">" . stripslashes($a->answer) . "</label></td>\r\n";
                        $i++;
                    }
                    $output .= "</tr>\r\n";
                }
                $output .= "</table>\r\n";
            } else {
                if ($q->questionType == QT_DROP) {
                    $oldAnswers = explode(",", $oldAnswer);
                    $output .= "<select name=\"" . $prefix . "question" . $q->id . "\" size=\"1\">\r\n" . "<option value=\"\">--</option>\r\n";
                    $answers = $wpdb->get_results($wpdb->prepare("SELECT id, answer FROM " . QUESTION_ANSWERS_TABLE . " WHERE questionID = %d", $q->id));
                    if (count($answers) > 0) {
                        foreach ($answers as $a) {
                            $output .= "<option value=\"" . $a->id . "\" " . (in_array(stripslashes($a->answer), $oldAnswers) ? " selected=\"selected\"" : "") . ">" . stripslashes($a->answer) . "</option>\r\n";
                        }
                    }
                    $output .= "</select>\r\n";
                } else {
                    if ($q->questionType == QT_LONG) {
                        $output .= "</td></tr><tr><td colspan=\"2\"><textarea name=\"" . $prefix . "question" . $q->id . "\" rows=\"5\" cols=\"35\">" . htmlentities($oldAnswer) . "</textarea>";
                    } else {
                        if ($q->questionType == QT_RADIO) {
                            $oldAnswers = explode(",", $oldAnswer);
                            $output .= "</td></tr><tr><td colspan=\"2\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\r\n";
                            $answers = $wpdb->get_results($wpdb->prepare("SELECT id, answer FROM " . QUESTION_ANSWERS_TABLE . " WHERE questionID = %d", $q->id));
                            if (count($answers) > 0) {
                                $i = 0;
                                $output .= "<tr>\r\n";
                                foreach ($answers as $a) {
                                    if ($i % 3 == 0) {
                                        $output .= "</tr><tr>";
                                    }
                                    $output .= "<td><input type=\"radio\" name=\"" . $prefix . "question" . $q->id . "\" id=\"" . $prefix . "question" . $q->id . $a->id . "\" value=\"" . $a->id . "\" " . (in_array(stripslashes($a->answer), $oldAnswers) ? " checked=\"checked\"" : "") . " /> " . "<label for=\"" . $prefix . "question" . $q->id . $a->id . "\">" . stripslashes($a->answer) . "</label></td>\r\n";
                                    $i++;
                                }
                                $output .= "</tr>\r\n";
                            }
                            $output .= "</table>\r\n";
                        } else {
                            // normal text input
                            $output .= "<input type=\"text\" name=\"" . $prefix . "question" . $q->id . "\" value=\"" . htmlentities($oldAnswer) . "\" size=\"25\" />";
                        }
                    }
                }
            }
            $output .= "</td>\r\n" . "</tr>\r\n";
        }
    }
    return $output;
}