﻿// error message boxes and timeout popups

var errtmh = new Array();
var errwin = new Array();

function showPopupT(i, msg)
{
    if (errwin[i] == null || errwin[i] == "undefined")
    {
        errwin[i] = dhtmlwindow.open("rcalert", "inline", "<div style=\"float: left;\"><img src=\"Images/Warning.png\" width=\"32\" height=\"32\"" +
                            " align=\"middle\" /></div><div style=\"float: left; margin-left: 10px; width: 290px;\">" + msg + "</div>" +
                            "<div style=\"float: left; text-align: center; width: 100%; position: relative; top: 7px\">" +
                            "<input type=\"button\" value=\"Ok\" onclick=\"hidePopup(" + i + ")\"></div>", 
                            "RC Task Warning", "width=350px,height=70px,resize=0,scrolling=0,center=1");
        errwin[i].onclose = function(){ errwin[i] = null; if (errtmh[i] != 0 && errtmh[i] != "undefined") { clearTimeout(errtmh[i]); errtmh[i] = 0; } return true; }
    }
    else
    {
        errwin[i].show();
    }

    if (errtmh[i] == null || errtmh[i] == 0 || errtmh[i].toString() == "undefined")
        errtmh[i] = setTimeout("hidePopup('" + i + "')", 4000);
}

function showPopup(i, msg)
{
    errwin[i] = dhtmlwindow.open("rcalert", "inline", "<div style=\"float: left;\"><img src=\"Images/Error.png\" width=\"32\" height=\"32\"" +
                            " align=\"middle\" /></div><div style=\"float: left; margin-left: 10px; width: 290px;\">" + msg + "</div>" +
                            "<div style=\"float: left; text-align: center; width: 100%; position: relative; top: 7px\">" +
                            "<input type=\"button\" value=\"Ok\" onclick=\"hidePopup(" + i + ")\"></div>",
                            "RC Task Error", "width=350px,height=70px,resize=0,scrolling=0,center=1");
    errwin[i].onclose = function(){ errwin[i] = null; return true; };
}

function hidePopupT(i)
{
    errtmh[i] = 0;
    if (errwin[i] != null && errwin[i] != "undefined")
        errwin[i].hide();
}

function hidePopup(i)
{
    if (errwin[i] != null && errwin[i] != "undefined")
        errwin[i].close();
}

// done

// feedback functions 

function feedbackProc()
{
    var noCorr = parseInt(document.getElementById('noCorrect').innerHTML, 10);
    var noAnsw = parseInt(document.getElementById('noAnswered').innerHTML, 10);
    var lastPass = (parseInt(document.getElementById('hfLastPass').value, 10) == 1) ? true : false;
    var passNum = parseInt(document.getElementById('hfPassCount').value, 10);
    var percR = noCorr / noAnsw;
    var ind = 0;
    var msg = "";
    
    if (lastPass)
        passNum = 3;
    else
        passNum--;
    
    if (percR <= 0.33)
    {
        ind = 20 + (3 * passNum);
    }
    else if (percR > 0.33 && percR <= 0.77)
    {
        ind = 21 + (3 * passNum);    
    }
    else if (percR > 0.77)
    {
        ind = 22 + (3 * passNum);
    }
        
    switch(ind)
    {
        case 23:
            msg = "Those questions were pretty hard for you. Let's see how you do on the next one.";
            break;
        case 24:
            msg = "Nice going. Let's see if you can do a little better on this next one.";
            break;
        case 25:
            msg = "Very nice work. Let's see if you can do as well on the next one.";
            break;
        case 26:
            msg = "That was a hard passage. Let's see how you do on the next one which should be a little easier."
            break;
        case 27:
            msg = "Way to go. See if you can do even better on the next passage.";
            break;
        case 28:
            msg = "Great work. You are good at this. Let's see if you can do as well on the next one.";
            break;
        case 29:
            msg = "Thank you for your effort on this task. That was the last one for now.";
            break;
        case 30:
            msg = "You did well, and that was the last one for now.";
            break;
        case 31:
            msg = "You did very well, and that's the last one for now.";
            break;
        default:
            msg = "";
            break;
    }
    
    if (ind != 0)
    {
        playSound('T' + (ind));
        document.getElementById('feedbackMsg').innerHTML = msg;
        document.getElementById('feedbackMsg').style.display = 'block';
    }
}

function feedbackFollowup()
{
    var lastPass = (document.getElementById('hfLastPass').value == '1') ? true : false;
    if (lastPass)
        setTimeout("playSound('T32')", 500);
    else
        setTimeout("playSound('T20')", 500);
}

// done with sound stuff

//document UI handling funcs

var btnCt = null; // answer questions button
var btnSa = null; // submit or done button
var hffEl = null;
var divAb = null; // questions
var elem_p = null; // passage
var elem_q = null; // questions
var elem_f = null; // flash div
var flash_mv = null;
var rz_status = true;
var played_t9t11 = false;
var myHeight = 400;
var mozbuf = 1;
var n_tried = 0;
var n_correct = 0;
var doneEnabled = false;
var mytheForm;

function docEnableQs()
{
    if (!rz_status)
    {
        rz_status = true;
        divAb.style.display = 'none';       
        btnSa.style.display = 'none';
        btnCt.value = 'Answer Questions';
        docSize();
    }
    else
    {
        rz_status = false;
        divAb.style.display = 'block';
        btnSa.style.display = 'inline';
        btnCt.value = 'View Passage Only';
        if (!played_t9t11)
        {
            getFlashMovie('RCTaskIntroLd').playT9T11();
            played_t9t11 = true;
        }
        docSize();
    }
}

function initVars()
{
    btnCt = document.getElementById('btnShowQs');
    divAb = document.getElementById('quesblock');
    elem_p = document.getElementById('passblock');
    elem_q = document.getElementById('quesblock');
    elem_f = document.getElementById('txtIntro');
    elem_s = document.getElementById('rcscoreBoardContainer');
    btnSa = document.getElementById('btSubmitAns');
    flash_mv = getFlashMovie('RCTaskIntroLd');
}

function docLoad()
{
    mytheForm = document.forms['form1'];
    if (!mytheForm)
    {
        mytheForm = document.form1;
    }
    initVars();
}

function docSize()
{
    if (!rz_status)
    {
        elem_p.style.height = "227px";
        elem_q.style.height = "150px";
    }
    else
    {
        elem_p.style.height = "404px";
    }
}

function itemClick()
{
    var i_chk = 0;
    
    for (i = 0; i < mytheForm.elements.length; i++)
    {
        if (mytheForm.elements[i].type == 'radio')
        {
            if (mytheForm.elements[i].checked)
            {
                i_chk++;
            }
        }
    }
    
    if (i_chk >= 3)
    {
        doneEnabled = true;
        btnSa.className = 'btControlClass';
    }
    else
    {
        btnSa.className = 'btControlClassD';
        doneEnabled = false;    
    }
}

function doneClick()
{   
    if (!doneEnabled)
    {
        showPopupT(3, 'Please do all the items.');
    }
    else
    {
        for (i = 0; i < mytheForm.elements.length; i++)
        {
            if (mytheForm.elements[i].type == 'radio')
            {
                if (mytheForm.elements[i].checked)
                {
                    if (mytheForm.elements[i].value == '1')
                        n_correct++;
                    n_tried++;
                }
            }
        }
        
        document.getElementById('noCorrect').innerHTML = n_correct;
        document.getElementById('noAnswered').innerHTML = n_tried;
        
        elem_p.style.display = 'none';
        elem_q.style.display = 'none';
        btnCt.style.display = 'none';
        btnSa.style.display = 'none';
        elem_s.style.display = 'block';
        
        if (n_correct <= 1)
        {
            getFlashMovie('RCTaskIntroLd').playT15();
            document.getElementById('feedbackMsg').innerHTML = "That was hard for you. Let’s see how you do on the next one.";
        }
        else if (n_correct == 2)
        {
            getFlashMovie('RCTaskIntroLd').playT16();
            document.getElementById('feedbackMsg').innerHTML = "You did pretty well. Let’s see if you can do a little better on this next one.";
        }
        else if (n_correct == 3)
        {
            getFlashMovie('RCTaskIntroLd').playT17();
            document.getElementById('feedbackMsg').innerHTML = "You did very well. Keep up the good work.";
        }
            
        setTimeout(moveToTheEnd, 10000);
    }
}

function moveToTheEnd()
{
    elem_s.style.display = 'none';
    showFlashMov();
    flash_mv.moveToEnd();
}

function setJSTimeouts1()
{
}

function setJSTimeouts2()
{
}

function handleNBtnClick1()
{
    hideFlashMov();
    btnCt.style.display = 'inline';
    flash_mv.playT6T8();
}

function handleNBtnClick2()
{
    //elem_p.style.display = 'none';
    //elem_q.style.display = 'none';
    //btnCt.style.display = 'none';
    //btnSa.style.display = 'none';
    alert('redirect');
}

function hideFlashMov()
{
    elem_f.style.height = "1px";
    elem_f.style.left = '-2048px';
}

function showFlashMov()
{
    elem_f.style.left = '0px';
    elem_f.style.height = "404px";
}

function getFlashMovie(movieName)
{
    var isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
    return (isIE) ? document.getElementById(movieName) : document.embeds[movieName];
}

// done with doc stuff
