<?php
	session_start();

	require_once( 'utils.php' );

	$error = "";
	$email = "";
	$email_confirm = "";
	$message = "";
	$contact = "";
	$convention = "";

	if ( isset( $_GET[ 'id' ] ) )
	{
		$_SESSION[ 'convention_email_id' ] = $_GET[ 'id' ];
		$convention_id = $_GET[ 'id' ];
	}
	else if ( isset( $_SESSION[ 'convention_email_id' ] ) )
	{
		$convention_id = $_SESSION[ 'convention_email_id' ];
	}
	else
	{
		$error = "No convention selected";
	}

	if ( $error == "" )
	{
		// Get convention details from database
		$db_connection = connect_to_database();

		$result = query_sql( "select * from convention where id = " . $convention_id, $db_connection );

		$row = mysql_fetch_array( $result );

		$convention = $row[ 'name' ];
		$contact = $row[ 'contact' ];
		$contact_email = $row[ 'email' ];

		mysql_free_result( $result );

		if ( isset( $_POST[ 'email' ] ) )
		{
			// Form filled in. Check and action it
			$email = $_POST[ 'email' ];
			$email_confirm = $_POST[ 'email_confirm' ];
			$message = $_POST[ 'message' ];
				
			if ( $email == "" )
			{
				$error = "Please enter your email address";
			}
			else if ( strcasecmp( $email, $email_confirm ) != 0 )
			{
				$error = "Please type you're e-mail address correctly in both boxes";
			}
			else if ( $message == "" )
			{
				$error = "Please enter a message to send";
			}
			else
			{
				$header = "Reply-To: " . $email . "\r\n"
						. "From: " . $email . "\r\n";

				$message = "This message was generated by a User of the Diplomatic Pouch\n"
						 . "Upcoming Conventions Page, www.diplom.org/Face/cons .\n"
						 . "\n"
						 . "They gave the address " . $email . "\n"
						 . "============================================================\n"
						 . $message;

				$mail_ok = mail( $contact_email, $convention, $message, $header );

				if ( $mail_ok == false )
				{
					$error = "Failed to send message";
				}
				else
				{
					goto_page( 'message_sent.php' );
				}
			}
		}
	}
?>

<html>
	<head>
		<title>
			E-mail a Convention Organiser
		</title>
	</head>
	<body>
		<p><h1>E-mail a Convention Organiser</h1></p>

		<?php report_error( $error ); ?>

		<form action="<?php link_to_self(); ?>" method="post">
			<table>
				<tr>
					<td>To</td>
					<td>
						<?php echo( $contact ); ?>
					</td>
				</tr>
				<tr>
					<td>Subject</td>
					<td>
						<?php echo( $convention ); ?>
					</td>
				</tr>
				<tr>
					<td>Your e-mail address</td>
					<td>
						<input type="text" name="email" value="<?php echo( $email ); ?>" size=40>
					</td>
				</tr>
				<tr>
					<td>Confirm e-mail address</td>
					<td>
						<input type="text" name="email_confirm" value="<?php echo( $email_confirm ); ?>" size=40>
					</td>
				</tr>
				<tr valign="top">
					<td>Message</td>
					<td>
						<textarea name="message" cols="60" rows="15"><?php echo( $message ); ?></textarea>
					</td>
				</tr>
				<tr>
					<td></td>
					<td>
						<input type="submit" name="Submit" value="Send E-mail">
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>
