#!/usr/bin/perl
use CGI qw(:standard);

print <<eND;
Content-type: text/html

eND

$cookieid = "atid";
$cookie = $ENV{"HTTP_COOKIE"};

$cookie =~ m/$cookieid\=([^;]+)/;
$id = $1;

$time = time;

$id = $time unless $id;

$subfolder = substr($id,0,3);

$dirlocation = "/home12c/sub007/sc13648-GPVH";
chdir($dirlocation) || print "Couldn't change directory $!<BR>\n";



$event = param("event");

if ($event eq "change") {
	$phrases = param("pos");
	open(DATA,">ids/$subfolder/$id/hypposz.txt") || open(DATA,">ids/$subfolder/$id/hypposz.txt");
	print DATA $phrases;
	close(DATA);
	$phrases = param("posq");
	open(DATA,">ids/$subfolder/$id/hypposq.txt") || open(DATA,">ids/$subfolder/$id/hypposq.txt");
	print DATA $phrases;
	close(DATA);
	$phrases = param("neg");
	open(DATA,">ids/$subfolder/$id/hypneg.txt") || open(DATA,">ids/$subfolder/$id/hypneg.txt");
	print DATA $phrases;
	close(DATA);
	$phrases = param("negq");
	open(DATA,">ids/$subfolder/$id/hypnegq.txt") || open(DATA,">ids/$subfolder/$id/hypnegq.txt");
	print DATA $phrases;
	close(DATA);

	push @togo, qq~<p><a href=/cgi-bin/atmain.pl>Return to training page</a>~;
	printonblankerpage(@togo);
}

if ($event eq "list") {
	push @togo, qq~Each of the four boxes contains information. In each box, each piece of information should be its own line/paragraph. In other words, write a phrase and then press enter when you are done. Then write another phrase. Try a few for practice to make sure you understand.~;
	open(DATA,"ids/$subfolder/$id/hypposz.txt") || open(DATA,"ids/$subfolder/$id/hypposz.txt");
	push @phrases, <DATA>;
	close(DATA);
	push @togo, qq~<p>These are the things you will be told to do.<p><form method=post action="/cgi-bin/rare/edithyp.pl"><center><textarea name=pos rows=15 cols=50>~;
	foreach (@phrases) {
		push @togo, $_;
	}
	push @togo, qq~</textarea></center><br><BR>\n~;

	open(DATA,"ids/$subfolder/$id/hypposq.txt") || open(DATA,"ids/$subfolder/$id/hypposq.txt");
	push @phrases2, <DATA>;
	close(DATA);
	push @togo, qq~These are the consequences for doing what you were supposed to do. Such as being happy, or being confident.<p><center><textarea name=posq rows=15 cols=50>~;
	foreach (@phrases2) {
		push @togo, $_;
	}
	push @togo, qq~</textarea></center><br><BR>\n~;

	open(DATA,"ids/$subfolder/$id/hypneg.txt") || open(DATA,"ids/$subfolder/$id/hypneg.txt");
	push @phrases3, <DATA>;
	close(DATA);
	push @togo, qq~These are things that you should not be doing.<p><center><textarea name=neg rows=15 cols=50>~;
	foreach (@phrases3) {
		push @togo, $_;
	}
	push @togo, qq~</textarea></center><br><BR>\n~;

	open(DATA,"ids/$subfolder/$id/hypnegq.txt") || open(DATA,"ids/$subfolder/$id/hypnegq.txt");
	push @phrases4, <DATA>;
	close(DATA);
	push @togo, qq~These are negative consequences, things you do not want to happen to you, such as failing and forgetting.<p><center><textarea name=negq rows=15 cols=50>~;
	foreach (@phrases4) {
		push @togo, $_;
	}
	push @togo, qq~</textarea></center><br><BR>\n~;

	push @togo, qq~<input type=submit value="make these changes"><input type=hidden name=event value=change></form></center>~;

	push @togo, qq~<p><a href=/cgi-bin/atmain.pl>Return to training page (without making any unsaved changes)</a>~;
	printonblankerpage(@togo);
}

exit;

sub printonblankerpage {
	@toprint = @_;
	chdir("/home12c/sub007/sc13648-GPVH/at");
	open(DATA,"cgi-bin/shells/atshell.htm") || open(DATA,"cgi-bin/atshell.htm")  || print "There was a problem reading body: $!";
	@lines = <DATA>;
	close(DATA);
	$contentmarker = "<!--content-->";

	foreach (@lines) {
		if (m/$contentmarker/o) {
			print @toprint;
		}
		else {
			print;
		}
	}
	exit;
}


