/*
   Filename: quiz_impl.js
   Author:   Stefan Dahlke, Falko Zander
   Created:  2007-06-01
   Update:   2007-09-13

   Abstract: This file contains all necessary JavaScript for Quizzes implementation.

   Notes:

*/

var textarea_elem = null;
var title_elem = null;
var questions_elem = null;
var answers_elem = null;
var dnd_elem = null;
var score_elem = null;
var points_elem = null;
var ok_elem = null;
var feedback_elem = null;
var quiz_image_elem = null;
var image_popup = null;
var ltc_elem = null;
var gtc_elem = null;
var feedback_popup = null;
var fp_main = null;
var fp_close = null;
var helpers_box = null;
var image_popup = null;
var img_main = null;
var img_close = null;
var quiznbr_elem = null;

var quiz_image_dnd_elem = null;
var feedback_dnd_elem = null;

var local_timeout = null;
var global_timeout = null;
var global_time = 0;
var ltc_control = null;
var gtc_control = null;

var quiz_type = "";
var quiz_points = 0;
var title_html = "";
var questions_html = "";
var answer_html = "";
var points_html = "";
var dnd_html = "";
var quiz_checked = 0;

var num_drag_targets = 0;
var num_drag_objects = 0;
var dnd_puzzle = null;

var flags = [];
var answers = [];
var questions = [];
var possible_answers = [];
var correct_answers = [];

var quiz_result = { answers: [], answer_types: [] };

var answer_type = { wrong: "answer_type.wrong",
                    correct: "answer_type.correct",
                    both: "answer_type.both",
                    none: "answer_type.none" }

// --------------- end global variables ------------------------------------------------------

function quiz_blank_interface() {
    textarea_elem = document.getElementById("textarea");
    title_elem = document.getElementById("title");
    questions_elem = document.getElementById("questions");
    answers_elem = document.getElementById("answers");
    dnd_elem = document.getElementById("dnd");
    score_elem = document.getElementById("score");
    points_elem = document.getElementById("points");
    ok_elem = document.getElementById("ok");
    feedback_elem = document.getElementById("feedback");
    quiz_image_elem = document.getElementById("quiz_image");
    image_popup = document.getElementById("image_popup");
    ltc_elem = document.getElementById("local_time_counter");
    gtc_elem = document.getElementById("global_time_counter");
    feedback_popup = document.getElementById("feedback_popup");
    fp_main = document.getElementById("fp_main");
    fp_close = document.getElementById("fp_close");
    helpers_box = document.getElementById("helpers_box");
    image_popup = document.getElementById("image_popup");
    img_main = document.getElementById("img_main");
    img_close = document.getElementById("img_close");
    quiznbr_elem = document.getElementById("quiznbr");
    var config = document.forms.config;

    var ids = [];

    if (!textarea_elem) ids.push("textarea");
    if (!questions_elem) ids.push("questions");
    if (!answers_elem) ids.push("answers");
    if (!title_elem) ids.push("title");
    if (!dnd_elem) ids.push("dnd");
    if (!score_elem) ids.push("score");
    //if (!points_elem) ids.push("points");     OPTIONAL
    if (!ok_elem) ids.push("ok");
    if (!feedback_elem) ids.push("feedback");
    if (!quiz_image_elem) ids.push("quiz_image");
    if (!image_popup) ids.push("image_popup");
    if (!ltc_elem) ids.push("local_time_counter");
    if (!gtc_elem) ids.push("global_time_counter");
    if (!feedback_popup) ids.push("feedback_popup");
    if (!fp_main) ids.push("fp_main");
    if (!fp_close) ids.push("fp_close");
    if (!helpers_box) ids.push("helpers_box");
    if (!image_popup) ids.push("image_popup");
    if (!img_main) ids.push("img_main");
    if (!img_close) ids.push("img_close");
    //if (!quiznbr_elem) ids.push("quiznbr");   OPTIONAL

    if (ids.length) {
        alert("INCOMPLETE quiz_blank.html\nthe following Object-IDs are missing:\n    - " +
              ids.join("\n    - "));
        return 0;
    }

    if (!config) {
        alert("INCOMPLETE quiz_blank.html\nthe following object is missing:\n    - document.forms.config");
        return 0;
    }

    return 1;
}

// --------------- end quiz_blank.html interface  ------------------------------------------------------

function quiz_set_caption(cap) {
    title_html = cap;
    quiz_result.title = cap;
}
function quiz_set_score(score) {
    quiz_points = score;
    quiz_result.points = score;
    points_html = score;
}
function quiz_set_question(question) {
    questions_html = question;
    quiz_result.question = question;
}
function quiz_set_flag(flag_name, flag_data) {
    flags[flag_name] = flag_data;
}
function quiz_add_question_answer_pair(question, answer) {
    questions.push(question);
    answers.push(answer);
}
function quiz_add_answer(answer) {
    answers.push(answer);
}

// --------------- end external data input interface ------------------------------------------------------

function mchoice_enable() {
    answer_html = "<table cellspacing='3' cellpadding='3' border='0'>";
    if (flags.allow_multiple_choices == 1) {
        for (var i = 0; i < answers.length; i++) {
            answer_html +=
                "<tr><td><input id='choice" + i + "' type='checkbox' class='mchoice_checkbox' name='choice' onclick='update_ok()'></td>" +
                "<td class='answer'>" + answers[i] + "</td></tr>";
        }
    } else {
        for (var i = 0; i < answers.length; i++) {
            answer_html +=
                "<tr><td><input id='choice" + i + "' type='radio' class='mchoice_radiobutton' name='choice' onclick='update_ok()'></td>" +
                "<td class='answer'>" + answers[i] + "</td></tr>";
        }
    }
    answer_html += "</table>";
    answers_elem.innerHTML = answer_html;
    textarea_elem.style.visibility = "visible";
    document.getElementById("choice0").focus();
    return 1;
}
function sqmaquiz_enable() {
    switch (flags.answer_type) {
        case "at_integer":
            for (var i = 0; i < answers.length; i++) {
                answer_html +=
                    '<input type="text" id="answer' + i + '" class="answer_input" name="answer' + i + '" size="50" onkeydown="ff_pre_input(this, event, 0);" onkeypress="return input_only_int(event);" onkeyup="update_ok();" /><br />';
            }
            break;
        case "at_float":
            for (var i = 0; i < answers.length; i++) {
                answer_html +=
                    '<input type="text" id="answer' + i + '" class="answer_input" name="answer' + i + '" size="50" onkeydown="ff_pre_input(this, event, 1);" onkeypress="return input_only_float(this, event);" onkeyup="update_ok();" /><br />';
            }
            break;
        default:
            for (var i = 0; i < answers.length; i++) {
                answer_html +=
                    '<input type="text" id="answer' + i + '" class="answer_input" name="answer' + i + '" size="50" onkeyup="update_ok()" /><br />';
            }
            break;
    }
    answers_elem.innerHTML = answer_html;
    textarea_elem.style.visibility = "visible";
    document.getElementById("answer0").focus();
    return 1;
}
function fibquiz_enable() {
    if (flags.quiz_type == "qt_drop_down" || flags.quiz_type == "qt_drag_drop") {
        for (var i = 0; i < answers.length; i++) {
            var answ = answers[i].split(",");
            var possible = [];
            var correct = [];
            possible_answers.push(possible);
            correct_answers.push(correct);
            for (var j = 0; j < answ.length; j++) {
                if (answ[j].substr(0,1) == '*') {
                    answ[j] = answ[j].substr(1, answ[j].length-1);
                    correct.push(answ[j]);
                }
                possible.push(answ[j]);
            }
        }

        if (flags.one_for_all == 1 && flags.shuffle_solutions == 1) {
            correct_answers[0].sort(Numsort);
            for (var i = 0; i < correct_answers[0].length; i++) {
                var x = parseInt(correct_answers[0][i]);
                if (!isNaN(x)) {
                    correct_answers[0][i] = correct_answers[0][i].substr(x.toString().length + 1);
                }
            }
            for (var i = 0; i < possible_answers[0].length; i++) {
                var x = parseInt(possible_answers[0][i]);
                if (!isNaN(x)) {
                    possible_answers[0][i] = possible_answers[0][i].substr(x.toString().length + 1);
                }
            }
        }
    }

    var questions = flags.fibtext.split("___");

    answer_html = "";
    var html = [];
    var dd = [];
    switch(flags.quiz_type) {
        case "qt_text":
        case "qt_cs_text":
            for (var i = 0; i < answers.length; i++) {
                var myhtml = "<input type='text' class='fib_input' id='fib" + i + "' name='fib" + i + "' size='10' onkeyup='update_ok()'>";
                html.push(myhtml);
            }
            break;
        case "qt_drop_down":
            for (var i = 0; i < questions.length-1; i++) {
                var myhtml = "<select id='fib" + i + "' name='fib" + i + "' onchange='update_ok()'>";
                myhtml += "<option></option>";
                if (flags.one_for_all == 1) {
                    for (var j = 0; j < possible_answers[0].length; j++) {
                        myhtml += "<option>" + possible_answers[0][j] + "</option>";
                    }
                } else {
                    for (var j = 0; j < possible_answers[i].length; j++) {
                        myhtml += "<option>" + possible_answers[i][j] + "</option>";
                    }
                }
                myhtml += "</select>"
                html.push(myhtml);
            }
            break;
        case "qt_drag_drop":
            for (var i = 0; i < questions.length-1; i++, num_drag_targets++) {
                var myhtml = "<span style='margin-right:4px' id='fib_t_" + num_drag_targets + "'></span>";
                html.push(myhtml);
            }
            if (flags.one_for_all == 1) {
                for (var i = 0; i < possible_answers[0].length; i++, num_drag_objects++) {
                    dnd_html += "<div style='position:absolute' id='fib_o_" + num_drag_objects + "'>&nbsp;" + possible_answers[0][i] + "&nbsp;</div>";
                }
            } else {
                dnd_html = [];
                for (var i = 0; i < possible_answers.length; i++) {
                    var a = [];
                    for (var j = 0; j < possible_answers[i].length; j++) {
                        var h = "<div style='position:absolute' id='fib_o_" + num_drag_objects + "'>&nbsp;" + possible_answers[i][j] + "&nbsp;</div>";
                        dnd_html.push(h);
                        num_drag_objects++;
                    }
                }
            }
            break;
        default:
            return 0;
    }

    for (var i = 0; i < questions.length; i++) {
        answer_html += questions[i];
        if (i < questions.length-1) {
            answer_html += html[i];
        }
    }

    // end pre-display phase

    answers_elem.innerHTML = answer_html;
    answers_elem.style.lineHeight = "20px";
    textarea_elem.style.visibility = "visible";

    if (flags.quiz_type == "qt_text" || flags.quiz_type == "qt_cs_text") {
        document.getElementById("fib0").focus();
        return 1;
    } else if (flags.quiz_type == "qt_drop_down") {
        document.getElementById("fib0").focus();
        return 1;
    }

    var dnd_o = [];
    var dnd_t = [];

    if (flags.one_for_all == 1) {
        dnd_elem.innerHTML = dnd_html;
    } else {
        dnd_elem.innerHTML = "";
        for (var i = 0; i < dnd_html.length; i++) {
            dnd_elem.innerHTML += dnd_html[i];
        }
    }

    var dim = get_max_dimensions("fib_o_", num_drag_objects);

    if (dim.max_width_idx == -1) return 0;

    var txt = document.getElementById("fib_o_" + dim.max_width_idx).innerHTML;
    for (var i = 0; i < num_drag_targets; i++) {
        var e = document.getElementById("fib_t_" + i);
        if (!e) return 0;
        var pre_left = e.offsetLeft;
        e.innerHTML = txt;
        if (e.offsetLeft == pre_left && i != 0) {
            e.style.top = e.offsetTop + e.offsetHeight + "px";
        }

        dnd_t.push(new dndTarget("fib_t_" + i));
        dnd_t[i].addEffect(tar_effects.CSS, tar_effects.centeredDrop, tar_effects.replaceableTarget);
    }

    for (var i = max_objects_per_row = 0; i < possible_answers.length; i++) {
        for (var j = 0, objects_per_row = 0; j < possible_answers[i].length; j++, objects_per_row++) {
            if (objects_per_row > max_objects_per_row) {
                max_objects_per_row = objects_per_row;
            }
        }
    }
    dnd_elem.style.height = (max_objects_per_row + 1) * (dim.max_height + 5) + 10 + "px";

    for (var i = act_object = 0; i < possible_answers.length; i++) {
        for (var j = 0; j < possible_answers[i].length; j++, act_object++) {
            var elem_id = "fib_o_" + act_object;
            var e = document.getElementById(elem_id);
            if (!e) return 0;

            e.style.left = i * (dim.max_width + 5) + e.offsetLeft + "px";
            e.style.top = j * (dim.max_height + 5) + e.offsetTop + "px";
            e.style.width = dim.max_width + "px";
            e.style.textAlign = "center";

            dnd_o.push(new dndObject(elem_id));
            if (flags.one_for_all == 1) {
                for (var k = 0; k < dnd_t.length; k++) {
                    dnd_o[act_object].addTarget(dnd_t[k]);
                }
            } else {
                dnd_o[act_object].addTarget(dnd_t[i]);
            }
            dnd_o[act_object].addEffect(dnd_effects.CSS);
            dnd_o[act_object].addFunction(dnd_functions.afterDrop, "update_ok();");
        }
    }
    return 1;
}
function matchquiz_enable() {
    var mixed_answers = answers.copy();
    for (var i = mixed_answers.length - 1; i > 0; i--) {
        if (mixed_answers[i] != mixed_answers[0]) {
            do {
                mixed_answers.mix();
            } while (mixed_answers.join("") == answers.join(""));
            break;
        }
    }

    var mapping = [];
    for (var i = 0; i < answers.length; i++) {
        for (var j = answers.length - 1; j >= 0; j--) {
            if (mixed_answers[j] == answers[i]) {
                mapping.push(j);
            }
        }
    }

    answer_html = "<table id='matchquiz'>";
    switch(flags.quiz_type) {
        case "qt_drop_down":
            for (var i = 0; i < questions.length; i++) {
                answer_html += "<tr><td id='tar_" + i + "'>" + questions[i] + "</td>" +
                               "<td id='spa_" + mapping[i] + "'></td>" +
                               "<td><select name='con_" + i + "' id='con_" + i + "'>";
                for (j = 0; j < mixed_answers.length; j++) {
                    answer_html += "<option id='obj_" + j + "'>" + mixed_answers[j] + "</option>";
                }
                answer_html += "</select></td></tr>";
            }
            break;
        case "qt_drag_drop":
            for (var i = 0; i < questions.length; i++) {
                answer_html += "<tr><td>" + questions[i] + "</td>" +
                               "<td id='tar_" + i + "'></td>" +
                               "<td id='spa_" + mapping[i] + "'></td>" +
                               "<td id='con_" + i + "'><div id='obj_" + i + "'>&nbsp;" + mixed_answers[i] + "&nbsp;</div></td></tr>";
            }
            break;
        default:
            return 0;
    }

    answer_html += "</table>";

    // end pre-display phase

    answers_elem.innerHTML = answer_html;
    textarea_elem.style.visibility = "visible";
    if (flags.quiz_type == "qt_drop_down") {
        document.getElementById("con_0").focus();
        update_ok();
        return 1;
    }

    var dnd_t = [];
    var dnd_o = [];

    var dim = get_max_dimensions("obj_", answers.length);

    document.getElementById("spa_0").style.width = "20px";
    document.getElementById("con_0").style.width = dim.max_width + "px";
    document.getElementById("tar_0").style.width = dim.max_width + "px";

    for (var i = 0; i < questions.length; i++) {
        document.getElementById("tar_" + i).style.height = dim.max_height + "px";
        dnd_t.push(new dndTarget("tar_" + i));
        dnd_t[i].addEffect(tar_effects.CSS, tar_effects.replaceableTarget, tar_effects.centeredDrop);
    }

    for (var i = 0; i < answers.length; i++) {
        var elem_id = "obj_" + i;

        var obj = document.getElementById(elem_id);
        if (!obj) return 0;

        dnd_o.push(new dndObject(elem_id));
        for (var j = 0; j < questions.length; j++) {
            dnd_o[i].addTarget(dnd_t[j]);
        }
        dnd_o[i].addEffect(dnd_effects.CSS);
        dnd_o[i].addFunction(dnd_functions.afterDrop, "update_ok();");

        var container = document.getElementById("con_" + i);
        var cont_pos = getAbsoluteDimensions(container);
        var obj_pos = getAbsoluteDimensions(obj);
        obj.style.left = cont_pos.offsetLeft - obj_pos.parentsLeft + "px";
        obj.style.top = cont_pos.offsetTop - obj_pos.parentsTop + "px";
        obj.style.width = dim.max_width + "px";
        obj.style.height = dim.max_height + "px";
        obj.style.textAlign = "center";
        dnd_o[i].setHome();
    }

    return 1;
}
function mixquiz_enable() {
    var dnd_member_ids = [];
    if (flags.quiz_type == "qt_drop_down") {
        var mixed_answers = answers.copy();
        for (var i = mixed_answers.length - 1; i > 0; i--) {
            if (mixed_answers[i] != mixed_answers[0]) {
                do {
                    mixed_answers.mix();
                } while (mixed_answers.join("") == answers.join(""));
                break;
            }
        }
        var selectbox = "";
        for (var i = 0; i < mixed_answers.length; i++) {
            selectbox += "<option>" + mixed_answers[i] + "</option>";
        }
        selectbox += "</select>";

        for (var i = 0; i < answers.length; i++) {
            answer_html += "<select name='mix_" + i + "' id='mix_" + i + "'>";
            answer_html += selectbox;
            if (flags.quiz_alignment == "qa_vertical") {
                answer_html += "<br />";
            }
        }
    } else {
        answer_html = "<table>";
        if (flags.quiz_alignment == "qa_horizontal") {
            answer_html += "<tr>";
        }

        for (var i = 0; i < answers.length; i++) {
            if (flags.quiz_alignment == "qa_horizontal") {
                answer_html +=
                    "<td id='obj_" + i + "_tar'><div style='position:absolute' id='obj_" + i + "'>&nbsp;" + answers[i] + "&nbsp;</div></td>";
            } else {
                answer_html +=
                    "<tr><td id='obj_" + i + "_tar'><div style='position:absolute' id='obj_" + i + "'>&nbsp;" + answers[i] + "&nbsp;</div></td></tr>";
            }
            dnd_member_ids.push("obj_" + i);
        }
        if (flags.quiz_alignment == "qa_horizontal") {
            answer_html += "</tr>";
        }
        answer_html += "</table>";
    }

    // end pre-display phase

    answers_elem.innerHTML = answer_html;
    textarea_elem.style.visibility = "visible";

    if (flags.quiz_type == "qt_drop_down") {
        update_ok();
        document.getElementById("mix_0").focus();
        return 1;
    }

    var dim = get_max_dimensions("obj_", answers.length);
    var display_order = [];

    for (var i = 0; i < answers.length; i++) {
        var e = document.getElementById("obj_" + i + "_tar");
        if (!e) return 0;

        e.style.width = dim.max_width + "px";
        e.style.height = dim.max_height + "px";
        e.innerHTML += "&nbsp;";

        display_order.push(i);

        var obj = document.getElementById("obj_" + i);
        if (!obj) return 0;
        obj.style.width = dim.max_width + "px";
        obj.style.textAlign = "center";
    }

    var dnd = eval("new sortGroup('" + dnd_member_ids.join("', '") + "');");
    dnd.addEffect(sort_effects.mixObjects, sort_effects.CSS);
    if (flags.quiz_alignment == "qa_horizontal") {
        dnd.addEffect(sort_effects.direcHorizontal);
    } else {
        dnd.addEffect(sort_effects.direcVertical);
    }

    update_ok();

    return 1;
}
function scalequiz_enable() {
    var scale_width = 600;

    var h = answers[1].split("-");
    var minimum = h[0];
    var maximum = h[1];

    answer_html =
        "<div id='minval' style='position:absolute'>" + minimum + "</div>" +
        "<div id='range' style='width:" + scale_width + "px;height:5px;border:1px solid black;font-size:1px;position:absolute;'></div>" +
        "<div id='maxval' style='position:absolute'>" + maximum + "</div>" +
        "<img id='slider' src='" + get_location("quiz_slider") + "' height='41' width='15' style='position:absolute'>" +
        "<input id='actual' type='text' size='10' value='" + minimum + "' readonly='readonly' style='position:absolute;text-align:center' class='answer_input'>";

    // end pre-display phase

    answers_elem.innerHTML = answer_html;
    textarea_elem.style.visibility = "visible";

    var slider = document.getElementById("slider");
    var range = document.getElementById("range");
    var actual = document.getElementById("actual");
    var minval = document.getElementById("minval");
    var maxval = document.getElementById("maxval");

    answers_elem.style.height = slider.offsetHeight + range.offsetHeight + actual.offsetHeight + 20 + "px"

    dnd_display_correction();
    slider.style.left = range.offsetLeft + "px";
    var rdim = getAbsoluteDimensions(range);

    var dnd = new dndObject("slider");
    dnd.addEffect(dnd_effects.restrictedHorizontal);
    dnd.setArea(-1, rdim.offsetLeft + rdim.offsetWidth + 5, -1, rdim.offsetLeft);
    dnd.addFunction(dnd_functions.whileDragging, "scalequiz_showpos(idx);");

    update_ok();
    return 1;
}
function gridquiz_enable() {
    answer_html = "<table id='grid' class='grid'><tr>" +
                  "<th>&nbsp;</th>";
    var ans = flags.grid_list.split(",");
    for (var i = 0; i < ans.length; i++) {
        answer_html += "<th class='gridspacer' rowspan='" + (questions.length + 1) + "'>&nbsp;</th>" +
                       "<th style='text-align:center'>" + ans[i] + "</th>";
    }
    answer_html += "<th class='gridspacer' rowspan='" + (questions.length + 1) + "'>&nbsp;</th>";
    answer_html += "</tr>";
    for (var i = 0; i < questions.length; i++) {
        answer_html += "<tr><td id='gridq_" + i + "' class='gridquestions'>" + questions[i] + "</td>";
        for (var j = 0; j < ans.length; j++) {
            answer_html += "<td class='gridanswers' id='grida_" + i + "_" + j + "' onclick='gridquiz_show(" + i + ", " + j + ")'>&nbsp;</td>";
        }
        answer_html +="</tr>";
    }
    answer_html += "</table>";

    // end pre-display phase

    answers_elem.innerHTML = answer_html;
    textarea_elem.style.visibility = "visible";

    var grid = document.getElementById("grid");
    var first_tr = grid.firstChild.firstChild;
    var max_width = 0;
    for (var i = 1; i < first_tr.childNodes.length; i++) {
        var e = first_tr.childNodes[i];
        if (e.className != "gridspacer") {
            max_width = Math.max(max_width, e.offsetWidth);
        }
    }
    for (var i = 1; i < first_tr.childNodes.length; i++) {
        var e = first_tr.childNodes[i];
        if (e.className != "gridspacer") {
            e.style.width = max_width + "px";
            e.style.paddingLeft = "4px";
        }
    }
    return;
}
function puzzlequiz_enable() {
    var img_src = flags.qm_file;
    answer_html = "<img src='" + img_src + "' style='visibility:hidden' id='puzzleimage' alt='image'>";
    answers_elem.innerHTML = answer_html;
    textarea_elem.style.visibility = "visible";

    // end pre-display phase

    wait_for_img_load("puzzleimage", "puzzlequiz_enable2");
}
function puzzlequiz_enable2() {
    var img_src = flags.qm_file;
    var img = document.getElementById("puzzleimage");
    var img_w = img.width;
    var img_h = img.height;

    var piece_w = Math.round(img_w / flags.qp_pieces_x);
    var piece_h = Math.round(img_h / flags.qp_pieces_y);

    answers_elem.style.height = answers_elem.offsetHeight + "px";

    answer_html = "<table cellspacing='1' cellpadding='0'>";
    for (var i = 0; i < flags.qp_pieces_y; i++) {
        answer_html += "<tr>";
        for (var j = 0; j < flags.qp_pieces_x; j++) {
            answer_html +=
                "<td style=''>" +
                "<div id='t_" + i + "_" + j + "' style='border:1px solid black;width:" + piece_w + "px;height:" + piece_h + "px'>" +
                "<div id='d_" + i + "_" + j + "' style='position:relative;width:" + piece_w + "px;height:" + piece_h + "px;overflow:hidden'>" +
                "<div style='position:relative;left:-" + j * piece_w + "px;top:-" + i * piece_h + "px;width:" + img_w + "px;height:" + img_h + "px;background-image:url(" + img_src + ")'></div>"; +
                "</div></div></td>";
        }
        answer_html += "</tr>";
    }
    answer_html += "</table>";

    if (flags.quiz_type == "qt_twoplace_puzzle") {
        answer_html += "<table cellspacing='1' cellpadding='0' style='margin-top:15px'>";
        for (var i = 0; i < flags.qp_pieces_y; i++) {
            answer_html += "<tr>";
            for (var j = 0; j < flags.qp_pieces_x; j++) {
                answer_html +=
                    "<td style=''>" +
                    "<div id='o_" + i + "_" + j + "' style='border:1px solid black;width:" + piece_w + "px;height:" + piece_h + "px'></div>" +
                    "</td>";
            }
            answer_html += "</tr>";
        }
        answer_html += "</table>";
    }

    answers_elem.innerHTML = answer_html;

    // end structural-display phase

    dnd_puzzle = new dndPuzzle("d_", {x: flags.qp_pieces_x, y: flags.qp_pieces_y }, (flags.quiz_type == "qt_oneplace_puzzle" ? 1 : 2));

    update_ok();
    return 1;
}
function hotspotquiz_enable() {
    answer_html = "<img src='" + flags.hs_image + "' alt='hotspotimage' id='hotspotimage' style='cursor:crosshair' onclick='hotspot_setpin(event)'>" +
                  "<img src='" + get_location("hotspot_pin") + "' id='hotspotpin' width='27' height='35' alt='hotspotpin' style='position:absolute;display:none'>";
    answers_elem.innerHTML = answer_html;
    textarea_elem.style.visibility = "visible";

    // end pre-display phase

    return 1;
}
function quiz_enable(qt) {
    if (typeof(ctx.pj_global_timeout) == 'undefined' || ctx.pj_global_timeout != 0) {
        quiz_type = qt;
        quiz_result.quiz_type = qt;

        if (!quiz_blank_interface()) return;
        show_score();

        title_elem.innerHTML = title_html;
        questions_elem.innerHTML = questions_html;
        if (points_elem != null) points_elem.innerHTML = points_html;
        if (quiznbr_elem != null) quiznbr_elem.innerHTML = flags.quiz_number + "/" + ctx.pj_quiz_numbers;
        if (ie) {
            title_elem.style.display = "inline";
        } else {
            title_elem.style.display = "table-cell";
        }

        set_feedback();
        if (!(quiz_type == "puzzlequiz" && flags.qp_show_hint == 0)) show_quiz_image();

        eval(quiz_type + "_enable();");
        if (flags.quiz_skipping == 1) {
            conf_next_button();
        }

        local_timeout_control();
    }

    global_timeout_control();

    if (!ie) {
        var x = document.getElementById("centerTable");
        x.style.height = window.innerHeight - x.offsetTop + "px";
    }

    dnd_display_correction();
}

// --------------- end quiz enable section ------------------------------------------------------

function set_hour(e1_id, e2_id, perc) {
    var e1 = document.getElementById(e1_id);
    var e2 = document.getElementById(e2_id);
    if (ie) {
        // this looks funny but is necessary for IE6!!
        // try: remove it and resize window
        var img = document.getElementById(e1_id.substr(0,3) + "_hg");
        img.style.top = "1px";
        img.style.top = "0px";
        var h1 = e1.offsetTop;
        var h2 = e2.offsetTop;
        e1.style.top = "0px";
        e2.style.top = "0px";
        e1.style.top = h1 + "px";
        e2.style.top = h2 + "px";
    }
    var x = Math.floor(22 * perc);
    e1.style.top = x - 10 + "px";
    e1.style.height = 22 - x + "px";
    e2.style.height = x + "px";
}
function global_timeout_control() {
    if (typeof(ctx.pj_global_timeout) == 'undefined') return;

    if (ctx.pj_global_timeout > 0) {
        var x = new Date;
        global_time = x.getTime();
        global_timeout = window.setTimeout("time_out(1)", ctx.pj_global_timeout * 1000);
        if (ctx.pj_global_timeout_clock != 0) {
            gtc_elem.parentNode.style.display = ie ? "block" : "table-row";
            if (document.forms.config.timeout_hourglas.value == "1" || typeof(document.forms.config.timeout_hourglas) == 'undefined') {
                gtc_elem.innerHTML =
                    "<table cellpadding='0' cellspacing='0' align='right'><tr>" +
                    "<td>" +
                        "<div id='gtc_hg_t' style='position:relative;font-size:1px;height:17px;width:25px;background-color:red;top:-10px;left:28px'></div>" +
                        "<div id='gtc_hg_b' style='position:relative;font-size:1px;height:1px;width:25px;background-color:red;top:10px;left:28px'></div>" +
                    "</td><td style='padding-right:7px;'>" +
                        "<img id='gtc_hg' style='position:relative;z-index:1' src='" + get_location("hourglass_gif") + "' alt='hourglass' width='30' height='49'>" +
                    "</td>" +
                    "<td id='gtc_dt' style='vertical-align:middle'>" + convert_time(ctx.pj_global_timeout) + "<span style='display:none'>" + ctx.pj_global_timeout + "</span></td>" +
                    "</tr></table>";
            } else {
                gtc_elem.innerHTML = convert_time(ctx.pj_global_timeout) + "<span style='display:none'>" + ctx.pj_global_timeout + "</span>";
            }
            var gtc_decrease = function() {
                elem = (document.forms.config.timeout_hourglas.value == "1" || typeof(document.forms.config.timeout_hourglas) == 'undefined')
                     ? document.getElementById("gtc_dt")
                     : gtc_elem;
                var time = elem.childNodes[1].innerHTML - 1;
                elem.innerHTML = convert_time(time) + "<span style='display:none'>" + time + "</span>";
                if (document.forms.config.timeout_hourglas.value == "1" || typeof(document.forms.config.timeout_hourglas) == 'undefined') {
                    set_hour("gtc_hg_t", "gtc_hg_b", (1 - time / ctx.pj_global_timeout));
                }
            }
            gtc_control = window.setInterval(gtc_decrease, 1000);
        }
    } else {
        var func = function () {
            ctx.cfg_action('PLAY');
        }
        window.setTimeout(func, 100);
    }
}
function local_timeout_control() {
    if (flags.quiz_timeout == 0) return;
    local_timeout = window.setTimeout("time_out(0)", flags.quiz_timeout * 1000);
    if (flags.quiz_timeout_clock != 0) {
        ltc_elem.parentNode.style.display = ie ? "block" : "table-row";
        if (document.forms.config.timeout_hourglas.value == "1" || typeof(document.forms.config.timeout_hourglas) == 'undefined') {
            ltc_elem.innerHTML =
                "<table cellpadding='0' cellspacing='0'><tr>" +
                "<td style='padding-right:7px;'>" +
                    "<img id='ltc_hg' style='position:relative;z-index:1' src='" + get_location("hourglass_gif") + "' alt='hourglass' width='30' height='49'>" +
                "</td><td>" +
                    "<div id='ltc_hg_t' style='position:relative;font-size:1px;height:17px;width:25px;background-color:red;top:-10px;left:-35px'></div>" +
                    "<div id='ltc_hg_b' style='position:relative;font-size:1px;height:1px;width:25px;background-color:red;top:10px;left:-35px'></div>" +
                "</td>" +
                "<td id='ltc_dt' style='vertical-align:middle'><div style='position:relative;left:-27px'>" + convert_time(flags.quiz_timeout) + "<span style='display:none'>" + flags.quiz_timeout + "</span></div></td>" +
                "</tr></table>";
        } else {
            ltc_elem.innerHTML = convert_time(flags.quiz_timeout) + "<span style='display:none'>" + flags.quiz_timeout + "</span>";
        }
        var ltc_decrease = function() {
            if (document.forms.config.timeout_hourglas.value == "1" || typeof(document.forms.config.timeout_hourglas) == 'undefined') {
                var elem = document.getElementById("ltc_dt");
                var time = elem.firstChild.childNodes[1].innerHTML - 1;
                elem.innerHTML =
                    "<div style='position:relative;left:-27px'>" + convert_time(time) + "<span style='display:none'>" + time + "</span></div>";
                set_hour("ltc_hg_t", "ltc_hg_b", (1 - time / flags.quiz_timeout));
            } else {
                var time = ltc_elem.childNodes[1].innerHTML - 1;
                ltc_elem.innerHTML =
                    convert_time(time) + "<span style='display:none'>" + time + "</span>";
            }
        }
        ltc_control = window.setInterval(ltc_decrease, 1000);
    }
}
function time_out(mode) {
    quiz_disable();
    conf_next_button(1, "next");
    show_feedback_popup("feedback_time");

    if (global_timeout != null) {
        stop_global_timeout();
        if (mode == 1) {
//            if (!ie) gtc_elem.innerHTML = "00:00:00";
            ctx.pj_global_timeout = 0;
        }
    }

    if (local_timeout != null) {
        stop_local_timeout();
//        if (mode == 0 && !ie) ltc_elem.innerHTML = "00:00:00";
    }
}
function stop_global_timeout() {
    window.clearTimeout(global_timeout);
    window.clearInterval(gtc_control);
    global_timeout = null;
    gtc_control = null;
    var x = new Date;
    var y = x.getTime() - global_time;
    y = Math.ceil(y / 1000);
    ctx.pj_global_timeout -= y;
}
function stop_local_timeout() {
    window.clearTimeout(local_timeout);
    window.clearInterval(ltc_control);
    local_timeout = null;
    ltc_control = null;
}
function set_feedback() {
    var x =
        "<div id='feedback_pass' style='display:none'>" + flags.feedback_pass + "</div>" +
        "<div id='feedback_fail' style='display:none'>" + flags.feedback_fail + "</div>" +
        "<div id='feedback_time' style='display:none'>" + flags.feedback_time + "</div>";

    if (flags.feedback_popup == 0) {
        feedback_elem.innerHTML = x;
    } else {
        fp_main.innerHTML = x;
    }
}
function get_feedback_solutions(fid) {
    var cs = "";
    if (flags.feedback_correct_solutions == 1 && fid.search(/pass/) == -1) {
        cs = "<p>";
        if (fid.search(/fail/) == -1) on_ok(1);
        for (var i = 0; i < quiz_result.answers.length; i++) {
            if (quiz_result.answer_types[i] == answer_type.correct ||
                quiz_result.answer_types[i] == answer_type.both) {
                cs += "<span style='color:green;font-weight:bold'>" + quiz_result.answers[i] + "</span><br>";
            }
        }
        cs += "</p>";
    }
    return cs;
}
function show_feedback_popup(fid) {
    var e = document.getElementById(fid);
    if (e == null) return;

    cs = get_feedback_solutions(fid);
    e.innerHTML += cs;
    e.style.display = "block";

    if (document.forms.config.dim_on_feedback_popup.value == "1") {
        var op = 0.8;
        var iv = null;
        var dim = function() {
            textarea_elem.style.MozOpacity = op.toString();
            textarea_elem.style.filter = "alpha(opacity=" + op * 100 + ")";
            op -= 0.1;
            if (op < 0.2) {
                window.clearInterval(iv);
                iv = null;
                show_feedback_popup2();
            }
        }
        iv = window.setInterval(dim, 50);
    } else {
        show_feedback_popup2();
    }
}
function show_feedback_popup2() {
    feedback_popup.style.display = "block";
    var ep = getAbsoluteDimensions(feedback_popup);
    feedback_popup.style.top = (ie) ? ((document.documentElement.clientHeight - feedback_popup.offsetHeight) / 2 - ep.parentsTop + "px")
                       : ((window.innerHeight - feedback_popup.offsetHeight) / 2 - ep.parentsTop + "px");
    feedback_popup.style.left = (document.body.offsetWidth - feedback_popup.offsetWidth) / 2 - ep.parentsLeft + "px";
    feedback_dnd_elem = new dndObject(feedback_popup.id);
    fp_main.onclick = function() { go_next(); }
    fp_close.onclick = function() { go_next(); }
}
function hide_feedback_popup() {
    feedback_popup.style.display = "none";
    feedback_dnd_elem = null;
}
function show_quiz_image() {
    if (flags.qm_file == "") {
        image_popup.style.display = "none";
        quiz_image_elem.style.display = "none";
        return;
    }

    helpers_box.innerHTML = flags.qm_file;
    helpers_box.style.display = "block";
    if (helpers_box.offsetWidth > 150) {
        var x;
        switch(flags.qm_file_type) {
            case "swf":
                quiz_image_elem.innerHTML =
                    "<img src='" + get_location("flv_logo") + "' width='25' height='27' alt='flash' onclick='show_image_popup();' id='image_popup_handler'>";
                img_main.innerHTML =
                    "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' align='middle' id='qm_file_swf'>" +
                    "<param name='allowScriptAccess' value='sameDomain' />" +
                    "<param name='movie' value='" + flags.qm_file + "' />" +
                    "<param name='quality' value='high' />" +
                    "<embed src='" + flags.qm_file + "' name='qm_file_swf' quality='high' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'" +
                     (flags.qm_width != -1 ? " width='" + flags.qm_width + "'" : "") +
                     (flags.qm_height != -1 ? " height='" + flags.qm_height + "'" : "") +
                     " />" +
                    "</object>";
                break;
            case "wmv":
                quiz_image_elem.innerHTML =
                    "<img src='" + get_location("wmv_logo") + "' width='28' height='27' alt='wmv' onclick='show_image_popup();' id='image_popup_handler'>";
                img_main.innerHTML =
                    "<object id='qm_file_wmv'" +
                      " width='" + (flags.qm_width != -1 ? flags.qm_width : "640") + "' " +
                      " height='" + (flags.qm_height != -1 ? flags.qm_height : "480") + "' " +
                    "classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'" +
                    "codebase='http://activex.microsoft.com/activex/controls/ mplayer/en/nsmp2inf.cab#Version=5,1,52,701'" +
                    "type='application/x-oleobject'>" +
                    "<param name='fileName' value='" + flags.qm_file + "'>" +
                    "<param name='animationatStart' value='1'>" +
                    "<param name='transparentatStart' value='1'>" +
                    "<param name='autoStart' value='1'>" +
                    "<param name='ShowControls' value='0'>" +
                    "<param name='ShowDisplay' value='0'>" +
                    "<param name='ShowStatusBar' value='0'>" +
                    "<param name='loop' value='0'>" +
                    "<embed type='application/x-mplayer2' " +
                    "pluginspage='http://microsoft.com/windows/mediaplayer/en/download/' "+
                    "id='qm_file_wmv' name='qm_file_wmv' displaysize='4' autosize='0' "+
                    "showcontrols='0' showtracker='1' "+
                    "showdisplay='0' showstatusbar='0' videoborder3d='0'" +
                      " width='" + (flags.qm_width != -1 ? flags.qm_width : "640") + "' " +
                      " height='" + (flags.qm_height != -1 ? flags.qm_height : "480") + "' " +
                    "src='" + flags.qm_file + "' autostart='1' designtimesp='5311' loop='0'>" +
                    "</embed>" +
                    "</object>";
                break;
            case "wma":
                quiz_image_elem.innerHTML =
	                "<object id='qm_file_wma' classid='CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95' " +
                    "codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715' " +
                    "width='150' height='50' type='application/x-oleobject' >" +
                    "<param name='FileName' value='" + flags.qm_file + "'>" +
                    "<param name='TransparentAtStart' Value='0'>" +
                    "<param name='AutoStart' value='0'>" +
                    "<param name='AnimationatStart' value='0'>" +
                    "<param name='ShowStatusBar' value='0'>" +
                    "<param name='ShowControls' value='1'>" +
                    "<param name='autoSize' value='1'>" +
                    "<param name='displaySize' value='0'>" +
                    "<param name='ShowAudioControls' value='1'>" +
                    "<param name='ShowPositionControls' value='0'>" +
                    "<embed type='application/x-mplayer2' pluginspage='http://microsoft.com/windows/mediaplayer/en/download/' " +
                      "src='" + flags.qm_file + "' width='150' height='50' transparentAtStart='0' autostart='0' " +
                      "animationAtStart='0' ShowControls='1' ShowAudioControls='1' ShowPositionControls='0' autoSize='0' " +
                      "ShowStatusBar='0' displaySize='0' ></embed></object>";
                break;
            case "mov":
                quiz_image_elem.innerHTML =
                    "<img src='" + get_location("quicktime_log") + "' width='28' height='27' alt='mov' onclick='show_image_popup();' id='image_popup_handler'>";
                img_main.innerHTML =
                    "<object classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B'" +
                      " width='" + (flags.qm_width != -1 ? flags.qm_width : "640") + "' " +
                      " height='" + (flags.qm_height != -1 ? flags.qm_height : "480") + "' " +
                    "codebase='http://www.apple.com/qtactivex/qtplugin.cab'>" +
                    "<param name='src' value='" + flags.qm_file + "'>" +
                    "<param name='autoplay' value='1'>" +
                    "<param name='controller' value='0'>" +
                    "<param name='loop' value='0'>" +
                    "<embed src='" + flags.qm_file + "'" +
                      " width='" + (flags.qm_width != -1 ? flags.qm_width : "640") + "' " +
                      " height='" + (flags.qm_height != -1 ? flags.qm_height : "480") + "' " +
                    "autoplay='1'" +
                    "controller='0' loop='0' pluginspage='http://www.apple.com/quicktime/download/'>" +
                    "</embed>" +
                    "</object>";
                break;
            case "img":
                quiz_image_elem.innerHTML =
                    "<img src='" + flags.qm_file + "' width='150' onclick='show_image_popup();' alt='quiz_image' id='image_popup_handler' style='border:1px solid black'>" +
                    "<br><a id='image_popup_handler2' href='javascript:void(0);' onclick='show_image_popup();' style='margin-left:10px'>" +
                    "<img style='border:none' src='" + get_location("quiz_lupe_gif") + "' alt='Zoom' width='16' height='16'>" +
                    "</a>";
                img_main.innerHTML = "<img src='" + flags.qm_file + "'" +
                                      (flags.qm_width != -1 ? " width='" + flags.qm_width + "'" : "") +
                                      (flags.qm_height != -1 ? " height='" + flags.qm_height + "'" : "") +
                                      " alt='quiz_image'>";
                break;
            default:
                return;
        }
    } else {
        switch(flags.qm_file_type) {
            case "swf":
                break;
            case "img":
                quiz_image_elem.innerHTML = flags.qm_file;
                break;
            default:
                return;
        }
    }
    helpers_box.style.display = "none";
}
function show_image_popup(dnd) {
    if (typeof(dnd) == 'undefined') dnd = 1;
    img_main.firstChild.onclick = hide_image_popup;
    var e = image_popup;
    e.style.display = "block";
    var ep = getAbsoluteDimensions(e);
    e.style.top = (ie) ? ((document.documentElement.clientHeight - e.offsetHeight) / 2 - ep.parentsTop + "px")
                       : ((window.innerHeight - e.offsetHeight) / 2 - ep.parentsTop + "px");
    e.style.left = (document.body.offsetWidth - e.offsetWidth) / 2 - ep.parentsLeft + "px";
    if (dnd) wwquiz_image_dnd_elem = new dndObject(e.id);
    img_close.onclick = hide_image_popup;
    var iph = document.getElementById("image_popup_handler");
    var iph2 = document.getElementById("image_popup_handler2");
    if (iph != null) iph.onclick = null;
    if (iph2 != null) iph2.onclick = null;
}
function hide_image_popup() {
    image_popup.style.display = "none";
    var iph = document.getElementById("image_popup_handler");
    var iph2 = document.getElementById("image_popup_handler2");
    if (iph != null) iph.onclick = show_image_popup;
    if (iph2 != null) iph2.onclick = show_image_popup;
    quiz_image_dnd_elem = null;
}
function show_score() {
    if (typeof(ctx) == 'undefined') {
        alert("ctx is undefined");
        return;
    }
    var score = ctx.cfg_getscore();
    score_elem.innerHTML = '' + score.current + "/" + score.max;
}
function scalequiz_showpos(idx) {
    var cont = document.getElementById("actual");
    if (cont == null) return;
    var cur = drag_objects[idx];

    var ra = answers[1].split("-");
    var min_range = parseInt(ra[0]);
    var max_range = parseInt(ra[1]);

    var scale = getAbsoluteDimensions(document.getElementById("range"));
    var slider = getAbsoluteDimensions(cur.elem);
    var slider_pos = slider.offsetLeft - scale.offsetLeft;
    var slider_max_pos = scale.offsetWidth - slider.offsetWidth + 5;

    var val = parseInt(slider_pos * (max_range - min_range) / slider_max_pos + min_range);
    cont.value = (val < max_range) ? ((val > min_range) ? val : min_range) : max_range;
}
function gridquiz_show(row, col) {
    var e = document.getElementById("grida_" + row + "_" + col);
    if (e == null) return;
    if (e.innerHTML == "&nbsp;") {
        var ans = flags.grid_list.split(",");
        for (var i = ans.length - 1; i >= 0; i--) {
            var x = document.getElementById("grida_" + row + "_" + i);
            x.style.backgroundImage = "none";
            x.innerHTML = "&nbsp;";
        }
        e.style.backgroundImage = "url(quiz_okay.gif)";
        e.innerHTML = "&nbsp;<div style='display:none;'></div>";
    } else {
        e.style.backgroundImage = "none";
        e.innerHTML = "&nbsp;";
    }
    update_ok();
}
function dnd_display_correction() {
    if (flags.quiz_type == "qt_drag_drop" || quiz_type == "puzzlequiz" || quiz_type == "hotspotquiz") {
        switch (quiz_type) {
            case "fibquiz":
                var dim = get_max_dimensions("fib_o_", num_drag_objects);
                var dnd_pos = getAbsoluteDimensions(dnd_elem);
                for (var i = act_object = 0; i < possible_answers.length; i++) {
                    for (var j = 0; j < possible_answers[i].length; j++, act_object++) {
                        var elem_id = "fib_o_" + act_object;
                        var e = document.getElementById(elem_id);
                        if (!e) return 0;

                        e.style.left = i * (dim.max_width + 5) + dnd_pos.offsetLeft + 10 + "px";
                        e.style.top = j * (dim.max_height + 5) + dnd_pos.offsetTop + 10 + "px";
                        var dnd_o = drag_objects[getDragIdx(elem_id)];
                        dnd_o.setHome();
                        dnd_o.resetToCurTarget();
                    }
                }
                break;
            case "matchquiz":
                for (var i = questions.length - 1; i >= 0; i--) {
                    var d_o = getDragIdx("obj_" + i);
                    if (drag_objects[d_o].ontarget == -1) {
                        var obj = drag_objects[d_o].elem;
                        var container = document.getElementById("con_" + i);
                        var cont_pos = getAbsoluteDimensions(container);
                        var obj_pos = getAbsoluteDimensions(obj);
                        obj.style.left = cont_pos.offsetLeft - obj_pos.parentsLeft + "px";
                        obj.style.top = cont_pos.offsetTop - obj_pos.parentsTop + "px";
                        drag_objects[d_o].setHome();
                    } else {
                        drag_objects[d_o].resetToCurTarget();
                    }
                }
                break;
            case "puzzlequiz":
                if (flags.quiz_type == "qt_oneplace_puzzle") {
                    var func = function() {
                        for (var i = drag_objects.length - 1; i >= 0; i--) {
                            drag_objects[i].resetToCurTarget();
                            drag_objects[i].setHome();
                            drag_objects[i].goHome();
                        }
                    }
                    if (ie) { window.setTimeout(func, 10) } else { func() }
                } else {
                    if (dnd_puzzle != null) {
                        if (ie) {
                            window.setTimeout("dnd_puzzle.resetPositions();", 10);
                        } else {
                            dnd_puzzle.resetPositions();
                        }
                    }
                }
                break;
            case "hotspotquiz":
                var txt = helpers_box.innerHTML;
                if (txt.search(/\|/) == -1) break;
                var c = txt.split("|");
                var pin = document.getElementById("hotspotpin");
                var ip = getAbsoluteDimensions(document.getElementById("hotspotimage"));
                pin.style.left = parseInt(c[0]) + ip.offsetLeft + "px";
                pin.style.top = parseInt(c[1]) + ip.offsetTop - pin.offsetHeight + "px";
                break;
            case "mixquiz":
                for (var i = sort_groups[0].members.length - 1; i >= 0; i--) {
                    sort_groups[0].members[i].resetToCurTarget();
                }
                break;
        }
    } else if (quiz_type == "scalequiz") {
        var slider = document.getElementById("slider");
        var range = document.getElementById("range");
        var actual = document.getElementById("actual");
        var minval = document.getElementById("minval");
        var maxval = document.getElementById("maxval");

        var aep = getAbsoluteDimensions(answers_elem);
        range.style.top = aep.offsetTop + 30 + "px";
        range.style.left = aep.offsetLeft + 24 + "px";
        minval.style.top = aep.offsetTop + 24 + "px";
        maxval.style.top = aep.offsetTop + 24 + "px";
        minval.style.left = aep.offsetLeft + 12 + "px";
        maxval.style.left = aep.offsetLeft + range.offsetWidth + 30 + "px";

        var rdim = getAbsoluteDimensions(range);
        var sdim = getAbsoluteDimensions(slider);
        var adim = getAbsoluteDimensions(actual);

        slider.style.top = rdim.offsetTop - sdim.parentsTop - sdim.offsetHeight / 2 + 4 + "px";
        actual.style.top = rdim.offsetTop - adim.parentsTop + sdim.offsetHeight - 10 + "px";
        actual.style.left = aep.offsetLeft + 12 + "px";

        var dnd = drag_objects[getDragIdx("slider")];
        if (dnd == null) return;
        dnd.setArea(-1, rdim.offsetLeft + rdim.offsetWidth + 5, -1, rdim.offsetLeft);

        var cont = document.getElementById("actual");
        if (cont == null) return;
        var cur = drag_objects[getDragIdx("slider")];
        var ra = answers[1].split("-");
        var min_range = parseInt(ra[0]);
        var max_range = parseInt(ra[1]);
        var scale = getAbsoluteDimensions(document.getElementById("range"));
        var slider_max_pos = scale.offsetWidth - cur.elem.offsetWidth + 5;
        var slider_pos = parseInt((cont.value - min_range) * slider_max_pos / (max_range - min_range));
        cur.elem.style.left = scale.offsetLeft + slider_pos + "px";
    }
}
function hotspot_setpin(e) {
    var ev = (ie) ? window.event : e;

    var ip = getAbsoluteDimensions(document.getElementById("hotspotimage"));

    var pin = document.getElementById("hotspotpin");
    pin.style.display = "block";
    pin.style.left = ev.clientX + "px";
    pin.style.top = ev.clientY - pin.offsetHeight + "px";

    helpers_box.innerHTML = (ev.clientX - ip.offsetLeft) + "|" + (ev.clientY - ip.offsetTop);

    update_ok();
}
function conf_next_button(act, mode) {
    if (!ok_elem) return;
    if (typeof(act) == 'undefined') act = 0;

    var ok_img_elem = null;
    for (var i = 0; i < ok_elem.childNodes.length; i++) {
        if (ok_elem.childNodes[i].nodeName.toUpperCase() == "IMG") {
            ok_img_elem = ok_elem.childNodes[i];
            break;
        }
    }
    if (!ok_img_elem) return;

    if (act) {
        ok_img_elem.src = get_location("next_enabled_gif");
        switch (mode) {
            case "check":
                ok_elem.onclick = function() { on_ok(); }
                ok_img_elem.alt = "check";
                break;
            case "next":
                ok_elem.onclick = function() { go_next(); }
                ok_img_elem.alt = "next";
                break;
        }
    } else {
        if (flags.quiz_skipping == 1 && mode != "disable") {
            ok_img_elem.src = get_location("next_enabled_gif");
            ok_elem.onclick = function() { go_next(); }
        } else {
            ok_img_elem.src = get_location("next_disabled_gif");
            ok_elem.onclick = null;
        }
    }
}

// --------------- end additional display and initialization section ------------------------------------------------------

function update_score() {
    if (typeof(ctx) == 'undefined') return;
    var score = ctx.cfg_getscore();
    ctx.cfg_setscore(score.current + quiz_points);
}
function update_score_sa(elem_id, correct_answer, i) {
    e = document.getElementById(elem_id);
    if (e == null) return 0;

    if (i != -1) possible_answers[i] = e.value.toLowerCase();

    switch(flags.answer_type) {
        case "at_integer":
        case "at_float":
            var l = correct_answer.length;
            var h = e.value.replace(/,/g, ".");
            var cur = parseFloat(h);

            var p = correct_answer.search(/-/);
            if (p != -1) {
                var min = parseFloat(correct_answer.substr(0, p));
                var max = parseFloat(correct_answer.substr(p + 1));
                return (cur >= min && cur <= max);
            } else if (correct_answer.search(/<>/) != -1) {
                var nbr = parseFloat(correct_answer.substr(2));
                return eval(cur + "!=" + nbr);
            } else if (correct_answer.search(/<=|>=/) != -1) {
                var nbr = parseFloat(correct_answer.substr(2));
                return eval(cur + correct_answer.substring(0,1) + "=" + nbr);
            } else if (correct_answer.search(/<|>/) != -1) {
                var nbr = parseFloat(correct_answer.substr(1));
                return eval(cur + correct_answer.substring(0,1) + nbr);
            }
            return (cur == parseFloat(correct_answer));
        case "at_text":
            return (e.value.toLowerCase() == correct_answer.toLowerCase());
        case "at_cs_text":
            return (e.value == correct_answer);
        case "at_regexp":
            var re = new RegExp(correct_answer, "i");
            return (e.value.search(re) != -1);
        case "at_comma_list":
            var li = correct_answer.split(",");
            for (var i = li.length - 1; i >= 0; i--) {
                if (e.value.toLowerCase() == li[i].toLowerCase()) return 1;
            }
            break;
        case "at_cs_comma_list":
            var li = correct_answer.split(",");
            for (var i = li.length - 1; i >= 0; i--) {
                if (e.value == li[i]) return 1;
            }
            break;
    }
    return 0;
}
function on_ok(just_results) {
    if (typeof(just_results) == 'undefined') just_results = 0;
    var update = 0;
    switch (quiz_type) {
        case "mchoice":
            if ("choice" in document.forms.quiz) {
                for (var i = 0, ok = 1; i < document.quiz.choice.length; i++) {
                    var f = "correct_answer_" + (i + 1);
                    quiz_result.answers.push(document.quiz.choice[i].parentNode.nextSibling.firstChild.nodeValue);
                    if (document.quiz.choice[i].checked && flags[f] == 0) {
                        quiz_result.answer_types.push(answer_type.wrong);
                        ok = 0;
                    } else if (!document.quiz.choice[i].checked && flags[f] == 1) {
                        quiz_result.answer_types.push(answer_type.correct);
                        ok = 0;
                    } else if (document.quiz.choice[i].checked && flags[f] == 1) {
                        quiz_result.answer_types.push(answer_type.both);
                    } else {
                        quiz_result.answer_types.push(answer_type.none);
                    }
                }
                if (ok) update = 1;
            }
            break;
        case "sqmaquiz":
            update = 1;
            quiz_result.answer_type = flags.answer_type;
            for (var i = 0; i < answers.length; i++) {
                if (!update_score_sa("answer" + i, answers[i], i)) {
                    quiz_result.answers.push(answers[i]);
                    quiz_result.answer_types.push(answer_type.correct);
                    quiz_result.answers.push(document.getElementById("answer" + i).value);
                    quiz_result.answer_types.push(answer_type.wrong);
                    update = 0;
                } else {
                    quiz_result.answers.push(document.getElementById("answer" + i).value);
                    quiz_result.answer_types.push(answer_type.both);
                }
            }
            break;
        case "fibquiz":
            quiz_result.fibtext = flags.fibtext;
            update = 1;
            switch(flags.quiz_type) {
                case "qt_text":
                case "qt_cs_text":
                    for (var i = 0; i < answers.length; i++) {
                        var v1 = document.getElementById("fib" + i).value;
                        var v2 = answers[i];
                        if (flags.quiz_type == "qt_text") {
                            v1 = v1.toLowerCase();
                            v2 = v2.toLowerCase();
                        }
                        if (v1 != v2) {
                            update = 0;
                            quiz_result.answers.push(document.getElementById("fib" + i).value);
                            quiz_result.answer_types.push(answer_type.wrong);
                            quiz_result.answers.push(answers[i]);
                            quiz_result.answer_types.push(answer_type.correct);
                        } else {
                            quiz_result.answers.push(document.getElementById("fib" + i).value);
                            quiz_result.answer_types.push(answer_type.both);
                        }
                    }
                    break;
                case "qt_drop_down":
                case "qt_drag_drop":
                    if (flags.one_for_all == 1) {
                        for (var i = 0; i < correct_answers[0].length; i++) {
                            var txt = (flags.quiz_type == "qt_drop_down")
                                    ? document.quiz["fib" + i].options[document.quiz["fib" + i].options.selectedIndex].text
                                    : document.getElementById(drag_objects[drag_targets[i].objects_on[0]].elem_id).innerHTML.split("&nbsp;")[1];
                            if (txt != correct_answers[0][i]) {
                                update = 0;
                                quiz_result.answers.push(txt);
                                quiz_result.answer_types.push(answer_type.wrong);
                                quiz_result.answers.push(correct_answers[0][i]);
                                quiz_result.answer_types.push(answer_type.correct);
                            } else {
                                quiz_result.answers.push(txt);
                                quiz_result.answer_types.push(answer_type.both);
                            }
                        }
                    } else {
                        for (var i = 0; i < correct_answers.length; i++) {
                            var txt = (flags.quiz_type == "qt_drop_down")
                                    ? document.quiz["fib" + i].options[document.quiz["fib" + i].options.selectedIndex].text
                                    : document.getElementById(drag_objects[drag_targets[i].objects_on[0]].elem_id).innerHTML.split("&nbsp;")[1];
                            for (var j = 0, found = 0; j < correct_answers[i].length; j++) {
                                if (txt == correct_answers[i][j]) {
                                    found = 1;
                                    quiz_result.answers.push(txt);
                                    quiz_result.answer_types.push(answer_type.both);
                                }
                            }
                            if (!found) {
                                update = 0;
                                quiz_result.answers.push(txt);
                                quiz_result.answer_types.push(answer_type.wrong);
                                quiz_result.answers.push(correct_answers[i].join(" oder "));
                                quiz_result.answer_types.push(answer_type.correct);
                            }
                        }
                    }
                    break;
            }
            break;
        case "matchquiz":
            quiz_result.questions = [];
            for (var i = 0, update = 1; i < answers.length; i++) {
                quiz_result.questions.push(questions[i]);

                var txt;
                if (flags.quiz_type == "qt_drop_down") {
                    var x = document.quiz["con_" + i];
                    var idx1 = x.options[x.options.selectedIndex].id.substr(4);
                    var idx2 = x.parentNode.previousSibling.id.substr(4);
                    var txt = x.options[x.options.selectedIndex].text;
                } else {
                    var x = document.getElementById(drag_objects[drag_targets[i].objects_on[0]].elem_id);
                    var idx1 = x.id.substr(4);
                    var idx2 = document.getElementById("tar_" + i).nextSibling.id.substr(4);
                    var txt = x.innerHTML.split("&nbsp;")[1];
                }
                if (idx1 != idx2) {
                    update = 0;
                    quiz_result.answers.push(answers[i]);
                    quiz_result.answer_types.push(answer_type.correct);
                    quiz_result.answers.push(txt);
                    quiz_result.answer_types.push(answer_type.wrong);
                } else {
                    quiz_result.answers.push(txt);
                    quiz_result.answer_types.push(answer_type.both);
                }
            }
            break;
        case "mixquiz":
            update = 1;
            if (flags.quiz_type == "qt_drop_down") {
                for (var i = 0; i < answers.length; i++) {
                    var txt = document.quiz["mix_" + i].options[document.quiz["mix_" + i].options.selectedIndex].text;
                    if (txt != answers[i]) {
                        update = 0;
                        quiz_result.answers.push(txt);
                        quiz_result.answer_types.push(answer_type.wrong);
                        quiz_result.answers.push(answers[i]);
                        quiz_result.answer_types.push(answer_type.correct);
                    } else {
                        quiz_result.answers.push(txt);
                        quiz_result.answer_types.push(answer_type.both);
                    }
                }
            } else {
                for (var i = sort_groups[0].targets.length - 1; i >= 0; i--) {
                    var t_id = sort_groups[0].targets[i].elem_id;
                    t_id = t_id.reverse().substr(4, t_id.length).reverse();
                    var o_id = drag_objects[sort_groups[0].targets[i].objects_on[0]].elem_id;
                    if (o_id != t_id) {
                        update = 0;
                        quiz_result.answers.push(document.getElementById(t_id).innerHTML);
                        quiz_result.answer_types.push(answer_type.correct);
                        quiz_result.answers.push(document.getElementById(o_id).innerHTML);
                        quiz_result.answer_types.push(answer_type.wrong);
                    } else {
                        quiz_result.answers.push(document.getElementById(o_id).innerHTML);
                        quiz_result.answer_types.push(answer_type.both);
                    }
                }
            }
            break;
        case "scalequiz":
            var v = document.getElementById("actual").value;
            if (v == answers[0]) {
                update = 1;
                quiz_result.answers.push(v);
                quiz_result.answer_types.push(answer_type.both);
            } else {
                quiz_result.answers.push(answers[0]);
                quiz_result.answer_types.push(answer_type.correct);
                quiz_result.answers.push(v);
                quiz_result.answer_types.push(answer_type.wrong);
            }
            break;
        case "gridquiz":
            quiz_result.questions = [];
            var an = flags.grid_list.split(",");
            var cols = an.length;
            update = 1;
            for (var i = 0; i < questions.length; i++) {
                var ok = 1;
                quiz_result.questions.push(questions[i]);
                var j = 0;
                for (; j < cols; j++) {
                    e = document.getElementById("grida_" + i + "_" + j);
                    if (e == null) return;

                    if (e.innerHTML != "&nbsp;" && answers[i] - 1 != j ||
                        answers[i] - 1 == j && e.innerHTML == "&nbsp;") {
                        ok = 0;
                        break;
                    }
                }

                if (ok) {
                    quiz_result.answers.push(an[answers[i] - 1]);
                    quiz_result.answer_types.push(answer_type.both);
                } else {
                    quiz_result.answers.push(an[answers[i] - 1]);
                    quiz_result.answer_types.push(answer_type.correct);
                    quiz_result.answers.push(an[j]);
                    quiz_result.answer_types.push(answer_type.wrong);
                    update = 0;
                }
            }
            break;
        case "puzzlequiz":
            update = 1;
            for (var i = drag_objects.length - 1; i >= drag_objects.length - 2; i--) {
                if (drag_objects[i].ontarget == -1 || drag_objects[i].elem.id.substr(2) != drag_targets[drag_objects[i].ontarget].elem.id.substr(2)) {
                    update = 0;
                    break;
                }
            }
            break;
        case "hotspotquiz":
            var pinpos = helpers_box.innerHTML.split("|");
            update = (pinpos[0] >= flags.selection_left && pinpos[0] <= flags.selection_right &&
                      pinpos[1] >= flags.selection_top && pinpos[1] <= flags.selection_bottom);
            break;
    }
    if (just_results) return;
    if (local_timeout != null) {
        stop_local_timeout();
    }
    if (global_timeout != null) {
        stop_global_timeout();
    }
    if (update) {
        update_score();
        show_score();
        quiz_result.points_received = quiz_result.points;
    } else {
        quiz_result.points_received = 0;
    }
    quiz_checked = 1;
    if (flags.feedback_enable == 1) {
        if (flags.feedback_popup == 1) {
            show_feedback_popup(update ? "feedback_pass" : "feedback_fail");
            conf_next_button(0, "disable");
        } else {
            var y = update ? "feedback_pass" : "feedback_fail";
            var x = document.getElementById(y);
            x.innerHTML += get_feedback_solutions(y);
            x.style.display = "block";
            conf_next_button(1, "next");
        }
        quiz_disable();
    } else {
        go_next();
    }
    //dnd_display_correction();
}
function quiz_disable() {
    hide_image_popup();
    var iph = document.getElementById("image_popup_handler");
    var iph2 = document.getElementById("image_popup_handler2");
    if (iph != null) iph.onclick = null;
    if (iph2 != null) iph2.onclick = null;
    switch(quiz_type) {
        case "mchoice":
        case "sqmaquiz":
            for (var i = 0; i < answers.length; i++) {
                 document.getElementById((quiz_type == "mchoice" ? "choice" : "answer") + i ).disabled = 1;
            }
            break;
        case "fibquiz":
            if (flags.quiz_type != "qt_drag_drop") {
                for (var i = 0; i < answers.length; i++) {
                    document.getElementById("fib" + i).disabled = 1;
                }
            } else {
                for (var i = 0; i < drag_objects.length; i++) {
                    drag_objects[i].addEffect(dnd_effects.disabled);
                }
            }
            break;
        case "matchquiz":
            for (var i = 0; i < answers.length; i++) {
                if (flags.quiz_type == "qt_drop_down") {
                    document.quiz["con_" + i].disabled = 1;
                } else {
                    drag_objects[i].addEffect(dnd_effects.disabled);
                }
            }
            break;
        case "mixquiz":
            if (flags.quiz_type == "qt_drop_down") {
                for (var i = 0; i < answers.length; i++) {
                    document.quiz["mix_" + i].disabled = 1;
                }
            } else {
                sort_groups[0].addEffect(sort_effects.disabled);
            }
            break;
        case "scalequiz":
            drag_objects[0].addEffect(dnd_effects.disabled);
            break;
        case "gridquiz":
            var ans = flags.grid_list.split(",");
            for (var i = 0; i < questions.length; i++) {
                for (var j = 0; j < ans.length; j++) {
                    document.getElementById("grida_" + i + "_" + j).onclick = null;
                }
            }
            break;
        case "puzzlequiz":
            for (var i = drag_objects.length - 1; i >= 0; i--) {
                drag_objects[i].addEffect(dnd_effects.disabled);
            }
            break;
        case "hotspotquiz":
            document.getElementById("hotspotimage").onclick = null;
            break;
    }
}
function go_next() {
    if (quiz_checked) {
        if (typeof(ctx.pj_quiz_result) == 'undefined' || ctx.pj_quiz_result == null) {
            ctx.pj_quiz_result = [];
        }
        ctx.pj_quiz_result.push(quiz_result);
    }
    try {ctx.cfg_action(1001) } catch (e) {}
}
function input_only_int(e) {
    if (!e) e = window.event;

    var iCC = (e.which) ? e.which : e.keyCode;
    var iChar = String.fromCharCode(iCC);
    var aString = '0123456789';

    return aString.indexOf(iChar) != -1;
}
function input_only_float(e) {
    if (!e) e = window.event;

    var iCC = (e.which) ? e.which : e.keyCode;
    var iChar = String.fromCharCode(iCC);
    var aString = '0123456789';

    return (iChar != "." && iChar != ",") ?
                aString.indexOf(iChar) != -1 :
                !(this.value == "" || this.value.search(/,/) != -1 || this.value.search(/\./) != -1);
}
function ff_pre_input(obj, e, mode) {
    if (!e) e = window.event;

    var iCC = (e.which) ? e.which : e.keyCode;
    if (iCC < 48) {
        obj.onkeypress = function() { return 1; }
    } else {
        obj.onkeypress = (mode == 0) ? input_only_int : input_only_float;
    }
}
function update_ok() {
    var ok = 0;
    switch(quiz_type) {
        case "mchoice":
            if ("choice" in document.forms.quiz) {
                for (var i = document.quiz.choice.length - 1; i >= 0; i--) {
                    if (document.quiz.choice[i].checked) {
                        ok = 1;
                        break;
                    }
                }
            }
            break;
        case "sqmaquiz":
            for (var i = answers.length - 1, ok = 1; i >= 0; i--) {
                var e = document.getElementById("answer" + i);
                if (e != null) {
                    if (e.value == "") {
                        ok = 0;
                        break;
                    }
                }
            }
            if ((flags.answer_type == "at_comma_list" || flags.answer_type == "at_regexp") && ok) {
                for (var i = answers.length - 2; i >= 0; i--) {
                    for (var j = answers.length - 1; j >= i + 1; j--) {
                        if (document.getElementById("answer" + i).value.toLowerCase() ==
                            document.getElementById("answer" + j).value.toLowerCase()) {
                            ok = 0;
                            break;
                        }
                    }
                    if (!ok) break;
                }
            }
            break;
        case "fibquiz":
        case "matchquiz":
            switch(flags.quiz_type) {
                case "qt_text":
                case "qt_cs_text":
                case "qt_drop_down":
                    for (var i = answers.length - 1, ok = 1; i >= 0; i--) {
                        if (document.getElementById("fib" + i).value == "") {
                            ok = 0;
                            break;
                        }
                    }
                    break;
                case "qt_drag_drop":
                    for (var i = drag_targets.length - 1, ok = 1; i >= 0; i--) {
                        if (drag_targets[i].objects_on.length == 0) {
                            ok = 0;
                            break;
                        }
                    }
                    break;
            }
            break;
        case "mixquiz":
        case "scalequiz":
        case "puzzlequiz":
        case "hotspotquiz":
            ok = 1;
            break;
        case "gridquiz":
            var an = flags.grid_list.split(",");
            var cols = an.length;
            for (var i = 0; i < questions.length; i++) {
                ok = 0;
                for (var j = 0; j < cols; j++) {
                    var e = document.getElementById("grida_" + i + "_" + j);
                    if (e == null) return;

                    if (e.innerHTML != "&nbsp;") {
                        ok = 1;
                        break;
                    }
                }
                if (!ok) break;
            }
            break;
    }

    conf_next_button(ok, "check");
}

// --------------- end quiz check and update section ------------------------------------------------------

function on_load() {
    update_ok();

    window.onresize = dnd_display_correction;
}

// --------------- end onload section ------------------------------------------------------

/*
   Copyright Notice:

   Copyright 2007, datango AG
   ALL RIGHTS RESERVED

   UNPUBLISHED -- Use of a copyright notice is precautionary only and
   does not imply publication or disclosure.

   THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
   INFORMATION OF DATANGO AG. ANY DUPLICATION, MODIFICATION,
   DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS
   STRICTLY PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF
   DATANGO AG.

*/
