$(function() {
    var numCorrect = 0;

    // true = 1, false = 0, sometimes = -1
    var arrkey = [
        1, // question 1
        1,
        0,
        0,
        -1,
        1,
        0,
        0,
        1,
        1, // 10
        0,
        1,
        0,
        1,
        0,
        0,
        1,
        1,
        0,
        0, // 20
        0,
        1,
        0,
        0,
        0,
        0,
        0,
        1,
        0,
        -1, // 30
        0,
        0,
        0,
        0,
        0,
        -1,
        0,
        0,
        0,
        0, // 40
        0,
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0            
    ];

    // hide all but the first question
    $('ul#exam > li').hide();
    $('ul#exam > li:eq(0)').show();

    $('#prev').click(function() {

        $q.hide('show');
        $q = $q.prev();
        $q.show('slow');            
        if (qidx == 0) {
            $('#prev').hide();
            $('#finish').hide();
        }
    });

    $('#next').click(function() {
        var $q = $('ul#exam > li:visible');
        var qidx = $q.index();  

        var ans = $("input[name='answer-rb']:checked").val();
        if (arrkey[qidx] == ans) {
            numCorrect++;
        }            
        // alert('you answered ' + ans + '\r\ncorrect answer = ' + arrkey[qidx] + '\r\ntotal correct so far = ' + numCorrect)

        $q.hide('show');
        $q = $q.next();
        qidx++;

        var friendlyidx = qidx + 1;  

        if (friendlyidx == 50) {
            $('#next').hide();
            $('#finish').show();
        }            

        $q.prepend(friendlyidx + '. ').show('slow');
        $("input[name='answer-rb']").attr('checked', false);
    });

    $('#finish').click(function() {
       var score = Math.round(numCorrect / arrkey.length * 100);
       
       if (score < 25)
           window.location = '/bw-level-1/?score=' + score;
       else if (25 < numCorrect < 50)
           window.location = '/bw-level-2/?score=' + score;
       else if (49 < numCorrect < 75)
           window.location = '/bw-level-3/?score=' + score;
       else if (numCorrect > 74)
           window.location = '/bw-level-4/?score=' + score;               
    });
});

function request( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}


