<?php

//If the form is submitted
if(isset($_POST['appsubmit'])) {
	$appname = stripslashes(trim($_POST['appname']));
	$appphone = stripslashes(trim($_POST['appphone']));
	$appdate = stripslashes(trim($_POST['appdate']));
	$appemail = '';
	
	$options = explode('|', $_POST['appdetails']);
	//If there is no error, send the email
	if(!isset($hasError)) {
		if($appname === '') {
			$appnameError = 'Please enter your name.';
			$hasError = true;
		} 
		if($appphone === '') {
			$appphoneError = 'Please enter your phone number.';
			$hasError = true;
		} 
		if($appdate === '') {
			$appdateError = 'Please enter an appointment date.';
			$hasError = true;
		} 
		//Check to make sure sure that a valid email address is submitted
		if(trim($_POST['appemail']) === '')  {
			$appmailError = 'You forgot to enter your email address.';
			$hasError = true;
		} else if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", trim($_POST['appemail']))) {
            $appmailError = 'You entered an invalid email address.';
            $hasError = true;
		} else {
			$appemail = trim($_POST['appemail']);
		}
		 
		
	}
	if(!isset($hasError)) {
		
		$status = "";
		
		require_once "class.phpmailer.php";
		$mail = new PHPMailer();
		$mail->IsMail();
		$mail->IsHTML(true);    
		$mail->CharSet  = "utf-8";
		$mail->From     = $appemail;
		$mail->FromName = $appname;
		$mail->WordWrap = 50;    
		$mail->Subject  = 'New Appointment';
		$mail->Body     =  
		'<table>
			<thead>
				<tr>
					<th>Name:</th>
					<th>Email: </th>
					<th>Phone Number: </th>
					<th>Desired date: </th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>'.$appname.'</td>
					<td>'.$appemail.'</td>
					<td>'.$appphone.'</td>
					<td>'.$appdate.'</td>
				</tr>
			</tbody>
		</table>';
		$mail->AddAddress($options[0]);
		$mail->AddReplyTo($appemail);
		
		if(!$mail->Send()) {  // send e-mail
			$status =  '<div class="error">'.__('Submission error. Please make sure all fields are filled correctly.', 'Medico').'</div>';
		}
		else
		{
			$status =  '<div class="success">'.$options[1].'</div>';
		}
		echo $status; die();	
	} 
	die;
} 
?>