<?php

$to     = 'info@nrgengineers.com';
$from   = 'info@nrgengineers.com';
$errors = array();
$send   = false;

// form values
$company = '';
$name    = '';
$email   = '';
$phone   = '';
$message = '';

function encode($text, $encoding = 'UTF-8') {
    return htmlspecialchars($text, ENT_QUOTES, $encoding);
}

function getParam($name, $defaultValue = null) {
    return isset($_GET[$name]) ? $_GET[$name] : (isset($_POST[$name]) ? $_POST[$name] : $defaultValue);
}

function isValidEmail($email) {
    return preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", $email);
}

if (isset($_POST['Submit'])) {

    $company = trim(getParam('company_name'));
    $name    = trim(getParam('name'));
    $email   = trim(getParam('email'));
    $phone   = trim(getParam('phone'));
    $message = trim(getParam('message'));

    if (empty($name)) {
        $errors[] = 'Please enter a name';
    } else if (empty($email) || !isValidEmail($email)) {
        $errors[] = 'Please enter a valid e-mail address';
    }  else if (empty($message)) {
        $errors[] = 'Please enter your message';
    }

    // only send mail if there are no errors
    if (empty($errors)) {
        $subject = 'Bericht Contactformulier nextrenewablegroup.com';
        $body    = "Naam: $name \nEmail: $email \nTelefoon: $phone \nBedrijf: $company \n\nBericht: $message";
        $headers = 'From: ' . ' <' . $from . '>' . "\r\n" . 'Reply-To: ' . $email;
        $send    = mail($to, $subject, $body, $headers);

        // clear input
        $company = $name = $email = $phone = $message = '';
    }
}


?>

<?php include "includes/header.php" ?>

<!--breadcrumb-->
<div class="container breadcrumb">
    <ol class="breadcrumb">
        <li><a href="index.php">Home</a></li>
        <li><a href="contact.php">Contact</a></li>
    </ol>
</div>

<!--content-->
<div id="content">
    <section class="row_1">
        <div class="container">
            <article class="row">
                <ul class="list1 products">
                    <li class="col-xs-12 header">
                        <h3>CONTACT</h3>
                        <b>WE WELCOME YOU TO DISCUSS YOUR RENEWABLE ENERGY OPPORTUNITY</b><br><br>
                        <p>Would you like to know more about the specific opportunities NRG holds for you? Please
                            contact us to discuss the possibilities for your case-specific in detail.</p><br>
                    </li>
                </ul>
            </article>
        </div>
    </section>

    <section class="row_9">
        <div class="container">
            <div class="row">
                <article class="col-lg-4 col-md-4 col-sm-4 address">
                    <h3>Address</h3>
                    <address>
                        <div class="info">
                            <strong>Next Renewable Group</strong>
                            <p>Westplein 6<br>3016 BM Rotterdam</p>
                            <p>Tel.: +31 (0)10-7420890<br>Fax: +31 (0)10-7420805</p>
                        </div>
                    </address>
                </article>
                <article class="col-lg-8 col-md-8 col-sm-8 address">
                    <h3>E-mail us</h3>
                    <?php
                    // in case errors occurred
                    if (!empty($errors)) {
                        ?>
                        <div class="alert alert-danger">
                            <ul>
                                <?php
                                foreach ($errors as $error) {
                                    echo '<li>', encode($error), '</li>';
                                }
                                ?>
                            </ul>
                        </div>
                        <?php
                    }
                    ?>

                    <?php
                    // if contact form is send
                    if (true === $send) {
                        ?>
                        <div class="alert alert-success">
                            Your email was sent.
                        </div>
                        <?php
                    }
                    ?>

                    <form role="form" action="contact.php" method="post">
                        <div class="form-group">
                            <label for="form_company_name">Company Name</label>
                            <input id="form_company_name" type="text" name="company_name" class="form-control"
                                   placeholder="Please enter your Company Name" value="<?= encode($company) ?>">
                            <div class="help-block with-errors"></div>
                        </div>
                        <div class="form-group">
                            <label for="form_name">Name *</label>
                            <input id="form_name" type="text" name="name" class="form-control"
                                   placeholder="Please enter your name *" required="required" data-error="Name is required." value="<?= encode($name) ?>">
                            <div class="help-block with-errors"></div>
                        </div>
                        <div class="form-group">
                            <label for="form_email">Email *</label>
                            <input id="form_email" type="email" name="email" class="form-control"
                                   placeholder="Please enter your email *" required="required" data-error="Valid email is required." value="<?= encode($email) ?>">
                            <div class="help-block with-errors"></div>
                        </div>
                        <div class="form-group">
                            <label for="form_phone">Phone</label>
                            <input id="form_phoen" type="text" name="phone" class="form-control"
                                   placeholder="Please enter your phone-number" value="<?= encode($phone) ?>">
                            <div class="help-block with-errors"></div>
                        </div>
                        <div class="form-group">
                            <label for="form_message">Message *</label>
                        <textarea id="form_message" name="message" class="form-control"
                                  placeholder="Please, leave us a message *" rows="4"
                                  required="required"><?= encode($message) ?></textarea>
                            <div class="help-block with-errors"></div>
                        </div>
                        <input type="submit" name="Submit" class="btn btn-success btn-send" value="Send message">
                        <p class="text-muted"><strong>*</strong> These fields are required.</p>
                    </form>
                </article>
            </div>
        </div>
    </section>

    <section>
        <div class="map">
            <iframe src="https://maps.google.nl/maps?q=westplein6+rotterdam&amp;ie=UTF8&amp;hl=nl&amp;hq=&amp;
              hnear=Westplein+6,+3016+BM+Centrum,+Rotterdam,+Zuid-Holland&amp;ll=51.909022,4.478412&amp;spn=0.042833,0.089178&amp;
              t=m&amp;z=14&amp;output=embed"></iframe></div>

    </section>
</div>
<!--footer-->
<?php include('includes/footer.php'); ?>

<script src="js/bootstrap.min.js"></script>
<script src="js/tm-scripts.js"></script>
