<?php

	include "library.php";

	$cxn = start_code();

	
			/* get variables */

	$account_id = val( $_SESSION, "account_id" );

	$send_to_id = val( $_GET, "sendtoid" );

	$send_type = val( $_GET, "sendtype" );

	$returnpoint = val( $_GET, "returnpoint" );

	$startpos = val( $_GET, "startpos" );

	if ($returnpoint == "")
		$returnpoint = val( $_SESSION, "returnpoint" );

	if ($startpos == "")
		$startpos = val( $_SESSION, "startpos" );

	
		/* if the user is not logged in log them in then back to here */

	if ($account_id == "")
	{
		$_SESSION['nextpage'] = url_encode( "send_message.php", "sendtoid", $send_to_id, "sendtype", $send_type );

		jump_to_page( "login.php" );	/* must happen before print output */

		exit();
	}

		/* check id's */


	crash_if_missing( $send_to_id, "Send to id missing in send_message", $cxn );

	crash_if_missing( $send_type, "Send type missing in send_message", $cxn );

	crash_if_missing( $returnpoint, "return point missing in send message", $cxn );



		/* check visibilty */

	$query = "SELECT * FROM account WHERE id = ".$account_id;
	$result = run_query( $query, $cxn );

	$data = get_row( $result );


		/* check for a valid email address */

	if ($data['email_address_verified'] == "no")
	{
		page_header( "", $cxn );
		echo black( "You cannot send enquiry messages until your email address has been verified.<br><br>" );
		echo black( " Please click on the link in the verification email that has arrived in your email inbox.<br><br>" );
		echo black( " Your email address is listed as <span class='gemail'>".$data['email_address']."</span>" );
		echo black( " If this is not your correct email address please click on 'Edit Account' on the home page and update your email address.<br><br>" );
		echo black( " Please check your inbox or " ).show_link( "click here", "send_verification_email.php", "sendid", $account_id, "code", $data['email_verification_code'] );
		echo black( " to send the verification email again.<br><br>" );
		echo show_home_link();

		exit();
	}


	if ($send_type == "email")
	{
			/* email box */

		page_header( "Send Email", $cxn );

		form_start( "send_message_go.php", "send", "yes", "sendtype", "email", "sendtoid", $send_to_id, "returnpoint", $returnpoint, "startpos", $startpos );

		form_text( "Subject", "subject", 100, "" );

		form_textarea( "Message", "message", 25, 100, "" );

		echo "<tr><td></td><td align='center'><br><br>";
		
		show_green_button_submit( "Send", 75 );
		
		echo "</td></tr>";

		form_end();
		
		echo "<br><br><br><br><br><br><br>";
		
//		show_ads( $cxn );
	}
	else
		crash( "Internal Error: invalid send type in send_message", $cxn );

?>