<?php

	include "library.php";

	$cxn = start_code();

    exit_if_search_bot();

    exit_if_session_expired( $cxn );



			/* get variables */

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

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

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

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

	page_header( "", $cxn );

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

	crash_if_missing( $jobseeker_id, "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 );

	$ip_address = get_ip_address();	


    $query2 = "SELECT * FROM jobseeker_profile WHERE id = ".$jobseeker_id;

	$result2 = run_query( $query2, $cxn );

	$data_from = get_row( $result2 );


    $query3 = "SELECT * FROM job WHERE id = ".$send_to_id;

	$result3 = run_query( $query3, $cxn );

	$data_to = get_row( $result3 );

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

			/* send the message */

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

        $send_email_system_password = val( $_POST, "send_email_system_password" );
        
        
        if ($send_email_system_password != "1234abcd%^%")
        {
            echo "Internal error, incorrect system password.<br><br>";
            
            echo show_home_link();
            
            exit();
        }
        else
        {
            $subject = $subject." (a message from ".sitename." jobseeker ".$data_from['username'].")";
            
			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 );


				// check for the email being a business proposal


			$username = $data_from['username'];

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


			$text_body = "This is a message from ".sitename." jobseeker '".$username."'\n\n\n";
			$text_body .= $message."\n\n\n";

			$text_body .= "To reply to this message please log into the site, go to View a Jobseeker, enter the jobseeker's username then click on View and Send Email.\n\n\n";

			$text_body .= "Kind Regards\n\n\n";
			$text_body .= sitename."\n\n";
			$text_body .= display_site_url;
			$text_body .= "\n\n\n\n";
    		$text_body .= "If you have received this message in error please contact Site Administration.\n\n\n";


			$html_body = convert_to_html($message);
			$html_body .= "<br><br><br><span style='font-family: arial; font-size: 10pt; font-style: italic; color: #a00000'>This is a message from ".sitename." jobseeker '".$username."'.<br><br>";

			$html_body .= "To reply to this message please log into the site, go to View a Jobseeker, enter the jobseeker's username then click on View and Send Email.<br><br>";

			$html_body .= "Kind Regards<br><br>";
			$html_body .= sitename."<br><br>";
			$html_body .= display_site_url;
			$html_body .= "<br><br><br>";
    		$html_body .= "If you have received this message in error please contact Site Administration.<br><br><br></span>";

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


				/* show succeeded on screen */

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


				/* warning about messages sent to themselves */

			if ($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( "their profile and click 'send email'<br><br>" );
			}

        }

	}


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



    echo "<br><br><br><br><br>";



    echo "</body>";
	
    echo "</html>";

	
?>