<?php

	include "library.php";

	$cxn = start_code();
	
			/* get variables */

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

	$reply_to_id = val( $_GET, "replytoid" );

	$reply = val( $_POST, "reply" );


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


		/* start the page  */

	page_header( "", $cxn );


		/* check parameters */

	crash_if_missing( $id, "id missing in reply to kiss", $cxn );

	crash_if_missing( $reply_to_id, "reply to id missing in reply to kiss", $cxn );

	crash_if_missing( $returnpoint, "return point missing in reply to kiss", $cxn );


	if ($reply == "")
	{
		echo black( "You must select a reply from the list" );
		exit();
	}

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

		/* store the reply */

	$query = "INSERT INTO message (sender_id, receiver_id, type, message, show_in_inbox, sent) VALUES (".$id.", ".$reply_to_id.", 'kiss reply', '".$reply."', 'yes', '".$sent."')";

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


		/* update the original kiss(es) as replied to */

	$query = "UPDATE message SET has_been_replied_to = 'yes' WHERE type = 'kiss' AND sender_id = ".$reply_to_id." AND receiver_id = ".$id;

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


		/* send an email externally */

	$query = "SELECT handle, sex FROM profile WHERE id = ".$id;
	$result = run_query( $query, $cxn );
	$data = get_row( $result );

	$subject = "You have received a kiss reply from ".$_SESSION['sitename']." member ".$data['handle'];

	$text_body = "You have received a kiss reply from ".$_SESSION['sitename']." member ".$data['handle']."\n\n\n";

	if ($data['sex'] == 'Male')
	{
		if ($reply == 1)
			$text_body .= "He thanks you for your kiss. He would like to hear from you and looks forward to receiving your email soon.\n";
		else
		if ($reply == 2)
			$text_body .= "He thanks you for your kiss. He appreciates your thought, but would not like to correspond furthur.\n";
		else
		if ($reply == 3)
			$text_body .= "He thanks you for your kiss. He appreciates your thought, but has recently started seeing someone and so is not responding at this time.\n";
		else
			crash( "unrecognised reply type", $cxn );
	}
	else
	{
		if ($reply == 1)
			$text_body .= "He thanks you for your kiss. He would like to hear from you and looks forward to receiving your email soon.\n";
		else
		if ($reply == 2)
			$text_body .= "He thanks you for your kiss. He appreciates your thought, but would not like to correspond furthur.\n";
		else
		if ($reply == 3)
			$text_body .= "He thanks you for your kiss. He appreciates your thought, but has recently started seeing someone and so is not responding at this time.\n";
		else
			crash( "unrecognised reply type", $cxn );
	}

	$html_body = "You have received a kiss reply from Academic Dating member ".$data['handle']."<br><br>";

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

	if ($data['sex'] == 'Male')
	{
		if ($reply == 1)
		{
			$html_body .= $data['handle']." thanks you for your kiss. He would like to hear from you and looks forward to receiving your email soon.<br><br>";
			$html_body .= " To log into the system ".show_link( "Click Here", "http://".$domain."/display_profile.php", "showid", $id, "returnpoint", "main", "external", "true" );
		}
		else
		if ($reply == 2)
			$html_body .= $data['handle']." thanks you for your kiss. He appreciates your thought, but would not like to correspond further.\n";
		else
		if ($reply == 3)
			$html_body .= $data['handle']." thanks you for your kiss. He appreciates your thought, but has recently started seeing someone and so is not responding at this time.\n";
		else
			crash( "unrecognised reply type", $cxn );
	}
	else
	{
		if ($reply == 1)
		{
			$html_body .= $data['handle']." thanks you for your kiss. She would like to hear from you and looks forward to receiving your email soon.<br><br>";
			$html_body .= " To log into the system ".show_link( "Click Here", "http://".$domain."/display_profile.php", "showid", $id, "returnpoint", "main", "external", "true" );
		}
		else
		if ($reply == 2)
			$html_body .= $data['handle']." thanks you for your kiss. She appreciates your thought, but would not like to correspond further.<br>";
		else
		if ($reply == 3)
			$html_body .= $data['handle']." thanks you for your kiss. She appreciates your thought, but has recently started seeing someone and so is not responding at this time.<br>";
		else
			crash( "unrecognised reply type", $cxn );
	}


	send_external( $reply_to_id, $subject, $text_body, $html_body, $cxn );


			/* show succeeded on screen */

	echo black( "Kiss reply sent<br><br>" );

	show_return_point( $returnpoint, $startpos, $reply_to_id );
?>

