﻿//W A R N I N G: You do NOT have permission to copy, 
//modify, reproduce, retransmit or distribute any of 
//this HTML, text or script.  
//Irregular Verbs Quiz javascript copyright 2009 Nikolay Babin
//Email: esldesk.com@gmail.com
//http://www.esldesk.com   	
var oq;
$(document).ready(function() { LoadSession() });
function LoadSession() {
    oq = { "qTimes": 1, "qSize": 15,  "appstate" : {"s_preview" : 0, "s_question" : 1, "s_feedback" : 3, "s_custom" : 4}};
    oq.qSize = pagesize;
    $('#selectpagesize').val(oq.qSize);
    oq.arwords = strwords.split(';');
    oq.arwordscustom = strwords.split(';');
    for (i = 0; i < oq.arwordscustom.length; i++) {oq.arwordscustom[i] = false; oq.arwords[i] = oq.arwords[i].replace(/\|/gi, ' - ');}
 
    oq.CustomSelected = function() { val = false; jQuery.each(oq.arwordscustom, function(i) { if (this == true) val = true; }); return val; }
    oq.CustomPop = false;
    
    oq.data = new String("1;2;3").split(';')  //strwords.split(';');
    PopData();
    PopSelection();
       
    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.QuizPoints = function() { return oq.data.length * oq.qTimes; }
    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;
    };

    $('#selectquiz').bind("change", function(e) { if (this.value == -1) GetState(oq.appstate.s_custom); else GetState(oq.appstate.s_preview); });

    $('#selectpagesize').bind("change", function(e) { ChangePageSize() });
    //$('#quizmessage').hide();
    $('#btnAction').fadeIn("slow");
    GetState(oq.appstate.s_preview)
}

function GetState(val) {
    if (val == oq.appstate.s_preview) {
        startpoint = parseInt($("#selectquiz option:selected").val());

        var quizCaption = 'List: <b>' + $("#selectquiz option:selected").text() + '</b>';
        if (startpoint == -1) {
            $('#selectpagesize').attr('disabled', 'disabled');
            quizCaption = quizCaption + ' <span class="linkaction" onclick="GetState(oq.appstate.s_custom)">Change selection</span>'
        }  else
            $('#selectpagesize').removeAttr('disabled');
            
        if ((startpoint==-1) && (oq.CustomSelected() == false)) {
            GetState(oq.appstate.s_custom);
            return
        }
        PopData();
        $('#quizpreviewdetailscaption').empty();
        $('#quizpreviewdetailscaption').html(quizCaption);
        $('#quizpreviewdetails').empty();
        jQuery.each(oq.data, function() { $('<div class="itemview" style="width:' + pageviewwidth + 'px">' + this.d + '</div>').appendTo('#quizpreviewdetails'); });
        SetState(val);
    } else if (val == oq.appstate.s_question) {
        QuizQuestion();
        SetState(val);
    } else if (val == oq.appstate.s_feedback) {
        $('#quizfeedbackdetails').empty();
        $('#quizfeedback').html(oq.showResult());
        $('#quizfeedback').show();
        if (oq.LostPoints() > 0) {
            jQuery.each(oq.data, function() { if (this.e > 0) this.es = this.e + " - "; else this.es = ""; });
            jQuery.each(oq.data, function() { $('<div class="itemview" style="width:' + pageviewwidth + 'px"><div class="heditemview">' + this.es + '</div>' + this.d + '</div>').appendTo('#quizfeedbackdetails'); });
        }
        SetState(val);
    } else if (val == oq.appstate.s_custom) {
        if (oq.CustomPop == false) {
            oq.CustomPop = true;
            jQuery.each(oq.arwords, function(index) { $('<div class="itemview" style="width:' + pageviewwidth + 'px"><input type="checkbox" class="chbc" id="custsel' + index + '" onclick="oq.arwordscustom[' + index + ']=this.checked;">' + this + '</div>').appendTo('#quizstatecustomwords'); });
        }
        SetState(val);
    }
}

function SetState(val) {
    $('#quizmessage').hide();
    if (val == oq.appstate.s_preview) {
        $('#btnAction').unbind('click').bind("click", function(e) { GetState(oq.appstate.s_question) });
        $('#btnAction').attr('value','Begin Quiz');
        setTimeout('iefocus("btnAction")', 100);
        
        $('#tdstatefeedback').hide();
        $('#tdstatequestion').hide();
        $('#tdstatecustom').hide();
        $('#tdstatepreview').fadeIn("slow");
        
        $('#pointsquiz').html("" + oq.QuizPoints())
        $('#pointsuser').html('0');
        $('#pointslost').html('0');
    } else if (val == oq.appstate.s_question) {
        $('#btnAction').unbind('click').bind("click", function(e) { checkForAnswer() });
        $('#btnAction').attr('value','Next Question');
        
        $('#tdstatefeedback').hide();
        $('#tdstatequestion').show();
        $('#tdstatecustom').hide();
        $('#tdstatepreview').hide();
    } else if (val == oq.appstate.s_feedback) {
        $('#btnAction').unbind('click').bind("click", function(e) { GetState(oq.appstate.s_preview) });
        $('#btnAction').attr('value','Retake Quiz');
        setTimeout('iefocus("btnAction")', 100);

        $('#tdstatefeedback').fadeIn("slow");
        $('#tdstatequestion').hide();
        $('#tdstatecustom').hide();
        $('#tdstatepreview').hide();
        $('#quizmessage').html('You may choose another List from a dropdown or retake this quiz again.');
        $('#quizmessage').fadeIn("slow");
    } else if (val == oq.appstate.s_custom) {
        $('#btnAction').unbind('click').bind("click", function(e) {
        if (oq.CustomSelected() == false) {
            $('#quizmessage').html('No selection has been made. Choose any of the words above or select other List from a dropdown.');
            $('#quizmessage').fadeIn("slow");
            return;
        }
        GetState(oq.appstate.s_preview)
        });
        $('#btnAction').attr('value', 'Save Selection');

        $('#tdstatefeedback').hide();
        $('#tdstatequestion').hide();
        $('#tdstatecustom').fadeIn("slow");
        $('#tdstatepreview').hide();

        $('#pointsquiz').html("" + oq.QuizPoints())
        $('#pointsuser').html('0');
        $('#pointslost').html('0');
    }
    
}


function PopSelection() {
    document.getElementById('selectquiz').length = 0;
    var pages = parseInt(oq.arwords.length / oq.qSize);
    if ((oq.arwords.length % oq.qSize) > 0) pages = pages + 1
    for (i = 0; i < pages; i++) {
        val = i * oq.qSize + 1
        if (oq.arwords.length > (i + 1) * oq.qSize)
            text = val + ' to ' + (i + 1) * oq.qSize;
        else
            text = val + ' to ' + oq.arwords.length;
        $('#selectquiz').append($('<option></option').val(val).html(text));
    }

    if (pagecustom > 0)
        $('#selectquiz').append($('<option></option').val('-1').html('Custom'));
}
function PopData() {
    startpoint = parseInt($("#selectquiz option:selected").val()) - 1;
    if (startpoint <0)
        oq.data = jQuery.grep(oq.arwords, function(n, i) {
            return (oq.arwordscustom[i] == true);
        });
    else {
        oq.data = jQuery.grep(oq.arwords, function(n,i) {
            return ( i >= startpoint && i < (startpoint + oq.qSize));
        });
    }
    for (i = 0; i < oq.data.length; i++) { oq.data[i] = { q: 0, e: 0, es: "", i: i, v: oq.data[i].split(' - '), d: oq.data[i]} }
}

function QuizQuestion() {
    $('#quizmessage').hide();
    var arr = jQuery.grep(oq.data, function(i) { return i.q < oq.qTimes; });
    if (arr.length == 0)
        GetState(oq.appstate.s_feedback)
    else {
        $('#tdstatequestion').css({ 'background-color': '#FFFFFF' });
        $('#tdstatequestion').show();
        var q = arr[Math.floor(Math.random() * arr.length)]
        $('#tdstatequestion').data("i", q.i);
        $('#questioncaptionident').html("" + q.v[0]);
        for (i = 0; i < q.v.length; i++) {
            $('#txtAnswer' + i).val("");
            $('#txtAnswerh' + i).hide();
            $('#txtAnswerh' + i).val(trim(q.v[i]));
            $('#qitem' + i).css({ 'background-color': '#FFFFFF' })
        }
        setTimeout('iefocus("txtAnswer0")', 500);
    }
}


function checkForAnswer() {
    ind = $('#tdstatequestion').data("i");
    if (ind == -1) {
        setTimeout('QuizQuestion()', 300);
        return;
    }
    
    var pass = true;
    for (i = 0; i < oq.data[ind].v.length; i++) {
        $('#txtAnswer' + i).val(trim($('#txtAnswer' + i).val()))
        if ($('#txtAnswer' + i).val() == '') {
            setTimeout('iefocus("txtAnswer" + i)', 100);
            return;
        }
    }
    for (i = 0; i < oq.data[ind].v.length; i++) {
        if (document.getElementById("txtAnswerh" + i).value.indexOf(' ') == -1) {
            document.getElementById("txtAnswer" + i).value = document.getElementById("txtAnswer" + i).value.split(' ').join('');
        }
        if ($('#txtAnswer' + i).val().toLowerCase() != $('#txtAnswerh' + i).val().toLowerCase()) {
            $('#qitem' + i).css({ 'background-color': 'yellow' })
            $('#txtAnswerh' + i).fadeIn("slow");
            pass = false;
        }
    }
    
    if (pass == true) {
        oq.data[ind].q = oq.data[ind].q + 1;
        $('#tdstatequestion').css({ 'background-color': '#FDFFCA' });
        setTimeout('QuizQuestion()', 300);
    } else {
        oq.data[ind].q = oq.data[ind].q - 1;
        oq.data[ind].e = oq.data[ind].e + 1;
        $('#tdstatequestion').data("i", -1);
        $('#quizmessage').html('Review the error. Click [Next Question] to continue.');
        $('#quizmessage').fadeIn("slow");
    }
    $('#pointsuser').html("" + oq.UserPoints());
    $('#pointslost').html("" + oq.LostPoints());

}

function ChangePageSize() {
    oq.qSize = parseInt($("#selectpagesize option:selected").val());
    PopSelection();
    GetState(oq.appstate.s_preview)
}
function CheckForEnter(e) {
    $('#questioncaptionident').html(' ............ ');
    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 trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function iefocus(val) {
    $('#' + val).focus()
}
