// This file contains

// function InitQuiz - Initialize student's answers & scores
// function IsEmpty - Clears previous input strings
// function MultipleChoice - Calculates multiple choice items
// function MultipleSelect - Calculates multiple select items
// function Matching - Calculates matching items
// function DisplayOutPut - Displays students results 
// function DisplayInlineOutput - Write the output
// function CheckForm (form) - called by the scoring button
// function ShowGenericFeedback - Loop through all the span to make visible
// function TextEntry - Calculates text entry items

// variable and function naming standard
  // global variable  - gClassnamePropertyName
  // local variable   - lowerCase
  // function         - StartsWithUpperCase

//********************************************************
// Global Variables
//********************************************************

  //Declare Quiz Variables
    var gQuizNumQuestions      //Number of questions in the quiz
    var gQuizDisplayFeedback   //Show the user feedback?
    var gQuizDisplayScore      //Show the user their score?
    var gQuizScorePerQuestion  //Max score for each question
	var gQuizUserScore         //User's total score

  //Declare Question Variables
    gQuestionType       = new Array()  //Type of question
    gQuestionNumChoices = new Array()  //Number of choices in the question
    gQuestionFeedback   = new Array()  //Feedback string
    gQuestionAnswer     = new Array()  //The correct answer(s)
    gQuestionUserScore  = new Array()  //The user's score
	gQuestionUserAnswer = new Array()  //The User's answers
	gQuestionCorrect    = new Array()  //Whether the whole question was correct or not

  //Delcare question type constants
    var gMultipleChoice = 1
    var gMultipleSelect = 2
    var gMatching       = 3
  	var gTextEntry      = 4  
  
//********************************************************
// function InitQuiz
//********************************************************

  function InitQuiz()
  {
    //Counter
      var n
	
    //Initialize student's answers and scores
    //convert all correct answers to UpperCase
      for (n = 1; n <= gQuizNumQuestions; n++)
      {
        gQuestionUserScore[n] = 0
	    gQuestionUserAnswer[n] = ""
	    gQuestionAnswer[n] = gQuestionAnswer[n].toUpperCase()
	    gQuestionCorrect[n] = 0
	  }

    //Calculate the points per question
      gQuizScorePerQuestion = 100 / gQuizNumQuestions
  }
  
  //Generic rountines follow

//********************************************************
// function IsEmpty
//********************************************************

  function IsEmpty(inputStr)
  {
    if (inputStr == null || inputStr == "")
	{
	  return true
	}
	return false
  }


  //Routines to grade the quiz follow
  
//********************************************************
// function MultipleChoice
//********************************************************

  function MultipleChoice (form, questionNumber, startingIndex)
  {
    //local variables
	  var k = 0 //index to loop through possible answers
	  var answered = "false"  //did the student answer the question
	  temp = new Array()
	  temp[0] = "A"
	  temp[1] = "B"
	  temp[2] = "C"
	  temp[3] = "D"
	  temp[4] = "E"
	  temp[5] = "F"
	  temp[6] = "G"
	  temp[7] = "H"
	  temp[8] = "I"
	  temp[9] = "J"
	  temp[10] = "K"
	  temp[11] = "L"
	  temp[12] = "M"
	  temp[13] = "N"
	  temp[14] = "O"
	  temp[15] = "P"
    //reinitialize the user's answers
	  gQuestionUserScore[questionNumber] = 0
	  gQuestionUserAnswer[questionNumber] = ""


    //calculate student's answers (find the radio button that was selected)
	  for (k = startingIndex; k < (startingIndex + gQuestionNumChoices[questionNumber]); k++)
	  {
	    if(form.elements[k].checked)
		{
		   gQuestionUserAnswer[questionNumber] = temp[k-startingIndex]
		   answered = "true"
		}
	  }

	  if(answered == "false")
	  {
	    gQuestionUserAnswer[questionNumber] = "not answered"
		gQuestionCorrect[questionNumber] = 0
	  }

	//grade student's answers
	  if (gQuestionUserAnswer[questionNumber] == gQuestionAnswer[questionNumber])
	  {
	    gQuestionUserScore[questionNumber] = gQuizScorePerQuestion
		gQuestionCorrect[questionNumber] = 1
	  }
	  else
	  {
	    gQuestionUserScore[questionNumber] = 0
		gQuestionCorrect[questionNumber] = 0
	  }

  }	  

//********************************************************
// function MultipleSelect
//********************************************************


  function MultipleSelect (form, questionNumber, startingIndex)
  {
    //local variables
	  var k = 0  //index to loop through possible answers
	  var m = 0  //index to loop through grading procedure
	  var scorePerPart
	  var localAnswers = ""
	  var localStudentAnswers = ""

	  temp = new Array()
	  temp[0] = "A"
	  temp[1] = "B"
	  temp[2] = "C"
	  temp[3] = "D"
	  temp[4] = "E"
	  temp[5] = "F"
	  temp[6] = "G"
	  temp[7] = "H"
	  temp[8] = "I"
	  temp[9] = "J"
	  temp[10] = "K"
	  temp[11] = "L"
	  temp[12] = "M"
	  temp[13] = "N"
	  temp[14] = "O"
	  temp[15] = "P"

    //reinitialize the user's answers
	  gQuestionUserScore[questionNumber] = 0
	  gQuestionUserAnswer[questionNumber] = ""


	//convert the correct answers to a TF string
	  for (k=0; k < gQuestionNumChoices[questionNumber]; k++)
	  {
	    if(gQuestionAnswer[questionNumber].charAt(m) == temp[k])
		{
		  localAnswers += "T"
		  m++
		}
		else
		{
		  localAnswers += "F"
		}
	  }

    //calculate student's answers (Check each checkbox)
	//calculate a TF string used for scoring and 
	//calculate a "abc" string for display to the user
	
	  for (k = startingIndex; k < (startingIndex + gQuestionNumChoices[questionNumber]); k++)
	  {
	    if(form.elements[k].checked)
		{
		   gQuestionUserAnswer[questionNumber] += temp[k-startingIndex]
           localStudentAnswers += "T"
		}
		else
		{
		  localStudentAnswers += "F"
		}
	  }
	  
    //check the whole answer if all right then mark as correct
	//else calculate partial credit
	if(localStudentAnswers == localAnswers)
	{
	  gQuestionUserScore[questionNumber] = gQuizScorePerQuestion
	  gQuestionCorrect[questionNumber] = 1
	}
	else
	{

      //Calculate the score for each part of the question that the user get right
	    scorePerPart = gQuizScorePerQuestion / gQuestionNumChoices[questionNumber]

	  //grade student's answers
	    for (m=0; m < gQuestionNumChoices[questionNumber]; m++)
	    {
	      if (localStudentAnswers.charAt(m) == localAnswers.charAt(m))
	      {
	        gQuestionUserScore[questionNumber] += scorePerPart
	      }
        }
	    if(gQuestionUserAnswer[questionNumber] == "") gQuestionUserAnswer[questionNumber] = "none selected" 
		gQuestionCorrect[questionNumber] = 0
	 }
  }

//********************************************************
// function Matching
//********************************************************

  
  function Matching (form, questionNumber, startingIndex)
  {
    //local variables
	  var k = 0  //index to loop through possible answers
	  var m = 0  //index to loop through grading procedure
	  var scorePerPart
	  var localStudentAnswers = ""

    //reinitialize the user's answers
	  gQuestionUserScore[questionNumber] = 0
	  gQuestionUserAnswer[questionNumber] = ""

    //calculate student's answers (Check each checkbox)
	//concatinate all the student's inputs into one string
	  for (k = startingIndex; k < (startingIndex + gQuestionNumChoices[questionNumber]); k++)
	  {
	    if(IsEmpty(form.elements[k].value))
		{
		  gQuestionUserAnswer[questionNumber] += "?"
		}
		else
		{
		  gQuestionUserAnswer[questionNumber] += form.elements[k].value.toUpperCase()
		}
	  }
   
   //check the whole answer if all right then mark as correct
	//else calculate partial credit
	if(gQuestionUserAnswer[questionNumber] == gQuestionAnswer[questionNumber])
	{
	  gQuestionUserScore[questionNumber] = gQuizScorePerQuestion
	  gQuestionCorrect[questionNumber] = 1
	}
	else
	{
      //Calculate the score for each part of the question that the user get right
	    scorePerPart = gQuizScorePerQuestion / gQuestionNumChoices[questionNumber]
 
	  //grade student's answers
	    for (m=0; m < gQuestionNumChoices[questionNumber]; m++)
	    {
	      if (gQuestionUserAnswer[questionNumber].charAt(m) == gQuestionAnswer[questionNumber].charAt(m))
	      {
	        gQuestionUserScore[questionNumber] += scorePerPart
	      }
        }
	  gQuestionCorrect[questionNumber] = 0
	}	
  }
  
//********************************************************
// function DisplayInlineOutput
//********************************************************

  function DisplayInlineOutput(index)
  {
    //local variables
      var resultStr  //used to store the html file
  
      resultStr = ""
	  
	  resultStr += "  Correct Answer: " + gQuestionAnswer[index] + "<br>"
	  if(gQuizDisplayFeedback != 0) resultStr += "  Feedback: " + gQuestionFeedback[index]
	   
	//write the output 
	  document.writeln( resultStr)
  }
  
//********************************************************
// function DisplayOutput
//********************************************************
  
  
  function DisplayOutput(form)
  {
    //local variables
      var i = 0       // index used to indicate the question number
      var resultStr  //used to store the html file
	  var spanID     //used to store a span ID
	  var divID      //used to store a div ID
  
      resultStr = ""
	  if(gQuizDisplayScore != 0)
	  {
	    resultStr += "Your score is " + Math.round(gQuizUserScore) + "%.\r\n"
	  }
	  
	//Loop through all the questions and store the results in a table
      for (i = 1; i <= gQuizNumQuestions; i++)
      {
		resultStr += "\r\nQuestion " + i + "\r\n"
        resultStr += "  Your Answer: " + gQuestionUserAnswer[i] + "\r\n"
		resultStr += "  Correct Answer: " + gQuestionAnswer[i] + "\r\n"
		if(gQuizDisplayFeedback != 0) resultStr += "  Feedback: " + gQuestionFeedback[i] + "\r\n"
		if(gQuizDisplayScore != 0)
		{
		  resultStr += "  Points: " + Math.round(gQuestionUserScore[i] * 10)/10 + "\r\n"
		}
	  }


	  
	//Loop through all the answers and highlite question numbers and show answers
	  for (i = 1; i <= gQuizNumQuestions; i++)
      {
	    //color the question number appropriately and display the feedback
		// and hide the inappropriate feedback
	    spanID = eval("feedback" + i)
	    if(gQuestionCorrect[i] == 1)
	    {
		  spanID.className = "correct"
		  divID = eval("q" + i + "cor")
		  divID.className = "on"
		  divID = eval("q" + i + "incor")
		  divID.className = "off"
		}
		else
		{
		  spanID.className = "incorrect"
		  divID = eval("q" + i + "cor")
		  divID.className = "off"
		  divID = eval("q" + i + "incor")
		  divID.className = "on"
		}
	    
	  }
  }
//********************************************************
// function CheckForm
//********************************************************
  
  function CheckForm (form) 
  {
    //local variables
      var i = 0 // index used to indicate the question number
	  var j = 0 // index used to indicate the current position in the list of controls in the Form
               // j=7 because FrontPage insists on moving the six hidden input fields to right
               // underneath the <form> tag, making the first answer index 6!
               // #@@!@@!### I hate FrontPage

    //reset the user's score
	  gQuizUserScore = 0

	//loop through each question
	  for(i = 1;i <= gQuizNumQuestions;i++)
	  {
	    // Determine the question type and call the appropriate function to grade it
          if (gQuestionType[i] == gMultipleChoice) MultipleChoice (form, i,j)
          if (gQuestionType[i] == gMultipleSelect) MultipleSelect (form, i,j)
          if (gQuestionType[i] == gMatching) Matching (form, i,j)
		  if (gQuestionType[i] == gTextEntry) TextEntry (form, i,j)
		  
        //update the user's score
          gQuizUserScore +=  gQuestionUserScore[i]

		//update the current position in the list of controls in the Form
		  j += gQuestionNumChoices[i]
	  }
	

    //Call the output function to display the results of the test
	  DisplayOutput(form)

	return false
  }
//********************************************************
// function ShowGenericFeedback
//********************************************************
    
  function ShowGenericFeedback (endAt)
  {
  	//Loop through all the spans to make visible
	  var i
	  var divID      //used to store a div ID
	    
	  for (i = 1; i <= endAt; i++)
      {
		  divID = eval("fb" + i)
		  divID.className = "on"
	  }
  }
//********************************************************
// function TextEntry
//********************************************************
 
  
  function TextEntry (form, questionNumber, startingIndex)
  {
    //reinitialize the user's answers
	  gQuestionUserScore[questionNumber] = 0
	  gQuestionUserScore[questionNumber] = ""

    //calculate student's answers (Check the text input)
	//concatinate all the student's inputs into one string

	  if(IsEmpty(form.elements[startingIndex].value))
      {
		gQuestionUserScore[questionNumber] = "not answered"
	  }
	  else
	  {
		gQuestionUserScore[questionNumber] = form.elements[startingIndex].value.toUpperCase()
	  }

   //check the answer if right then mark as correct
	if(gQuestionUserScore[questionNumber] == gQuestionAnswer[questionNumber])
	{
	  gQuestionUserScore[questionNumber] = gQuizScorePerQuestion
	  gQuestionCorrect[questionNumber] = 1
	}
	else
	{
	  gQuestionUserScore[questionNumber] += 0
	  gQuestionCorrect[questionNumber] = 0
	}	

  }
  