<!--hide this script from non-javascript-enabled browsers

//////////////////////////////////////////////////////////
//
// quiz.js
//
// Produced by Presenting Science
//  www.PresentingScience.com
//
// Copyright (C) Presenting Science 2008
//
//////////////////////////////////////////////////////////

function dohidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function doshowdiv(id) {
	//safe function to show an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

function getQuestionHTML() {
	var s = "<h4>";
	s += "Question "+this.number;
	s += "</h4><p>";
	s += "<strong>";
	s += this.question;
	s += "</strong></p>";
	
	var i, n = this.responses.length;

	s += "<form name='form'>";
	for (i=0; i<n; i++) {
		s += "<div class=\"formitem\"><input name='radiobutton' type='radio' value='radiobutton' onclick='responseSupplied()' id=" + i + " /><label for=" + i + ">";
		s += this.responses[i];
		s += (i==n-1)? "</div>" : "</label></div>";
	}
	s += "</form>";
	return s;
}

function getQuestionAnswer() {
	var s = "<h3>";
	if (document.form.radiobutton[this.correct].checked) {
		nCorrect++;
		s += "Right!";
	}
	else {
		s += "Wrong!";
	}
	s += "</h3>";
	s += "<p>";
	s += this.answer;
	s += "</p>";
	return s;
}


function Question(number, question, responses, correct, answer) {
	this.number = number;
	this.question = question;
	this.responses = responses;
	this.correct = correct;
	this.answer = answer;
	this.getHTML = getQuestionHTML;
	this.getAnswer = getQuestionAnswer;
}

var nQuestions = 0;
var nCorrect;
var questions = new Array();

// 1
questions[nQuestions++] = new Question(nQuestions, 
    "Roughly what percentage of the US population believes in astrology?",
	[ "10%", "20%", "30%" ], 2,
	"According to a National Science Foundation report in 2004, 28% of Americans believe in astrology and 15% read their horoscopes regularly.</p><p>Only in America? No way. Superstitious beliefs are everywhere. Look at all the skyscrapers without 13th floors all over the world. Or the Chinese belief in the lucky number 8.  It’s no accident the Beijing Olympics began on 08/08/08, you know.");

// 2
questions[nQuestions++] = new Question(nQuestions, 
    "Which of the following countries has the highest life expectancy?",
	[ "Greece – top of the international smoking league, with a cigarette consumption of 4,313 a year per adult", "The USA, where the adult population puffs through an average of 1,941 cigarettes in a year", "Denmark, where the adult cigarette consumption is a modest 1,919 cigarettes each year" ], 0,
	"Typically, Greeks can hope to puff their way to the age of 70.4. It’s hardly the highest life expectancy in the world, but it’s a few months longer than that of the abstemious Danes. And the average Greek lives nearly three years longer than the average American.");

// 3
questions[nQuestions++] = new Question(nQuestions, 
    "Medical error is the fourth biggest cause of death in the USA.",
	[ "True", "False" ], 1,
	"In fact, according to a recent study, it’s the third largest cause of death, behind cancer and heart disease – killing over 225,000 Americans every year. Like all medical statistics, you have to take these findings with a pinch of salt, but there’s no doubt that a lot of people die from medical error rather than illness or injury.");

// 4
questions[nQuestions++] = new Question(nQuestions, 
    "Imagine you invested $10,000 across the US stock market in 1802. How much would your investment have been worth by 2006?",
	[ "$27,800", "$10,888,350", "$8,390,179,500" ], 2,
	"In the long term, the stock market is always the best bet. A theoretical $10,000 invested in the US market in 1802 (across all shares) would have been worth nearly $8.4 billion by 2006. The other figures ($27,800 and $10,888,350) are what you’d have ended up with if you’d invested in gold and long-term government bonds respectively.");

// 5
questions[nQuestions++] = new Question(nQuestions, 
    "Over the 65 years leading up to 2006 (that’s safely before the subprime crisis) the US property market rose more than the stock market (as measured by the performance of the S&P 500 index).",
	[ "True", "False" ], 1,
	"Remember the answer to the previous question! In the long term, the stock market is safer even than houses. According to US government data, real estate prices increased over those 65 years, but only by a quarter as much as the S&P 500. In addition, after the subprime crisis real estate prices fell as much as 25% within a single year.");

// 6
questions[nQuestions++] = new Question(nQuestions, 
    "It’s Monday October 19, 1987. First thing in the morning, you invest $10,000 in a representative selection of US stocks. Approximately how much is your investment worth by the end of the day?",
	[ "$7,800", "$10,000", "$12,200" ], 0,
	"Sorry, you’ve only got $7,800-worth of shares left. This is Black Monday, the worst single day in the stock market’s history, when an incredible 22% was wiped off its value. </p><p>As we said before, the stock market is a good bet in the long term. But it can be a disastrous place to invest in the short term. Of course, there are some days when its value soars too. The point is that no one can tell you when Black Mondays or Ruby Tuesdays are going to happen.");

// 7
questions[nQuestions++] = new Question(nQuestions, 
    "In 1998 Daimler-Benz bought Chrysler for $36 billion. It was hailed as the merger of the century with vast profits predicted. But in 2007, Daimler-Benz sold Chrysler for:",
	[ "$36 billion", "$5 billion", "minus $650 million" ], 2,
	"This was a corporate tragedy on a grand scale. After years of losses, lay-offs and culture clashes, Daimler unloaded 80% of its shares on private equity firm Cerberus, actually paying the buyer $650 million for the privilege.</p><p>Even if you got the answer wrong, you weren’t as wrong as the analysts back in 1998. Their worst-case scenario was a combined market value of $40 billion.");

// 8
questions[nQuestions++] = new Question(nQuestions, 
    "In 2007 the market capitalization of Google was an incredible $220 billion. But what was the asking price of its founders when they tried to sell up back in the late 1990s?",
	[ "$1.6 billion", "$160 million", "$1.6 million" ], 2,
	"This was a corporate fairytale on an even grander scale than the Chrysler tragedy of Question 7. Even if you got the answer wrong, you weren't as wrong as Google's founders back in the late '90s when they tried to get $1.6 million for the company.</p><p>Fortunately for them everyone else was wrong too. Excite, a one-time competitor of Yahoo, made the closest bid of $750,000. But Yahoo, the now-defunct Infoseek, and a whole bunch of venture capitalists didn't even bother making an offer.");

// 9
questions[nQuestions++] = new Question(nQuestions, 
    "What is the approximate probability that in a room of 60 randomly selected people, 2 of them will have the same birthday?",
	[ "25% or 1 in 4", "50% or 1 in 2", "75% or 3 in 4", "nearly 100%" ], 3,
	"Most people find it surprising that the probability of this situation occurring is very close to 1. That’s the problem with probability – it’s often very counter-intuitive.");

// 10
questions[nQuestions++] = new Question(nQuestions, 
    "Which of the following methods has proved most effective for bank managers when deciding whether to give customers a loan?",
	[ "Looking into the customer’s eyes and going with their intuition.", "A few simple questions about the customer’s finances, plus a quick credit check.", "A thorough interview covering all aspects of the customer’s financial well-being." ], 1,
	"Over the years, banks have discovered that human beings make lousy judgments about other human beings’ creditworthiness. Today, they tend to rely on asking a few simple questions and running a credit check. ‘Simple modelling’ like this is not only more reliable than human judgment, but also much quicker and cheaper.");

// 11
questions[nQuestions++] = new Question(nQuestions, 
    "Which of these everyday activities do women find least satisfying (at least according to some recent research)?",
	[ "sex", "eating", "shopping", "childcare" ], 3,
	"The research, based on the responses of a sample of 909 Texan women, put childcare at the bottom, marginally ahead of working and commuting.");

// 12
questions[nQuestions++] = new Question(nQuestions, 
    "Which of the following countries scores highest in the national happiness league tables?",
	[ "Japan, where they live long and enjoy many a cigarette along the way", "USA, land of the free and the Big Mac", "Costa Rica, the Central American country that sends us so much coffee and bananas", "Cuba, the revolutionary tropical paradise with big fat cigars and lilting Latin rhythms" ], 2,
	"Researchers have found that – above a certain level of income ($16,000 GDP per person per year in fact) – money can’t buy you national happiness. However, there are a few exceptions. Cuba is happier than Japan (thought not as happy as the USA) but people in Costa Rica are happier than those in USA.");

// 13
questions[nQuestions++] = new Question(nQuestions, 
    "Golf makes you live longer.",
	[ "True", "False" ], 0,
	"According to a recent Swedish study, playing golf does indeed make people live longer. All this really proves, however, is that academics do some pretty strange research. Perhaps fresh air, long walks, gentle exercise and good company are the real recipe for a longer life?");


var currentQuestion;

function startQuiz()
{
	dohidediv("quizpreamble");
	currentQuestion = -1;
	nCorrect = 0;
	nextQuestion();
}

function endQuiz() {
	setContents("quizquestion", "");
	setContents("quizfeedback", giveFeedback());
	setContents("quiztoolbar", "");
	writeQuizCookie(nCorrect);
	doshowdiv("quizend");
}

function giveFeedback() {
	var s = "<h2>Your results</h2>";
	s += "<p><strong>You scored "+nCorrect + "/" +nQuestions+".</strong></p><p>";
	
	if (nCorrect<7)
		s += "Bad luck. When people do badly at something they tend to put it down to bad luck. And why not in this case? But if you’d like to know a bit more about these issues and discover what role luck really plays in your life, there’s a book that we highly recommend called <em>Dance with Chance</em>.";
	
	else if (nCorrect==7) 
	    s += "Apparently 7 is a lucky number. But 13 isn’t. Who are we kidding? There’s no such thing. But it’s not a bad score, you know. If you’d like to know a bit more about these issues and discover what role luck really plays in your life, there’s a book that we highly recommend called <em>Dance with Chance</em>.";
	
	else
	   s += "Congratulations. What skill! (When people do well at something, they tend to put it down to skill rather than luck.) You’re clearly the kind of person who’s interested in the issues raised in this quiz. And, as it happens, we know the perfect book for people like you: <em>Dance with Chance</em>.";

	s += "</p>";
    return s;
}

function nextQuestion() {
	currentQuestion++;
	if (currentQuestion==nQuestions) {
		endQuiz();
	}
	else {
		setContents("quizquestion", questions[currentQuestion].getHTML());
		setContents("quizfeedback", "");
		setContents("quiztoolbar", "");
	}
}

function responseSupplied() {
	setContents("quizfeedback", "<p><a href='javascript:markResponse()' id=\"amiright\">Am I right?</a></p>");
}

function markResponse() {
	var i, n = document.form.radiobutton.length;
	for (i=0; i<n; i++) document.form.radiobutton[i].disabled = true;
	setContents("quizfeedback", questions[currentQuestion].getAnswer());
	if (currentQuestion==nQuestions-1)
		setContents("quiztoolbar", "<p>That was the last question.</p><a class='navigation' href='javascript:nextQuestion()' id=\"tell-me-my-score\">Tell me my score!</a>");
	else
		setContents("quiztoolbar", "<a href='javascript:nextQuestion()' id=\"next-question\">Next question</a>");
}

function setContents(id, contents) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).innerHTML = contents;
		if (contents.length==0) dohidediv(id);
		else doshowdiv(id);
	}
	
}

 
/**
 * Write the startup cookie
 */
 function writeQuizCookie(value) {
     document.cookie = "QUIZSCORE="+escape(value);
 }


// stop hiding -->