// Multiple choice quiz

// The Question constructor function
function Question(question, correctAnswer) {
    var args = Question.arguments;
    this.question = question;
    this.correctAnswer = correctAnswer;
    this.userAnswer = null;
    this.isCorrect = isCorrect;
    this.showForm = showForm;
    this.userChoices = new Array();
    for (var i = 0; i < args.length - 2; i++) { 
        this.userChoices[i] = args[i + 2];
    }
}

// Method to determine if question is answered correctly
function isCorrect() {
    if (this.correctAnswer == this.userAnswer) {
        return true;
    }
    else {
        return false;
    }
}

// Method to display contents of Question object
function showForm(n) {
    document.write((n + 1) + '. ' + this.question + '<blockquote><form>');
    for (var i = 0; i < this.userChoices.length; i++) {
        document.write('<input type="radio" name="q' + n +
                       '" onClick = "quiz[' + n + '].userAnswer = ' + 
                       i + '">');
        document.write(' ' + this.userChoices[i] + '<br \/>');
    }
    document.write('</form></blockquote>');
}

// Function to correct the quiz and display score and correct answers
function correctQuiz() {
    // Initialize correct and start correctPage string
    var correct = 0;
    var correctPage = '<html><head><title>Corrections</title></head>' +
                      '<body bgcolor="#FFFFFF">';

    // Loop through Question objects, call isCorrect(), and count 
    // correct answers
    for (var i = 0; i < quiz.length; i++) {
        if (quiz[i].isCorrect()) {
            correct++;
        }
    }

    // Compute the score and add it to the correctPage string
    var score = Math.round((correct / quiz.length) * 100);
    correctPage += 'Score: <strong>' + score + '</strong> %';

    // If there are incorrect answers, list the correct ones
    if (correct < quiz.length) { 
        correctPage += "<p>Here are the correct answers to the ";
        correctPage += "questions you got wrong:<p>";
        for (var i = 0; i < quiz.length; i++) {
            if (!quiz[i].isCorrect()) {
                correctPage += (i + 1) + '. ' +
                    quiz[i].userChoices[quiz[i].correctAnswer] + '<br>';
            }
        }
    }
    else {
        // Otherwise, offer congratulations
        correctPage += "<p>Well done, you aced it.<p>"; 
    }

    // Finish correctPage, create a new window, and display correctPage
    correctPage += '</body></html>';
    var correctWin = window.open('', '', 
                                 'height=200,width=300,scrollbars=yes');
    correctWin.document.write(correctPage);
    correctWin.document.close();
}

// Create Question objects
var quiz = new Array();
quiz[0] = new Question(
    "From the mid-19th century through the 1980s, thousands of young Basques came to the American West to herd:", 
    2,                     // Correct answer
    "Cows",                // Choice 0
    "Pigs",                // Choice 1
    "Sheep",               // Choice 2
    "Llamas");             // Choice 3
quiz[1] = new Question(
    "Reasons for their immigration were:",    
    3,                                                
    "Political",                  
    "Cultural", 
    "Economic",           
    "All of the above");          
quiz[2] = new Question(
    "The herder lived alone on the range with two dogs, a horse or a donkey, and:", 
    2,                                                
    "about 100 sheep",                               
    "about 500 sheep",                           
    "about 1,500 sheep",                              
    "about 5,000 sheep");              
quiz[3] = new Question(
    "Among sheepherders it was a custom to carve pictures, messages, and thoughts on:",
    1,                                                
    "sidewalks",          
    "the bark of aspen trees",                         
    "wooden boxes",              
    "cement walls");      
quiz[4] = new Question(
    "Tree carvings are also called:",
    1,                                                
    "petroglyphs",          
    "arborglyphs or dendroglyphs",                                      
    "pictographs");      
quiz[5] = new Question(
    "Basque sheepherders carved onto the aspens mostly:",
    2,                                                
    "phone numbers",          
    "answers to quizzes",                                      
    "names and dates");      
quiz[6] = new Question(
    "The languages in which the Basque sheepherders carved their words were:",
    4,                                                
    "Spanish",          
    "Basque",                                      
    "French",
    "English",
    "All");      
quiz[7] = new Question(
    "Peavine Mountain was used as a summer rangeland for sheep as far back as:",
    3,                                                
    "the 1920s",          
    "the 1900s", 
    "the 1880s",                                     
    "the 1860s");      
quiz[8] = new Question(
    "Three main groves on Peavine Mountain contain:",
    2,                                                
    "about 100 trees carved by 25 carvers",          
    "about 250 trees carved by 50 carvers",   
    "about 500 trees carved by 105 carvers",                                   
    "about 1000 trees carved by 250 carvers");      
quiz[9] = new Question(
    "Aspen trees generally live to be about:",
    1,                                                
    "75 years old",          
    "100 years old",         
    "150 years old",                             
    "200 years old");      
