// Norton Quiz Machine Version 1.03
// Released 3/16/01
// Written by Cliff Landesman; modified by Yaching Yu and Cliff Landesman
// Revised by Cliff 7/18/01
// Revised by Cliff 9/24/01 Fixed 5.0 problem with line that strips out html for email. 
//  Improved function structure and removed a bug for Opera 5.12.

// The code assumes the following external files:
// quiz_demo.htm (which sets up the frames, contains all the quiz content and calls this script)
// upper.htm
// upper2.htm
// lower.htm
// confirm.htm (usually located in the directory of htm pages for the site)
// quiz_style.css
// ../images/begin_button.gif (called from lower.htm)
// ../images/quiz_title.gif
// ../images/send_quiz.gif
// ../images/next_question.gif

// General settings
// number_questions=parent.number_questions

first_try = new Array(number_questions)
alpha_count = new Array("a","b","c","d","e","f","g","h","i","j")
var my_correct = 0
var my_wrong = 0
var current_q = 0
var answ_html = ""

// Turn off random ordering of questions?
randomize_questions = "y";

// Randomize order of questions
randomize()

function randomize() {
 order = new Array(number_questions) 
 if (randomize_questions == "y") {
  pool = new Array(number_questions)
  no_remaining = number_questions

// Fill "pool"
  for (i=0; i<(number_questions); i++) {
   pool[i] = i+1
  }

// Set order of questions by filling "order"
  for (i=0; i<number_questions; i++) {

// Get random number
   rand = Math.random() * (no_remaining-1)
   rand = Math.round(rand)

// Pick from pool and assign to order
   order[i]=pool[rand]

// Repack pool, skipping rand pick, and decrement no_remaining
      for (j=0; j<(no_remaining-1); j++) {
    if (j>=rand) {
     pool[j]=pool[j+1]
    }
   }
   no_remaining=(no_remaining-1)
  }
 }

// Order questions without randomizing.
 else {
  for (i=0; i<number_questions; i++) {
   order[i]=i+1;
  }
 }
}

// upper2.htm calls this function. The lower frame, as rewritten by choices(), also calls it.
function write_upper_frame() {
 if (current_q<number_questions) {
  choices()
 }

// If no more questions, present email fields.
 else {
  email()
 }

// Make the lower frame blank.
 reset_lower()
}

// Write initial page.
// lower.htm calls this function.

function write_initial() {
 upper.document.open()
 upper.document.write(""+
 "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheets/quiz_styles.css\"></head><body>"+
 "<FORM ACTION=\"null\" NAME=how_many METHOD=POST>\n"+

"<TABLE WIDTH=100% BORDER=0 cellpadding=20>\n"+
 "<TR>\n"+
 "<TD valign=\"top\"><INPUT NAME=\"NoQuestions\" TYPE=\"HIDDEN\" VALUE=\"" + number_questions + "\">\n"+
 "<P class=\"text\">There are " + "<span class=\"qnum\">" + number_questions + "</span>\n"+
 " questions available for this quiz. Answer feedback is immediate and often includes an explanation as to why a given answer is correct. You may select only one answer per question.<br><br>Select the number of questions you would like: </TD></TR></TABLE>\n"+
 "<TABLE BORDER=0 cellpadding=0>\n"+
 "<TR>\n" +
 "<TD><IMG SRC=\"../images/spacer.gif\" width=30 height=1></td>\n"+
 "<TD><INPUT TYPE=radio NAME=how_many VALUE=\"5\" onClick=\"parent.number_questions=5\"></TD><TD class=\"ans\">5&nbsp;&nbsp;&nbsp;</TD>\n"+ "<TD><INPUT TYPE=radio NAME=how_many VALUE=\"10\" onClick=\"parent.number_questions=10\"></TD><TD class=\"ans\">10&nbsp;&nbsp;&nbsp;</TD>\n"+
 "</TR>\n"+
 "</TABLE>\n"+
 "</FORM>"+
 "</body></html>")
 upper.document.close()
}

// Present multiple choice questions
function choices() {

// Write top of page
 upper.document.open()
 upper.document.write("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheets/quiz_styles.css\"></head><body><FORM ACTION=\"null\" NAME=which_choices METHOD=POST target=\"_top\">\n"+
 "<BR>")

// Loop through questions
 upper.document.write(""+
 "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=2>")
 shown_no=current_q+1

// Question
 upper.document.write(""+
 "<TR>"+
 "<TD VALIGN=\"top\" width=10% align=right class=\"qnum\">"+
 shown_no + ".</TD> \n"+
 "<TD COLSPAN=3 width=90% class=\"ques\">")

 question="q" + order[current_q]
 question=eval(question)
 upper.document.write(question)

 upper.document.write(""+
 "<BR><BR></TD>\n"+
 "</TR>\n")

// Loop through answers
 for (j=0; j<(number_answers[order[current_q]-1]); j++) {
  upper.document.write(""+
  "<TR>\n"+
  "<TD width=10%>&nbsp;</TD>\n"+
  "<TD VALIGN=\"top\" width=2% class=\"ans\">"+
  "<INPUT TYPE=\"radio\" NAME=\"quest" + shown_no + "\" ONCLICK=\"parent.score(" + 
  shown_no + "," + order[current_q] + "," + j + ")\"></TD>"+
  "<TD VALIGN=\"top\"><B>"+alpha_count[j]+")</B></td><td width=95% class=\"ans\">")
  answer="q" + order[current_q] + alpha_count[j]
  answer=eval(answer)
  upper.document.write(answer)
  upper.document.write(""+
  "</TD></TR>\n")
 }

// Bottom of document
 upper.document.write(""+
 "</TABLE></FORM></BODY></HTML>\n")
 upper.document.close()
}

function email() {

 my_score = (my_correct/number_questions)*100
 my_score = Math.round(my_score);
 upper.document.open()
 upper.document.write(""+
 "<HTML><HEAD><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheets/quiz_styles.css\"></head><body><FORM ACTION=\"http://www.wwnorton.com/scripts/mailer_d.asp\" NAME=form1 METHOD=POST target=\"_top\">"+
 "<INPUT TYPE=\"HIDDEN\" NAME=\"final_page\" VALUE=\"" + confirm + "\">" +
 "<INPUT TYPE=\"HIDDEN\" NAME=\"quiz_name\" VALUE=\"" + quiz_name + "\">"+
 "<INPUT TYPE=\"HIDDEN\" NAME=\"answ\" VALUE=\"\">"+
 "<INPUT TYPE=\"HIDDEN\" NAME=\"my_score\" VALUE=\"\">")
 upper.document.write(""+
 "<BR><TABLE ALIGN=CENTER BORDER=0 CELLSPACING=0 CELLPADDING=2>"+
 "<TR><TD colspan=2 class=\"email\">"+
 "Your Score: "+ my_score + "%  <A HREF=javascript:parent.show_answers()>Your Answers</A><BR><BR></TD></TR>" +
 "<TR><TD class=\"email\">Student's Name: </td><td><INPUT TYPE=TEXT NAME=stu_name size=30></td></tr>"+
 "<tr><td class=\"email\">Student Email: </td><td><INPUT TYPE=TEXT NAME=stu_email size=30></td></tr>"+
 "<tr><td class=\"email\">Professor's Email: </td><td><INPUT TYPE=TEXT NAME=prof_email size=30></td></tr>"+
 "<tr align=\"center\"><td colspan=2><br><br><a href=javascript:parent.error_check()><img src=\"../images/send_quiz.gif\" border=\"0\"></a></td></tr>"+
 "<tr><td>&nbsp;</td></tr>"+
 "</TABLE></FORM></BODY></HTML>")

 upper.document.close()
 long_results()
 upper.document.form1.stu_name.focus();
}

function show_answers() {
 resultsWindow=window.open("","resultsWindow","width=400,height=400,scrollbars=yes,resizable=yes")
 resultsWindow.document.write(""+
 "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheets/quiz_styles.css\"></head>"+
 "<body><p class=showansbd>Results:</p>" + 
 "<P class=showans>Total number of questions: " + parent.number_questions +
 "<BR>Percent correct: " + my_score + "%" +
 answ_html + "<BR><BR><div align=\"center\"><INPUT TYPE=button onClick=\"window.close()\" VALUE=\"Close\"></div></body>")
}

// Score
function score(shown_no, question_no, answer_no) {
 correct = "q" + question_no + "answer"
 correct = eval(correct)
 choice = alpha_count[answer_no]
 review = "q" + question_no + "review"
 review = eval(review)
 repeat = "repeat = first_try[" + question_no + "]"
 repeat = eval(repeat)
 lower.document.open()
 lower.document.write(""+
 "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheets/quiz_styles.css\"></head><body><TABLE WIDTH=100% border=0 CELLPADDING=2>")

// Student gets only one try
 if (repeat || repeat == 0) {
  lower.document.write(""+
  "<TR><TD width=10%>&nbsp;</TD><TD valign=top class=\"guessing\">Sorry, no second guessing.</TD><TD align=right valign=top><A HREF=javascript:parent.write_upper_frame()><img src=\"../images/next_question.gif\" border=0></a><br></TD></TR>")

// Lock radio button so it always shows first asnwer
  switch_radio = "upper.document.which_choices.quest" + shown_no + "[" + repeat + "].checked =true";
  eval(switch_radio)
 }
 else {
  if (correct==choice) {
   lower.document.write(""+
   "<TR><TD width=10%>&nbsp;</TD><TD valign=top><span class=\"correct\">"+
   "<b>Correct!</b> </span><br><span class=\"review\">" + review + "</span></TD></TR>"+
   "<TR><TD width=10%>&nbsp;</TD><TD align=right valign=top><A HREF=javascript:parent.write_upper_frame()><img src=\"../images/next_question.gif\" border=0></a></TD></TR>")

// Update student's score
   my_correct++

// We record first try answer for locking radio button
   first_try[question_no] = answer_no
  }
  else {
   lower.document.write(""+
   "<TR><TD width=10%>&nbsp;</TD><TD valign=top>"+
  "<span class=\"incorrect\">Sorry, but the correct answer is (<b>" + correct + "</b>). </span><br><span class=\"review\">"+ review + "<span></TD></TR>"+
   "<TR><TD width=10%>&nbsp;</TD><TD align=right valign=top><A HREF=javascript:parent.write_upper_frame()><img src=\"../images/next_question.gif\" border=0></a></TD></TR>")

// Update student's score
   my_wrong++

// We record first try answer for locking radio button
   first_try[question_no] = answer_no
  }
  current_q++
 }
 lower.document.write(""+
 "</TABLE></body></html>")
 lower.document.close()
}

function long_results() {

// Include answers.
 for(i=0; i<number_questions; i++) {
  ques = "q" + order[i]
  ques = eval(ques)

// Test for an answer
  if (first_try[order[i]]||first_try[order[i]]==0) {
   letter = alpha_count[first_try[order[i]]]
   ans = "q" + order[i] + letter
   ans = eval(ans)
   letter = letter + ") "
  }
  else {
   letter="No Answer"
   ans= " "
  }

  correct_ans = "q" + order[i] + "answer"
  correct_ans = eval(correct_ans)
  correct_ans_words = "q" + order[i] + correct_ans
  correct_ans_words = eval(correct_ans_words)

// Create html version of results for presenting to browser.
  answ_html = answ_html + "\n <p class=\"showans\"><span class=\"showansbd\">Question: </span>" + ques 
  answ_html = answ_html + "\n<BR><span class=\"showansbd\">Student answered: </span>" + letter + ans 
  answ_html = answ_html + "\n<BR><span class=\"showansbd\">Correct answer: </span>" + correct_ans + "\) " + correct_ans_words + "\n"

// Create the email version for sending out.
  upper.document.form1.answ.value=upper.document.form1.answ.value + "\nQuestion: " + ques 
  upper.document.form1.answ.value=upper.document.form1.answ.value + "\nStudent answered: " + letter + ans
  upper.document.form1.answ.value=upper.document.form1.answ.value + "\nCorrect answer is: " + correct_ans + "\) " + correct_ans_words + "\n"
 }
 upper.document.form1.my_score.value=my_score

// Delete all html tags when writing answers for email.
// We can't use ? for nongreedy pattern matching because it trips up IE 5.0.
// We search for > and < instead.

 if(window.RegExp) {
  upper.document.form1.answ.value=upper.document.form1.answ.value.replace(/\<[^<]*\>([^<>]*)\<[^>]*\>/g, "$1") 
 }

 upper.document.form1.answ.value=upper.document.form1.answ.value + "\n"
}

// Check that student has entered addresses for student and professor.
// Then prepare to email.
function error_check(){
 if(upper.document.form1.stu_name.value != '') {
  if(upper.document.form1.stu_email.value != '') {
   if(upper.document.form1.prof_email.value != '') {

// Email Header
    upper.document.form1.answ.value="\nPercent correct: " + my_score + "%\n\n" + upper.document.form1.answ.value
    upper.document.form1.answ.value="\nNumber of questions: " + parent.number_questions + upper.document.form1.answ.value
    upper.document.form1.answ.value="\nStudent's email: " + upper.document.form1.stu_email.value + upper.document.form1.answ.value
    upper.document.form1.answ.value="\nStudent's name: " + upper.document.form1.stu_name.value + upper.document.form1.answ.value
    upper.document.form1.answ.value="\nQuiz name: " + quiz_name + upper.document.form1.answ.value
    upper.document.form1.submit()
   }
   else {
    lower.document.open()
    lower.document.write("<head><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheets/quiz_styles.css\"></head><body><div align=\"center\"><p class=\"error\">Error: <span class=\"text\">You are required to enter your professors\'s email addresses.</span></p></div></body>")
    lower.document.close()
   }
  }
  else {
   lower.document.open()
   lower.document.write("<head><link rel=\"stylesheet\" type=\"text/css\" href=../stylesheets/quiz_styles.css\"></head><body><div align=\"center\"><p class=\"error\">Error: <span class=\"text\">You are required to enter your email addresses.</span></p></div></body>")
   lower.document.close()
  }
 }
 else {
  lower.document.open()
  lower.document.write("<head><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheets/quiz_styles.css\"></head><body><div align=\"center\"><p class=\"error\">Error: <span class=\"text\">You are required to enter your name or student ID #.</span></p></div></body>")
  lower.document.close()
 }
}

function reset_lower() {
 lower.document.open()
 lower.document.write("<head><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheets/quiz_styles.css\"></head><body></body>")
 my_score = (my_correct/number_questions)*100
 my_score = Math.round(my_score)
 lower.document.close()
}
