Exam = function(inTitle)
{
   this.Questions = [];
   this.Title = '';
   this.titleimage = '';

   this.questioncount = 0;
   this.correctcount = 0;
   this.incorrectcount = 0;
   this.unansweredcount = 0;

   if (inTitle == '')
      alert('Exam should have a title:  usage curExam = new Exam(\'Chapter 2: Anatomy\'');
   else
      this.Title = inTitle;

   this.addQuestion = function(inQuestionObj)
   {
      this.Questions.push(inQuestionObj);
   }

   this.setTitleImage = function(inImageurl)
   {
      this.titleimage = inImageurl;
   }

   this.paintTitle = function()
   {
      retval = '';

      retval += '<table cellpadding="10" bgcolor="#0000AA">';
      retval += '   <td>';
      retval += '   <font size="+2" color="#FFFFFF">';
      retval += this.Title;
      retval += '   </font>';
      retval += '   <font size="-2" color="#FFFFFF"><BR>All rights reserved by Link Publishing. Program copyright © by Ron Lewis 2006.</font>';
      retval += '   </td>';
      if (this.titleimage != '')
      {
         retval += '   <td>';
         retval += '   <img src="' + this.titleimage + '">';
         retval += '   </td>';
      }
      retval += '</table>';

      return retval;
   }

   this.paintFinish = function()
   {
      retval = '';

      retval += '<script>';
      retval += 'function SubmitExamToGrade()';
      retval += '{';
      for (i = 0; i < this.Questions.length; i++)
      {
         if (this.Questions[i].type == 'multi')
         {
            retval += '   for (i = 0; i < document.forms[0].elements.length; i++)';
            retval += '      if (document.forms[0].elements[i].name == "Q' + (i + 1) + '" && document.forms[0].elements[i].checked)';
            retval += '         document.forms(0).action += "&Q' + (i + 1) + '=" + document.forms[0].elements[i].value;';
         }
         else
            retval += '   document.forms(0).action += "&Q' + (i + 1) + '=" + document.all.item("Q' + (i + 1) + '").value;';
      }
//      retval += '   document.forms(0).submit();';
//      retval += 'alert(document.forms(0).action);';
      retval += '   window.location.replace(document.forms(0).action);';
      retval += '}';
      retval += '</script>';

      document.write(retval);
   }

   this.gradeExam = function(parameters)
   {

      arrAnswers = [];
      for (i = 0; i < this.Questions.length; i++)
         arrAnswers.push('');

      ResetParamTokenLoc();
      while ((curitem = ParamToken(parameters)))
      {
         curquestion = ParamName(curitem);
         curanswer = ParamVal(curitem);
         if (curquestion.substring(0, 1) == 'Q')
         {
            //   get question #
            curquestionnum = parseInt(curquestion.substring(1));
            arrAnswers[curquestionnum - 1] = curanswer;
//            for (i = arrAnswers.length; i < curquestionnum - 1; i++)
//               arrAnswers.push('');
//            arrAnswers.push(curanswer);
         }
      }
      //   fill in remainder for unanswered questions at end
//      for (i = arrAnswers.length; i < this.Questions.length; i++)
//         arrAnswers.push('');

      //   score exam and print score table
      for (ii = 0; ii < this.Questions.length; ii++)
      {
         this.Questions[ii].score(arrAnswers[ii])
         this.questioncount += this.Questions[ii].questioncount;
         this.correctcount += this.Questions[ii].correctcount;
         this.incorrectcount += this.Questions[ii].incorrectcount;
         this.unansweredcount += this.Questions[ii].unansweredcount;
      }
   }

   this.showExamScore = function()
   {
      var retval = '';

      retval += '<table>';
      retval += '<tr>';
      retval += '   <td ' + this.Questions[0].formatnumbercelltext + '><font ' + this.Questions[0].formatnumberfonttext + '><b>G<br>R<br>A<br>D<br>E<br></b></font></td>';
      retval += '   <td ' + this.Questions[0].formatquestioncelltext + '><font ' + this.Questions[0].formatquestionfonttext + '>';
      retval += '   <br>';
      retval += '   Number of questions: ' + this.questioncount + '<br>';
      retval += '   Number Correct: ' + this.correctcount + '<br>';
      retval += '   Number Incorrect: ' + this.incorrectcount + '<br>';
      retval += '   Number Unanswered: ' + this.unansweredcount + '<br>';
      retval += '   <hr>';
      retval += '   <b>Exam Grade</b>: ' + parseInt((this.correctcount + .0000001) * 100 / (this.questioncount + .0000001) + .0000001) + '%';
      retval += '   </font>';
      retval += '   </td>';
      retval += '</tr>';
      retval += '</table>';
      retval += '<hr>';

      return retval;
   }
}
