<?php 
$answers=Array('r','r','r','j','j','r','r','r','j','j','j');
$questionCount=count($answers);
$missed=array();
if (array_key_exists("doit", $_POST)) {
	for ($i=0; $i< $questionCount; $i++) {
		if (!array_key_exists("question" . $i, $_POST)) {
			$missed[] = $i;
			$_POST["question" . $i] = "null";
		}
	}
} else {
	// First display so put default values in for answers
	for ($i=0; $i< $questionCount; $i++) {
		$_POST["question" . $i] = "null";
	}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>Know your twinkles</title>
</head>

<body>
<table border="0" cellpadding="25">
	<tr>
		<td>
			<h1>Take the twinkle test</h1><br>
<?php
	if (array_key_exists("doit", $_POST)) {
		if (count($missed) == 0) {
			$score=0;
			for ($i=0; $i< $questionCount; $i++) {
				if ($answers[$i] == $_POST["question" . $i]) {
					$score++;
					echo "<br>";
				}
			}
			// <cfif form.question1 eq "jane"><cfset score=score+1></cfif>
			echo '<font color="blue"><b>Your score was ' . $score . ' out of ' . $questionCount .'<br><br>';
			if ($questionCount == $score) {
				echo 'Well done! All correct.';
			} else if (($questionCount - $score) == 1) {
				echo 'Close! Just one wrong.';
			} else if (($questionCount - $score) == 2) {
				echo 'Not half bad. Just a couple wrong.';
			} else if ($score > ($questionCount / 2)) {
				echo 'Not too bad - at least you got more than half right.';
			} else {
				echo 'Not good. Have you met the twinkles?';
			}
			echo '<br><br>Try again...</b></font>';
		} else {
			echo 'You missed ' . count($missed) . ' questions - answer the ones marked in red and try again...';
		}
	} else {
		echo 'So, you think you know your twinkles? Take the test and see how you do...<br><br>';
	}
	echo '<table border="0">
				<form action="test.php" method="post">';
	for ($i=0; $i< $questionCount; $i++) {
		echo '<tr><td><img src="pic' . ($i+1) . '.jpg"></td>';
		echo '<td ' . ((array_search($i, $missed) !== false) ? ' bgColor="red"' : '') . '>';
		echo '<input type="radio" name="question' . $i . '" value="j" ' . ($_POST['question' . $i] == 'j' ? 'checked' : '') . '>Jane &nbsp; &nbsp; &nbsp;';
		echo '<input type="radio" name="question' . $i . '" value="r" ' . ($_POST['question' . $i] == 'r' ? 'checked' : '') . '>Rachel';
		echo '						</td></tr>';
	}
	echo '<tr><td><input type="submit" name="doit" value="How\'d I do?"></td></tr></form></table></td>
		<td valign="top"><img src="b.jpg"></td>
	</tr>
</table>
</body>
</html>';
?>
