// Define global variables and arrays
var questionIndex	= 0;
var checker 		= true;
var keeper 		= new Array();
var performance 	= new Array('10 percent','20 percent','30 percent','40 percent','50 percent','60 percent','70 percent','80 percent','90 percent','100 percent');
var correctAnswers 	= 0;
var jsAbility;
var preventer = 1;
var spawn;

// For resetting all variables and arrays for a retake
function itemReset() {
	questionIndex 	= 0;
	keeper 		= new Array();
	correctAnswers 	= 0;
	preventer	= 1;
	jsAbility	= null;
	}

// Administer the test and record the answers
function startQuiz(openStatus, whetherOrNot) {
	checker = openStatus;
	if (whetherOrNot < questionIndex) { busted(); return; }
	if (checker) { 
		spawn = open('','','width=550,height=450,scrollbars=yes'); 
		!checker;
		}

	if (questionIndex == questions.length) { gradeTest(); return; }
	
	// Separate each array element to form the questions and answr options
	var question 	= questions[questionIndex].substring(0, questions[questionIndex].indexOf('$$'));
	var optionA 	= questions[questionIndex].substring(questions[questionIndex].indexOf('$$') + 2, questions[questionIndex].indexOf('%%'));
	var optionB 	= questions[questionIndex].substring(questions[questionIndex].indexOf('%%') + 2, questions[questionIndex].indexOf('^^'));
	var optionC 	= questions[questionIndex].substring(questions[questionIndex].indexOf('^^') + 2, questions[questionIndex].indexOf('&&'));
	var optionD 	= questions[questionIndex].substring(questions[questionIndex].indexOf('&&') + 2, questions[questionIndex].length);

	// Write the questons and answrs options to the new window
	spawn.document.clear();
	spawn.document.open();
	spawn.document.writeln('<HTML><HEAD><TITLE>Immigration Quiz</TITLE></HEAD>');
	spawn.document.writeln('<BODY BGCOLOR="\WHITE\">');
	spawn.document.writeln('<div align=\"center\"><img src=\"images/discrimination_quiz.gif\"></div>');
	spawn.document.writeln('<FONT FACE=\"Arial\">');
	spawn.document.writeln('<H2>Question ' + (questionIndex + 1) + '</H2>');
	spawn.document.writeln('<FORM>');
	spawn.document.writeln('<P>' + question + '</P>');
	spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"a\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionA + '<BR>');
	spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"b\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionB + '<BR>');
	//spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"c\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionC + '<BR>');
	//spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"d\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionD + '<BR>');
	spawn.document.writeln('</FORM></BODY></HTML>');
	spawn.document.close();
	
	// Increment variables for the next question
	questionIndex++;
	preventer++;
	}

function gradeTest() {
	// Increment the variables for the last time
	questionIndex++;
	preventer++;
	
	// Compare student answers with correct answers
	for (var i = 0; i < questions.length; i++) {
		if (keeper[i] == answers[i]) {
			correctAnswers++;
			}
		}
	
	// Determine a ranking according to number of corrrect answers
	jsAbility = performance[Math.ceil((correctAnswers/questions.length) * performance.length - 1)];

	// Print the test results
	printResults();
	}

// Print the questions, answer options, and other info
function printResults() {
	spawn.document.clear();
	spawn.document.open();
	spawn.document.writeln('<HTML><HEAD>');
	spawn.document.writeln('<TITLE>Immigrants Rights</TITLE>');
	spawn.document.writeln('</HEAD><BODY onclose=\"alert(\"Lost Focus\")\" BGCOLOR=\"WHITE\"><form action=\"addtryme.asp\" name=\"frmuser\" method=\"post\">');						
	spawn.document.writeln('<div align=\"center\"><img src=\"images/immigrants_rights.gif\"></div>');
        spawn.document.writeln('<FONT FACE=\"Arial\">');
	spawn.document.writeln('<H2>You scored ' + correctAnswers + '/' + questions.length + ' correctly.</H2>');
        spawn.document.writeln('<input type=\"hidden\" value=\"' + correctAnswers +' \" name=\"correct\">');
        spawn.document.writeln('<input type=\"hidden\" value=\"Immigrant Rights\" name=\"test_name\">');
	spawn.document.writeln('<B>Ranking: ' + jsAbility + '</B><BR>');
	spawn.document.writeln('<BR><BR><FONT SIZE=\"4\">Here is how you scored: </FONT><BR><BR>');

	for (var i = 0; i < questions.length; i++) {
		spawn.document.writeln('\n\n<B>Question ' + (i + 1) + '</B><BR>');
		spawn.document.writeln(questions[i].substring(0, questions[i].indexOf('$$')));
		spawn.document.writeln('<BR><BR>\n<FONT SIZE=\"-1\">a. ' + questions[i].substring((questions[i].indexOf('$$') + 2), questions[i].indexOf('%%')) + '<BR>');
		spawn.document.writeln('b. ' + questions[i].substring(questions[i].indexOf('%%') + 2, questions[i].indexOf('^^')) + '<BR>');
//		spawn.document.writeln('c. ' + questions[i].substring(questions[i].indexOf('^^') + 2, questions[i].indexOf('&&')) + '<BR>');
//		spawn.document.writeln('d. ' + questions[i].substring(questions[i].indexOf('&&') + 2, questions[i].length) + '<BR></FONT>');

			// Determine if the student answered each question appropriately, and display accordingly
			if (keeper[i] == answers[i]) {
			spawn.document.writeln('<B><I><FONT COLOR=\"GREEN\">The correct answer is:</i> '+ answers[i] +'<br>You answered this correctly.</FONT><br><br><b>The reason is:&nbsp;</b></b>' + questions[i].substring(questions[i].indexOf('^^') + 2, questions[i].indexOf('&&')) + '<hr><input type=\"hidden\" name=\"question' +(i+1)+ '\" value=\"' +answers[i] + '"></I></B>\n<BR>');
				}		
			else { spawn.document.writeln('<B><I><FONT COLOR=\"RED\">The correct answer is:</i> '+ answers[i] +'<br><input type=\"hidden\" name=\"question'+(i+1)+'\" value=\"' +(i+1)+ '"></font><br>The reason is:</b> ' + questions[i].substring(questions[i].indexOf('^^') + 2, questions[i].indexOf('&&')) + '<hr><BR></B>');}
		}

	// Add extra info about the ranking, questions, and application
	spawn.document.writeln('\n\n<BR>If you came out on top, congratulations! If not, don\'t worry. This test was ');
	spawn.document.writeln('designed to be difficult. You can always <A HREF="javascript: opener.itemReset(); opener.startQuiz(false);" onMouseOver="window.status=\'\'; return true"><B>retake</B></A> it. ');
	spawn.document.writeln('\n\n<br><br><div align=\"center\"><input type=\"button\" onclick=\"self.close()\" value=\"Close this window\"></div></FORM></BODY></HTML>');
//<input type=\"submit\"> Add this line instead of the button to submit to the database...
	spawn.document.close();
	}

function busted() {
	alert('Sorry, you only get one shot at each question.');
	spawn.history.forward(); 
	spawn.focus();
	}