/**
 * Norton CSS quiz engine. 
 * 
 * Required files for it to work:
 *  1. ##.htm (eg. 04.htm) - Where questions are displayed.
 *  2. q##.js (eg. q04.js) - Where questions are stored.
 *  3. quiz.js (this file) - Engine for display.
 *  4. quiz_styles.css 
 *
 * Settings (optional):
 *
 *   HISTOGRAM = [true | false]  //false by default
 *   SECTIONS = [true | false]   //true by default (only used if HISTOGRAM true)
 *   TYPES = [true | false]
 *   randomize_questions = [true | false]  //false by default
 *     - For all the above vars: Accept the defaults, set in this quiz.js file, or
 *       override the default via setting them in a questions file (eg. q04.js). 
 *       NOTE: HISTOGRAM can have one, both or no SECTIONS or TYPES. 
 *
 * Settings (required):
 *   DEBUG = true | false  //false by default
 *     set DEBUG in this quiz.js (~line 79 below) to see alerts as you take a quiz.
 *
 * @author: Joe Lucca (joe@joepage.com)
 *
 * Updates log:
 * 11/15/06 (AGonzalez)  line 473, moved from hidden div to show div the hidden variables becuase these weren't being submitted
						Added Alt-Tags on images
						Changed the order of email elements and added chapter No
						Fixed gradebook url and added scrollbars and addressbar 
 * 020607 (JLucca) Line 391, email() function: Added window.resize to shrink window for quizzes not using HISTOGRAMS.
 						Avoids giant whitespace at top of non-histogram quizzes.
 */


///////////////////////////////////////////////////
// Begin .js to prevent right-clicking/view source
///////////////////////////////////////////////////
var message = "Function Disabled!";

function clickIE4(){
    if (event.button==2){
        alert(message);
        return false;
    }
}

function clickNS4(e){
    if (document.layers || document.getElementById && !document.all){
        if (e.which == 2 || e.which == 3){
            alert(message);
            return false;
        }
    }
}

if (document.layers){
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown = clickNS4();
}
else if (document.all && !document.getElementById){
    document.onmousedown = clickIE4();
}

//debug alerts when right mouse clicked
//document.oncontextmenu = new Function("alert(message);return false")

document.oncontextmenu = new Function("return false")
///////////////////////////////////////////////////
// End .js to prevent right-clicking/view source
///////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////
// Begin Quiz Engine 
//////////////////////////////////////////////////////////////////////////////

// Debug flag turns on key alerts to see what script does as you take a quiz
var DEBUG = false;

//////////////////////////////////////////////////////////////////////////////
// Does this quiz use histograms?
//      Can set the default via the SECOND line below. Leave the first alone.
//      Can override per quiz by setting HISTOGRAM=false; in any q##.js page.
//////////////////////////////////////////////////////////////////////////////
var HISTOGRAM;	//Do Not Change this line...ever.

//if HISTOGRAM variable is not set in questions file (q##.js), uses this default
if (HISTOGRAM == undefined) HISTOGRAM = false; 


//////////////////////////////////////////////////////////////////////////////
// Histograms have sections and/or types.
//      Default sets both true.
//      Can override per quiz by setting SECTION or TYPE=false in any chXX.htm.
//      If both are false, you should just set HISTOGRAM=false instead.
//////////////////////////////////////////////////////////////////////////////
var SECTIONS, TYPES;
if (SECTIONS == undefined) SECTIONS = true;
if (TYPES == undefined) TYPES = true;

var section_correct = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var section_answered = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var type_correct = new Array(0, 0, 0, 0, 0);
var type_answered = new Array(0, 0, 0, 0, 0);


//////////////////////////////////////////////////////////////////////////////
// General Gradebook settings 
//////////////////////////////////////////////////////////////////////////////
var my_score = 0; 	//% score
var my_correct = 0; 	//# correct
var my_wrong = 0; 	//# wrong
var objOnPage = null;
var q_index = 0;
var answ_html = "";
var questions = new Array(number_questions);
var alpha_count = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m");
var CSS_Path = "quiz_styles.css"
var Img_Path = "images";

// Used for score 
var responses = new Array();		//Colon delimited for uploading to db
var alphaResponses = new Array();	//Letter responses for each question

//////////////////////////////////////////////////////////////////////////////
// Default ordering of questions is random (false).
//    Can override for any quiz by setting individually in each q##.js file
//////////////////////////////////////////////////////////////////////////////
var randomize_questions;
if (randomize_questions == undefined) randomize_questions = false;
randomize();

//Debug
if (DEBUG)
{
    var debugMsg = "DEBUG is on. Turn off in quiz.js by setting DEBUG=false\n";
    debugMsg += "HISTOGRAM: " + HISTOGRAM + "\n";
    debugMsg += "randomize_questions: " + randomize_questions + "\n";
    debugMsg += "SECTIONS: " + SECTIONS + "\n";
    debugMsg += "TYPES: " + TYPES + "\n";

    alert(debugMsg);
}

// Calls function to print intro text
setTimeout('writeIntro()', 50);

/**
 * Writes intro text, number of questions. 
 * Allows student to select number of questions. 
 */
function writeIntro()
{
    // Intro text, num questions
    var str = '';
    str += '<html><head>';
    str += '<link rel="stylesheet" type="text/css" href="' + CSS_Path + '">';
    str += '</head><body>';
    str += '<form action="null" name="how_many" method="POST">\n';
    str += '<table border="0" cellpadding="10">\n';
    str += '<tr>\n';
    str += '<td valign="top">';
    str += '<input name="NoQuestions" type="hidden" value="';
    str += number_questions + '">\n';
    str += '<p class="text">There are <span class="qnum">';
    str += number_questions;
    str += '</span>\n';
    str += ' questions available for this quiz.<br><br>Answer feedback is ';
    str += 'immediate and often includes an explanation as to why a given answer ';
    str += 'is correct. You may select only one answer per question.<br><br>At ';
    str += 'the end of the quiz you may use the ';
    str += '<a href="#" onClick="javascript:popGradebook();return false;">Norton Gradebook</a> ';
    str += 'to share your results with your professor and track your progress. ';
    str += '<br><br>Select the number of questions you would like: </td></tr></table>\n';
    str += '<table border="0" cellpadding="0">\n';
    str += '<tr><td width="10">&nbsp;</td>\n';

    // Let student select number of questions. We don't offer more than are available.
    for (var i = 1; i * 5 < number_questions; i++)
    {
        str += '<td><input type="radio" name="how_many" value="' + (i * 5) + '"';
        str += ' onClick="parent.number_questions=' + (i * 5) + '">';
        str += '</td><td class="ans">' + (i * 5) + '&nbsp;</td>\n';
    }

    // We do offer the maximum number available.
    str += '<td><input type="radio" name="how_many" value="';
    str += number_questions;
    str += '" onClick="parent.number_questions=' + number_questions + '">';
    str += '</td><td class="ans">';
    str += number_questions;
    str += '&nbsp;</td></tr>\n';
    str += '</table>\n';
    str += '</form>';
    str += '</body></html>';

    //Show intro text
    document.getElementById('qDiv').innerHTML = str;

    show('qDiv');
}

/**
 * Called when user clicks Begin Quiz button
 */
function beginQuiz(buttonID)
{
    hide(buttonID);
    writeQuestion();
}

/**
 * Formats and displays multiple choice question to the user.
 * When user clicks a radio button, calls score(question_no, alpha_letter)
 */
function writeQuestion()
{
    var str = '';
    str += '<form onsubmit="return false;" name="which_choices" method="post" action="">';
    str += '<table width="95%" border="0" cellspacing="0" cellpadding="2">';
    str += '<tr>';
    str += '<td width="8%" valign="top" align="right" class="qnum">';

    // Question Number
    var qNumberActual = questions[q_index];
    var qNumberToDisplay = q_index + 1;

    str += qNumberToDisplay + '.';
    str += '</td>';
    str += '<td colspan="3" class="ques">&nbsp;';

    // Question
    str += eval("q" + qNumberActual);
    str += '</td>';
    str += '</tr>';
    str += '<tr><td colspan="4">&nbsp;</td></tr>';
    str += '<tr>';

    // Answer Choices for this qNumberActual
    var answerChoices = number_answers[qNumberActual - 1];

    // Display radio, alphabet letter) and question answer choice text for Q
    for (var x = 0; x < answerChoices; x++)
    {
        str += '<td width="8%">&nbsp;';
        str += '</td>';
        str += '<td width="2%" valign="top" class="ans">';
        //Radio buttons
        str += '<input type="radio" name="qName" onclick="score(';
        str += qNumberActual + ', \'' + alpha_count[x] + '\')"/>';
        str += '</td>';
        str += '<td width="2%" valign="top" class="ans">';
        str += '<b>' + alpha_count[x] + ')</b>';
        //Letter selection
        str += '</td>';
        str += '<td valign="top" class="ans">';
        str += eval("q" + qNumberActual + alpha_count[x]);
        //Answer choice text
        str += '</td>';
        str += '</tr>';
    }

    str += '</table>';
    str += '</form>';

    document.getElementById('qDiv').innerHTML = str;

    show('qDiv');
}

/**
 * Update cululative scores and counters (my_correct, my_wrong, q_index) 
 * For a HISTOGRAM section/type arrays updated (section_correct, type_correct)
 * After scores are updated, calls writeAnswer(question_no, choice)
 * @params question_no Actual hard-wired question number.
 * @params choice User's answer choice number that maps to a letter in alphaResponses[]
 */
function score(question_no, choice)
{
    // Get correct answer
    var answer = eval("q" + question_no + "answer");

    // Update histogram main counter arrays
    if (HISTOGRAM)
    {
        // sections that have been answered
        var section_number = eval("q" + question_no + "section");
        section_answered[section_number] = section_answered[section_number] + 1;

        // types that have been answered
        var type_number = eval("q" + question_no + "type");
        type_answered[type_number] = type_answered[type_number] + 1;
    }

    // Score for uploading to gradebook database
    // JWL 022406 fixed: Add next response to ordered array. Thanks Alex :)
    responses[responses.length] = parent.book_no + ":" + parent.chapter_no + ":" + question_no + ":" + choice + ":" + answer;
    
    //Debug
//    for (var z=0; z < responses.length; z++) {
//		alert("responses[" + z + "] " + responses[z]);
//	}
    
    // Record user's answer for each question
    alphaResponses[question_no] = choice;

    // Tell them if they got the right answer
    if (answer == choice)
    {
        // Update student's total correct score.
        my_correct++;

        if (HISTOGRAM)
        {
            // Update histogram 'correct answer' counter arrays
            // that record correct section and type answers
            section_correct[section_number] = section_correct[section_number] + 1;
            type_correct[type_number] = type_correct[type_number] + 1;
        }
    }
    else
    {
        // Update student's total wrong score
        my_wrong++;
    }

    // Next question counter
    q_index++;

    //Debug
    if (DEBUG)
    {   	
        alert("correct: " + my_correct + "\n" 
        + "wrong: " + my_wrong + "\n" 
		+ "actual question_no: " + question_no + "\n" 
		+ "answer choice: " + choice);         
    }

    // Display answer to user
    writeAnswer(question_no, choice);
}

/**
 * Formats and displays answer to the user. 
 * Creates a link for user to click that calls nextQuestion(q_index)
 */
function writeAnswer(question_no, choice)
{
    disableRadios();

    var str = '<table width="100%" border="0" cellspacing="0" cellpadding="2">';
    str += '<tr>';
    str += '<td width="8%">&nbsp;</td>';
    str += '<td>';

    // Get correct answer
    var answer = eval("q" + question_no + "answer");

    // Tell them if they got the right answer
    if (answer == choice)
    {
        str += '<span class="correct">Correct!</span><br>';
    }
    else
    {
        str += '<span class="incorrect"> Sorry, but the correct answer is (<b>' + answer + '</b>).</span><br>';
    }

    // Add any review
    str += '<span class="review">' + eval("q" + question_no + "review") + '</span>';

    str += '</td>';
    str += '<td align="right">';
    //Next question button
    str += '<a href="javascript: nextQuestion(' + q_index + ')"><img  alt=\"Next question\" src="' + Img_Path + '/next_question.gif" border = "0"></a>';
    str += '</td>';
    str += '</tr>';
    str += '</table>';

    // Show answer
    document.getElementById('ansDiv').innerHTML = str;
    show('ansDiv');
    
    
}

/** 
 * Determines if are more questions 
 * or if it's time to wrap up and present email fields. 
 * Called each time student clicks 'next question'
 */
function nextQuestion()
{
    if (q_index < number_questions)
    {
        hide('ansDiv');
        writeQuestion(questions[q_index]);
    }
    else
    {
        // If no more questions, hide all divs & present email fields.
        hide('qDiv');
        hide('ansDiv');
        email();
    }
}

/**
 * Last question has been answered. Displays final score screen with email info.
 * HISTOGRAM quizzes true show histograms. 
 * All results posted to /college/nrl/gradebook/post.asp
 */
function email()
{
    my_score = (my_correct / parent.number_questions) * 100
    my_score = Math.round(my_score);

    // Hidden form variables
    var hidText = '<html><head>';
    hidText += '<link rel="stylesheet" type="text/css" href="';
    hidText += CSS_Path + '">';
    hidText += '</head><body>';
    hidText += '<form action="http://www.wwnorton.com/college/nrl/gradebook/post.asp" ';
    hidText += 'name="form1" id="form1" method="post">';
    hidText += '<input type="hidden" name="final_page" id="final_page" value="';
    hidText += parent.confirm + '">';
    hidText += '<input type="hidden" name="book_title" id="book_title" value="';
    hidText += parent.book_title + '">';
    hidText += '<input type="hidden" name="book_no" id="book_no" value="';
    hidText += parent.book_no + '">';
    hidText += '<input type="hidden" name="chapter_no" id="chapter_no" value="';
    hidText += parent.chapter_no + '">';
    hidText += '<input type="hidden" name="answ" id="answ" value="">';
    hidText += '<input type="hidden" name="responses" id="responses" value="">';
    hidText += '<input type="hidden" name="number_questions" id="number_questions" value="';
    hidText += parent.number_questions + '">';
    hidText += '<input type="hidden" name="my_score" id="my_score" value="">';
    hidText += '<input type="hidden" name="post_responses" id="post_responses" value="true">';
    hidText += '<input type="hidden" name="email_responses" id="email_responses" value="false">';
    
    // Final text and email address text fields
    var shoText = '<br><table width="95%" align="center" border="0" cellspacing="7" cellpadding="0">';
    shoText += '<tr>';
    shoText += '<td colspan="3" class="email">';
    shoText += 'Your Score: ' + my_score + '% &nbsp; &nbsp;';
    shoText += '<a href="#" onClick="showAllAnswers();return false;">';
    shoText += 'Your Answers</a><br><br>';
    shoText += '</td>';
    shoText += '</tr>';
    shoText += '<td colspan="3">Complete this form to share your results ';
    shoText += 'with your professor and track your progress. We will post ';
    shoText += 'to the <a href="#" onClick="popGradebook();return false;">';
    shoText += 'Norton Gradebook</a> ';
    shoText += 'and email you a copy of the scored quiz.';
    shoText += '</td>';
    shoText += '</tr>';
    shoText += '<tr>';
    shoText += '<td class="email" width="30%">First Name: </td>';
    shoText += '<td><input type="text" name="first_name" size="30" id="first_name"></td>';
    shoText += '<td>&nbsp;</td>';
    shoText += '</tr>';
    shoText += '<tr>';
    shoText += '<td class="email" width="30%">Last Name: </td>';
    shoText += '<td><input type="text" name="last_name" size="30" id="last_name"></td>';
    shoText += '<td>&nbsp;</td>';
    shoText += '</tr>';
    shoText += '<tr>';
    shoText += '<td class="email" width="30%">Your Email: </td>';
    shoText += '<td><input type="text" name="stu_email" size="30" id="stu_email"></td>';
    shoText += '<td>&nbsp;</td>';
    shoText += '</tr>';
    shoText += '<tr>';
    shoText += '<td class="email" width="30%">Your Professor\'s Email: </td>';
    shoText += '<td><input type="text" name="prof_email" size="30" id="prof_email"></td>';
    shoText += '<td>&nbsp;</td>';
    shoText += '</tr>';
    shoText += '<tr>';
    shoText += '<td class="email" width="30%">Section: </td>';
    shoText += '<td><input type="text" name="section" size="30" id="section"></td>';
    shoText += '<td align="center">';
    shoText += '<a href="#" onClick="errorCheck();return false;">';
    shoText += '<img src="' + Img_Path + '/send_quiz.gif" alt=\"Send Quiz\" border="0" ';
    shoText += 'alt="Send Quiz"></a>';
    shoText += '</td>';
    shoText += '</tr>';
    shoText += '</table></form>';
    shoText += '</body></html>';
    
   if (HISTOGRAM)
   {
      // Show histograms for this quiz
      histograms(); 
   }
   else {
      //Hide the question/histogram row of the quiz table.
      document.getElementById('question').style.display = 'none';
      //window.resizeTo(600,380);
   }   

    document.getElementById('ansDiv').innerHTML = hidText + shoText;
    show('ansDiv');

    // Compile long results (transcript) of student answers, correct answers.
    // Used for email version to be sent out and html transcript display
    longResults();

    document.getElementById('first_name').focus();
}

/**
 * Compile long results (transcript) of student answers and correct answers.
 * Called from the email() function.
 * Prepares two versions:
 *  1) html version for browser display when user clicks transcript.
 *     html version is displayed from show_answers() function.
 *  2) email version 
 */
function longResults()
{
    // Include answers.
    for (var i = 0; i < parent.number_questions; i++)
    {
        var ques = eval("q" + questions[i]);
        var letter = alphaResponses[questions[i]];

        //Debug
        //if (DEBUG) alert("letter: " + letter);

        var ans = eval("q" + questions[i] + letter);
        var correct_ans = eval("q" + questions[i] + "answer");
        var correct_ans_words = eval("q" + questions[i] + correct_ans);

        // Create html version of results for presenting to browser.
        answ_html += '\n <p class="showans"><span class="showansbd">Question: </span>' + ques;
        answ_html += '\n<br><span class="showansbd">Student answered: </span>' + letter + ') ' + ans;
        answ_html += '\n<br><span class="showansbd">Correct answer: </span>' + correct_ans + ') ' + correct_ans_words + '\n';

        // Create the email version for sending out.
        document.getElementById('answ').value += "\nQuestion: " + ques;
        document.getElementById('answ').value += "\nStudent answered: " + letter + ') ' + ans;
        document.getElementById('answ').value += "\nCorrect answer is: " + correct_ans + "\) " + correct_ans_words + "\n";
    }

    document.getElementById('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)
    {
        document.getElementById('answ').value = document.getElementById('answ').value.replace(/\<[^<]*\>([^<>]*)\<[^>]*\>/g, "$1");
    }

    document.getElementById('answ').value += '\n';

    // GRADEBOOK: Update responses for uploading to database.
    document.getElementById('responses').value = responses;

    //Debug
    if (DEBUG)
    {
        //alert('document.getElementById(answ).value: ' + document.getElementById('answ').value + '\n\n');
        //alert('document.getElementById(my_score).value = my_score: ' + document.getElementById('my_score').value + '\n\n');
        //alert('document.getElementById(responses).value: ' + document.getElementById('responses').value + '\n\n');
        alert('answ_html: ' + answ_html + '\n\n');
    }
}

/**
 * Called when user clicks 'Your Answers' on last page of quiz.
 * Called from links created in email() function.
 */
function showAllAnswers()
{
    var winOptions = 'width=400,height=400,scrollbars=yes,resizable=yes';
    var resultsWindow = window.open('', 'resultsWindow', winOptions);

    var shoText = '<head><link rel="stylesheet" type="text/css" ';
    shoText += 'href=' + CSS_Path + '></head>';
    shoText += '<body><p class="showansbd">Results:</p>';
    shoText += '<p class=showans>Total number of questions: ';
    shoText += parent.number_questions;
    shoText += '<br>Percent correct: ';
    shoText += my_score + '% ';
    shoText += answ_html;
    shoText += '<br><br><div align="center">';
    shoText += '<input type="button" onClick="window.close();return false;" ';
    shoText += 'value="Close"></div></body>';

    resultsWindow.document.write(shoText);
}

/**
 * Used for linking to the Gradebook.
 */
function popGradebook()
{
    var winOptions = 'width=800,height=600,status=yes,scrollbars=yes,toolbar=yes,resizable=yes';
    var destination = 'http://www.wwnorton.com/college/nrl/gradebook/';
    var win = window.open(destination, "Gradebook", winOptions);
    win.focus();
}

/** 
 * Checks that student has filled required fields. Called from email()
 * Then prepare to email.
 */
function errorCheck()
{
    var error_msg = "-1"
    var error_code = - 1

    // GRADEBOOK: We alway send to gradebook, but never to professor.
    var pass_checkbox = 1

    // These 2 lines were commented out in the original quiz_gradebook.js.
    // if(upper.document.form1.email_responses.checked) {pass_checkbox=1}
    // if(upper.document.form1.post_responses.checked) {pass_checkbox=1}

    // Get student name
    var stu_name = document.getElementById('first_name').value + " ";
    stu_name += document.getElementById('last_name').value;

    while (error_code == - 1)
    {
        if (document.getElementById('first_name').value == '')
        {
            // GRADEBOOK: Error with first name
            error_code = 1
            error_msg = "You are required to enter your first name."
            break;
        }

        if (document.getElementById('last_name').value == '')
        {
            // GRADEBOOK: Error with last name
            error_code = 2
            error_msg = "You are required to enter your last name."
            break;
        }

        if (document.getElementById('stu_email').value == '')
        {
            // BOTH: Error with student email
            error_code = 3
            error_msg = "You are required to enter your email addresses."
            break;
        }

        if (document.getElementById('prof_email').value == '')
        {
            // BOTH: Error with prof. email
            error_code = 4
            error_msg = "You are required to enter your professors\'s email addresses."
            break;
        }

        if (pass_checkbox == 0)
        {
            // GRADEBOOK: Student didn't check either the email or the gradebook checkbox
            error_code = 5
            error_msg = "You are required to check one of the checkboxes."
            break;
        }

        // If we got here, no errors
        error_code = 0
    }

    // Display outcome
    if (error_code == 0)
    {
        // No errors, email header

//AGonzalez (11-15-06)  Changed the order of these elements and added chapter Number
        var ansElement = "Quiz name: " + parent.book_title;

        ansElement += "\nChapter Number: " + parent.chapter_no;
		
        ansElement += "\nStudent's name: " + stu_name;

        ansElement += "\nStudent's email: " + document.getElementById('stu_email').value;

        ansElement += "\nNumber of questions: " + parent.number_questions;

        ansElement += "\nPercent correct: " + my_score + "%\n\n";

        document.getElementById('answ').value = ansElement + document.getElementById('answ').value;

        //Debug
        if (DEBUG)
        {
            alert("answ value to be submitted to gradebook: " + document.getElementById('answ').value);
        }

        document.getElementById('form1').submit();
    }
    else
    {
        // BOTH: Display error message in lower frame
        var shoText = '<head><link rel="stylesheet" type="text/css" href="';
        shoText += CSS_Path + '"></head><body><div align="center">';
        shoText += '<p class="error">Error: <span class="text">';
        shoText += error_msg + '</span></p></div></body>';

        document.getElementById('errDiv').innerHTML = shoText;
        show('errDiv');
    }
}

/**
 * Displays histograms.
 * After last question answered the email() function calls histograms().
 */
function histograms()
{
    var labels = new Array();

    // Upper case alphabetic labels converted from our alpha_count array
    for (var x = 0; x < alpha_count.length; x++)
    {
        labels[x + 1] = new String(alpha_count[x].toUpperCase());
    }

    //Debug
    //alert(labels.toString());

    var shoText = '<center><span class="qnum">Your Total Score: ' + my_score + '%</center></span>';
    shoText += '<br><br>';

    ///////////////////////////////////////////////////////////////////////////
    // We print the headers.
    ///////////////////////////////////////////////////////////////////////////

    shoText += '<table border=0 cellpadding=7>';
    //start table 1.0
    shoText += '<tr>';

    //Sections
    if (SECTIONS)
    {
        shoText += '<td bgcolor="#efefef" class="correct"><b>Sections:</b></td>';
    }

    //Types
    if (TYPES)
    {
        shoText += '<td bgcolor="#efefef" class="correct"><b>Types:</b></td>';
    }

    shoText += '</tr>';

    ///////////////////////////////////////////////////////////////////////////
    // We print the Section titles and data.
    ///////////////////////////////////////////////////////////////////////////
    shoText += '<tr><td valign=top>';

    if (SECTIONS)
    {
        shoText += '<table border=0>';
        //start table 1.1

        for (i = 1; i < section_title.length; i++)
        {
            shoText += '<tr><td class="correct">' + labels[i] + ')' + '</td>';
            shoText += '<td class="correct">' + parent.section_title[i] + '</td>';
            shoText += '<td>&nbsp</td>';
            shoText += '<td class="correct">' + section_correct[i];
            shoText += ' out of ' + section_answered[i] + '</td>';
            shoText += '</tr>';
        }

        shoText += '</table>';
        //end table 1.1
    }

    shoText += '</td>';

    ///////////////////////////////////////////////////////////////////////////
    // We print the Type titles and data.
    ///////////////////////////////////////////////////////////////////////////
    shoText += '<td valign=top class="correct">';

    if (TYPES)
    {
        shoText += '<table border=0>';
        //start table 1.2

        for (i = 1; i < type_title.length; i++)
        {
            shoText += '<tr>';
            shoText += '<td class="correct">' + labels[i] + ')' + '</td>';
            shoText += '<td class="correct">' + parent.type_title[i] + '</td>';
            shoText += '<td>&nbsp</td>';
            shoText += '<td class="correct">' + type_correct[i];
            shoText += ' out of ' + type_answered[i] + '</td>';
            shoText += '</tr>';
        }

        shoText += '</table>';
        //end table 1.2
    }

    shoText += '</td></tr>';

    ///////////////////////////////////////////////////////////////////////////
    // We print the Sections histogram.
    ///////////////////////////////////////////////////////////////////////////
    shoText += '<tr><td>';

    if (SECTIONS)
    {
        shoText += '<table border=0 cellpadding=0>';
        //start table 1.3.0
        shoText += '<tr><td>';

        shoText += '<table cellpadding=0>';
        //start table 1.3.1
        shoText += '<tr><td><img src=' + Img_Path + '/spacer.gif></td><tr>';
        shoText += '<tr><td valign=bottom><img src=' + Img_Path + '/axis.gif height=200></td><tr>';
        shoText += '</table>';
        //end table 1.3.1

        shoText += '</td>';

        for (i = 1; i < section_title.length; i++)
        {
            section_percent = section_correct[i] / section_answered[i];
            section_height = section_percent * 200;

            shoText += '<td valign=bottom>';

            shoText += '<table cellpadding=0>';
            //start table 1.3.2
            shoText += '<tr><td><img src=' + Img_Path + '/spacer.gif></td></tr>';
            shoText += '<tr>';
            shoText += '<td class=histogramBar width=15 height=' + section_height + '>';
            shoText += '<img src=' + Img_Path + '/spacer.gif></td>';
            shoText += '</tr>';
            shoText += '</table>';
            //end table 1.3.2

            shoText += '</td>';
        }

        shoText += '</tr>';

        ///////////////////////////////////////////////////////////////////////////
        // We add the Section labels.
        ///////////////////////////////////////////////////////////////////////////
        shoText += '<tr><td>&nbsp;</td>';

        for (i = 1; i < section_title.length; i++)
        {
            shoText += '<td height=20 align=center class="correct">' + labels[i] + '</td>';
        }

        shoText += '</tr></table>';
        //end table 1.3.0
    }

    shoText += '</td><td>';

    ///////////////////////////////////////////////////////////////////////////
    // We print the Type histogram.
    ///////////////////////////////////////////////////////////////////////////
    if (TYPES)
    {
        shoText += '<table border=0 height=200 cellpadding=0>';
        //start table 1.4.0
        shoText += '<tr><td>';
        shoText += '<table cellpadding=0>';
        //start table 1.4.1
        shoText += '<tr><td><img src=' + Img_Path + '/spacer.gif></td><tr>';
        shoText += '<tr>';
        shoText += '<td valign=bottom>';
        shoText += '<img src=' + Img_Path + '/axis.gif height=200></td>';
        shoText += '<tr>';
        shoText += '</table>';
        //end table 1.4.1

        shoText += '</td>';

        for (i = 1; i < type_title.length; i++)
        {
            type_percent = type_correct[i] / type_answered[i];
            type_height = type_percent * 200;
            shoText += '<td valign=bottom>';
            shoText += '<table cellpadding=0>';
            //start table 1.4.2
            shoText += '<tr><td class=histogramBar width=15 height=' + type_height + '>';
            shoText += '<img src=' + Img_Path + '/spacer.gif></td>';
            shoText += '</tr>';
            shoText += '</table>';
            //end table 1.4.2

            shoText += '</td>';
        }

        ///////////////////////////////////////////////////////////////////////////
        // We add the Type labels.
        ///////////////////////////////////////////////////////////////////////////
        shoText += '</tr><tr><td>&nbsp;</td>';

        for (i = 1; i < type_title.length; i++)
        {
            shoText += '<td class="correct" height=20 align=center>' + labels[i] + '</td>';
        }

        shoText += '</tr></table>';
        //end table 1.4.0
    }

    shoText += '</td></tr></table>';
    //end table 1.0

    document.getElementById('histDiv').innerHTML = shoText;
    show('histDiv');
}


//////////////////////////////////////////////////////////////////////////////
// Utility functions
//////////////////////////////////////////////////////////////////////////////

/**
 * Disables all 'qName' radio buttons on forms[0]
 */
function disableRadios()
{
    // Disable all radio buttons. Only allowed to answer once.
    var radioGrp = document.forms[0].qName;

    for (var i = 0; i < radioGrp.length; i++)
    {
        radioGrp[i].disabled = true;
    }
}

/**
 * Shows a named object by setting its display style to 'block'
 */
function show(objectID)
{
    objOnPage = document.getElementById(objectID);
    objOnPage.style.display = 'block';
}

/**
 * Hides a named object by setting its display style to 'none'
 */
function hide(objectID)
{
    objOnPage = document.getElementById(objectID);
    objOnPage.style.display = 'none';
}

/** 
 * Fills the questions[] global array with question numbers.
 * Either randomly order the questions from the pool 
 * or order questions w/o randomizing. 
 * Gradebook usually true and histogram false
 */
function randomize()
{
    if (randomize_questions == true)
    {
        var pool = new Array(number_questions);
        var no_remaining = number_questions;

        // Fill the pool[] for use in random selection
        for (var i = 0; i < number_questions; i++)
        {
            pool[i] = i + 1;
        }

        // Set order of questions by filling questions[]
        for (i = 0; i < number_questions; i++)
        {
            // Get random number
            var rand = Math.random() * (no_remaining - 1);
            rand = Math.round(rand);

            // Pick from pool and assign to questions
            questions[i] = pool[rand];

            // Repack pool[], skipping rand pick, and decrement no_remaining
            for (var j = 0; j < (no_remaining - 1); j++)
            {
                if (j >= rand)
                {
                    pool[j] = pool[j + 1];
                }
            }

            no_remaining = (no_remaining - 1);
        }
    }
    else // Order questions without randomizing.
    {
        for (i = 0; i < number_questions; i++)
        {
            questions[i] = i + 1;
        }
    }
}

