var quiz = new Array();

//primary public quiz function, generates the quiz form
function generateQuiz(numQuestions) {
	getQuestions(numQuestions);
	for (var k = 0; k < quiz.length; k++) {
		quiz[k].showForm(k);
	}
	document.write ("<h3><a href=\"javascript:correctQuiz();\">Correct Quiz</a></h3>\n");
}

// 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"><font face="arial,helvetica,verdana,sans serif" size="2">';
	
	// 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 correct 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 += "<p>Play Again? <a href=\"#\" onClick=\"opener.location.reload(true); window.close()\">Yes</a> or <a href=\"#\"  onClick=\"opener.location=\'../index.html\'; window.close()\">No</a></p>";
	correctPage += '</font></body></html>';
	var correctWin = window.open('', '', 'height=200,width=300,scrollbars=yes');
	correctWin.document.write(correctPage);
	correctWin.document.close();
}


			var qCount = 0; //number of questions available to chose from
			var qArray = new Array();
			// these 66 are the first set of questions generated for the April 04 newsletter
			qArray[qCount] = new questItem("What is the Deep Impact mission?",1,"A project to develop a satellite to try and change the orbit of the moon.","A project to send a probe to collide with a comet in our solar system in order to understand the composition and structure of comets.","A project where a space probe is dropped from the Earth\'s orbit into the Pacific Ocean.");
			qArray[qCount] = new questItem("What do we hope to learn from the Deep Impact mission?",1,"How to avoid making craters in other objects in the solar system.","A variety of answers to questions like, \'Where is the pristine material in comets?\' and, \'What do we know about crater formation?\'","How come comets glow at night?");
			qArray[qCount] = new questItem("What kinds of data will the Deep Impact mission collect?",2,"Magnetograms will be taken of the sun\'s magnetic fields.","Gravitational data will be taken to determine how the orbit of the Earth changes.","Spectroscopic and visual images will be taken before, during and after the impact with a comet.");
			qArray[qCount] = new questItem("Who discovered the target of the Deep Impact mission?",2,"Edwin Hubble","Mike A'Hearn","Wilhelm Tempel");
			qArray[qCount] = new questItem("The Deep Impact mission is a collaboration between which three organizations?",1," Johns Hopkins Univ. Applied Physics Lab (JHUAPL), Univ. of South Carolina, and Lockheed Martin","Jet Propulsion Laboratory (JPL), Univ. of Maryland, and Ball Aerospace & Technologies Corp.","Goddard Space Flight Center (GSFC), Univ. of Colorado, and Northrop Grumman");
			qArray[qCount] = new questItem("The Deep Impact mission is part of which multi-mission program?",2,"Explorer","New Millennium","Discovery");
			qArray[qCount] = new questItem("Who is in charge of the Deep Impact mission?",1,"Edwin Hubble","Mike A'Hearn","Wilhelm Tempel");
			qArray[qCount] = new questItem("How many panels are hinged together to make the solar panel?",1,"One","Two","Three");
			qArray[qCount] = new questItem("Why is Comet Tempel 1 the target of the Deep Impact mission?",2,"Because it is a perfect sphere and is very bright and easy to see.","Because we don\'t like this comet.","Because it satisfied several basic parameters such as size, orbital path, brightness and schedule.");
			qArray[qCount] = new questItem("What are Whipple Shields?",0,"They are special shields designed to protect the instruments from particles as the spacecraft flies through the coma of the comet.","They are special shields designed for people who don\'t know how to eat icecream cones.","They are special shields designed to protect trees.");
			qArray[qCount] = new questItem("How will the data from the spacecraft be returned to Earth?",2,"By Pony Express.","FedEx, UPS, or USPS -- whoever has the cheapest rates.","By radio signal through the NASA Deep Space Network.");
			qArray[qCount] = new questItem("How did the Deep Impact mission get its name?",0,"From an idea from team members Delamere and Belton as they reviewed other projects and Halley data.","From the 1998 movie of the same name.","Whack-a-comet just didn\'t seem like an appropriate name.");
			qArray[qCount] = new questItem("In July 2005, you will be able to see Tempel 1 with binoculars.",0,"True","False");
			qArray[qCount] = new questItem("Why is the study of comets important for the understanding of our solar system?",2,"Because they help us to determine the direction in which our solar system is traveling.","Because you can tell from them when the alignment of the planets is near.","Comets are believed to be the left over bits and pieces from the formation of our solar system.");
			qArray[qCount] = new questItem("What is an impact crater?",1,"It is a small planar mountain on the dark side of the moon.","It is a huge concave dent put into a surface by another smaller mass.","It is the big red spot astronomers are able to see on Jupiter.");
			qArray[qCount] = new questItem("Could Tempel 1 break apart and head for Earth?",2,"Yes, this is what will happen unless the space-aeronautics industry can prevent it.","Yes, it definitely will break apart, but won\'t be heading this way.","No, the chances are slim to none that this comet will break apart and come this way.");
			qArray[qCount] = new questItem("What is the mass of the impactor?",1,"225 kg","360 kg","1000 kg");
			qArray[qCount] = new questItem("What is the Impactor's speed relative to comet Tempel 1 at the time of impact?",0,"Around 10 km/sec","Around 15 km/sec","Around 50 km/sec");
			qArray[qCount] = new questItem("What amount of energy will the impact have?",1,"19 Joules","19 Gigajoules","19 Newtons");
			qArray[qCount] = new questItem("Which of these events will occur in December 2004?",2,"The impact of the probe into Tempel 1.","The return of the probe to Earth.","The launch of the Flyby and Impactor.");
			qArray[qCount] = new questItem("Which of these events will occur in July 2005?",0,"The impact of the probe into Tempel 1.","The return of the probe to Earth.","The launch of the Flyby and Impactor.");
			qArray[qCount] = new questItem("What company developed the targeting software?",0,"JPL (Jet Propulsion Lab)","Sony","TI (Texas Instruments)");
			qArray[qCount] = new questItem("Which one of the following jobs is NOT one on the Deep Impact mission?",2,"Astronomer","Engineer","Veterinarian");
			qArray[qCount] = new questItem("Which one of the following jobs is one on the Deep Impact mission?",0,"Astronomer","Race car driver","Veterinarian");
			qArray[qCount] = new questItem("Which one of the following jobs is one on the Deep Impact mission?",1,"Fisherman","Engineer","Botanist");
			qArray[qCount] = new questItem("Which one of the following jobs is one on the Deep Impact mission?",2,"Entymologist","Ballerina","Webmaster");
			qArray[qCount] = new questItem("Which comet is the target of the Deep Impact mission?",1,"Hale-Bopp","Tempel 1","Halley\'s");
			qArray[qCount] = new questItem("What is one of the reasons Tempel 1 was selected as the target of the Deep Impact mission?",0,"It has a slow rotation rate.","It is made mostly of metal","It is nice and small for our probe to land on.");
			qArray[qCount] = new questItem("What is the launch vehicle for the Deep Impact mission?",0,"Boeing Delta II","McDonald Douglas Delta","MIR");
			qArray[qCount] = new questItem("Where is the spacecraft being built?",2,"Various NASA laboratories","China","Ball Aerospace & Technologies Corp.");
			qArray[qCount] = new questItem("Which of these is most similar in size to the flyby spacecraft?",1,"A desk","An SUV","A two-bedroom apartment");
			qArray[qCount] = new questItem("Which of these is most similar in size to the impactor?",0,"A desk","A compact car","Comet Tempel 1");
			qArray[qCount] = new questItem("About how long will it take for the flyby spacecraft\'s signal to reach Earth?",2,"It\'s instantaneous.","An hour.","8 minutes.");
			qArray[qCount] = new questItem("What is the approximate diameter of Tempel 1?",1,"60 meters","6 kilometers","60 kilometers");
			qArray[qCount] = new questItem("How many imaging instruments will be on the flyby spacecraft?",1,"One","Two","Three");
			qArray[qCount] = new questItem("When does the Deep Impact project end?",2,"When the impactor hits Tempel 1 on July 4th, 2005.","When the last of the signals are sent back to Earth in August of 2005.","When the collected data has been analysed and archived in April of 2006.");
			qArray[qCount] = new questItem("How far away from Earth will Tempel 1 be at the time of impact?",0,"About 83 million miles away.","About 137 million miles away.","About 395 million miles away.");
			qArray[qCount] = new questItem("How soon after the impact will we be able to view the event on Earth?",0,"After about 8 minutes.","After about 8 days.","After about 8 weeks.");
			qArray[qCount] = new questItem("How much TNT would we have to use in order to accurately simulate the energy of the impact?",1,"3 sticks","About 4.8 tons","About 4 pounds");
			qArray[qCount] = new questItem("Which two asteroids are the targets of the Dawn mission?",2,"Ceres and Eros","Apollo and Juno","Ceres and Vesta");
			qArray[qCount] = new questItem("What is the total mass of the entire fight system?",0,"1020 kg. (2250 lbs.)","1570 kg. (3460 lbs.)","1935 kg. (4265 lbs.)");
			qArray[qCount] = new questItem("Which of these is NOT a crater formation stage?",2,"Excavation stage","Compression stage","Third stage");
			qArray[qCount] = new questItem("What is the meaning of \'kometes\', the Greek root for the modern term \'comet\'?",1,"Bringer of war","Long hair","Periodic extraterrestrial object");
			qArray[qCount] = new questItem("Approximately how large are we expecting the crater to be?",1,"The size of the impactor.","The size of a football field.","There will be no crater because the impact will destroy Tempel 1.");
			qArray[qCount] = new questItem("About how old are the comets and planets of our solar system?",1,"About 4 billion years old.","About 4.5 billion years old.","About 13.7 billion years old.");
			qArray[qCount] = new questItem("What are the Oort cloud and the Kuiper belt?",2,"Earth-based geological formations.","Along with \'coma\' and \'tail\', they are terms for parts of comets.","Two regions in our solar system which are thought to be reservoirs of comets.");
			qArray[qCount] = new questItem("What is the general shape of comet Tempel 1?",0,"That of a cucumber","That of an orange","That of a football");
			qArray[qCount] = new questItem("Comets are composed mainly of what?",1,"Rock and metal","Frozen water, carbon dioxide, and carbon monoxide","Blue cheese");
			qArray[qCount] = new questItem("Generally, what happens to the ice of a comet when it gets closer to the sun?",2,"Nothing. It is simply illuminated by the light of the sun.","It liquefies, much the same way an ice cube does when left out.","It undergoes sublimation, that is, it goes straight from a solid to a gas.");
			qArray[qCount] = new questItem("Who is Michael F. A\'Hearn?",2,"The discoverer of Tempel 1.","The man who came up with the idea for the Deep Impact project.","The Principal Investigator of the Deep Impact project.");
			qArray[qCount] = new questItem("What is the SI unit for energy?",1,"Watts","Joules","Ohms");
			qArray[qCount] = new questItem("Which of these is not a scientific organization affiliated with the Deep Impact mission?",0,"Star Division Software","Ball Aerospace & Technologies Corp.","NASA");
			qArray[qCount] = new questItem("A light year is a measure of ________.",2,"Energy","Time","Distance");
			qArray[qCount] = new questItem("In the context of the Deep Impact mission, what does MRI stand for?",2,"Meteor Related Impact","Magnetic Resonance Imaging","Medium Resolution Instrument");
			qArray[qCount] = new questItem("What is the overall velocity of the Impactor before it hits Tempel 1?",1,"10.2 km/s","22.4 km/s","32.4 km/s");
			qArray[qCount] = new questItem("What is the velocity of Tempel 1 during the time of the impact?",2,"10.2 km/s","20.2 km/s","29.9 km/s");
			qArray[qCount] = new questItem("What is the abbreviated term for \'picture element\'?",2,"JPEG","PicEl","Pixel");
			qArray[qCount] = new questItem("Tempel 1 was discovered in what year?",1,"1064","1867","1997");
			qArray[qCount] = new questItem("Tempel 1 currently takes about how long to go around the sun once?",0,"5.5 years","5.5 months","555 days");
			qArray[qCount] = new questItem("One Tempel-year is about how long (in Earth units)?",0,"5.5 years","5.5 months","555 days");
			qArray[qCount] = new questItem("Which planet can change Tempel 1\'s orbit if Tempel 1 gets too close to it?",1,"Earth","Jupiter","Saturn");
			qArray[qCount] = new questItem("In the context of astronomy, what is an apparition?",1,"Special ghosts that only appear on certain nights in the year.","Time periods when celestial objects are visible.","Special dividers placed in the sky between constellations.");
			qArray[qCount] = new questItem("The nuclei of several hundred comets have been mapped.",1,"True","False");
			qArray[qCount] = new questItem("The nuclei of only 3 comets have been imaged (before Deep Impact).",0,"True","False");
			qArray[qCount] = new questItem("Which three comets have had close flybys before Tempel 1?",2,"Halley, Hyakutake, Hale-Bopp ","Eros, Mathilde, Braille","Halley, Borrelly, Wild 2");
			qArray[qCount] = new questItem("Why is Deep Impact such an exciting mission?",3,"It is the first mission that will let us see into a comet.","It is the first mission that will recreate a natural process (collisions) to learn more about comets and cratering.","Individuals, either with their own telescopes or by attending observing sessions hosted by local clubs, observatories, or planetariums, can watch how the comet\'s appearance changes over several days.","All of the above.");
			function questItem() {
				this.a = questItem.arguments;
				qCount++;
			}

			function getQuestions(numToAsk) {
				var indexs = new Array();
				var i = 0;

				while ( i < numToAsk && i < qArray.length )
				{
					var rInt = Math.floor(Math.random() * qArray.length);
        				var j = 0;

					//go through the currently selected indexs to check if the new
					//random question index has already been selected. If it has,
					//then select a new random question index, and begin the checking
					//from the beginning until a unique index is selected
					while ( j < indexs.length )
					{
						if ( rInt == indexs[j] ) {
							j = 0;
							rInt = Math.floor(Math.random() * qArray.length);
						} else {
							j++;
						}
					}

					//now that we have a unique index, add it to the selected indexs
					indexs[i] = rInt;

					//set-up the question() call the question to the document
					var newQuestion = "quiz[" + i + "] = new Question(\"" + qArray[rInt].a[0] + "\"," + qArray[rInt].a[1];
					var j = 2;
					while ( j < qArray[rInt].a.length ) {
						newQuestion += (",\"" + qArray[rInt].a[j++] + "\"");
					}
					newQuestion += ");";

					//perform a question() call
					eval(newQuestion);

					//proceed on to search for the next index
					i++;
				}
			}