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

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