//
// Language Mentor
// Developing Language Proficiency--Listening
// CONTENT:  JavaScript checking of a multiple-choice question
//           Student has two tries to answer correctly, only the 
//           first response counts towards cumulative score.
//           Also contains bookmarking and audio control functions.
//

// modified March 2002 to reflect dynamic display of the score in the score.html 
// frame with scores to be stored in the title frame.
// functions displayNumbers and updateDisplay were no longer needed and removed.
// similar originals of those functions are found in the dlpr/cat/cat-orig.js
// -rrs

var tries = 0;
var answeredCorrect = 0;
var totalScore = 0;
var savedScore = 0;
var savedTopic = 1;
var curTopic = 1;
var savedDes = "modb";
var daysExpire = 30;
var expires = new Date();
expires.setTime(expires.getTime() + (30 * 24 * 60 * 60 * 1000));
var expiryDate = expires.toGMTString();


function makeArray(n)
{
    this.length = n;
    for (var i=1; i<=n; i++)
    {
        this[i] = null;
    }
    return this;
}

function haveSelected(form)
{
    var aT = top.TITLE.document.retainValues.audioToggle.value;
    if(aT == 0)
    {
        form.feedback.value = "You have not listened to the Audio"; 
        form.choice.value = 0;
    }
    else if(form.choice.value == 0)
    {
        form.feedback.value = "You have not answered the question"; 
    } 
    else
    {
        gotoNext();
    } 
}
function printscore()
{
		document.write("Score = ");
		document.write(parent.parent.parent.TITLE.document.retainValues.correctNum.value);
}

function addOne()
{
    //
    // Regardless of whether we answered correctly or not, we need to make sure
    // that since they answered the question, that the current topic number is incremented
    // to start them on the next question if they return using a bookmark.
    //

    curTopic = Math.abs(parent.parent.parent.TITLE.document.retainValues.topicNum.value) + 1;
    parent.parent.parent.TITLE.document.retainValues.topicNum.value = curTopic;

    //
    // Now we can check for incrementing the score as well
    //

    if(answeredCorrect)
    {
        totalScore = Math.abs(parent.parent.parent.TITLE.document.retainValues.correctNum.value) + 1;
        parent.parent.parent.TITLE.document.retainValues.correctNum.value = totalScore;
        //displayNumbers();
        parent.F4.SCORE.document.location.reload();
    }

} // end addOne

function saveValues(topic)
{
    //
    // Topic is the beginning topic number we receive from index.htm
    // Score should be zero as we are making a fresh start here by not using the bookmark
    //

    top.TITLE.document.retainValues.correctNum.value = 0;
    top.TITLE.document.retainValues.topicNum.value = topic;
}

function setup()
{
    tries = 0;
    answeredCorrect = 0;
    document.ques.choice.value = 0;
}

function check(form)
{
    var cans = form.ans.value;
    var ch = form.choice.value;
    var letters = new makeArray(4);
    letters[1] = "a";
    letters[2] = "b";
    letters[3] = "c";
    letters[4] = "d";

    var aT = top.TITLE.document.retainValues.audioToggle.value;

  if(aT == 0)
  {
      form.feedback.value = "You have not listened to the Audio"; 
      form.choice.value = 0;
  }
  else
  {

    tries = tries + 1;

    // users have two tries to get the correct answer HOWEVER
    // notice below that only the FIRST try scores a point 
    if( tries <= 2 )
    {
        if(ch == cans)
        {
            form.feedback.value = "CORRECT, click on CONTINUE below";
            // only set answeredCorrect on the first try
            // this is the users only chance to score a point
            if(tries == 1)
                answeredCorrect = 1;
        }
        else
        {
            if(tries > 1)
            {
                form.feedback.value = letters[cans] + ". is correct - Click on CONTINUE below";
            }
            else
            {
                form.feedback.value = "Try Again";
            }
        }
    }
    else
    {
        form.feedback.value = letters[cans] + ". is correct - Click on CONTINUE below";
    }


  } // end else
} // end check(form)


//
//
// The following section of code saves the score and load it again the next time the user
// enters the topic--if the user so chooses. 
//
//

function checkPlace(topicDes)
{
    var thisCookie = ((document.cookie != "") && (document.cookie != null));

    savedScore = (thisCookie) ? getCookie("dlplMODBscore") : 0;
    savedTopic= (thisCookie) ? getCookie("dlplMODBtopicNum") : 1;
    savedDes = topicDes;

    if(thisCookie)
    {
	// A cookie was set, so show the button to return to that place marker
	// A place marker means a savedTopic greater than 1 was detected
	// Any other storeTopic means they may as well start at the beginning

	document.write('<TD ALIGN=CENTER VALIGN=MIDDLE>\n');
	document.write('<A HREF="#" ONCLICK="loadScore();"><IMG ALT="Bookmark found" ');
	document.write('BORDER=0 SRC="../img/restore.gif"></A>\n');
	document.write('</TD>\n');
	document.write('<TD ALIGN=LEFT VALIGN=MIDDLE>\n');
	document.write('<FONT COLOR="#663399"><B>Text #' + savedTopic + '</B></FONT>\n');
	document.write('<BR>\n');
	document.write('<FONT COLOR="#663399"><B>Score = ' + savedScore + '</B></FONT><BR>\n');
	document.write('<FONT COLOR="#990000"><B>Use your bookmark or select a text below</B></FONT>\n');
	document.write('</TD>\n');
    }
    else
    {
	// A cookie was not set, so show all buttons to pick a topic number

	document.write('<TD ALIGN=CENTER VALIGN=MIDDLE>\n');
	document.write('<IMG ALT="No bookmark found" SRC="../img/noscore.gif">\n');
	document.write('</TD>\n');
	document.write('<TD ALIGN=LEFT VALIGN=MIDDLE>\n');
	document.write('<FONT COLOR="#990000"><B>No bookmark found, select a text below</B></FONT>\n');
	document.write('</TD>\n');
    }

} // end checkPlace


function getCookie(name)
{
    var cookieFound = false;
    var start = 0;
    var end = 0;
    var cookieString = document.cookie;

    var i = 0;

    // Scan for the field name
    while (i <= cookieString.length)
    {
	start = i;
	end = start + name.length;
	if (cookieString.substring(start,end) == name)
	{
	    cookieFound = true;
	    break;
	}

	i++;
    }

    // Did we find the field we were looking for?
    if (cookieFound)
    {
	start = end + 1;
	end = document.cookie.indexOf(";", start);
	if (end < start)
	    end = document.cookie.length;
	return document.cookie.substring(start, end);
    }

    return "";

} // end getCookie(name)


function newCookie(name,value)
{
    document.cookie = name + "=" + escape(value) + "; expires=" + expiryDate;

} // end newCookie


function saveScore()
{
    //
    // The values are determined in relation to the file accessing this function.
    // Since the dlpr/score.htm file accesses this function directly, then it must
    // reference the values within its own HTML code, i.e. document.forms......
    //

    totalScore = Math.abs(parent.parent.parent.TITLE.document.retainValues.correctNum.value);
    newCookie("dlplMODBscore", totalScore);
    curTopic = Math.abs(parent.parent.parent.TITLE.document.retainValues.topicNum.value);

    if(curTopic > 25)
        curTopic = 25;

    newCookie("dlplMODBtopicNum", curTopic);
    alert("Your bookmark has been set");

} // end saveScore


function loadScore()
{
    totalScore = savedScore;
    top.TITLE.document.retainValues.correctNum.value = savedScore;
    top.TITLE.document.retainValues.topicNum.value = savedTopic;
    document.location = savedDes + savedTopic + "/index.htm";

} // end loadScore

//
// Audio Control Functions
//

function playAudioClip(sAudioURL)
{
    document.playaud.playFile(sAudioURL);
}

function showToggle()
{
    alert(top.TITLE.document.retainValues.audioToggle.value);
}

function setToggle(num)
{
    top.TITLE.document.retainValues.audioToggle.value = num;
}

function stopSound()
{
    document.playaud.stopFile();
}

function remoteCtrlStop()
{
    parent.F4.AUDIO.document.playaud.stopFile();
}

function remoteSoundCheck()
{
    var aT = top.TITLE.document.retainValues.audioToggle.value;
    if(aT != 0)
    {
        remoteCtrlStop();
        top.TITLE.document.retainValues.audioToggle.value = 0;
    }
}

function remoteStop()
{
    parent.F1.document.playaud.stopFile();
}

function checkSound()
{
    var aT = top.TITLE.document.retainValues.audioToggle.value;
    if(aT != 0)
    {
        remoteStop();
        top.TITLE.document.retainValues.audioToggle.value = 0;
    }
}

function toggleSound(sAudioURL)
{
    var aT = top.TITLE.document.retainValues.audioToggle.value;
    if((aT == 1) || (aT == 0))
    {
        document.btns.AUDIO.value="Stop Audio";
        top.TITLE.document.retainValues.audioToggle.value = 2;
        playAudioClip(sAudioURL);
    }
    else
    {
        document.btns.AUDIO.value="Play Audio";
        top.TITLE.document.retainValues.audioToggle.value = 1;
        stopSound();
    }

} // end toggleSound


