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

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