<?php

$errFirstName = '';
$errLastName = '';
$errEmailInput = '';
$errInterestedIn = '';
$errMessage = '';
$errRobot = '';
$title = '';
$firstName = '';
$lastName = '';
$email = '';
$interestedCopyediting = '';
$interestedLanguageEditing = '';
$interestedIndexing = '';
$interestedPublishingConsultancy = '';
$interestedCopywriting = '';
$interestedTranslation = '';
$interestedOther = '';
$message = '';
$body = '';
$result = '';
$success = '';
$robot = '';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = htmlspecialchars($_POST["title"]);
    $title = filter_var($title,FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_LOW);
    $title = substr($title,0,10);
    $firstName = htmlspecialchars($_POST["firstName"]);
    $firstName = filter_var($firstName,FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_LOW);
    $firstName = substr($firstName,0,30);
    $lastName = htmlspecialchars($_POST["lastName"]);
    $lastName = filter_var($lastName,FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_LOW);
    $lastName = substr($lastName,0,30);
    $email = htmlspecialchars($_POST["emailInput"]);
    $email = filter_var($email,FILTER_SANITIZE_EMAIL);
    $email = substr($email,0,30);
    
    $interestedCopyediting = isset($_POST["checkboxCopyediting"]);
    $interestedLanguageEditing = isset($_POST["checkboxLanguageEditing"]);
    $interestedIndexing = isset($_POST["checkboxIndexing"]);
    $interestedPublishingConsultancy = isset($_POST["checkboxPublishingConsultancy"]);
    $interestedCopywriting = isset($_POST["checkboxCopywriting"]);
    $interestedTranslation = isset($_POST["checkboxTranslation"]);
    $interestedOther = isset($_POST["checkboxOther"]);
    
    $message = htmlspecialchars($_POST["message"]);
    $message = filter_var($message,FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_LOW);
    $robot = htmlspecialchars($_POST["robot"]);
    $robot = filter_var($robot,FILTER_SANITIZE_NUMBER_INT);
    
    $emailTo = "alissajonesnelson@gmail.com";
    $subject = "Message from alissajonesnelson.com";

    $body .= "Title: ";
    $body .= $title;
    $body .= "\n";
    $body .= "First Name: ";
    $body .= $firstName;
    $body .= "\n";
    $body .= "Last Name: ";
    $body .= $lastName;
    $body .= "\n";
    $body .= "Email: ";
    $body .= $email;
    $body .= "\n";
    $body .= "Interested in: ";
    
    if ($interestedCopyediting) {
        $body .= "Copyediting, ";
    }
    if ($interestedLanguageEditing) {
        $body .= "Language Editing, ";
    }
    if ($interestedIndexing) {
        $body .= "Indexing, ";
    }
    if ($interestedCopywriting) {
        $body .= "Copywriting, ";
    }
    if ($interestedTranslation) {
        $body .= "Translation, ";
    }
    if ($interestedPublishingConsultancy) {
        $body .= "Publishing Consultancy, ";
    }
    if ($interestedOther) {
        $body .= "Other";
    }
    
    $body .= "\n";
    $body .= "Inquiry: ";
    $body .= $message;
    $body = strip_tags($body);
    $body = str_replace("\n.", "\n..", $body);
    
    $errFirstName = '';
    $errLastName = '';
    $errEmailInput = '';
    $errInterestedIn = '';
    $errMessage = '';
    $errRobot = '';

        if (!$_POST["firstName"]) {
            $errFirstName = 'Please enter your first name';
        }
        if (!$_POST["lastName"]) {
            $errLastName = 'Please enter your last name';
        }
        if (!$_POST["emailInput"]) {
            $errEmailInput = 'Please enter your email';
        }
        if (!$interestedCopyediting && !$interestedLanguageEditing && !$interestedIndexing && !$interestedPublishingConsultancy && !$interestedCopywriting && !$interestedTranslation && !$interestedOther) {
            $errInterestedIn = 'Please select at least one';
        }
        if (!$_POST["message"]) {
            $errMessage = 'Please enter an inquiry';
        }
        if ($robot != "7") {
            $errRobot = 'Please double check your math';
        }
        if (!$errFirstName && !$errLastName && !$errEmailInput && !$errInterestedIn && !$errMessage && !$errRobot) {
            $success = 1;
            $mail = mail($emailTo, $subject, $body, "From: donotreply@alissajonesnelson.com");
            if ($mail) {
                $result = '<p class="text-success">Your inquiry was successfully submitted. Thank you.</p>';
                
            }
        } else {
            $result = '<p class="text-danger">Your inquiry was not submitted. Please correct the errors above.</p>';
        }
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="icon" href="favicon.ico">
        
        <title>Hire your Editor</title>
        <link href="css/bootstrap.min.css" rel="stylesheet">
		<link href="css/bootstrapOverride.css" rel="stylesheet">
        
        <link rel="apple-touch-icon" sizes="152x152" href="/apple-touch-icon.png">
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
        <link rel="manifest" href="/manifest.json">
        <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
        <meta name="theme-color" content="#ffffff">
    </head>
    <body>
		<nav class="navbar navbar-toggleable-sm sticky-top navbar-inverse bg-inverse" id="home">
			<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
				<span class="navbar-toggler-icon"></span>
			</button>
			<a class="navbar-brand" href="index.html">Alissa Jones Nelson</a>
			<div class="collapse navbar-collapse" id="navbarNav">
				<div class="mr-auto">
				</div>
				<ul class="navbar-nav">
					<li class="nav-item">
						<a class="nav-link" href="index.html">Home</a>
					</li>
					<li class="nav-item">
						<a class="nav-link" href="about.html">About</a>
					</li>
					<li class="nav-item">
						<a class="nav-link" href="services.html">Services &amp; Pricing</a>
					</li>
					<li class="nav-item">
						<a class="nav-link" href="testimonials.html">Testimonials</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="writing.html">Writing</a>
                    </li>
					<li class="nav-item">
						<a class="nav-link active" href="contact.html">Contact</a>
					</li>
				</ul>
			</div>
		</nav>
        <div class="container mb-3" style="max-width: 46rem;">
            <div class="row text-right mt-1">
                <div class="col-12">
                    <a class="" href="contact.html">English</a><span style="color:grey;"> | </span><a class="active" href="de/kontakt.html">Deutsch</a>
                </div>
            </div>
			<div class="mb-3">
                <h1 class="display-4 my-2 text-center c4">Contact Me</h1>               
                <hr/>
            </div>
            <p>To get started, please provide some information about yourself and your project.</p>
			<form id="contactForm" method="post" action="contact.html">
				<div class="row">
					<div class="col-md-2 col-sm-2 col-3">
						<div class="form-group">
							<label for="title">Title</label>
							<input type="text" class="form-control" id="title" name="title" value="<?php if(!$success) echo $title; ?>">
						</div>
					</div>
					<div class="col-md-5 col-sm-5">
						<div class="form-group">
							<label for="firstName">First Name</label>
							<input type="text" class="form-control" id="firstName" name="firstName" value="<?php if(!$success) echo $firstName; ?>">
                            <?php echo "<p class='text-danger'>$errFirstName</p>"; ?>
						</div>
					</div>
					<div class="col-md-5 col-sm-5">
						<div class="form-group">
							<label for="lastName">Last Name</label>
							<input type="text" class="form-control" id="lastName" name="lastName" value="<?php if(!$success) echo $lastName; ?>">
                            <?php echo "<p class='text-danger'>$errLastName</p>"; ?>
						</div>
					</div>
                </div>
                <div class="row">
					<div class="col-sm-6">
						<div class="form-group">
							<label for="emailInput">Email Address</label>
							<input type="email" class="form-control" id="emailInput" name="emailInput" placeholder="you@mail.com" value="<?php if(!$success) echo $email; ?>">
                            <?php echo "<p class='text-danger'>$errEmailInput</p>"; ?>
						</div>
					</div>
                </div>
                <div class="row">
					<div class="col-12">
						<div class="form-group">
							<label for="interestedIn">I'm interested in</label>
                            <div class="card card-block">
                                <div class="row">
                                    <div class="col-sm-6">
                                        <div class="form-check">
                                            <label class="form-check-label">
                                                <input class="form-check-input" type="checkbox" id="checkboxCopyediting" name="checkboxCopyediting" <?php if($interestedCopyediting && !$success) echo "checked";?>>
                                                Copyediting                                
                                            </label>
                                        </div>
                                        <div class="form-check">
                                            <label class="form-check-label">
                                                <input class="form-check-input" type="checkbox" id="checkboxLanguageEditing" name="checkboxLanguageEditing" <?php if($interestedLanguageEditing && !$success) echo "checked";?>>
                                                Language Editing
                                            </label>
                                        </div>
                                        <div class="form-check">
                                            <label class="form-check-label">
                                                <input class="form-check-input" type="checkbox" id="checkboxIndexing" name="checkboxIndexing" <?php if($interestedIndexing && !$success) echo "checked";?>>
                                                Indexing
                                            </label>
                                        </div>
                                        <div class="form-check">
                                            <label class="form-check-label">
                                                <input class="form-check-input" type="checkbox" id="checkboxPublishingConsultancy" name="checkboxPublishingConsultancy" <?php if($interestedPublishingConsultancy && !$success) echo "checked";?>>
                                                Publishing Consultancy
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-check">
                                            <label class="form-check-label">
                                                <input class="form-check-input" type="checkbox" id="checkboxCopywriting" name="checkboxCopywriting" <?php if($interestedCopywriting && !$success) echo "checked";?>>
                                                Copywriting
                                            </label>
                                        </div>
                                        <div class="form-check">
                                            <label class="form-check-label">
                                                <input class="form-check-input" type="checkbox" id="checkboxTranslation" name="checkboxTranslation" <?php if($interestedTranslation && !$success) echo "checked";?>>
                                                Translation
                                            </label>
                                        </div>
                                        <div class="form-check">
                                            <label class="form-check-label">
                                                <input class="form-check-input" type="checkbox" id="checkboxOther" name="checkboxOther" <?php if($interestedOther && !$success) echo "checked";?>>
                                                Other (please explain below)
                                            </label>                                
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <?php echo "<p class='text-danger'>$errInterestedIn</p>"; ?>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-12">
                        <div class="form-group">
                            <label for="message">Inquiry</label>
                            <textarea class="form-control" id="message" name="message" rows="5"><?php if(!$success) echo $message; ?></textarea>
                            <?php echo "<p class='text-danger'>$errMessage</p>"; ?>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-12">
                        <div class="form-group">
                            <label for="robot">Prove you're not a robot: 3 + 4 = ?</label>
                            <input style="max-width: 6em;" type="number" class="form-control" id="robot" name="robot" value="<?php if (!$success) echo $robot; ?>">
                            <?php echo "<p class='text-danger'>$errRobot</p>";?>
                        </div>
                    </div>
                </div>
                <button type="submit" class="btn btn-danger">Submit</button>
                <?php echo $result; ?>
			</form>
        </div>
        <div style="background-color: #DDD">
            <div class="container" style="max-width: 46rem;">
                <div class="row">
                    <div class="col-12">
                        <p class="work-company mt-2">If you use the contact form to get in touch with us, please be aware that we will store the information you provide in the form, including the contact details you enter, in order to be able to process your inquiry and any follow-up inquiries. We will not pass on any of your data to any third party without your permission.</p>
                    </div>
                </div>
            </div>
        </div>
		<footer class="footer bg-inverse" id="footer">
			<div class="container">
				<div class="row">
					<div class="col-sm-12 col-12 hidden-sm-down">
						<div class="navbar navbar-inverse bg-inverse">
							<ul class="footerbar-nav footy mx-auto">
								<li class="nav-item">
									<a class="footer-link" href="index.html">Home</a>
								</li>
								<li class="nav-item">
									<a class="footer-link" href="about.html">About</a>
								</li>
                                <li class="nav-item">
									<a class="footer-link" href="impressum.html">Impressum</a>
								</li>
								<li class="nav-item">
									<a class="footer-link" href="services.html">Services &amp; Pricing</a>
								</li>               
								<li class="nav-item">
									<a class="footer-link" href="testimonials.html">Testimonials</a>
                                </li>
								<li class="nav-item">
									<a class="footer-link" href="writing.html">Writing</a>
								</li>
								<li class="nav-item">
									<a class="footer-link" href="contact.html">Contact</a>
								</li>
							</ul>
						</div>
					</div>
                    <div class="col-sm-12 col-12 hidden-md-up">
						<div class="navbar navbar-inverse bg-inverse footy">
                            <a class="footer-link-xs" href="index.html">Home</a>
                            <a class="footer-link-xs" href="about.html">About</a>
                            <a class="footer-link-xs" href="impressum.html">Impressum</a>
                            <a class="footer-link-xs" href="services.html">Services &amp; Pricing</a>
                            <a class="footer-link-xs" href="testimonials.html">Testimonials</a>
                            <a class="footer-link-xs" href="writing.html">Writing</a>
                            <a class="footer-link-xs" href="contact.html">Contact</a>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col">
						<div class="footer-text text-muted text-center">© 2017-2018 Alissa Jones Nelson
						</div>
						<div class="footer-text text-muted text-center">Site designed and hosted by KJ Technology LLC</div>
					</div>
				</div>
			</div>
        </footer>
    </body>
	<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
	<script src="https://npmcdn.com/tether@1.2.4/dist/js/tether.min.js"></script>
	<script src="js/bootstrap.min.js"></script>
</html>

