<?php

	include "library.php";

	$cxn = start_code();

	
			/* get variables */

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

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

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

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

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

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

	if ($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 blocked list */

	$query = "SELECT * FROM blocked_list WHERE (blocker_id = ".$id." AND blockee_id = ".$send_to_id." ) OR ( blocker_id = ".$send_to_id." AND blockee_id = ".$id." )";
	$result = run_query( $query, $cxn );

	if (mysql_num_rows($result) > 0)
	{
		page_header( "", $cxn );
		echo black( "Communication with this member is currently blocked" );
		exit();
	}


		/* check visibilty */

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

	$data = get_row( $result );

	if ($data['visibility'] == "hidden" && ! is_system_user( $id, $cxn ))
	{
		page_header( "", $cxn );
		echo black( "You cannot communicate with other members while your profile status is 'hidden'" );
		exit();
	}

		/* check for a valid email address */

	if ($data['email_address_valid'] == "no")
	{
		page_header( "", $cxn );
		echo black( "You cannot send messages to other members until your email address" );
		echo black( " has been verified.<br><br>" );
		echo black( " You must 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 <h2>".$data['email']."</h2>" );
		echo black( " If this is not your correct email address please click on 'Edit Profile' on the home page and update your profile<br><br>" );
		echo black( " Please check your inbox or " ).show_link( "click here", "send_verification_email.php", "sendid", $id, "code", $data['email_verification_code'] );
		echo black( " to send the verification email again.<br><br>" );
		echo show_home_link();

		exit();
	}

		/* check for the 12 message limit */

		/* can have unlimited contact with 12 unique members each day */

	if (! query_has_rows( "SELECT id FROM message WHERE sender_id = ".$id." AND receiver_id = ".$send_to_id." AND date(sent) = '".date("Y-m-d")."'", $cxn ))
	{
		$query = "SELECT count(*) as num FROM (SELECT DISTINCT receiver_id FROM message WHERE sender_id = ".$id." AND date(sent) = '".date("Y-m-d")."') x";

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

		if (mysql_num_rows($result) != 0)
		{
			$data = get_row( $result );

			if ($data['num'] >= 12)
			{

				page_header( "", $cxn );

				echo black( "To prevent overloading members with messages, there is a" );
				echo black( " limit of 12 messages a day that you can send.<br><br>" );
				echo black( " Please try again tomorrow<br><br>" );

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

				exit();
			}
		}
	}


		/* avoid repeated kisses to the same member */

	if ($send_type == "kiss")
	{
		if (query_has_rows( "SELECT id FROM message WHERE type = 'kiss' AND sender_id = ".$id." AND receiver_id = ".$send_to_id, $cxn ))
		{
			echo black( "You have already sent a kiss to this member.<br><br>" );

			echo show_home_link();

			exit();
		}
	}

		/* work out credits used if any */

	$_SESSION['credits_used'] = 0;


		/* check if an email has already been sent */

	$query = "SELECT id FROM message WHERE type = 'email' AND (sender_id = ".$id." AND receiver_id = ".$send_to_id.") OR (sender_id = ".$send_to_id." AND receiver_id = ".$id.")";

	if (query_has_rows( $query, $cxn ))
		$chanel_open = true;
	else
		$chanel_open = false;


		/* if already contacted today the 3-message limit doesnt apply */

	if (already_had_contact_today( $id, $send_to_id, $cxn ))
		$num_sent = 0;
	else
		$num_sent = num_contacted_today( $id, $send_to_id, $cxn );


//	{
//		if (get_membership_type( $id, $cxn ) == "subscription")
//		{
//				/* check for subscription not running */
//
//			if ($num_sent >= 3 && query_has_rows( "SELECT id FROM profile WHERE id = ".$id." AND subscription_valid = 'no'", $cxn ))
//			{
//				echo red( "Your membership type is 'subscription' but your subscription is not currently operating.<br><br>" );
//				echo red( "Please go to the 'Monthly Membership' page, click on 'Subscribe' and enter your details.<br><br>" );
//				echo red( "Alternatively you can change your subscription type in your profile to a 'basic membership'.<br><br>" );
//
//				echo show_link( "Monthly Membership", "subscribe.php" );
//
//				exit();
//			}
//		}
//		else

		/* basic membership */

	if (1 == 2)
	{
//		if ($num_sent >= 3)
		{
			$query = "SELECT credits_balance FROM profile WHERE id = ".$id;

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

			$data = get_row( $result );

			if ($send_type == "kiss")
			{
				if ($data['credits_balance'] >= 0.25)
					$_SESSION['credits_used'] = 0.25;
				else
				{
//					echo red( "You have used your 3 free messages for today and you do not have credits to pay for this kiss.<br><br>" );
					echo red( "Please buy some credits on the 'Buy Credits' page.<br><br>" );
//					echo red( "Credits cost US 80 cents each.<br><br>" );
//					echo red( "It costs 1 credit to send an email.<br><br>" );
//					echo red( "It costs 0.25 credits to send a 'kiss' message.<br><br>" );
//					echo red( "Once the first email has been sent you can communicate with that member for free.<br><br>" );
					echo show_link( "Buy Credits", "buy_credits.php" );
					echo "<br><br>";
//					echo show_home_link();

					exit();
				}
			}
			else
			{
				if ($data['credits_balance'] >= 1)
					$_SESSION['credits_used'] = 1;
				else
				{
//					echo red( "You have used your 3 free messages for today and you do not have credits to pay for this email.<br><br>" );
					echo red( "Please buy some credits on the 'Buy Credits' page.<br><br>" );
//					echo red( "Credits cost US 80 cents each.<br><br>" );
//					echo red( "It costs 1 credit to send an email.<br><br>" );
//					echo red( "It costs 0.25 credits to send a 'kiss' message.<br><br>" );
//					echo red( "Once the first email has been sent you can communicate with that member for free.<br><br>" );
					echo show_link( "Buy Credits", "buy_credits.php" );
					echo "<br><br>";
//					echo show_home_link();

					exit();
				}
			}
		}
	}

	if ($send_type == "kiss")
	{
		page_header( "Send Kiss", $cxn );

		echo black( "Are you sure you want to send a kiss to this member?<br><br>" );

			/* yes */

		echo sp(2);

		show_button_h( "yes", "send_message_go.php", "send", "yes", "sendtype", "kiss", "sendtoid", $send_to_id );

			/* no */

		echo sp(2);

		show_button_h( "no", "send_message_go.php", "send", "no", "sendtype", "kiss", "sendtoid", $send_to_id );
	}
	else
	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 );

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

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

		echo "<tr><td></td><td align='center'><br><br><input type='image' src='buttons/".button_filename( "send" )."' border='0'></td></tr>";

		form_end();
	}
	else
		crash( "Internal Error: invalid send type in send_message", $cxn );

?>


