﻿var oq;

$(document).ready(function() { LoadSession() });
function LoadSession() {
    oq = { "qTimes": 1, "keys": pagecode.toLowerCase().split(' ') };
    for (i = 0; i < oq.keys.length; i++) {
        replstr = new RegExp(oq.keys[i] + '\\^', "gi");
       $('#quizdata').html($('#quizdata').html().replace(replstr, "#" + oq.keys[i].toLowerCase() + "#"));
   }
    oq.data = $('#quizdata').html().split('|');
    for (i = 0; i < oq.data.length; i++) { oq.data[i] = { q: 0, e: 0, i: i, v: oq.data[i].split('#')[1], p1:oq.data[i].split('#')[0],p2:oq.data[i].split('#')[2] } }
    oq.QuizPoints = function() { return oq.data.length * oq.qTimes; }
    oq.UserPoints = function() { val = 0; jQuery.each(oq.data, function() { val = val + this.q; }); return val; }
    oq.LostPoints = function() { val = 0; jQuery.each(oq.data, function() { val = val + this.e; }); return val; }
    oq.showResult = function() {
        if (this.UserPoints() == 0) return ' ';
        var EarnedPoints = this.UserPoints() - this.LostPoints(); var PercentPoints = Math.round(EarnedPoints / this.QuizPoints() * 100);
        var strOut = "<div "; var PointPercent = ' - ' + EarnedPoints + ' out of ' + this.QuizPoints() + ' - ' + PercentPoints + '%'
        if (PercentPoints == 100) strOut = strOut + ' class="gr5">Excellent' + PointPercent + '</div>';
        else if (PercentPoints > 89) strOut = strOut + ' class="gr4">Very Good' + PointPercent + '</div>';
        else if (PercentPoints > 79) strOut = strOut + ' class="gr3">Good' + PointPercent + '</div>';
        else if (PercentPoints > 59) strOut = strOut + ' class="gr2">Poor' + PointPercent + '</div>';
        else if (PercentPoints > 19) strOut = strOut + ' class="gr1">Fail' + PointPercent + '</div>';
        else strOut = strOut + ' class="gr1">Fail - too many errors. Please retake this quiz again.</div>';
        return strOut;
    };

    $('#quizmodeselect').empty();
    //jQuery.each(oq.keys, function() { $('<div class="quizdivword">' + this + '</div>').data("w", this.toLowerCase()).appendTo('#quizmodeselect'); });
    jQuery.each(oq.keys, function() { $('<input type="button" class="btn" value="' + this + '" />').appendTo('#quizmodeselect'); });

    //$(".quizdivword").bind("click", function(e) { checkForAnswer($(this).data("w")) });
    $("#quizmodeselect .btn").bind("click", function(e) { checkForAnswer($(this).val()) });
    $("#quizmodeselect .quizdivword").bind("mouseenter mouseleave", function(e) {
        $(this).toggleClass("over");
    });
    $('#btnNextQuestion').bind("click", function(e) {  checkForAnswer("") });
    $('#btnStartQuiz').bind("click", function(e) { QuizRestart() });
    $('#selAnswerMode').bind("change", function(e) { ChangeMode() });
    
    
    $('#QuizPoints').html("" + oq.QuizPoints())
}

function CheckForEnter(e) {
    var code;
    if (!e) var e = window.event
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if ((code == 13) || (code == 9)) 
        checkForAnswer();
}

function ChangeMode() {
    if ($("#selAnswerMode option:selected").val() == 1) {
        $('#quizmodeselect').hide();
        $('#quizmodetype').show();
        $('#txtAnswer').removeClass('qinputd').addClass('qinput');
        $('#txtAnswer').removeAttr('readonly');
        setTimeout('iefocus()', 500);
    } else {
        $('#quizmodeselect').show();
        $('#quizmodetype').hide();
        $('#txtAnswer').removeClass('qinput').addClass('qinputd');
        $('#txtAnswer').attr('readonly', 'readonly');
    }
}

function QuizRestart() {
    jQuery.each(oq.data, function() { this.q = 0; this.e = 0; });
    $('#QuizPoints').html(oq.QuizPoints());
    $('#UserPoints').html('0');
    $('#LostPoints').html('0');
    $('#quizstart').hide();
    $('#quizresult').hide();
    $('#btnStartQuiz').val(" Retake Quiz ");
    $('#quizmode').show();
    $('#quizquestion').show();
    QuizQuestion();
}

function QuizQuestion() {
    var arr = jQuery.grep(oq.data, function(i) { return i.q < oq.qTimes; });
    if (arr.length == 0) {
        $('#quizresult').html(oq.showResult());
        $('#quizresult').show();
        $('#quizstart').show();
        $('#quizmode').hide();
        $('#quizquestion').hide();
        return;
    } else {
        $('#quizquestion').css({ 'background-color': '#FFFFFF' });
        $('#quizquestion').fadeIn("slow");
        var q = arr[Math.floor(Math.random() * arr.length)]
        $('#quizquestion').data("i", q.i);
        $('#txtAnswer').val("");
        $('#ansp1').html(q.p1);
        $('#ansp2').html(q.p2);
        $('#txtAnswerh').val(q.v);
        if ($('#txtAnswer').hasClass('qinput')) {
            setTimeout('iefocus()', 500);
        }
    }
}
function iefocus(){
    $('#txtAnswer').focus()
}

function checkForAnswer(val) {
    if (val != '') { $('#txtAnswer').val(val); }
    ind = $('#quizquestion').data("i");
    if ($('#txtAnswer').val().toLowerCase() == $('#txtAnswerh').val().toLowerCase()) {
        oq.data[ind].q = oq.data[ind].q + 1;
        $('#quizquestion').css({ 'background-color': '#FDFFCA' });
    } else {
        oq.data[ind].q = oq.data[ind].q - 1;
        oq.data[ind].e = oq.data[ind].e + 1;
        $('#quizquestion').css({ 'background-color': 'yellow' })
    }
    $('#UserPoints').html("" + oq.UserPoints());
    $('#LostPoints').html("" + oq.LostPoints());  
    $("#quizquestion").fadeOut("slow");
    setTimeout('QuizQuestion()', 500);
}

