<?php

require "lib.php";
$return = '';
$error_404="unknown";
//if index.php => index.html
$uri=explode("/",$_SERVER['REQUEST_URI']);
if ( $uri[count($uri)-1] == "index.php" )
{
	header("Location: /index.html\n\n");
	exit;
}

//is word var : stripword
if (isset($_GET['word']))
{
	$word=stripword($_GET['word']);
	if ( $word == "" )
		$wrong_char=1;
} else
        $word="";

//check accent var
$add_accent="no";
if (array_key_exists('accent', $_COOKIE) && $_COOKIE['accent'] == 'yes')
{
	$add_accent="yes";
}
if ( mb_substr($word, 0, 1) == ":" )
{
	$word   = removeaccent(mb_substr($word, 1));
	$add_accent="yes";
	setcookie("accent", $add_accent, time()+3600*24*30, "/");
}

//title
$title = "sensmotdire - conjugaison francaise libre";
if (($word != "") and (!isset($wrong_char)))
	$title .= " de $word";

//class display_html
$display_html = new display_html($title);
$display_html->head();

//default charset : UTF-8
mb_internal_encoding("UTF-8");

//menu
$content = "\n\t<div id=\"menu\">";
$content .= "\n\t\t<ul>";
$content .= "\n\t\t\t<li><a href=\"add_browser.php\" id=\"aaddbrowser\">" . translate("Add to your browser") ."</a></li>";
$content .= "\n\t\t\t<li><a href=\"accessibility.php\" id=\"aaccess\" accesskey=\"0\">" . translate("Accessibility") . "</a></li>";
$content .= "\n\t\t\t<li><a href=\"rss20.xml\" id=\"anews\">" . translate("News") ."</a></li>";
$content .= "\n\t\t\t<li><a href=\"faq.php\" id=\"afaq\">F.A.Q.</a></li>";
$content .= "\n\t\t\t<li><a href=\"about.php\" id=\"aabout\">" . translate("About") . "</a></li>";
$content .= "\n\t\t</ul>";
$content .= "\n\t</div>";

//search form
$content .= "\t" . '<form id="formword" method="get" action="index.php">' . "\n\t\t<fieldset id=\"fieldword\"><legend>" . translate("Verb to be combined") . "</legend>\n\t\t\t" . '<input type="text" id="textword" name="word" accesskey="4"';
if ( ! $word == "" )
	$content .= " value=\"" . $word . "\"";
$content .= "/>\n\t\t\t" . '<label><input type="checkbox" name="accent" id="checkword"';
if ( $add_accent == "yes" )
	$content .= ' checked="checked" ';
$content .= ' accesskey="5" />' . translate("I write without accent") . '</label>' . "\n\t\t\t" . '<input type="submit" value="' . translate("Combine") . '" id="conjuguer" />' . "\n\t\t</fieldset>\n\t</form>";

//if forbidden char
if (isset($wrong_char))
	$content .= "\n\t\t\t<div class=\"error\">" . translate("Only alphabetical characters, accentuated characters, apostrophe and space are accepted.") . "</div>" . display_suggest("error");
else {

//array with all the search word (remove null word)
	if ($word != "")
	{
		$orig_word=$word;
		$word_array=is_a_conjugated_verb($word, $add_accent);
		$content .= "\n\t<h1><span>" . translate("Conjugation to") . " $word</span></h1>\n\t<div class=\"tab\">\n\t\t<div class=\"tabber\">";

		if ($word != "" and mb_strlen($word) < 25 )
		{
			// if array $verbs with all infinitive verb exist
			if (isset($word_array))
			{
				$i=0;	
				foreach ($word_array as $verb=>$info)
				{
					$i++;
					$display = new display($verb, $word);
					$display->display_tenses($tenses, $add_accent, $info);
					$return .= $display->close();
					if ($i > $NB_VERBS)
						break;
				}
				$error_404="no";
			} else {
				// if $verbs is not set
				if ( $error_404 == "unknown" )
					$error_404="yes";
//				if (is_array($add_pronouns))
//					$return .= "\n\t\t\t<div class=\"tabbertab\" id=\"" . $word . '">' . "\n\t\t\t\t<h2>" . translate("Unknown verb") . ': ' . $word . "</h2>\n\t\t\t\t<div class=\"error\">" . translate("Unknown verb or wrong conjugation") . ": $orig_word</div>";
//				else
					$return .= "\n\t\t\t<div class=\"tabbertab\" id=\"" . $word . '">' . "\n\t\t\t\t<h2>" . translate("Unknown verb") . ': ' . $word . "</h2>\n\t\t\t\t<div class=\"error\">" . translate("Unknown verb") . ": $word</div>";
				$return .= display_suggest($word);
				$return .= "\n\t\t\t</div>";
			}
			// display all
			unset($verbs);
		} else
			$return .= "\n\t\t\t<div class=\"tabbertab\" id=\"" . $word . '">' . "\n\t\t\t\t<h2>" . translate("Wrong verb") . ': ' . $word . "</h2>\n\t\t\t\t<div class=\"error\">" . translate("Your demand is too long") . ": $orig_word</div>";
	}
}

if (isset($return))
	$return .= "\n\t\t</div>\n\t</div>";
else
	$return = "";

if ($error_404 == "yes")
	header("HTTP/1.1 404 Not Found");

$display_html->body($content . $return, "index");
print $display_html->close();
