#!/usr/bin/perl

# (c) NueDream Inc. 2000-2001

##############    GLOBAL VARS AND CONSTS    ############
$file = "questions.db"; # This is the name of your questions file.
$quizname = "Mathematical Statistics Sections 3.5-5.9"; # THis is what appears in the TITLE.

############   THESE ARE OPTIONAL CHANGES    ###########
$spacer = '|';# leave it unless you already have a database

##############   GET INFO   ############

$content = $ENV{'QUERY_STRING'};
($temp1, $temp2) = split(/&/, $content);
($question, $answer) = split(/=/, $temp1);
($type, $question) = split(/=/, $temp2);

$start = $question+1;
$url = "quiz.cgi?$type=$start";


$cookie = $ENV{'HTTP_COOKIE'};
(@cookies) = split(/;/, $cookie);

##############    MAIN PROGRAM    ############

open(DATA,$file) or dienice("Couldn't open $file :: $!\n");
#flock(DATA,0); # uncomment this line if using unix based server
seek(DATA,0,0);
@data = <DATA>;
close(DATA);

foreach $line (@data) {
 	chomp($line);
 	($quiz,$num,$q,$ans,$a,$b,$c,$d,$e,$f,$reason) = split(/\|/,$line);
	if ($quiz eq $type && $question eq $num) {
		if(@cookies != "") {
 			
 			foreach $crum (@cookies) {
 				if ($crum =~ /$type/) {
 					($name,$value) = split(/=/,$crum);
 					
 					if ($value !~ /$question/) {
		                        	$tempcookie = $value . "-" . $question . "_" . $answer . $ans;
		                        	print "Set-cookie: $type=$tempcookie\n";
						print "Location: $url\n\n";
					} else {
                                                print "Content-type: text/html\n\n";
		                  	        print "<br><br><center><font face=\"verdana\" size=\"2\"><b>You Cheated!!!</b><br><br>If you did not try to cheat, try again. This time do not double click the next button.<br> Click once and wait until the next question is fully displayed before clicking again.</font></center>";
					}
		   			 
				}
 			}
 			
		} else { 
			$tempcookie = $question . "_" . $answer . $ans;
			print "Set-cookie: $type=$tempcookie\n";
			print "Location: $url\n\n";
		}
	}
}
