<?php
  
if($_POST) {
    $visitor_name = "";
	$visitor_phone = "";
    $visitor_email = "";
    $email_title = "";
    $concerned_department = "";
    $visitor_message = "";
    $email_body = "<div>";
      
    if(isset($_POST['visitor_name'])) {
        $visitor_name = filter_var($_POST['visitor_name'], FILTER_SANITIZE_STRING);
        $email_body .= "<div>
                           <label><b>Nombre:</b></label>&nbsp;<span>".$visitor_name."</span>
                        </div>";
    }
	
	if(isset($_POST['visitor_phone'])) {
        $visitor_phone = filter_var($_POST['visitor_phone'], FILTER_SANITIZE_STRING);
        $email_body .= "<div>
                           <label><b>Teléfono:</b></label>&nbsp;<span>".$visitor_phone."</span>
                        </div>";
    }
 
    if(isset($_POST['visitor_email'])) {
        $visitor_email = str_replace(array("\r", "\n", "%0a", "%0d"), '', $_POST['visitor_email']);
        $visitor_email = filter_var($visitor_email, FILTER_VALIDATE_EMAIL);
        $email_body .= "<div>
                           <label><b>Email:</b></label>&nbsp;<span>".$visitor_email."</span>
                        </div>";
    }
	
	if(isset($_POST['visitor_cedula'])) {
        $visitor_cedula = filter_var($_POST['visitor_cedula'], FILTER_SANITIZE_STRING);
        $email_body .= "<div>
                           <label><b>Cedula:</b></label>&nbsp;<span>".$visitor_cedula."</span>
                        </div>";
    }
      
    if(isset($_POST['email_title'])) {
        $email_title = filter_var($_POST['email_title'], FILTER_SANITIZE_STRING);
        $email_body .= "<div>
                           <label><b>Reason For Contacting Us:</b></label>&nbsp;<span>".$email_title."</span>
                        </div>";
    }
      
    if(isset($_POST['concerned_department'])) {
        $concerned_department = filter_var($_POST['concerned_department'], FILTER_SANITIZE_STRING);
        $email_body .= "<div>
                           <label><b>Distancia:</b></label>&nbsp;<span>".$concerned_department."</span>
                        </div>";
    }
      
    if(isset($_POST['visitor_message'])) {
        $visitor_message = htmlspecialchars($_POST['visitor_message']);
        $email_body .= "<div>
                           <label><b>Visitor Message:</b></label>
                           <div>".$visitor_message."</div>
                        </div>";
    }
      
    if($concerned_department == "5kilometros") {
        $recipient = "luis@one-sm.net";
    }
    else if($concerned_department == "10kilometros") {
        $recipient = "luis@one-sm.net";
    
    }
    else {
        $recipient = "luis@one-sm.net";
    }
      
    $email_body .= "</div>";
 
    $headers  = 'MIME-Version: 1.0' . "\r\n"
    .'Content-type: text/html; charset=utf-8' . "\r\n"
    .'From: ' . $visitor_email . "\r\n";
      
    if(mail($recipient, $email_title, $email_body, $headers)) {
        echo "<p>La informacion se ha enviado correctamente. Pronto estaremos en contacto con usted para confirmarle su inscripcion, muchas gracias.</p>";
    } else {
        echo '<p>Lo sentimos, no se ha enviado la información, por favor pruebe en unos minutos.</p>';
    }
      
} else {
    echo '<p>Ha ocurrido un error.</p>';
}
?>