home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Multimedia Spanish 3
/
ProOne-MultimediaSpanishIII-WinMac.bin
/
mac
/
span3web
/
vfillfr.js
< prev
next >
Wrap
Text File
|
1998-04-19
|
16KB
|
731 lines
/**** The "indices" array for disk 3 ****/
var indices = new Array( 150, 166, 186, 206, 226, 244 );
var spanish; // spanish question
var english; // english translation of question
var choice; // the "choice" string (possible fill-ins)
var choiceWords; // the "choice" string split into words
var nChoices;
var order; // the randomized order of the questions
var instruction; // the instructions (could be different for every example)
var instrRef; // reference to instruction
var scoreData; // list of scores for each question (-1 = wrong)
var currProblem;
var nProblems; // number of problems available to answer
var nTries; // number of tries on a single problem
var answered; // has this problem been answered OK already?
var score; // your score so far
var possible; // total points possible so far
var pointArray; // array of points per problem
var playSound; // should we send out sound?
var useSavedData;
var nFeedback = 9; // number of feedback pictures
var nFields; // number of fields to fill in
var xlate; // translation table for accented characters
var lastNav; // last navigation area
function stringPart( string, start, length)
{
return string.substring( start, start + length );
}
function GetInstrRef( n )
{
var i;
for (i = instrRef.length-1; i >=0; i--)
{
if (n >= instrRef[i])
{
return i;
}
}
return 0;
}
function Accent( str )
{
var i, j;
var pair;
var delim;
delim = "'";
for (j=0; j<2; j++)
{
i = str.indexOf(delim);
while (i > 0)
{
pair = str.substring( i-1, i+1);
if (xlate[pair])
{
str = str.substring(0, i-1) + xlate[pair] + str.substring(i+1);
}
i = str.indexOf(delim, i+1);
}
delim = '~';
}
return str;
}
function Deblank( str )
{
var i, j;
i = 0;
while (str.charAt(i) == ' ')
{
i++;
}
str = str.substring(i);
if (str.length > 0)
{
i = str.length-1;
while (str.charAt(i) == ' ')
{
i--;
}
str = str.substring(0, i+1);
}
pos = 0;
i = str.indexOf(' ');
while (i >= 0 && pos < str.length)
{
j = i + 1;
while (str.charAt(j) == ' ')
{
j++;
}
if (j != i+1)
{
str = str.substring(0, i+1) + str.substring(j);
}
pos = i+1;
i = str.indexOf(' ', pos);
}
return str;
}
function SetWords( wArray, startPos )
{
var i;
for (i=2; i<SetWords.arguments.length; i++)
{
wArray[startPos + i - 2] = SetWords.arguments[i];
}
}
function SetNumeric( nArray, n )
{
var i;
for (i=0; i < n; i++)
{
nArray[i] = i;
}
}
function Shuffle( nArray, startAt, n )
{
var i, j, swap;
for (i = startAt; i < startAt + n; i++)
{
j = startAt + Math.round(Math.random() * (n - 1));
swap = nArray[i];
nArray[i] = nArray[j];
nArray[j] = swap;
}
}
/*******************************************************************************
Split words from string str into a word array (wordArr).
Words are delimited by the delim argument.
Returns the number of words that were split out.
Note: this function is here because not all browsers have Javascript's
"split" function enabled.
*******************************************************************************/
function SplitWords( str, delim, wordArr ) // for browsers not having a "split" function
{
var fromPos, toPos, n;
n = 0;
fromPos = 0;
toPos = str.indexOf( delim, 0 );
while (toPos >= 0)
{
wordArr[n++] = str.substring( fromPos, toPos );
fromPos = toPos + 1;
while (str.charAt( fromPos ) == ' ')
{
fromPos++;
}
toPos = str.indexOf( delim, fromPos );
}
/* take last word on line and store it */
wordArr[n++] = str.substring( fromPos, str.length );
return n;
}
function UpdateScore( doc )
{
doc.scoreForm.score.value = score + " of " + possible;
}
function WriteScoringArea( doc )
{
var name;
doc.writeln("<table><tr><td valign='top'>");
doc.writeln("<form name='scoreForm'>");
doc.writeln("Problem ", currProblem + (maxProblem-nProblems) + 1, " of ", maxProblem, ".<br>");
doc.writeln("Your score: <input type='text' size='10' name='score' value=''>");
doc.writeln("</form></td><td width='10'></td><td valign='top'>");
if (answered)
{
name = (scoreData[order[currProblem]] & 0x07 == 0)
? "already0.gif" : "already1.gif";
doc.writeln("<img name=\"feedback_img\" src=\"pics/", name, "\" ",
"width='150' height='75' border='0'>");
}
else
{
doc.writeln("<img name=\"feedback_img\" src=\"pics/mtpic.gif\" ",
"width='150' height='75' border='0'>");
}
doc.writeln("</td></tr></table>");
}
function WriteProblem( n )
{
var i;
var maxlen;
var pos;
var prob;
var doc;
var swap;
prob = order[currProblem]; // save chosen problem in temp. var for ease of reference
i = scoreData[prob];
nTries = (scoreData[prob] == 0) ? 0 : ((i >> 3) & 0x03);
if ((nTries == 1) && (((i >> 3) & 0x04) != 0))
{
nTries++;
}
answered = ((nTries == 2) || (scoreData[prob] & 0x07 > 0));
top.problemFrame.document.open(); // when we re-open
doc = top.problemFrame.document; // re-get the address
doc.writeln("<html><head></head><body bgcolor='#ffffff'>");
doc.writeln("<div align='center'><img src='pics/barflgs.gif' width='514' height='16'></div>");
doc.writeln(instruction[GetInstrRef(prob)]);
doc.writeln("<hr><form name='inputForm' onSubmit='top.CheckAnswer( -1 ); return false;'>");
choiceWords = new Array( 6 );
nChoices = SplitWords( choice[prob], "/", choiceWords );
maxlen=0;
for (i=0; i<nChoices; i++)
{
if (choiceWords[i].indexOf(",") >= 0)
{
maxlen = Math.max(maxlen, 15);
}
else
{
maxlen = Math.max(maxlen, choiceWords[i].length);
}
}
maxlen += 3; // make room for big chars
nFields = 0;
pos = 0;
i = spanish[prob].indexOf("_");
while (i >= 0 && pos < spanish[prob].length)
{
doc.write(spanish[prob].substring(pos, i));
doc.write("<input type='text' size='", maxlen, "' name='in", nFields,
"'>");
nFields++;
pos = i + 1;
i = spanish[prob].indexOf("_", pos);
}
doc.write(spanish[prob].substring(pos));
doc.writeln("<br>");
doc.writeln(english[prob], "<p>");
doc.writeln("</form>");
WriteScoringArea( doc );
doc.writeln("</body></html>");
doc.close();
UpdateScore( top.problemFrame.document );
if (answered)
{
SetNav("v");
}
else
{
SetNav("c");
}
top.problemFrame.document.inputForm.in0.focus();
}
function WriteHint( )
{
var i;
var prob;
var doc;
var swap;
var pos;
SetNav("v");
i = scoreData[order[currProblem]];
if (nTries == 0)
{
nTries++; // if you want a hint right away, it costs you a try
}
prob = order[currProblem]; // save chosen problem in temp. var for ease of reference
// top.problemFrame.document.open();
doc = top.problemFrame.document;
doc.writeln("<html><head></head><body bgcolor='#ffffff' link='#006600' vlink='#009900' alink='#00ff00'>");
doc.writeln("<div align='center'><img src='pics/barflgs.gif' width='514' height='16'></div><p>");
doc.writeln("<font size=3>");
doc.writeln(instruction[GetInstrRef(prob)]);
doc.writeln("<hr>");
pos = 0;
i = spanish[prob].indexOf("_");
while (i >= 0 && pos < spanish[prob].length)
{
doc.write(spanish[prob].substring(pos, i));
doc.write("______");
pos = i + 1;
i = spanish[prob].indexOf("_", pos);
}
doc.writeln(spanish[prob].substring(pos), "<br>");
doc.writeln(english[prob], "<p>");
/* no need to split out choice array - already happened in WriteProblem */
swap = new Array( nChoices );
SetNumeric( swap, nChoices );
Shuffle( swap, 0, nChoices );
for (i = 0; i < nChoices; i++)
{
doc.write("<a href=\"javascript:top.CheckAnswer(", swap[i], ");\">");
doc.writeln(choiceWords[swap[i]], "</a> ");
}
WriteScoringArea( doc );
doc.writeln("</body></html>");
doc.close();
if (currProblem != 0)
{
UpdateScore( doc );
}
}
function ShowAnswer( n )
{
var i;
var prob;
var doc;
var correct = new Array(nFields);
var swap;
var fld;
var pos;
prob = order[currProblem]; // save chosen problem in temp. var for ease of reference
top.problemFrame.document.open();
doc = top.problemFrame.document;
doc.writeln("<html><head></head><body bgcolor='#ffffff' link='#006600' vlink='#009900' alink='#00ff00'>");
doc.writeln("Here is the correct answer:");
doc.writeln("<hr>");
pos = 0;
fld = 0;
i = spanish[prob].indexOf("_");
correct = choiceWords[answer.charAt(2*order[currProblem])].split(',');
while (i >= 0 && pos < spanish[prob].length)
{
doc.write(spanish[prob].substring(pos, i));
doc.write("<font color='#990000'>" + correct[fld] + "</font>");
fld++;
pos = i + 1;
i = spanish[prob].indexOf("_", pos);
}
doc.writeln(spanish[prob].substring(pos), "<br>");
doc.writeln(english[prob], "<p>");
doc.writeln("<a href='javascript:top.PlayAnswer();'>Hear answer</a><p>");
doc.writeln("</body></html>");
doc.close();
SetNav("v");
}
function SetNav( ch )
{
var str;
str = ch + "butnBN.htm";
if (currProblem == 0)
{
str = ch + "butnN.htm";
}
else if (currProblem == nProblems - 1)
{
str = ch + "butnB.htm";
}
if (str != lastNav)
{
top.naviFrame.location.href = str;
lastNav = str;
}
}
function LoadSound(name)
{
var d;
top.soundFrame.document.open();
d = top.soundFrame.document;
d.writeln("<html><head></head><body bgcolor=\"#ffffff\">");
d.writeln("<center>");
if (navigator.appVersion.indexOf("Mac") == -1)
{
d.writeln("<embed src=\"" + name + "\" autostart=\"true\" ");
d.writeln(" width=\"144\" height=\"15\" controls=\"smallconsole\">");
}
else
{
d.writeln("<embed src=\"" + name + "\" width=\"144\" height=\"24\" autoplay=\"true\"",
" autostart=\"true\" controls=\"smallconsole\">");
}
d.writeln("</center>");
d.writeln("</body></html>");
d.close();
}
function PlayAnswer()
{
var dest;
if (playSound)
{
dest = waveOffset + order[currProblem];
dest = soundDir + dest + ".wav";
LoadSound( dest );
}
}
function CheckAnswer( n_chosen )
{
var i;
var m;
var dest;
var nPoints;
var correct = new Array(nFields);
var goodAnswer;
var student;
if (answered)
{
GoNext();
}
nTries++;
/* find a random congratulatory picture */
m = Math.floor(Math.random() * nFeedback) + 1;
/* check to see if answer is correct; if so, play sound,
and set answeredOK to TRUE to prevent further answers */
goodAnswer = true;
if (n_chosen < 0)
{
nPoints = 0;
correct = choiceWords[answer.charAt(2*order[currProblem])].split(',');
for (i=0; i<nFields; i++)
{
correct[i] = Deblank( correct[i] );
correct[i] = correct[i].toLowerCase();
student = Accent( top.problemFrame.document.inputForm.elements[i].value );
student = Deblank( student );
student = student.toLowerCase();
if (student == "")
{
return;
}
nPoints += (student == correct[i]) ? 1 : 0;
}
goodAnswer = (nPoints == nFields);
}
else
{
goodAnswer = (answer.charAt(2*order[currProblem]) == n_chosen);
}
if (goodAnswer)
{
top.problemFrame.document.feedback_img.src = "pics/good" + m + ".gif";
SetNav("v");
answered = 1;
if (n_chosen < 0)
{
score += nPoints;
}
else
{
if ((nChoices >2) || (nChoices == 2 && nTries == 2))
{
nPoints = 1;
score++;
}
}
possible += parseInt(pointList.charAt(2*order[currProblem]));
scoreData[order[currProblem]] = nPoints +
((nTries + ((n_chosen < 0) ? 0 : 3)) << 3);
UpdateScore(top.problemFrame.document);
if (playSound)
{
dest = waveOffset + order[currProblem];
dest = soundDir + dest + ".wav";
LoadSound( dest );
}
}
else
{
top.problemFrame.document.feedback_img.src = "pics/bad" + m + ".gif";
scoreData[order[currProblem]] =
((nTries + ((n_chosen < 0) ? 0 : 3)) << 3);
if (nTries == 2)
{
if (n_chosen < 0)
{
score += nPoints;
}
possible += parseInt(pointList.charAt(2*order[currProblem]));
ShowAnswer();
}
}
}
function GoNext()
{
if (currProblem != nProblems-1)
{
nTries = 0;
currProblem++;
answered = false;
WriteProblem( currProblem );
}
}
function GoBack()
{
if (currProblem != 0)
{
currProblem--;
WriteProblem( currProblem );
}
}
function GoIndex()
{
/* set up the cookie. Remember: lessonNumber is ONE-BASED! */
var str, sub;
var prefix, thisLesson, suffix;
var i, n, oldLen;
var pos;
var letter;
var today = new Date();
var alpha="0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
today.setYear(today.getYear() + 1);
if (document.cookie != "")
{
str = document.cookie;
oldLen = str.length;
// create a substring of the proper size
sub = "";
n = indices[lessonNumber] - indices[lessonNumber-1];
for (i=0; i<n; i++)
{
sub = sub + ( alpha.charAt(scoreData[i]) );
}
// grab "spanX=" and everything up to this lesson number
pos = str.indexOf("=") + 1;
prefix = str.substring( 0, pos + indices[lessonNumber-1] );
suffix = str.substring( pos+indices[lessonNumber] );
// construct the new cookie
document.cookie =
prefix +
sub +
suffix + ";expires=" + today.toGMTString();
if (document.cookie.length != oldLen)
{
alert("Saved data length error\n" + "old=" + oldLen +
" new=" + document.cookie.length);
}
}
top.location.href = lessonURL + "?prefs=" + playSound + useSavedData;
}
function Setup()
{
english = new Array(maxProblem);
spanish = new Array(maxProblem);
choice = new Array(maxProblem);
order = new Array(maxProblem);
scoreData = new Array(maxProblem);
currProblem = 0;
score = 0;
possible = 0;
nTries = 0;
lastNav = "";
SetPrefs();
if (nProblems == 0)
{
top.location.href = "allok.htm?prefs=" + playSound + useSavedData +
"&vfillfr/" + lessonNumber;
}
else
{
top.Initialize();
xlate = new Object();
xlate["A'"] = xlate["a'"] = "á";
xlate["E'"] = xlate["e'"] = "é";
xlate["I'"] = xlate["i'"] = "í";
xlate["O'"] = xlate["o'"] = "ó";
xlate["U'"] = xlate["u'"] = "ú";
xlate["N'"] = xlate["n'"] = "ñ";
xlate["N~"] = xlate["n~"] = "ñ";
WriteProblem(0);
}
}
function SoundOn()
{
playSound = 1;
top.soundControlFrame.location.href= "soundon.htm";
top.soundFrame.location.href="null.htm";
}
function SoundOff()
{
playSound = 0;
top.soundControlFrame.location.href= "soundoff.htm";
top.soundFrame.location.href="null.htm";
}
function SetPrefs()
{
var prefs;
var i, n, c;
var str;
var pos;
var alpha = "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
prefs = top.location.search;
prefs = prefs.substring(prefs.indexOf("=") + 1);
if (prefs.charAt(0) == "0")
{
SoundOff();
}
else
{
SoundOn();
}
useSavedData = prefs.charAt(1);
if (useSavedData == "")
{
useSavedData = "0";
}
// set up the problem order array
if (true) // was: ((useSavedData == "0") || (document.cookie == ""))
{
SetNumeric( order, maxProblem );
nProblems = maxProblem;
for (i=0; i < maxProblem; i++)
{
scoreData[i] = 0;
}
}
else
{
n = indices[lessonNumber] - indices[lessonNumber-1];
str = document.cookie;
pos = str.indexOf("=") + 1;
str = str.substring( pos + indices[lessonNumber-1],
pos + indices[lessonNumber]);
// pick up the unworked problems and [partially] incorrect problems
nProblems = 0;
for (i=0; i<n; i++)
{
c = str.charAt(i);
if (c != pointList.charAt(2*i))
{
order[nProblems++] = i;
}
else
{
score += (scoreData[i] & 0x07);
possible += parseInt(pointList.charAt(2*i));
}
}
}
}