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

  1. /**** The "indices" array for disk 3 ****/
  2. var indices = new Array( 432, 438, 443, 448, 453, 459 );
  3.  
  4. var    passage;            // the passage to read
  5. var question;            // the question
  6. var choice;                // the "choice" string (possible fill-ins)
  7. var order;                // the randomized order of the questions
  8. var instruction;        // the instructions
  9. var scoreData;            // list of scores on each question
  10.  
  11. var currProblem;
  12. var nProblems;            // number of problems available to be filled
  13. var    nTries;                // number of tries on a single problem
  14. var nChoices;            // number of choices for this question
  15. var answered;            // has this problem been answered OK already?
  16. var score;                // your score so far
  17. var possible;            // total points possible so far
  18. var playSound;            // should we send out sound?
  19. var useSavedData;
  20.  
  21. var nFeedback = 9;        // number of feedback pictures
  22.  
  23. function stringPart( string, start, length)
  24. {
  25.     return string.substring( start, start + length );
  26. }
  27.  
  28.  
  29. function SetWords( wArray, startPos )
  30. {
  31.     var i;
  32.  
  33.     for (i=2; i<SetWords.arguments.length; i++)
  34.     {
  35.         wArray[startPos + i - 2] = SetWords.arguments[i];
  36.     }
  37. }
  38.  
  39. function SetNumeric( nArray, n )
  40. {
  41.     var i;
  42.     
  43.     for (i=0; i < n; i++)
  44.     {
  45.         nArray[i] = i;
  46.     }
  47. }
  48.  
  49. function Shuffle( nArray, startAt, n )
  50. {
  51.     var i, j, swap;
  52.     
  53.     for (i = startAt; i < startAt + n; i++)
  54.     {
  55.         j = startAt + Math.round(Math.random() * (n - 1));
  56.         swap = nArray[i];
  57.         nArray[i] = nArray[j];
  58.         nArray[j] = swap;
  59.     }
  60. }
  61.  
  62. /*******************************************************************************
  63.     Split words from string str into a word array (wordArr).
  64.     Words are delimited by the delim argument.
  65.     
  66.     Returns the number of words that were split out.
  67.  
  68.     Note: this function is here because not all browsers have Javascript's
  69.     "split" function enabled.
  70. *******************************************************************************/
  71. function SplitWords( str, delim, wordArr )    // for browsers not having a "split" function
  72. {
  73.     var fromPos, toPos, n;
  74.     
  75.     n = 0;
  76.     fromPos = 0;
  77.     toPos = str.indexOf( delim, 0 );
  78.     while (toPos >= 0)
  79.     {
  80.         wordArr[n++] = str.substring( fromPos, toPos );
  81.         fromPos = toPos + 1;
  82.         while (str.charAt( fromPos ) == ' ')
  83.         {
  84.             fromPos++;
  85.         }
  86.         toPos = str.indexOf( delim, fromPos );
  87.     }
  88.     /* take last word on line and store it */
  89.     wordArr[n++] = str.substring( fromPos, str.length );
  90.     return n;
  91. }
  92.  
  93. function UpdateScore( doc )
  94. {
  95.     doc.forms[0].score.value = score + " of "  + possible;
  96. }
  97.  
  98. function LoadSound(name)
  99. {
  100. /****** not used in this module
  101.     var     d;
  102.     
  103.     d = top.soundFrame.document;
  104.     d.open();
  105.     d.writeln("<html><head></head><body bgcolor=\"#ffffff\">");
  106.     d.writeln("<center>");
  107.     if (navigator.appVersion.indexOf("Mac") == -1)
  108.     {
  109.         d.writeln("<embed src=\"" + name + "\" autostart=\"true\" ");
  110.         d.writeln(" width=\"144\" height=\"15\" controls=\"smallconsole\">");
  111.     }
  112.     else
  113.     {
  114.         d.writeln("<embed src=\"" + name + "\" width=\"144\" height=\"24\" autoplay=\"true\"",
  115.             " autostart=\"true\" controls=\"smallconsole\">");
  116.     }
  117.     d.writeln("</center>");
  118.     d.writeln("</body></html>");
  119.     d.close();
  120. *************/
  121. }
  122.  
  123.  
  124. function WriteProblem( n )
  125. {
  126.     var    i;
  127.     var    prob;
  128.     var    doc;
  129.     var choiceWords;
  130.     var swap;
  131.     
  132.     prob = order[currProblem];    // save chosen problem in temp. var for ease of reference
  133.  
  134.     i = scoreData[prob];
  135.     nTries = (scoreData[prob] == 0) ? 0 : ((i >> 3) & 0x03);
  136.     if ((nTries == 1) && (((i >> 3) & 0x04) != 0))
  137.     {
  138.         nTries++;
  139.     }
  140.     
  141.     answered = ((nTries == 2) || ((scoreData[prob] & 0x07) > 0));
  142.  
  143.     doc = top.problemFrame.document;
  144.     
  145.     doc.open();
  146.  
  147.     doc.writeln("<html><head></head><body bgcolor='#ffffff' link='#006600' vlink='#009900' alink='#00ff00'>");
  148.     doc.writeln("<div align='center'><img src='pics/barflgs.gif' width='514' height='16'></div>");
  149.     doc.writeln(instruction);
  150.  
  151.     doc.writeln("<hr>");
  152.     
  153.     doc.writeln(passage);
  154.     
  155.     doc.writeln("<hr>");
  156.     
  157.     doc.writeln(question[prob], "<p>");
  158.     
  159.     choiceWords = new Array( 6 );
  160.     nChoices = SplitWords( choice[prob], "/", choiceWords );
  161.     swap = new Array( nChoices );
  162.     SetNumeric( swap, nChoices );
  163.     Shuffle( swap, 0, nChoices );
  164.         
  165.     for (i = 0; i < nChoices; i++)
  166.     {
  167.         doc.write("<a href=\"javascript:top.CheckAnswer(", swap[i], ");\">");
  168.         doc.writeln(choiceWords[swap[i]], "</a>    ");
  169.     }
  170.     doc.writeln("<table><tr><td valign='top'>");
  171.     doc.writeln("<form name='scoreForm'>");
  172.     doc.writeln("Problem ", currProblem + (maxProblem-nProblems) + 1, " of ", maxProblem, ".<br>");
  173.     doc.writeln("Your score: <input type='text' size='10' name='score' value=''>");
  174.     doc.writeln("</form></td><td width='10'></td><td valign='top'>");
  175.     doc.writeln("<img name='feedback_img' src=\"pics/mtpic.gif\" width='150' height='75' border='0'>");
  176.     doc.writeln("</td></tr></table>");
  177.     
  178.     if (currProblem != 0)
  179.     {
  180.         UpdateScore( doc );
  181.     }
  182.  
  183.     frames[1].location.href = "vbutnX.htm";
  184. /*
  185.  if allowing users to back up to previous problems:
  186.     if (currProblem == 0)
  187.     {
  188.         frames[1].location.href = "vbutnN.htm";
  189.     }
  190.     else if (currProblem == nProblems-1)
  191.     {
  192.         frames[1].location.href = "vbutnB.htm";
  193.     }
  194.     else
  195.     {
  196.         frames[1].location.href = "vbutnBN.htm";
  197.     }
  198. */
  199.     doc.writeln("</body></html>");
  200.     doc.close();
  201. }
  202.  
  203. function ShowAnswer( n )
  204. {
  205.     var    i;
  206.     var    prob;
  207.     var    doc;
  208.     var choiceWords;
  209.     var swap;
  210.     
  211.     doc = top.problemFrame.document;
  212.     
  213.     prob = order[currProblem];    // save chosen problem in temp. var for ease of reference
  214.     doc.open();
  215.  
  216.     doc.writeln("<html><head></head><body bgcolor='#ffffff'>");
  217.     doc.writeln("Here is the correct answer:");
  218.  
  219.     doc.writeln("<hr>");
  220.     
  221.     doc.writeln(question[prob], "<p>");
  222.     
  223.     doc.writeln("<font color='#990000'><b>");
  224.     choiceWords = new Array( 6 );
  225.     SplitWords( choice[prob], "/", choiceWords );
  226.     i = answerLetters.indexOf(answer.charAt(2*order[currProblem]));
  227.     doc.writeln(choiceWords[i]);
  228.     doc.writeln("</b></font><p>");
  229.  
  230.     if (currProblem != nProblems-1)
  231.     {
  232.         frames[1].location.href = "vbutnN.htm";
  233.     }
  234.     else
  235.     {
  236.         frames[1].location.href = "vbutnX.htm";
  237.     }
  238.     doc.writeln("</body></html>");
  239.     doc.close();
  240. }
  241.  
  242. function CheckAnswer( n )
  243. {
  244.     var m;
  245.     var    dest;
  246.  
  247.     if (answered)
  248.     {
  249.         return;
  250.     }
  251.     
  252.     nTries++;
  253.  
  254.     if (nTries == 1)
  255.     {
  256.         possible += 1;
  257.     }
  258.  
  259.     /* find a random congratulatory picture */
  260.     m = Math.floor(Math.random() * nFeedback) + 1;
  261.     
  262.     /* check to see if answer is correct; if so, play sound,
  263.         and set answered to TRUE to prevent further answers */
  264.     if (answer.charAt(2*order[currProblem]) == answerLetters.charAt(n))
  265.     {
  266.         top.problemFrame.document.feedback_img.src = "pics/good" + m + ".gif";
  267.         top.naviFrame.location.href = (currProblem == nProblems-1 ) ?
  268.             "vbutnX.htm" : "vbutnN.htm";
  269.         answered = true;
  270.         score++;
  271.         scoreData[order[currProblem]] = 1 + (nTries << 3);
  272.         UpdateScore(top.problemFrame.document);
  273.     }
  274.     else
  275.     {
  276.         top.problemFrame.document.feedback_img.src = "pics/bad" + m + ".gif";
  277.         scoreData[order[currProblem]] = (nTries << 3);
  278.         if (nTries == 2 || nChoices == 2)
  279.         {
  280.             ShowAnswer();
  281.         }
  282.     }
  283. }
  284.  
  285. function GoNext()
  286. {
  287.     if (currProblem != nProblems-1)
  288.     {
  289.         nTries = 0;
  290.         currProblem++;
  291.         WriteProblem( currProblem );
  292.     }
  293. }
  294.  
  295. function GoBack()
  296. {
  297.     if (currProblem != 0)
  298.     {
  299.         currProblem--;
  300.         WriteProblem( currProblem );
  301.     }
  302. }
  303.  
  304. function Setup()
  305. {
  306.     var        i;
  307.  
  308.     question = new Array(maxProblem);
  309.     choice = new Array(maxProblem);
  310.     order = new Array(maxProblem);
  311.     scoreData = new Array(maxProblem)
  312.     
  313.     nTries = 0;
  314.     currProblem = 0;
  315.     score = 0;
  316.     possible = 0;
  317.  
  318.     // set up the problem order array
  319.     SetNumeric( order, maxProblem );
  320.  
  321.     if (randomize)
  322.     {
  323.         Shuffle( order, 0, maxProblem ); // only if the questions should be in random order
  324.     }
  325.     
  326.     for (i=0; i < maxProblem; i++)
  327.     {
  328.         scoreData[i] = 0;
  329.     }
  330.     SetPrefs();
  331.     score = 0;
  332.     possible = 0;
  333.     currProblem = 0;
  334.     Initialize();
  335.     WriteProblem(0);
  336. }
  337.  
  338. function GoIndex()
  339. {
  340.     /* set up the cookie. Remember: lessonNumber is ONE-BASED! */
  341.     var    str, sub;
  342.     var prefix, thisLesson, suffix;
  343.     var i, n;
  344.     var pos;
  345.     var letter;
  346.     var today = new Date();
  347.     var alpha="0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  348.         
  349.     today.setYear(today.getYear() + 1);
  350.  
  351.     if (document.cookie != "")
  352.     {
  353.         str = document.cookie;
  354.     
  355.         oldLen = str.length;
  356.     
  357.         // create a substring of the proper size
  358.         
  359.         sub = "";
  360.         n = indices[lessonNumber] - indices[lessonNumber-1];
  361.     
  362.         for (i=0; i<n; i++)
  363.         {
  364.             sub = sub + ( alpha.charAt(scoreData[i]) );
  365.         }
  366.     
  367.         // grab "spanX=" and everything up to this lesson number
  368.         pos = str.indexOf("=") + 1;
  369.     
  370.         prefix = str.substring( 0, pos + indices[lessonNumber-1] );
  371.     
  372.         suffix = str.substring( pos+indices[lessonNumber] );
  373.     
  374.         // construct the new cookie
  375.         document.cookie =
  376.             prefix +
  377.             sub +
  378.             suffix + ";expires=" + today.toGMTString();
  379.  
  380.         if (document.cookie.length != oldLen)
  381.         {
  382.             alert("Saved data length error\n" + "old=" + oldLen +
  383.                 " new=" + document.cookie.length);
  384.         }
  385.     }
  386.       top.location.href = lessonURL + "?prefs=" + playSound + useSavedData;
  387. }
  388.  
  389. function SoundOn()
  390. {
  391. //    playSound = 1;
  392. //    top.soundControlFrame.location.href= "soundon.htm";
  393. }
  394.  
  395. function SoundOff()
  396. {
  397. //    playSound = 0;
  398. //    top.soundControlFrame.location.href= "soundoff.htm";
  399. }
  400.  
  401. function SetPrefs()
  402. {
  403.     var    prefs;
  404.     var i, n;
  405.  
  406.     prefs = top.location.search;
  407.     prefs = prefs.substring(prefs.indexOf("=") + 1);
  408.     
  409.     playSound = prefs.charAt(0);
  410.     if (playSound == "")
  411.     {
  412.         playSound = "0";
  413.     }
  414.     else
  415.     {
  416.         playSound = "1";
  417.     }
  418.     useSavedData = prefs.charAt(1);
  419.     if (useSavedData == "")
  420.     {
  421.         useSavedData = "0";
  422.     }
  423.     else
  424.     {
  425.         useSavedData = "1";
  426.     }
  427.  
  428.     SetNumeric( order, maxProblem );
  429.     nProblems = maxProblem;
  430.     for (i=0; i < maxProblem; i++)
  431.     {
  432.         scoreData[i] = 0;
  433.     }
  434. }
  435.  
  436.