#!/usr/local/bin/perl




# Preference variables

$page_return = 'http://staff.city.ac.uk/psychstudies/TV/Finished.htm';
$surveyFile;
$condition;
$surveyVersion;
$p_numberfile;

$resultData;
$usernumber;
my @fieldnames;	
my @fieldvalues;
my %var_Hash;
$state ;


my $now = localtime time;
$request_method = $ENV{'REQUEST_METHOD'};

if ($request_method eq "GET") {
 

   if (length ($ENV{'QUERY_STRING'}) > 0){
      $buffer = $ENV{'QUERY_STRING'};
      @pairs = split(/&/, $buffer);
      foreach $pair (@pairs){
           ($name, $value) = split(/=/, $pair);
           $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
           $in{$name} = $value; 
       }

      $state = $in{'State'};
       $resultData = $in{'resultData'};
      }
  }else {




           

  read (STDIN, $buffer1, $ENV {'CONTENT_LENGTH'});
  @pairs1 = split (/&/,$buffer1);
  $ip = $ENV{'REMOTE_ADDR'};
  foreach $pair1 (@pairs1){
    ($name,$value) = split(/=/,$pair1);

    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-f0-9][a-fA-f0-9])/pack("C", hex($1))/eg;
    $value =~ s/<!--(.|\n)*--//g;
    #$value=~ s/\n/,/g;
    
    $name =~  tr/+/ /;
    $name =~ s/%([a-fA-f0-9][a-fA-f0-9])/pack("C", hex($1))/eg;
    $name =~ s/<!--(.|\n)*--//g;
    $var_Hash{$name} =$value;
    push(@fieldnames, $name);
    push(@fieldvalues,$value);
    
        
   if ($name eq "State"){
       $state = $value ;
    }
    if ($name eq "resultData"){
       $resultData = $value ;
    }
    if ($name eq "SurveyCondition"){
       $SurveyCondition= $value ;
    }
    if ($name eq "SurveyVersion"){
       $surveyVersion = $value ;
    }
    
    
  }


}#else

$numfields = scalar(@fieldnames);


    if ($state eq "Intro"){
       $surveyFile = "/u5.swansea/s33/ra902/public_html/TV/Main.htm";
       $p_numberfile = "/u5.swansea/s33/ra902/public_html/TV/conditionindex.txt";
       open(EFILE,"$p_numberfile") or dienice("Can't open $p_numberfile for reading: $!");
 		   @P_number = <EFILE>;
    	 close(EFILE);
 			 $usernumber = @P_number[0];

        if (($usernumber % 7) eq 0){
                      $condition = "G";
     			}elsif (($usernumber % 7) eq 1){
                      $condition = "A";
     			}elsif (($usernumber % 7) eq 2){
                    $condition = "B";
     			}elsif (($usernumber % 7) eq 3){
                    $condition = "C";
     			}elsif (($usernumber % 7) eq 4){
                    $condition = "D";
     			}elsif (($usernumber % 7) eq 5){
                    $condition = "E";
     			}elsif (($usernumber % 7) eq 6){
                    $condition = "F";
     			}

		$usernumber = @P_number[0] + 1;
		open(fileOUT,">$p_numberfile") ;
		flock(fileOUT, 2);
		print fileOUT $usernumber;
		close(fileOUT);
                

		open(EFILE,$surveyFile) or dienice("Can't open $surveyFile for reading: $!");
		flock(EFILE, 2);
		@arrresults = <EFILE>;
		close(EFILE);



     print "Content-type:text/html\n\n";

		foreach $line (@arrresults) {

  		   if (index($line,"</form>")==-1){
       			  print  $line;
                        
    		   }else{
            print  "<input type=\"hidden\" name=\"State\" value=\"Store\">\n";
       	#		print  "<input type=\"hidden\" name=\"p_number\" value=\"@P_number[0]\">\n";
            print  "<input type=\"hidden\" name=\"SurveyVersion\" value=\"$condition\" id = \"SurveyVersion\" >\n";
       			print   $line;
    		   }

		}





        }elsif($state eq "Store"){
		open(fileOUT, ">>/u5.swansea/s33/ra902/public_html/TV/Secure/Results.txt") or dienice("Surveyresults.txt for writing: $!");

		flock(fileOUT, 2);

		seek(fileOUT, 0, 2);

                print fileOUT "$surveyVersion,";
                print fileOUT "$now,";
                print fileOUT "$resultData";
                print fileOUT ",$SurveyCondition,";
                print fileOUT "$ip";
                print fileOUT "\n";
                close(fileOUT);
                finalPage();
        }else{
        }

              



sub finalPage
{

   print "Content-type:text/html\n\n";
   print  "<html>";
   print "<head>" ;
   print "<title>Finished</title>\n";
   print "</head>";
   print "<body>" ;
   print "<p align=\"center\">&nbsp;</p>\n";
   print "<p align=\"center\">&nbsp;</p>\n";
   print "<p align=\"center\"><font face=\"Arial\" size=\"4\">Congratulations!</font></p>\n";
   print "<p align=\"center\"><font face=\"Arial\" size=\"4\">Your performance score was $SurveyCondition</font></p>\n";
   print "<p align=\"center\"><font face=\"Arial\" size=\"4\">Thank you</font></p>\n" ;
   print "<p align=\"center\">&nbsp;</p>\n";
   print "<p align=\"center\">\n";
   print "<a href=\"https://cityss.qualtrics.com/SE/?SID=SV_1QYu4U0jAQdaP2c\">Survey</a></p>\n";
   print "</body>";
   print "</html>";
    exit;
}

sub dienice
{
    print "Content-type:text/html\n\n";
    my($msg) = @_;
    print "<html>\n<head>\n<title>Error Opening File!</title>\n";
    print "</head>\n";
    print "<body><h2>Error</h2>\n<b>";
    print $msg;
    print "\n</b></body>\n</html>";
    exit;
}
