<?php

	include "library.php";

	$cxn = start_code();
	
	$cxn_common = db_common_login();
	

	
			/* get variables */

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

	$send = val( $_GET, "send" );
	$send_type = val( $_GET, "sendtype" );
	$send_to_id = val( $_GET, "sendtoid" );

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

	page_header( "", $cxn, $cxn_common );

	$sent = date("Y-m-d H:i:s");

	crash_if_missing( $account_id, "account_id missing in send_message", $cxn );

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

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

	$returnpoint = "main";
	

//	if ($send == "no")
//		echo black( "Send cancelled<br><br>" );
//	else
//	{
		/* send the message */

	$subject = val( $_POST, "subject" );
	$message = val( $_POST, "message" );

	if ($subject == "" && $message == "")
	{
		echo black( "This email does not have a subject or body text" );
		exit();
	}

	$sqlsubject = prep_for_sql( $subject );
	$sqlmessage = prep_for_sql( $message );

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


	$username = $data['username'];

	$query = "INSERT INTO message (sender_id, receiver_id, type, subject, message, sent, show_in_inbox) values (".$account_id.", ".$send_to_id.", 'email', '".$sqlsubject."', '".$sqlmessage."', '".$sent."', 'yes' )";
	$result = run_query( $query, $cxn );

	$query = "SELECT id as message_id FROM message WHERE sender_id = ".$account_id." AND receiver_id = ".$send_to_id." AND sent = '".$sent."'";
	$result = run_query( $query, $cxn );
	$data = get_row( $result );

	$message_id = $data['message_id'];

	$domain = val( $_SERVER, "HTTP_HOST" );


		/* send an email externally */

	$text_body = $message."\n\n\n";
	$text_body .= "# This is a message from ".$_SESSION['sitename']." member ".$username.". Please do not reply to this address.\n\n\n";
	$text_body .= "# To reply to this message log into the system at ".$domain." and go to your 'messages' inbox.\n";

	$html_body = convert_to_html($message)."<br><br><br>";
	$html_body .= "<br><br>This is a message from ".$_SESSION['sitename']." member ".$username.".<br>";
	
	$html_body .= "<br>To reply to this message log into the system at ".$domain." and go to your 'messages' inbox.<br>";
	
//			$html_body .= "<br><font color='#999999'>To reply to this message click ".show_link( "reply", "http://".$domain."/display_profile.php", "showid", $id, "returnpoint", "main", "external", "true" )."</font>";

	send_external( $send_to_id, $subject, $text_body, $html_body, $cxn, $cxn_common, "live" );


		/* show succeeded on screen */

	echo black( "Email sent<br><br>" );


		/* warning about messages sent to themselves */

	if ($account_id == $send_to_id)
	{
		echo black( "Please note that you have sent this message to yourself. " );
		echo black( "If you want to send a message to another member, go to " );
		echo black( "a listing and click 'send enquiry'<br><br>" );
	}

//	}
//	else
//		crash( "Internal error: invalid send type in send message", $cxn );

	$query = "UPDATE message SET has_been_replied_to = 'yes' WHERE sender_id = ".$send_to_id." AND receiver_id = ".$account_id;

	$result = run_query( $query, $cxn );


	show_return_point( $returnpoint, $startpos, $send_to_id );

?>