<?php
/*
 * Questions to SANET
 *
 * SCRIPT NAME : send_form.php
 * CREATED : Aug 26 2008
 * LAST UPDATED : Dec 25 2011 by okunuki@nagoya-u
 * LAST UPDATED : Jan 12 2011 by okunuki@nagoya-u
 * LAST UPDATED : Sep 5 2010 by fujita@csis
 * COPYRIGHT : kayo okabe;
 *
 */
 
/* USER SETTING */
/*----------------------------------------------------------*/
$mail_to = "sanet.question@gmail.com";
$mail_from = "sanet.question@gmail.com";
$subject = "Help with SANET: ";

/*----------------------------------------------------------*/
/* SETTING */
$self = $_SERVER['SCRIPT_NAME'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {

	$name = trim($_POST['name']);
	$mail = trim($_POST['mail']);
	$address = trim($_POST['address']);
	$affiliation = trim($_POST['affiliation']);
	$question = trim($_POST['question']);
	

	/* ERROR CHEACK */
	if (empty($name)) {
		$errorCmt1 = "your name<br>\n";
	}
	if (empty($mail)) {
		$errorCmt2 = "your e-mail address<br>\n";
	}
	if (empty($address)) {
		$errorCmt3 = "your country<br>\n";
	}
	if (empty($affiliation)) {
		$errorCmt4 = "your affiliation<br>\n";
	}
	if (empty($question)) {
		$errorCmt5 = "your question<br>\n";
	}
	
	$message = "
<SANET questions >
--------------------------------------------------------------
*your name. :  $name
*your e-mail address : $mail
*your country : $address
*your affiliation : $affiliation
*your question: $question
";

	if ($mail > "") {
		if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $mail)) {
			$errorCmt6 = "There is a misatake in your e-mail address.<br>\n";
		}
	}

	if (isset($errorCmt1) || isset($errorCmt2) || isset($errorCmt3) || isset($errorCmt4) || isset($errorCmt5) ) {
		$error = "$errorCmt1$errorCmt2$errorCmt3$errorCmt4$errorCmt5 is missing.";
	}

	if (isset($errorCmt13)) {
		$error = $errorCmt13;
	}

	/* encoding  */
	mb_language("English");
	mb_internal_encoding("SJIS");

	/* mail sending */
	if (empty($error)) {

		// $send_email = mb_send_mail($mail_to, $subject, $message, $mail_from);

		$headers = "From: $mail_from\n";
		$headers .= "Mime-Version: 1.0\n";
		$headers .= "Content-Type: text/plain; charset=ISO-2022-JP\n";
		$headers .= "Content-Transfer-Encoding: 7bit\n";

		$subject .= $name;

		mail($mail_to, mb_encode_mimeheader(mb_convert_encoding($subject, "JIS"), "ISO-2022-JP", "B"), mb_convert_encoding($message, "JIS"), $headers);
		
		$msg = "<P align=\"center\">Completed</P><P align=\"center\">Thanks for sending us a question. We will reply you soon by e-mail.</P>\n";
		message($msg, 2);
		
	} else {
		message($error, 1);
	}
}

/* message */
function message($message, $button) {

	$back = "<input type=\"button\" value=\"@back@\" onclick=\"history.back()\">";
	$top = "<input type=\"button\" value=\"BACK TO  MAIN MENU\" onClick=\"top.location.href='http://sanet.csis.u-tokyo.ac.jp/index.html'\">";

	$_back = ($button == 1) ? $back : $top;

	/* error dialog */
	die("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=shift_jis\" /><title>Questions to SANET</title></head>\n<body bgcolor=\"#E6FCFF\">\n<P align=\"center\"><font color=\"red\">$message</font></P>\n
<P align=\"center\">$_back</P>\n
</body>\n</html>");

}

?>

<!-- HTML -->
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis" />
<title>question to SANET</title>
</head>
<body leftmargin="30" bgcolor="#E6FCFF">
<form method="post" action="<?php echo $self; ?>">
  <table  width="600" height="600" border="0" bordercolor="#E6FCFF" bgcolor="#E6FCFF">
    <tr>
      <td width="425" align="center"><h2 align="center"><font color="#FF6600">Questions to SANET</font></h2></td>
    </tr>
    <tr>
      <td width="425" align="center"><h3 align="left"><font color="#0000FF">Please enter your question.</font></h3></td>
    </tr>
    <td width="600" height="10"></td>
    <tr>
      <td width="600">Please enter your name.</td>
    </tr>
    <tr>
      <td width="600"><input type="text" name="name" size="50" ></td>
    </tr>
    <tr>
      <td width="600">&nbsp;</td>
    </tr>
    <tr>
      <td width="600">Please enter your e-mail address.</td>
    </tr>
    <tr>
      <td width="600"><input type="text" name="mail" size="50" ></td>
    </tr>
    <tr>
      <td width="600">&nbsp;</td>
    </tr>
    <tr>
      <td width="600">Please enter your country.</td>
    </tr>
    <tr>
      <td width="600"><input type="text" name="address" size="50" ></td>
    </tr>
    <tr>
      <td width="600">&nbsp;</td>
    </tr>
    <tr>
      <td width="600">Please enter your affiliation.</td>
    </tr>
    <tr>
      <td width="600"><input type="text" name="affiliation" size="50" ></td>
    </tr>
    <tr>
      <td width="600">&nbsp;</td>
    </tr>
    <tr>
      <td width="600">Please type your question.</td>
    </tr>
    <tr>
      <td width="600"><textarea name="question" rows="8" cols="50"></textarea></td>
    </tr>
    <tr>
      <td width="600">&nbsp;</td>
    </tr>
    <tr>
      <td width="600" align="left">Submit&gt;&gt;
      <input type="submit"  value="send"></td>
    </tr>
<!--
    <tr>
      <td width="600" align="left">Clear and try again&gt;&gt;
      <input type="submit" value="reset"></td>
    </tr>
-->
  </table>
</form>
</body>
