home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Spanish 3 / ProOne-MultimediaSpanishIII-WinMac.bin / pc / span3web / pracfr.js < prev    next >
Text File  |  1998-04-19  |  16KB  |  737 lines

  1. /**** Disk 3 indices ****/
  2. var indices = new Array( 244, 274, 309, 341, 376, 406 );
  3.  
  4. var spanish;            // spanish question
  5. var english;            // english translation of question
  6. var choice;                // the "choice" string (possible fill-ins)
  7. var choiceWords;        // the "choice" string split into words
  8. var nChoices;
  9. var order;                // the randomized order of the questions
  10. var instruction;        // the instructions (could be different for every example)
  11. var instrRef;            // reference to instruction
  12. var scoreData;            // list of scores for each question
  13. /*
  14.     0 = no answer yet
  15.     1 = correct first try (entry)
  16.     2 = wrong first try (entry)
  17.     3 = correct second try (entry)
  18.     4 = wrong second try (entry)
  19.     5 = correct first try (hint)
  20.     6 = wrong first try (hint)
  21.     7 = correct second try (hint)
  22.     8 = wrong second try (hint)
  23. */
  24.  
  25. var currProblem;
  26. var    nProblems;            // number of problems available to answer
  27. var    nTries;                // number of tries on a single problem
  28. var answered;            // has this problem been answered already?
  29. var score;                // your score so far
  30. var possible;            // total points possible so far
  31. var pointArray;            // array of points per problem
  32. var playSound;            // should we send out sound?
  33. var useSavedData;
  34.  
  35. var nFeedback = 9;        // number of feedback pictures
  36.  
  37. var    nFields;            // number of fields to fill in
  38.  
  39. var xlate;                // translation table for accented characters
  40. var skipWave;
  41.  
  42. function stringPart( string, start, length)
  43. {
  44.     return string.substring( start, start + length );
  45. }
  46.  
  47. function GetInstrRef( n )
  48. {
  49.     var i;
  50.     
  51.     for (i = instrRef.length-1; i >=0; i--)
  52.     {
  53.         if (n >= instrRef[i])
  54.         {
  55.             return i;
  56.         }
  57.     }
  58.     return 0;
  59. }
  60.  
  61. function Accent( str )
  62. {
  63.     var    i, j;
  64.     var    pair;
  65.     var delim;
  66.     
  67.     delim = "'";
  68.     for (j=0; j<2; j++)
  69.     {
  70.         i = str.indexOf(delim);
  71.         while (i > 0)
  72.         {
  73.             pair = str.substring( i-1, i+1);
  74.             if (xlate[pair])
  75.             {
  76.                 str = str.substring(0, i-1) + xlate[pair] + str.substring(i+1);
  77.             }
  78.             i = str.indexOf(delim, i+1);
  79.         }
  80.         delim = '~';
  81.     }
  82.     return str;
  83. }
  84.  
  85. function Deblank( str )
  86. {
  87.     var    i, j;
  88.     
  89.     i = 0;
  90.     while (str.charAt(i) == ' ')
  91.     {
  92.         i++;
  93.     }
  94.     str = str.substring(i);
  95.     if (str.length > 0)
  96.     {
  97.         i = str.length-1;
  98.         while (str.charAt(i) == ' ')
  99.         {
  100.             i--;
  101.         }
  102.         str = str.substring(0, i+1);
  103.     }
  104.     pos = 0;
  105.     i = str.indexOf(' ');
  106.     while (i >= 0 && pos < str.length)
  107.     {
  108.         j = i + 1;
  109.         while (str.charAt(j) == ' ')
  110.         {
  111.             j++;
  112.         }
  113.         if (j != i+1)
  114.         {
  115.             str = str.substring(0, i+1) + str.substring(j);
  116.         }
  117.         pos = i+1;
  118.         i = str.indexOf(' ', pos);
  119.     }
  120.     return str;
  121. }
  122.  
  123. function SetWords( wArray, startPos )
  124. {
  125.     var i;
  126.  
  127.     for (i=2; i<SetWords.arguments.length; i++)
  128.     {
  129.         wArray[startPos + i - 2] = SetWords.arguments[i];
  130.     }
  131. }
  132.  
  133. function SetNumeric( nArray, n )
  134. {
  135.     var i;
  136.     
  137.     for (i=0; i < n; i++)
  138.     {
  139.         nArray[i] = i;
  140.     }
  141. }
  142.  
  143. function Shuffle( nArray, startAt, n )
  144. {
  145.     var i, j, swap;
  146.     
  147.     for (i = startAt; i < startAt + n; i++)
  148.     {
  149.         j = startAt + Math.round(Math.random() * (n - 1));
  150.         swap = nArray[i];
  151.         nArray[i] = nArray[j];
  152.         nArray[j] = swap;
  153.     }
  154. }
  155.  
  156. /*******************************************************************************
  157.     Split words from string str into a word array (wordArr).
  158.     Words are delimited by the delim argument.
  159.     
  160.     Returns the number of words that were split out.
  161.  
  162.     Note: this function is here because not all browsers have Javascript's
  163.     "split" function enabled.
  164. *******************************************************************************/
  165. function SplitWords( str, delim, wordArr )    // for browsers not having a "split" function
  166. {
  167.     var fromPos, toPos, n;
  168.  
  169.     n = 0;
  170.     fromPos = 0;
  171.     toPos = str.indexOf( delim, 0 );
  172.     while (toPos >= 0)
  173.     {
  174.         wordArr[n++] = str.substring( fromPos, toPos );
  175.         fromPos = toPos + 1;
  176.         while (str.charAt( fromPos ) == ' ')
  177.         {
  178.             fromPos++;
  179.         }
  180.         toPos = str.indexOf( delim, fromPos );
  181.     }
  182.     /* take last word on line and store it */
  183.     wordArr[n++] = str.substring( fromPos, str.length );
  184.     return n;
  185. }
  186.  
  187. function UpdateScore( doc )
  188. {
  189.     doc.scoreForm.score.value = score + " of "  + possible;
  190. }
  191.  
  192. function WriteScoringArea( doc )
  193. {
  194.     var    name;
  195.  
  196.     doc.writeln("<table><tr><td valign='top'>");
  197.     doc.writeln("<form name='scoreForm'>");
  198.     doc.writeln("Problem ", currProblem + (maxProblem-nProblems) + 1, " of ", maxProblem, ".<br>");
  199.     doc.writeln("Your score: <input type='text' size='10' name='score' value=''>");
  200.     doc.writeln("</form></td><td width='10'></td><td valign='top'>");
  201.     if (answered)
  202.     {
  203.         name = ((scoreData[order[currProblem]] & 0x07) == 0)
  204.             ? "already0.gif" : "already1.gif";
  205.         doc.writeln("<img name=\"feedback_img\" src=\"pics/", name, "\" ",
  206.             "width='150' height='75' border='0'>");
  207.     }
  208.     else
  209.     {
  210.         doc.writeln("<img name=\"feedback_img\" src=\"pics/mtpic.gif\" ",
  211.             "width='150' height='75' border='0'>");
  212.     }
  213.     doc.writeln("</td></tr></table>");
  214. }
  215.  
  216. function WriteProblem( n )
  217. {
  218.     var    i;
  219.     var maxlen;
  220.     var pos;
  221.     var    prob;
  222.     var    doc;
  223.     var swap;
  224.  
  225.     prob = order[currProblem];    // save chosen problem in temp. var for ease of reference
  226.  
  227.     i = scoreData[prob];
  228.     nTries = (scoreData[prob] == 0) ? 0 : ((i >> 3) & 0x03);
  229.     if ((nTries == 1) && (((i >> 3) & 0x04) != 0))
  230.     {
  231.         nTries++;
  232.     }
  233.     
  234.     answered = ((nTries == 2) || ((scoreData[prob] & 0x07) > 0));
  235.  
  236.     top.problemFrame.document.open();    // when we re-open
  237.     doc = top.problemFrame.document;    // re-get the address
  238.  
  239.     doc.writeln("<html><head></head><body bgcolor='#ffffff'>");
  240.     doc.writeln("<div align='center'><img src='pics/barflgs.gif' width='514' height='16'></div><p>");
  241.     doc.writeln(instruction[GetInstrRef(prob)]);
  242.  
  243.     doc.writeln("<hr><form name='inputForm' onSubmit='top.CheckAnswer(-1); return false;'>");
  244.     
  245.     choiceWords = new Array( 6 );
  246.     nChoices = SplitWords( choice[prob], "/", choiceWords );
  247.  
  248.     maxlen=0;
  249.     for (i=0; i<nChoices; i++)
  250.     {
  251.         if (choiceWords[i].indexOf(",") >= 0)
  252.         {
  253.             maxlen = Math.max(maxlen, 15);
  254.         }
  255.         else
  256.         {
  257.             maxlen = Math.max(maxlen, choiceWords[i].length);
  258.         }
  259.     }
  260.     maxlen += 3; // make room for big chars
  261.  
  262.     nFields = 0;
  263.     pos = 0;
  264.     i = spanish[prob].indexOf("_");
  265.     while (i >= 0 && pos < spanish[prob].length)
  266.     {
  267.         doc.write(spanish[prob].substring(pos, i));
  268.         doc.write("<input type='text' size='", maxlen, "' name='in", nFields, 
  269.             "'>");
  270.         nFields++;
  271.         pos = i + 1;
  272.         i = spanish[prob].indexOf("_", pos);
  273.     }
  274.     doc.write(spanish[prob].substring(pos));
  275.  
  276.     doc.writeln("<br>");
  277.     doc.writeln("</form>");
  278.     doc.writeln(english[prob], "<p>");
  279.     
  280.     WriteScoringArea( doc );
  281.  
  282.     doc.writeln("</body></html>");
  283.     doc.close();
  284.  
  285.     UpdateScore( top.problemFrame.document );
  286.  
  287.     if (answered)
  288.     {
  289.         SetNav("v");
  290.     }
  291.     else
  292.     {
  293.         SetNav("c");
  294.     }
  295.     
  296.     top.problemFrame.document.inputForm.in0.focus();
  297. }
  298.  
  299. function WriteHint( n )
  300. {
  301.     var    i;
  302.     var    prob;
  303.     var    doc;
  304.     var swap;
  305.     var pos;
  306.     
  307.     SetNav("v");
  308.  
  309.     i = scoreData[order[currProblem]];
  310.  
  311.     if (nTries == 0)
  312.     {
  313.         nTries++;    // if you want a hint right away, it costs you a try
  314.     }
  315.     
  316.     prob = order[currProblem];    // save chosen problem in temp. var for ease of reference
  317. //    top.problemFrame.document.open();
  318.     doc = top.problemFrame.document;
  319.  
  320.     doc.writeln("<html><head></head><body bgcolor='#ffffff' link='#006600' vlink='#009900' alink='#00ff00'>");
  321.     doc.writeln("<div align='center'><img src='pics/barflgs.gif' width='514' height='16'></div><p>");
  322.     doc.writeln("<font size=3>");
  323.     doc.writeln(instruction[GetInstrRef(prob)]);
  324.  
  325.     doc.writeln("<hr>");
  326.     
  327.     pos = 0;
  328.     i = spanish[prob].indexOf("_");
  329.     while (i >= 0 && pos < spanish[prob].length)
  330.     {
  331.         doc.write(spanish[prob].substring(pos, i));
  332.         doc.write("______");
  333.         pos = i + 1;
  334.         i = spanish[prob].indexOf("_", pos);
  335.     }
  336.     doc.writeln(spanish[prob].substring(pos), "<br>");
  337.     doc.writeln(english[prob], "<p>");
  338.     
  339.     /* no need to split out choice array - already happened in WriteProblem */
  340.  
  341.     swap = new Array( nChoices );
  342.     SetNumeric( swap, nChoices );
  343.     Shuffle( swap, 0, nChoices );
  344.         
  345.     for (i = 0; i < nChoices; i++)
  346.     {
  347.         doc.write("<a href=\"javascript:top.CheckAnswer(", swap[i], ");\">");
  348.         doc.writeln(choiceWords[swap[i]], "</a>    ");
  349.     }
  350.     WriteScoringArea( doc );
  351.     
  352.     doc.writeln("</body></html>");
  353.     doc.close();
  354.  
  355.     if (currProblem != 0)
  356.     {
  357.         UpdateScore( doc );
  358.     }
  359.  
  360. }
  361.  
  362. function ShowAnswer( n )
  363. {
  364.     var    i;
  365.     var    prob;
  366.     var    doc;
  367.     var correct = new Array(nFields);
  368.     var swap;
  369.     var fld;
  370.     var pos;    
  371.     
  372.     prob = order[currProblem];    // save chosen problem in temp. var for ease of reference
  373.  
  374.     top.problemFrame.document.open();
  375.     doc = top.problemFrame.document;
  376.  
  377.     doc.writeln("<html><head></head><body bgcolor='#ffffff' link='#006600' vlink='#009900' alink='#00ff00'>");
  378.     doc.writeln("Here is the correct answer:");
  379.  
  380.     doc.writeln("<hr>");
  381.     
  382.     pos = 0;
  383.     fld = 0;
  384.     i = spanish[prob].indexOf("_");
  385.     correct = choiceWords[answer.charAt(2*order[currProblem])].split(',');
  386.     while (i >= 0 && pos < spanish[prob].length)
  387.     {
  388.         doc.write(spanish[prob].substring(pos, i));
  389.         doc.write("<font color='#990000'>" + correct[fld] + "</font>");
  390.         fld++;
  391.         pos = i + 1;
  392.         i = spanish[prob].indexOf("_", pos);
  393.     }
  394.     doc.writeln(spanish[prob].substring(pos), "<br>");
  395.     doc.writeln(english[prob], "<p>");
  396.     
  397.     doc.writeln("<a href='javascript:top.PlayAnswer();'>Hear answer</a><p>");
  398.     doc.writeln("</body></html>");
  399.     doc.close();
  400.  
  401.     SetNav("v");
  402.  
  403. }
  404.  
  405. function SetNav( ch )
  406. {
  407.     var    str;
  408.  
  409.     str = ch + "butnBN.htm";
  410.     if (currProblem == 0)
  411.     {
  412.         str = ch + "butnN.htm";
  413.     }
  414.     else if (currProblem == nProblems - 1)
  415.     {
  416.         str = ch + "butnB.htm";
  417.     }
  418.     top.naviFrame.location.href = str;
  419. }
  420.  
  421. function LoadSound(name)
  422. {
  423.     var     d;
  424.     
  425.     top.soundFrame.document.open();
  426.     d = top.soundFrame.document;
  427.     d.writeln("<html><head></head><body bgcolor=\"#ffffff\">");
  428.     d.writeln("<center>");
  429.     if (navigator.appVersion.indexOf("Mac") == -1)
  430.     {
  431.         d.writeln("<embed src=\"" + name + "\" autostart=\"true\" ");
  432.         d.writeln(" width=\"144\" height=\"15\" controls=\"smallconsole\">");
  433.     }
  434.     else
  435.     {
  436.         d.writeln("<embed src=\"" + name + "\" width=\"144\" height=\"24\" autoplay=\"true\"",
  437.             " autostart=\"true\" controls=\"smallconsole\">");
  438.     }
  439.     d.writeln("</center>");
  440.     d.writeln("</body></html>");
  441.     d.close();
  442. }
  443.  
  444.  
  445. function PlayAnswer()
  446. {
  447.     var    dest;
  448.     if (playSound)
  449.     {
  450.         dest = waveOffset + order[currProblem];
  451.         if (dest >= skipWave)
  452.         {
  453.             dest++;
  454.         }
  455.         dest = soundDir + dest + ".wav";
  456.         LoadSound( dest );
  457.     }
  458. }
  459.  
  460. function CheckAnswer( n_chosen )
  461. {
  462.     var i;
  463.     var m;
  464.     var    dest;
  465.     var nPoints;
  466.     var    correct = new Array(nFields);
  467.     var goodAnswer;
  468.     var student;
  469.  
  470.     if (answered)
  471.     {
  472.         GoNext();
  473.         return;
  474.     }
  475.     
  476.     nTries++;
  477.  
  478.     /* find a random congratulatory picture */
  479.     m = Math.floor(Math.random() * nFeedback) + 1;
  480.     
  481.     /* check to see if answer is correct; if so, play sound,
  482.         and set answeredOK to TRUE to prevent further answers */
  483.     goodAnswer = true;
  484.  
  485.     if (n_chosen < 0)
  486.     {
  487.         nPoints = 0;
  488.         correct = choiceWords[answer.charAt(2*order[currProblem])].split(',');
  489.     
  490.         for (i=0; i<nFields; i++)
  491.         {
  492.             correct[i] = Deblank( correct[i] );
  493.             correct[i] = correct[i].toLowerCase();
  494.             student = Accent( top.problemFrame.document.inputForm.elements[i].value );
  495.             student = Deblank( student );
  496.             student = student.toLowerCase();
  497.             if (student == "")
  498.             {
  499.                 return;
  500.             }
  501.             nPoints += (student == correct[i]) ? 1 : 0;
  502.         }
  503.         goodAnswer = (nPoints == nFields);
  504.     }
  505.     else
  506.     {
  507.         goodAnswer = (answer.charAt(2*order[currProblem]) == n_chosen);
  508.     }
  509.     
  510.     if (goodAnswer)
  511.     {
  512.         top.problemFrame.document.feedback_img.src = "pics/good" + m + ".gif";
  513.         SetNav("v");
  514.         answered = true;
  515.         
  516.         if (n_chosen < 0)
  517.         {
  518.             score += nPoints;
  519.         }
  520.         else
  521.         {
  522.             if ((nChoices >2) || (nChoices == 2 && nTries == 2))
  523.             {
  524.                 nPoints = 1;
  525.                 score++;
  526.             }
  527.         }
  528.         possible += 1;
  529.         scoreData[order[currProblem]] = nPoints +
  530.             ((nTries + ((n_chosen < 0) ? 0 : 3)) << 3);
  531.         UpdateScore(top.problemFrame.document);
  532.  
  533.         PlayAnswer();
  534.     }
  535.     else
  536.     {
  537.         top.problemFrame.document.feedback_img.src = "pics/bad" + m + ".gif";
  538.         scoreData[order[currProblem]] = 
  539.             ((nTries + ((n_chosen < 0) ? 0 : 3)) << 3);
  540.         if (nTries == 2)
  541.         {
  542.             if (n_chosen < 0)
  543.             {
  544.                 score += nPoints;
  545.             }
  546.             possible += 1;
  547.             ShowAnswer();
  548.         }
  549.     }
  550. }
  551.  
  552.  
  553. function GoNext()
  554. {
  555.     if (currProblem != nProblems-1)
  556.     {
  557.         nTries = 0;
  558.         currProblem++;
  559.         answered = false;
  560.         WriteProblem( currProblem );
  561.     }
  562. }
  563.  
  564. function GoBack()
  565. {
  566.     if (currProblem != 0)
  567.     {
  568.         currProblem--;
  569.         WriteProblem( currProblem );
  570.     }
  571. }
  572.  
  573. function GoIndex()
  574. {
  575.     /* set up the cookie. Remember: lessonNumber is ONE-BASED! */
  576.     var    str, sub;
  577.     var prefix, thisLesson, suffix;
  578.     var i, n, oldLen;
  579.     var pos;
  580.     var letter;
  581.     var today = new Date();
  582.     var alpha="0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  583.     
  584.     today.setYear(today.getYear() + 1);
  585.  
  586.     if (document.cookie != "")
  587.     {
  588.         str = document.cookie;
  589.     
  590.         oldLen = str.length;
  591.     
  592.         // create a substring of the proper size
  593.         
  594.         sub = "";
  595.         n = indices[lessonNumber] - indices[lessonNumber-1];
  596.     
  597.         for (i=0; i<n; i++)
  598.         {
  599.             sub = sub + ( alpha.charAt(scoreData[i]) );
  600.         }
  601.     
  602.         // grab "spanX=" and everything up to this lesson number
  603.         pos = str.indexOf("=") + 1;
  604.     
  605.         prefix = str.substring( 0, pos + indices[lessonNumber-1] );
  606.     
  607.         suffix = str.substring( pos+indices[lessonNumber] );
  608.     
  609.         // construct the new cookie
  610.         document.cookie =
  611.             prefix +
  612.             sub +
  613.             suffix + ";expires=" + today.toGMTString();
  614.      
  615.         if (document.cookie.length != oldLen)
  616.         {
  617.             alert("Saved data length error\n" + "old=" + oldLen +
  618.                 " new=" + document.cookie.length);
  619.         }
  620.      }
  621.       top.location.href = lessonURL + "?prefs=" + playSound + useSavedData;
  622. }
  623.  
  624. function Setup()
  625. {
  626.     english = new Array(maxProblem);
  627.     spanish = new Array(maxProblem);
  628.     choice = new Array(maxProblem);
  629.     order = new Array(maxProblem);
  630.     scoreData = new Array(maxProblem);
  631.  
  632.     currProblem = 0;
  633.     score = 0;
  634.     possible = 0;
  635.     nTries = 0;
  636.     skipWave = 999;
  637.  
  638.     SetPrefs();
  639.  
  640.     if (nProblems == 0)
  641.     {
  642.         top.location.href = "allok.htm?prefs=" + playSound + useSavedData +
  643.             "&pracfr/" + lessonNumber;
  644.     }
  645.     else
  646.     {
  647.         top.Initialize();
  648.     
  649.         xlate = new Object();
  650.         xlate["A'"] = xlate["a'"] = "á";
  651.         xlate["E'"] = xlate["e'"] = "é";
  652.         xlate["I'"] = xlate["i'"] = "í";
  653.         xlate["O'"] = xlate["o'"] = "ó";
  654.         xlate["U'"] = xlate["u'"] = "ú";
  655.         xlate["N'"] = xlate["n'"] = "ñ";
  656.         xlate["N~"] = xlate["n~"] = "ñ";
  657.         WriteProblem(0);
  658.     }
  659. }
  660.  
  661.  
  662. function SoundOn()
  663. {
  664.     playSound = 1;
  665.     top.soundControlFrame.location.href= "soundon.htm";
  666.     top.soundFrame.location.href="null.htm";
  667. }
  668.  
  669. function SoundOff()
  670. {
  671.     playSound = 0;
  672.     top.soundControlFrame.location.href= "soundoff.htm";
  673.     top.soundFrame.location.href="null.htm";
  674. }
  675.  
  676. function SetPrefs()
  677. {
  678.     var    prefs;
  679.     var i, n, c;
  680.     var    str;
  681.     var pos;
  682.     var alpha = "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  683.  
  684.     prefs = top.location.search;
  685.     prefs = prefs.substring(prefs.indexOf("=") + 1);
  686.  
  687.     if (prefs.charAt(0) == "0")
  688.     {
  689.         SoundOff();
  690.     }
  691.     else
  692.     {
  693.         SoundOn();
  694.     }
  695.     useSavedData = prefs.charAt(1);
  696.     if (useSavedData == "")
  697.     {
  698.         useSavedData = "0";
  699.     }
  700.  
  701.     // set up the problem order array
  702.     if (true) // was: ((useSavedData == "0") || (document.cookie == ""))
  703.     {
  704.         SetNumeric( order, maxProblem );
  705.         nProblems = maxProblem;
  706.         for (i=0; i < maxProblem; i++)
  707.         {
  708.             scoreData[i] = 0;
  709.         }
  710.     }
  711.     else
  712.     {
  713.         n = indices[lessonNumber] - indices[lessonNumber-1];
  714.         pos = document.cookie.indexOf("=") + 1;
  715.         str = document.cookie.substring( pos + indices[lessonNumber-1],
  716.             pos + indices[lessonNumber]);
  717.  
  718.         // pick up the unworked problems and [partially] incorrect problems
  719.  
  720.         nProblems = 0;
  721.         for (i=0; i<n; i++)
  722.         {
  723.             scoreData[i] = alpha.indexOf( str.charAt( i ) );
  724.             if (scoreData[i] == 0)
  725.             {
  726.                 order[nProblems] = i;
  727.                 nProblems++;
  728.             }
  729.             else
  730.             {
  731.                 score += (scoreData[i] & 0x07);
  732.                 possible++;
  733.             }
  734.         }
  735.     }
  736. }
  737.