
rnd.today=new Date();
rnd.seed=rnd.today.getTime();

var questions = new Array(	'Which countries are in  ',
							'Which country\'s capital is ',
							'Which country\'s currency is ',
							'Which countries are land  ', //locked
							'Which countries are members of the ', //EU
							'Which country is doubly ' //ked
							);


// this stores all the options, and answers
var myarray=	[
			["Towerbridge.jpg",	"England",		"Europe",	"London",		"Pound",	"",			"EU",	""],
			["southafrica.jpg",	"South Africa",	"Africa",	"Cape Town",	"Rand",		"",			"",		""],
			["greatzim.jpg",	"Zimbabwe",		"Africa",	"Harrare",		"Dollar",	"",			"",		""],
			["france.jpg",		"France",		"Europe",	"Paris",		"Euro",		"",			"EU",	""],
			["geneva.jpg",		"Switzerland",	"Europe",	"Bern",			"Franc",	"locked",	"",		""],
			["Liechtenstein.jpg","Liechtenstein","Europe",	"Vaduz",		"Franc",	"locked",	"",		"land locked"],
			["x1.jpg",			"USA",			"Americas",	"Washington",	"Dollar",	"",			"",		""],
			["mag.jpg",			"Australia",	"Oceania",	"Canberra",		"Dollar",	"",			"",		""],
			["castle.jpg",		"Jordan",		"Middle East","Amman",		"Dinar",	"",			"",		""],
			["bgate.jpg",		"Germany",		"Europe",	"Berlin",		"Euro",		"",			"EU",	""],
			["bled.jpg",		"Slovenia",		"Europe",	"Ljubljana",	"Tollar",		"",			"EU",	""],
			["budda.jpg",		"Thailand",		"Asia",		"Bangkok",		"Baht",		"",			"",		""],
			["dubai.jpg",		"United Arab Emirates",	"Middle East","Abu Dhabi","Dirham","",		"",		""],
			["egypt.jpg",		"Eygpt",		"Africa",	"Cario",		"Pound",	"",			"",		""],
			["Esplanade.jpg",	"Singapore",	"Asia",		"Singapore",	"Dollar",	"",			"",		""],
			["flam.jpg",		"Norway",		"Europe",	"Oslo",			"Krone",	"",			"",		""],
			["florence.jpg",	"Italy",		"Europe",	"Rome",			"Euro",		"",			"EU",	""],
			["Helsingborg.jpg",	"Sweden",		"Europe",	"Stockholm",	"Krona",	"",			"EU",	""],
			["helsinkicity.jpg","Finland",		"Europe",	"Helsinki",		"Euro",		"",			"EU",	""],
			["itctower.jpg",	"Hong Kong",	"Asia",		"Hong Kong",	"Dollar",	"",			"",		""],
			["jerusalem.jpg",	"Israel",		"Middle East",	"Jerusalem","Shekel",	"",			"",		""],
			["kiev.jpg",		"Ukraine",		"Europe",	"Kiev",			"Hryvnia",	"",			"",		""],
			["kinkaku_ji.jpg",	"Japan",		"Asia",		"Tokyo",		"Yen",		"",			"",		""],
			["LakeMyvatn.jpg",	"Iceland",		"Europe",	"Reykjavik",	"Krona",	"",			"",		""],
			["lux.jpg",			"Luxembourg",	"Europe",	"Luxembourg",	"Euro",		"locked",	"EU",	""],
			["madrid.jpg",		"Spain",		"Europe",	"Madrid",		"Euro",		"",			"EU",	""],
			["mon.jpg",			"Denmark",		"Europe",	"Copenhagen",	"Krone",	"",			"EU",	""]
				];

var questionorder = new Array(9);

var stuff = new Array('pic0','pic1','pic2','pic3','pic4','pic5','pic6','pic7','pic8','pic9','pic10',
'pic11','pic12','pic13','pic14','pic15','pic16','pic17','pic18','pic19','pic20',
'pic21','pic22','pic23','pic24','pic25','pic26','pic27','pic28','pic29','pic30');

var questtype; // what question am i going to ask
var answer; // and what answer do I want
var anscount; // how many right answers are there?

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

// Generate me a random number!
function rand(number) {
        return Math.ceil(rnd()*number);
};

function doonclick(thepic)
{
	if (myarray[thepic][questtype+2] == myarray[answer][questtype+2])
	{
		document.images[stuff[thepic]].src = "../images/bigtick.gif";
		document.forms[0].right.value++ ;
	} else {
		document.images[stuff[thepic]].src = "../images/bigcross.gif";
		document.forms[0].wrong.value++ ;
	}

	if (document.forms[0].right.value == anscount)
	{
		if (document.forms[0].wrong.value == 0)
		{
			alert("Well done!\n\n\nNo Wrong Answers!!");
		}
		else
		{
				alert("Not bad, but you did get some wrong!");
		}
	}

}

// given a number this prints the HMTL that's required - an image and a title.
function picture(pic)
{
	document.write("<TD class=\"photo\"><CENTER>"+
	"<IMG SRC='../images/"+myarray[pic][0]+"' onclick='doonclick(\""+pic+"\");' width='150'  height='150' name=\"pic"+pic+"\">"+
	"<br><p onclick=\"doonclick('"+pic+"');\">"+myarray[pic][1]+"</p>" +
	"</center></TD>");
}

// display me all the options in a nice table!
function generate_board()
{
	document.write("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"photo\">");

	var count =0;

	for (y=0;y<3;y++)
	{
		document.write("<TR>");
		for (x=0;x<3;x++)
		{
			if (count < questionorder.length)
			{
				picture(questionorder[count]-1);
				count++;
			}
		}
		document.write("</TR>");
	}
	document.write("</TABLE>");
}



function generate_question(quest)
{
	document.write("<p class=\"standardtext\"><br><br><b>Question: </b>"+questions[questtype]+" "+myarray[answer][questtype+2]+"?<br>");
}


// this makes sure that the answer we're looking for isn't empty!
// some questions don't have a choice - is this country land locked? 
// so we can't just choose a random one, it has to be the one with something in the array
function gen_rand() {
	
	do {
		temprand = rand(myarray.length)-1;
	} while ( myarray[temprand][questtype+2] == "" );

	return temprand;
}


// counts how many right answers there are for the chosen question type, chosen question answer, AND chosen question options 
function count_right_answers()
{
	anscount = 0;

	for (x=0;x<questionorder.length;x++)
	{
		// why "questtype)+2"? Well, that array stores the image name, and title , then the questions, so we just move in up 2
		// why "questionorder[x]-1"? 
		if (myarray[(questionorder[x]-1)][(questtype)+2] == myarray[answer][questtype+2])
		{
			anscount++;	
		}
	}
	return anscount;
}

// simply shows to the user how many right answers they need to find
function display_right_answers() {
	if (anscount == 1)
	{
		document.write("(there is "+ anscount +" correct answer!)");
	}
	else
	{
		document.write("(there are "+ anscount +" correct answers!)");
	}
	document.write("<br><br>");
}



// let's make sure the count items in the form are set to zero
function resetcount() {
	document.forms[0].right.value = 0;
	document.forms[0].wrong.value = 0;
}


// we have lots of options, we don't want to display all the options for each question, nor do we
// want to display them in the same order each time. 
function shuffle_deck() {

	for (x=0;x<questionorder.length;x++)
	{
		do
		{
			unique = rand(myarray.length);
		} while ( place(x,unique) );
	}
}

// add the random numbers to the array of question order - but make sure there are NO duplicates!
function place(index,randomq) {

	for (y=0;y<questionorder.length-1;y++)
	{
		if (questionorder[y] == randomq) {
			return true;
		}
	}
	questionorder[index] = randomq;
	return false;
}



// this is where we kick the whole thing off!
function show_quiz() {

	shuffle_deck(); // place options in a random order

	do {
		questtype = rand(questions.length)-1; // what question are we going to ask
		answer = gen_rand(); // and what answer from that question
	}
	while (count_right_answers() == 0);
	
	generate_board();
	generate_question();
	display_right_answers(9);
}

