<?php
require('../setClassPath.php');
require_once('object/User.php');
require_once('quiz/Quiz.php');
require_once('ui/Component.php');

session_start();
$user = $_SESSION['user'];
$quiz = $_SESSION['quiz'];

if (!isset($user)) {
    echo 'You are not logged in.';
    exit();
}

//if no quiz in the session, check the db to see if one was started previously
if (!isset($quiz)) {
    require_once('db/QuizDB.php');
    require_once('db/TransManager.php');

    $trans = TransManager::getInstance();
    $trans->beginTrans();
    $quizdb = new QuizDB();

    $quiz = $quizdb->lookupPendingQuiz($user, $trans->getPDO());
    if ($quiz)
        $quiz = $quizdb->loadQuizData($quiz, $trans->getPDO());

    $trans->commit();
    $trans->disconnect();
}

if ($quiz && $quiz->getQuizStepId() != 1) {
    $_SESSION['quiz'] = $quiz;
    header("HTTP/1.1 302 Found");
    switch ($quiz->getQuizStepId()) {
    case 2:
        header("Location: /quiz/create2.html");
        break;
    case 3:
        header("Location: /quiz/create4.html");
        break;
    case 4:
        header("Location: /quiz/create5.html");
        break;
    }
    exit();
}


$err = ($_GET['err'] == '1');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Create Your Compatability Quiz</title>
<link type="text/css" rel="stylesheet" href="/css/main.css">
<script type="text/javascript">
function copySelectedOptions() {
    var options = new Object();
    var from = document.forms[0]['examples'];
    var to = document.forms[0]['results[]'];
    if (hasOptions(to)) {
        for (var i=0; i < to.options.length; i++) {
            options[to.options[i].value] = to.options[i].text;
        }
    }
    if (!hasOptions(from)) {
        return false;
    }
    for (var i=0; i < from.options.length ;i++) {
        var o = from.options[i];
        if (o.selected) {
            if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
                if (!hasOptions(to)) {
                    var index = 0;
                } 
                else
                    var index=to.options.length;
                to.options[index] = new Option( o.text, o.value, false, false);
            }
        }
    }
    from.selectedIndex = -1;
    to.selectedIndex = -1;
    return false;
}
function hasOptions(obj){
    if (obj!=null && obj.options!=null)
        return true;
    else
        return false;
}
function removeSelectedOptions() {
    var from = document.forms[0]['results[]'];
    if (!hasOptions(from))
        return;
    if (from.type=="select-one")
        from.options[from.selectedIndex] = null;
    else {
        for (var i=(from.options.length-1); i >= 0; i--) {
            var o=from.options[i];
            if (o.selected)
                from.options[i] = null;
        }
    }
    from.selectedIndex = -1;
    return false;
}
function addOption() {
    var value = document.forms[0]['addme'].value;
    if (value == '')
        return false;
    var options = new Object();
    var to = document.forms[0]['results[]'];
    if (hasOptions(to)) {
        for (var i=0; i < to.options.length; i++)
            options[to.options[i].value] = to.options[i].text;
    }
    if (options[value] == null || options[value] == "undefined")
        to.options[to.options.length] = new Option(value, value, false, false);

    to.selectedIndex = -1;
    document.forms[0]['addme'].value = '';
    return false;
}

function submitForm() {
    var res = document.forms[0]['results[]'];
    for (var i=(res.options.length-1); i >= 0; i--)
        res.options[i].selected=true;
    return false;
}
</script>
<style type="text/css">
.selectbx { width: 200px; height: 200px; margin: 0; }
.tdbx { vertical-align: top; width: 202px; padding: 0; }
.tdbtn { vertical-align: middle; width: 60px; text-align: center; }
</style>
</head>
<body>
<div id="body">
<div id="widetop">
<h1>Create Your Compatability Quiz</h1>

<p class="lg">Okay <?= $user->getFirstName() ?>, this is where you create your compatability quiz. 
Here's how it works.  You first decide the compatability topics you want your quiz to measure. 
Common ones are listed below in the left box, but feel free to type in your own.  In the next step, you will 
start adding questions and deciding how these questions affect the results.</p>
</div>

<p><b>Note:</b> javascript must be enabled to create a quiz.</p>

<?php
if ($err)
    echo '<p><b>ERROR.</b> You have to choose at least one compatability topic. You can highlight one or more from the left box and click the arrow button to move them into your set, or you can use the text box underneath to add your own custom ones.</p>';
?>

<form id="main" action="create1_update.html" method="post">
  <table>
    <tr>
      <th>Examples</th>
      <th>&nbsp;</th>
      <th>Your Compatability Topics</th>
    </tr>
    <tr>
      <td class="tdbx">
        <select name="examples" class="selectbx" size="10" multiple>
          <option value="Common Beliefs">Common Beliefs</option>
          <option value="Hobbies and Interests">Hobbies and Interests</option>
          <option value="Financial Situation">Financial Situation</option>
          <option value="Sense of Humor">Sense of Humor</option>
          <option value="Sense of Fashion">Sense of Fashion</option>
          <option value="Relationship Intentions">Relationship Intentions</option>
          <option value="Intellectual Pursuits">Intellectual Pursuits</option>
          <option value="Physical Appearance">Physical Appearance</option>
        </select>
      </td>
      <td class="tdbtn">
        <input type="button" value=" --&gt; " onclick="copySelectedOptions();">
      </td>
      <td class="tdbx">
        <select name="results[]" class="selectbx" size="10" multiple>
        </select><br>
        <input type="button" value="Remove Selected" onclick="removeSelectedOptions();"><br>
      </td>
    </tr>
    <tr>
      <td colspan="2">
        Add a custom compatability topic:
      </td>
      <td>
        <input type="text" size="20" maxlength="30" name="addme">
        <input type="button" value="Add" onclick="addOption();">
      </td>
   </table>

   <br><br>
   When you are satisfied with your choices, press the button below. You will not be able to 
   change these later.<br>
   <input type="submit" value="Continue" onclick="submitForm();">
</form>

<?php
$ui = Component::getInstance();
$ui->drawHeader($_SESSION['user']);
$ui->drawFooter();
?>
</div>
</body>
</html>

