<?php

//This page has references to district-by-district explicitly. These should be changed. Load from ini?

require_once("includes/SQLConnect.php");
require_once("includes/user_data.php");

if(isset($_POST['to'])){
	if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) === FALSE)
		$error = "That email address isn't valid. Perhaps you forgot the @ or domain.";
	else{
		if(strlen($_POST['subject'])>0){
			if(strlen($_POST['body'])>0){
				if(strlen($_POST['to'])){	
					
					$to = $_POST['to'];
					$email = $_POST['email'];
					$subject = $_POST['subject'];
					$body = $_POST['body'];
					
					if(mysql_query("INSERT INTO Users_Invites(user_ID,to_name,to_email,subject) 
						VALUES(".$user_data['ID'].",'".mysql_real_escape_string($to)."','".mysql_real_escape_string($email)."','".mysql_real_escape_string($subject)."')")){
						//User hasn't been emailed before, so try and send them an email
												$send_name = "District-by-District Organizing Tool"; //senders name
						
						$recipient = $email; //recipient's email
						$email = $user_data['email']; //senders e-mail adress
						
						$body = str_replace('http://'.$_SERVER['SERVER_NAME'].'/register_user.php','http://'.$_SERVER['SERVER_NAME'].'/register_user.php?name='.urlencode($to).'&email='.urlencode($recipient).'&ID='.mysql_insert_id(),$body);
						
						$mail_body = stripslashes($body);
						$subject = stripslashes($subject);
						$header = "From: ". $user_data['first_name'].' '.$user_data['last_name']. " <" . $user_data['email'] . ">\r\n"; //optional headerfields
						
						ini_set('sendmail_from', $email);
						
						if(mail($recipient, $subject, $mail_body, $header))
							header("Location:user_invite.php?success=".$to." has been invited. They should receive their invitation email shortly.");
						else
							$error = "There was a problem sending to ".$_POST['email'].". Maybe this email isn't valid. If you have an alternate address, you might want to try that.";
			
					}
					else
						$error = "This person has already been invited to join! Hopefully they'll join shortly.";
				}
				else
					$error = "Who are you sending the message to? Please fill in their name.";
			}
			else
				$error = "You didn't write a message to send to ".$_POST['to'];
		}
		else
			$error = "You didn't write a subject for the message to send to ".$_POST['to'];
	}

}

include_once("includes/header.php");

?>

	<h2>Invite a Friend</h2>
	<form action="user_invite.php" method="POST">
	
		<label>Friend's Name:</label>
		<input type="text" name="to">
		
		<label>Their Email Address:</label
		<input type="text" name="email">
		
		<label>Subject:</label>
		<input type="text" name="subject" class="large" value="Joining the District by DIstrict Organizing Tool">
		
		<label>Invite Message:</label>
		<textarea name="body" style="width:625px" rows="6">I'm a member of a social networking site aimed at effecting change at the congressional level in our district. 
		
I was wondering if you'd like to join. Signing up is easy and takes about two minutes. 
		
You can sign up at this URL: http://<?=$_SERVER['SERVER_NAME'];?>/register_user.php
		</textarea>
		
		<input type="submit" class="submit" value="Invite">
	</form>


<?php

include_once("includes/footer.php");

?>