home *** CD-ROM | disk | FTP | other *** search
/ Macmillan Math: Grade 1 …Pupil's Edition (Florida) / Math Grade 1 - Pupil's Edition (Florida).iso / pc / corsair / generic / javascript / assessment.js next >
Encoding:
JavaScript  |  2003-10-21  |  4.0 KB  |  188 lines

  1. // used to dynamically resize textareas based on browser type
  2. var isIE  = navigator.userAgent.indexOf( "MSIE" ) != -1;
  3. var isNN  = ( navigator.userAgent.indexOf( "Mozilla" ) != -1 ) && ( !isIE );
  4. var isWin = navigator.userAgent.indexOf( "Win" ) != -1;
  5. var isMac = navigator.userAgent.indexOf( "Mac" ) != -1;
  6.  
  7.  
  8. function getTextArea( qCount ) {
  9.     var columns = 0;
  10.     if ( isIE && isWin ) {
  11.         columns = 45;
  12.     } else if ( isIE && isMac ) {
  13.         columns = 60;
  14.     } else if ( isNN && isWin ) {
  15.         columns = 35;
  16.     } else if ( isNN && isMac ) {
  17.         columns = 55;
  18.     } else {
  19.         // unsupported browser, guess at correct size
  20.         columns = 40;
  21.     }
  22.     var html = "<textarea name='";
  23.     html += qCount;
  24.     html += "' wrap='hard' rows='5' cols='";
  25.     html += columns;
  26.     html += "'></textarea>";
  27.     return html;
  28. }
  29.  
  30.  
  31. // for the mutiple choice
  32. function getChecked (buttonGroup) 
  33. {
  34.   for(var j = 0; j < buttonGroup.length; j++) {
  35.     if(buttonGroup[j].checked) {
  36.       return j;
  37.     }
  38.   }
  39.   return -1;
  40. }
  41.  
  42. function isFormFilled(assessObj) {
  43.  
  44.   with(assessObj) {
  45.  
  46.     var temp = ""; // hold the name of the objects we have checked already
  47.     var regexp;
  48.     var selectedButton;
  49.  
  50.     for (var i=0;i<elements.length;i++) {
  51.  
  52.       if (elements[i].type == 'radio') {
  53.  
  54.         var regexp = new RegExp(elements[i].name);
  55.         if (regexp.test(temp) == false) { // has not checked yet
  56.           temp = temp + "|" + elements[i].name;
  57.  
  58.           selectedButton = getChecked(eval(elements[i].name));
  59.           if(selectedButton == -1)
  60.             return false;
  61.         }
  62.       } // if
  63.     } // for
  64.   } // with
  65.  
  66.   return true;
  67. }
  68.  
  69. function checkForm(assessObj)
  70. {
  71.     if(isFormFilled(assessObj) == false) {
  72.         alert("There are unanswered questions.\nPlease answer them before submit!");
  73.         return false;
  74.     }
  75.     return true;
  76. }
  77.  
  78.  
  79. function printAnswer(assessObj, isbn)
  80. {
  81.   if (checkForm(assessObj) == true) {
  82.     openAssessmentWnd(assessObj.name, "print", isbn);
  83.   }
  84. }
  85.  
  86.  
  87. function submitAnswer(assessObj, isbn)
  88. {
  89.   if (checkForm(assessObj) == true) {
  90.     openAssessmentWnd(assessObj.name, "check", isbn);
  91.   }
  92. }
  93.  
  94.  
  95. function emailAnswer(assessObj, isbn)
  96. {
  97.   if (checkForm(assessObj) == true) {
  98.     openAssessmentWnd(assessObj.name, "email", isbn);
  99.   }
  100. }
  101.  
  102. function isValidEmail(str) {
  103.     var index = str.indexOf("@");
  104.     if(index < 1)
  105.         return false;
  106.  
  107.     return str.substring(index + 1, str.length - 1).indexOf(".") > 1;
  108. }
  109.  
  110. function checkEmail(Sender, Recipient) {
  111.     if(!isValidEmail(Sender)) {
  112.         alert("The Sender email address is invalid.");
  113.         return false;
  114.     }
  115.     
  116.     if(!isValidEmail(Recipient)) {
  117.         alert("The Recipient email address is invalid.");
  118.         return false;
  119.     }
  120.     return true;
  121. }
  122.  
  123. function generateHiddenInput(qnum, answer)
  124. {
  125.     var html = "<input name='" + qnum + "' type='hidden' value='" + answer + "'/>";
  126.     return html;
  127. }
  128.  
  129. function getFormattedAnswer( answer , mode )
  130. {
  131.     if(mode == 'normal') {
  132.         var html = "<span class='AText'>" + answer + "</span> "
  133.         return html;
  134.     }
  135.     if(mode == 'correct') {
  136.         var html = "<span class='CText'>" + answer + "</span> "
  137.         return html;
  138.     }
  139.     if(mode == 'wrong') {
  140.         var html = "<span class='WText'>" + answer + "</span> "
  141.         return html;
  142.     }
  143.  
  144. }
  145.  
  146. function getChecked (radioGroup) {
  147.   for(var j = 0; j < radioGroup.length; j++) {
  148.     if(radioGroup[j].checked) {
  149.       return j;
  150.     }
  151.   }
  152.   return -1;
  153. }
  154.  
  155. function evaluateAnswer(qnum, option)
  156. {
  157.     for(i = 0; i < ansArray.length; i++) {
  158.         if(ansArray[i][0] == qnum) {
  159.             if(ansArray[i][1] == option) {
  160.                 calculateScore(qnum);
  161.                 return true;
  162.             }
  163.             else
  164.                 return false;
  165.         }
  166.     }
  167. }
  168.  
  169. function getCorrectAnswer(qnum)
  170. {
  171.     for(i = 0; i < ansArray.length; i++) {
  172.         if(ansArray[i][0] == qnum) {
  173.             return ansArray[i][1];
  174.         }
  175.     }
  176. }
  177.  
  178. function calculateScore(qnum) {
  179.     for(i = 0; i < ansArray.length; i++) {
  180.         if(ansArray[i][0] == qnum) {
  181.             score = score + ((100 /parseInt(ansArray[i][2])) / 100);
  182.         }
  183.     }
  184. }
  185.  
  186. function addBonusScore() {
  187.         score = score + 1;
  188. }