#!/usr/bin/perl -w

$| = 1;

#use strict;
#use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

our $cgi = new CGI;
print $cgi->header();

our $dir = './';

my $temp_genus       = $cgi->param('genus');
my $temp_noun_or_not = $cgi->param('noun_or_not');
my $temp_word_type   = $cgi->param('word_type');     

my $question = $cgi->param('ask');

my $log  = $cgi->param('log');
my $lang = $cgi->param('lang') || $cgi->param('language');

sub LANGUAGE {
	return $lang || 'de';
}



sub footer {
	return if $show_simple;
	return << "	EOT";
	<p>&nbsp;</p>
	<p>&nbsp;</p>
<center>
<script type="text/javascript"><!--
google_ad_client = "pub-9576216575503343";
/* 728x90, Erstellt 01.03.08 */
google_ad_slot = "7473292167";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</center>
</div>
</div>

<p>&nbsp;</p>

<p>
<center>Alle Texte auf dieser Website k&ouml;nnen unter den Bedingungen der GNU Free Documentation License verwendet werden.</center>
</p>

<p>&nbsp;</p><p>&nbsp;</p>
</body>
</html>

	
	EOT
}


my $submit_and_get_answer = (LANGUAGE() eq 'de') ? 'Absenden und vorherigen Satz beantworten'
                                        : 'submit and get answer';
 
sub impl_get_genus {
	my ( $CLIENT_ref, $word ) = @_;
	my $CLIENT = $$CLIENT_ref;

	if ($temp_genus) {
		my $temp = $temp_genus;
		$temp_genus = undef;
		return $temp;
	}

	print << "	EOT";
	
	--></div>
	
	<p>
	EOT
	if ( LANGUAGE() eq 'de' ) {
		print << "		EOT";
	Welches grammatische Geschlecht (der, die, das) hat das Wort "$word"?
		EOT
	}
	else {
		print << "		EOT";
	Which genus is "$word"?
		EOT
	}
	print << "	EOT";
	</p>
	
	<form method="post">
	<select name='genus' size='5'>
	EOT
	if ( LANGUAGE() eq 'de' ) {
		print << "		EOT";
 		<option value='1'>m&auml;nnlich</option>
 		<option value='2'>weiblich</option>
 		<option value='3'>s&auml;chlich</option>
		EOT
	}
	else {
		print << "		EOT";
 		<option value='1'>male</option>
 		<option value='2'>female</option>
 		<option value='3'>thing</option>
		EOT
	}
	print << "	EOT";
 	</select>	<br/>	<br/>
	<input type="hidden" name="lang" value="$lang" />
	<input type="hidden" name="log" value="$log" />
 	<input type="hidden" name="ask" value="$question" />
 	<button name="ok" type='submit'>$submit_and_get_answer</button>
	EOT
	print '<input type="hidden" name="noun_or_not" value="' . ($cgi->param('noun_or_not')) . '" />';
	
	print << "	EOT";
	</form>
	
	EOT
	
	print footer();

	exit 0;
}

sub impl_get_noun_or_not {
	my ( $CLIENT_ref, $word ) = @_;
	my $CLIENT = $$CLIENT_ref;

	if ($temp_noun_or_not) {
		my $temp = $temp_noun_or_not;
		$temp_noun_or_not = undef;
		return $temp;
	}

	print << "	EOT";

	--></div>
	
	<p>
	EOT
	if ( LANGUAGE() eq 'de' ) {
		print << "		EOT";
	Ist das Wort "$word" als Nomen gemeint ?
		EOT
	}
	else {
		print << "		EOT";
	Is "$word" meant as a noun?
		EOT
	}
	print << "	EOT";
	</p>
	
	<form method="post">
	<select name='noun_or_not' size='5'>
	EOT
	if ( LANGUAGE() eq 'de' ) {
		print << "		EOT";
 		<option value='1'>Nomen</option>
 		<option value='2'>kein Nomen</option>
		EOT
	}
	else {
		print << "		EOT";
 		<option value='1'>it is a noun</option>
 		<option value='2'>it is no noun</option>
		EOT
	}
	print << "	EOT";
 	</select>	<br/>	<br/>
	<input type="hidden" name="lang" value="$lang" />
	<input type="hidden" name="log" value="$log" />
 	<input type="hidden" name="ask" value="$question" />
 	<button name="ok" type='submit'>$submit_and_get_answer</button>
	</form>
	
	EOT

	print footer();

	exit 0;
}

sub impl_get_word_type {
	my ( $CLIENT_ref, $word ) = @_;
	my $CLIENT = $$CLIENT_ref;

	if ($temp_word_type) {
		my $temp = $temp_word_type;
		$temp_word_type = undef;
		return $temp;
	}

	print << "	EOT";
	
	--></div>

	<p>
	EOT
	if ( LANGUAGE() eq 'de' ) {
		print << "		EOT";
	Welche Wortart hat das Wort "$word"?
		EOT
	}
	else {
		print << "		EOT";
	Which word type is "$word"?
		EOT
	}
	print << "	EOT";
	</p>
	
	<form method="post">
 	<select name='word_type' size='6'>
	EOT
	if ( LANGUAGE() eq 'de' ) {
		print << "		EOT";
 		<option value='2'>Nomen oder Name oder Pronomen</option>
 		<option value='1'>Verb</option>
 		<option value='3'>Adjektiv oder Adverb</option>
 		<option value='5'>Fragewort oder Konjunktion</option>
 		<option value='6'>Pr&auml;position</option>
 		<option value='7'>Interjektion / Ausruf</option>
		EOT
	}
	else {
		print << "		EOT";
 		<option value='2'>noun or name or pronoun</option>
 		<option value='1'>verb</option>
 		<option value='3'>adjective or adverb</option>
 		<option value='5'>question word or conjunction</option>
 		<option value='6'>preposition</option>
 		<option value='7'>interjection</option>
		EOT
	}
	print << "	EOT";
 	</select><br/><br/>
	<input type="hidden" name="lang" value="$lang" />
	<input type="hidden" name="log" value="$log" />
 	<input type="hidden" name="ask" value="$question" />
 	<button name="ok" type='submit'>$submit_and_get_answer</button>
	EOT
	print '<input type="hidden" name="noun_or_not" value="' . ($cgi->param('noun_or_not')) . '" />';
	
	print << "	EOT";

	</form>		 		
	EOT

	print footer();

	exit 0;
}

print << "EOT" if !$show_simple;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
	<title>JEliza - Die Opensource KI</title>
	<link rel="stylesheet" type="text/css" media="screen" href="http://www.jeliza.org/styles.css" />
	
	
</head>
<body>

<div id="top">&nbsp;</div>

<div style="border: 1px solid grey; background-color: white; font-size: 11px; position: absolute; top: 10px; right: 300px; z-index: 5; padding: 5px;">
					<form method="post" action="http://developer.jeliza.org/online/jeliza-cgi.pl">
	Sprechen Sie mit JEliza! Geben Sie hier einen Satz ein:
	<br />
	<input type="text" name="ask" value="" />
	<input type="hidden" name="log" value="" />
	
	<button name="ok" type='submit'>Antwort erhalten!</button>
					</form>
</div>

<div class="jelizaeigenschaften">
	<table border="0" style="border: 0px;">
		<tr>
			<td>
				<a href="http://www.jeliza.org/download.php">
					<img src="http://www.jeliza.org/download.gif" alt="Download" /><br/>
					Download
				</a>
			</td>
			<td>
				<a href="http://wiki.jeliza.org/">
					<img src="http://www.jeliza.org/support.gif" alt="Wiki" /><br/>
					Wiki
				</a>
			</td>
			<td>
				<a href="http://forum.jeliza.org/">
					<img src="http://www.jeliza.org/support.gif" alt="Forum" /><br/>
					Forum
				</a>
			</td>
		</tr>
	</table>
</div>
<div id="sidebartop_l">
	<div class="menuheading">Navigation</div>
	<div class="sidebar">
		<strong>Das JEliza Projekt</strong><br />
		<ul>
		<li><a href="http://www.jeliza.org/"><span class="topic">Startseite</span></a></li>
		<li><a href="http://wiki.jeliza.org/"><span class="topic">JEliza Wiki</span></a></li>
		<li><a href="http://forum.jeliza.org/"><span class="topic">JEliza Forum</span></a></li>
		<li><a href="http://www.jeliza.org/license.php"><span class="topic">Lizenz</span></a></li>
		</ul>
		<br />

		<strong>JEliza Standard</strong><br />
		<ul>
		<li><a href="http://www.jeliza.org/jeliza-cpp-python.php"><span class="topic">Beschreibung</span></a></li>
		<li><a href="http://www.jeliza.org/download.php"><span class="topic">Download</span></a></li>
		<li><a href="http://wiki.jeliza.org/wiki/index.php5?title=JEliza-Erste_Schritte"><span class="topic">Erste Schritte</span></a></li>
		<li><a href="http://wiki.jeliza.org/wiki/index.php5?title=JEliza-FAQ"><span class="topic">H&auml;ufig gestellte Fragen</span></a></li>
		<li><a href="http://www.jeliza.org/screenshots.php"><span class="topic">Screenshots</span></a></li>
		</ul>
		<br />

		<strong>JEliza Perl</strong><br />
		<ul>
		<li><a href="http://www.jeliza.org/jeliza-perl.php"><span class="topic">Beschreibung</span></a></li>
		<li><a href="http://www.jeliza.org/download.php"><span class="topic">Download</span></a></li>
		<li><a href="http://wiki.jeliza.org/wiki/index.php5?title=JEliza-Perl-Erste_Schritte"><span class="topic">Erste Schritte</span></a></li>
		<li><a href="http://developer.jeliza.org/online/jeliza-cgi.pl"><span class="topic">Online-Version</span></a></li>
		</ul>
		<br />
		
		<strong>Allgemein</strong><br />
		<ul>
		<li><a href="http://www.jeliza.org/semnet.php"><span class="topic">Das Semantische Netz</span></a></li>
		<li><a href="http://wiki.jeliza.org/wiki/index.php5?title=JEliza_Dokumentation"><span class="topic">Dokumentation</span></a></li>
		<li><a href="http://www.jeliza.org/features.php"><span class="topic">Features</span></a></li>
		<li><a href="http://www.jeliza.org/museum.php"><span class="topic">JEliza Museum</span></a></li>
		</ul>
		<br />
		
		<strong>Entwicklung</strong><br />
		<ul>
		<li><a href="http://developer.jeliza.org/"><span class="topic">Entwicklerportal</span></a></li>
		<li><a href="http://wiki.jeliza.org/wiki/index.php5?title=JEliza-Entwicklertagebuch"><span class="topic">Entwickler- Tagebuch</span></a></li>
		</ul>
		<br />
		
		<strong>Kontakt</strong><br />
		<ul>
		<li><a href="http://www.jeliza.org/team.php"><span class="topic">Das Team / Impressum</span></a></li>
		<li><a href="http://www.jeliza.org/kontakt.php"><span class="topic">Kontakt</span></a></li>
		</ul>
		<br />
	</div>
	
</div>

<div class="contentpart">

<div style="float: right; border: 1px solid silver; padding: 5px; margin: 5px;">

<script type="text/javascript"><!--
google_ad_client = "pub-9576216575503343";
/* 120x600, Erstellt 25.02.08 */
google_ad_slot = "9946834990";
google_ad_width = 120;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="textcontentheading">JEliza - Online Version</div>
<div class="textcontent">
EOT
if ( LANGUAGE() eq 'de' ) {
	print << "	EOT";
<p>Dies ist die Online Version von <i>JEliza Perl</i>. Hier k&ouml;nnen Sie die k&uuml;nstliche Open-Source-Intelligenz
in ihrem Webbrowser ausprobieren.</p>
	EOT
}
else {
	print << "	EOT";
<p>This is the online version of our artificial intelligence <i>JEliza Perl</i>.</p>
	EOT
}
print << "EOT";

EOT

require 'jeliza-engine.pl';
$in_cgi_mode = 1;

#print "LANG: ", $lang;

#print $cgi->param('log');

if ( $cgi->param('check_is_true') ) {
	load_word_types();
	load_database_file( $dir . "/lang_" . LANGUAGE() . "/facts.pro" );

	do_check_is_true_on_facts($cgi->param('check_is_true'));
}

if ( $cgi->param('ask') ) {
	my $display_str = '';

	open my $CLIENT, ">>", "to-server.log";

	print "<div style='display:none;'><!--";

	load_word_types();
	load_database_file( $dir . "/lang_" . LANGUAGE() . "/facts.pro" );

	my $dialog = ask( \$CLIENT, $cgi->param('ask'), \$display_str );
	$log .= $dialog;

	print "--></div>\n\n";
	
	$log =~ s/[:]+/:/igm;
	
	$log =~ s/\n//gm;

	print $log;

	$log = (split /[<]iframe/, $log)[0];
	open my $dialog_file, ">>", 'dialog.txt';
	chomp $dialog;
	$dialog =~ s/[<]br[>]/\n/igm; 
	chomp $dialog;
	$dialog =~ s/[<](.*?)[>]//igm;
	$dialog =~ s/\s*$//igm;
	$dialog =~ s/^\s*//igm;
	print $dialog_file $dialog . "\n";
	close $dialog_file;

}

my $please_enter = (LANGUAGE() eq 'de') ? 'Bitte Satz eingeben:'
                                        : 'Please enter a sentence:';
                                        
my $button_answer = (LANGUAGE() eq 'de') ? 'Antworten!'
                                         : 'Get answer!';


print << "EOT" if !$show_simple;
	<p>&nbsp;</p>
	<form method="post">
	<table style="border: none;">
	<tr>
	<td style="padding-right: 20px;">
		$please_enter
	</td>
	<td colspan="2">
		<input type="text" name="ask" id="ask" value="" size="50" />
		<input type="hidden" name="log" value="$log" /><br />
	</td>
	</tr>
	<tr>
	<td>&nbsp;</td>
	<td style="width: 50px !important; padding-top: 10px !important;">
	<select name="lang">
EOT
print '	<option value="en" ' . (LANGUAGE() eq 'en' ? 'selected="selected"' : '') . '>  EN  </option>'  if !$show_simple;
print '	<option value="de" ' . (LANGUAGE() ne 'en' ? 'selected="selected"' : '') . '>  DE  </option>'  if !$show_simple;
print << "EOT" if !$show_simple;
	</select>
	</td>
	<td style="padding-top: 10px !important;">
	<button name="ok" type='submit'>$button_answer</button>
	</td>
	</tr>
	</table>
	</form>
	
	<script type="text/javascript">
		document.getElementById('ask').focus();
	</script>

EOT

if ($show_simple) {
	print "<iframe src='http://developer.jeliza.org/online/jeliza-cgi.pl' style='border: none; width: 0px; height: 0px;'></iframe";
}

print footer();
if ( $cgi->param('ask') ) {
	do_check_is_true_on_facts();
}
exit(0);


1;
