<?php
    
    //error_reporting(E_ALL);
    
    function contactFormAvailable() {
        if (isset($_POST['submit'])) {
            if ($_POST['submit'] == 'Send Enquiry') {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
    $errorMessages = array();
    $thanks = false;
    
    function validateEmailAddressFormat($emailAddress) {
        return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $emailAddress);
    }
    
    function validateFormData() {
        global $errorMessages;
        if ($_POST['name'] == '') {
            $errorMessages[] = 'Please enter your name.';
        }
        if ($_POST['emailAddress'] == '') {
            $errorMessages[] = 'Please enter your email address.';
        } else if (!validateEmailAddressFormat($_POST['emailAddress'])) {
            $errorMessages[] = 'Please enter a valid email address.';
        }
        if ($_POST['phone'] == '') {
            $errorMessages[] = 'Please enter your phone number.';
        }
        if ($_POST['sharing'] == 'select') {
            $errorMessages[] = 'Please tell us how many will be sharing.';
        }
        if ($_POST['source'] == 'select') {
            $errorMessages[] = 'Please tell us how you heard about us. If specifying &quot;Other&quot; specify the other source in the field provided.';
        }
        if ($_POST['source'] == 'other' && $_POST['sourceOther'] == '') {
            $errorMessages[] = 'Please tell us how you heard about us. If specifying &quot;Other&quot; specify the other source in the field provided.';
        }
        return count($errorMessages) == 0;
    }
    
    function buildContactMsgTXT($eol) {
        $msg = "You have a web-site enquiry.".$eol.$eol;
        $msg .= "Name: ".$_POST['name'].$eol;
        $msg .= "Email Address: ".$_POST['emailAddress'].$eol;
        $msg .= "Phone: ".$_POST['phone'].$eol.$eol;
        $msg .= "Staying from: ".$_POST['fromDate'].$eol.$eol;
        $msg .= "Staying to: ".$_POST['toDate'].$eol.$eol;
        $msg .= "Sharing: ".$_POST['sharing'].$eol.$eol;
        $msg .= "Address".$eol;
        $msg .= "Street 1: ".$_POST['street1'].$eol;
        $msg .= "Street 2: ".$_POST['street2'].$eol;
        $msg .= "Town: ".$_POST['town'].$eol;
        $msg .= "County: ".$_POST['county'].$eol;
        $msg .= "Postcode: ".$_POST['postcode'].$eol;
        $msg .= "Country: ".$_POST['country'].$eol;
        if ($_POST['source'] == 'other') {
            $msg .= "Source: ".$_POST['sourceOther'].$eol;
        } else {
            $msg .= "Source: ".$_POST['source'].$eol;
        }
        $msg .= "Message: ".$_POST['message'].$eol;
        return $msg;
    }
    
    function buildContactMsgHTML($eol) {
        $msg = "<html><head>".$eol;
        $msg .= "<title>HolidayInSharmElSheikh.com Contact Form</title>".$eol;
        $msg .= "</head><body>".$eol;
        $msg .= "<p><strong>Name:</strong> ".$_POST['name']."</p>".$eol;
        $msg .= "<p><strong>Email Address:</strong> ".$_POST['emailAddress']."</p>".$eol;
        $msg .= "<p><strong>Phone:</strong> ".$_POST['phone']."</p>".$eol;
        $msg .= "<p><strong>Staying from:</strong> ".$_POST['fromDate']."</p>".$eol;
        $msg .= "<p><strong>Staying to:</strong> ".$_POST['toDate']."</p>".$eol;
        $msg .= "<p><strong>How many sharing:</strong> ".$_POST['sharing']."</p>".$eol;
        $msg .= "<p><strong>Address:</strong><br /><strong>Street 1:</strong> ".$_POST['street1']."<br />";
        $msg .= "<strong>Street 2:</strong> ".$_POST['street2']."<br />";
        $msg .= "<strong>Town:</strong> ".$_POST['town']."<br />";
        $msg .= "<strong>County:</strong> ".$_POST['county']."<br />";
        $msg .= "<strong>Postcode:</strong> ".$_POST['postcode']."<br />";
        $msg .= "<strong>Country:</strong> ".$_POST['country']."</p>".$eol;
        if ($_POST['sourceOther'] == 'other') {
            $msg .= "<p><strong>Source:</strong> ".$_POST['sourceOther']."</p>".$eol;
        } else {
            $msg .= "<p><strong>Source:</strong> ".$_POST['source']."</p>".$eol;
        }
        $msg .= "<p><strong>Message:</strong> ".$_POST['message']."</p>".$eol;
        $msg .= "</body></html>".$eol;
        return $msg;
    }
    
    function sendContactEmail() {
        global $errorMessages, $thanks;
        # Get EOL for OS
        if (strtoupper(substr(PHP_OS,0,3) == 'WIN')) {
            $eol = "\r\n";
        } elseif (strtoupper(substr(PHP_OS,0,3) == 'MAC')) {
            $eol = "\r";
        } else {
            $eol = "\n";
        }
        
        # Get MIME Boundary
        $mime_boundary = md5(time());
        $htmlalt_mime_boundary = $mime_boundary."_htmlalt";
        
        # Define Email Addresses
        $senderEmail = $_POST['emailAddress'];
        $recipientEmail = 'annteresabrown@yahoo.com';
        
        # Headers
        $headers = "From: $senderEmail".$eol;
        $headers .= 'MIME-Version: 1.0'.$eol;
        //$headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
        $headers .= "Content-Type: multipart/alternative; boundary=\"".$htmlalt_mime_boundary."\"".$eol;
        
        # Open MIME
        //$msg = "--".$mime_boundary.$eol;
        $msg = "";
        
        # Text Part
        $msg .= "--".$htmlalt_mime_boundary.$eol;
        $msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
        $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
        $msg .= buildContactMsgTXT($eol);
        $msg .= $eol.$eol;
        
        # HTML Part
        $msg .= "--".$htmlalt_mime_boundary.$eol;
        $msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
        $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
        $msg .= buildContactMsgHTML($eol);
        $msg .= $eol.$eol;
        $msg .= "--".$htmlalt_mime_boundary."--".$eol.$eol;
        
        # Close MIME
        //$msg .= "--".$mime_boundary."--".$eol.$eol;
        
        # Send Mail
        mail($recipientEmail, "HolidayInSharmElSheikh.com Contact Form", $msg, $headers);
        $thanks = true;
    }
    
    if (contactFormAvailable()) {
        if (validateFormData()) {
            sendContactEmail();
        }
    }
    
    # Prepare calendars
    require_once('calendar/classes/tc_calendar.php');
    
    $fromDate = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
    $toDate = mktime(0, 0, 0, date("m"),   date("d")+10,   date("Y"));
    $allowEndDate = mktime(0, 0, 0, 12,   31,   date("Y")+5);
    
    $fromCalendar = new tc_calendar("fromDate", true, false);
    $fromCalendar->setIcon("calendar/images/iconCalendar.gif");
    $fromCalendar->setDate(date('d', $fromDate)
                         , date('m', $fromDate)
                         , date('Y', $fromDate));
    $fromCalendar->dateAllow(date("Y-m-d", $fromDate), date("Y-m-d", $allowEndDate));
    $fromCalendar->setPath("calendar/");
    $fromCalendar->setYearInterval(date('Y', strtotime('now')), date('Y', strtotime('now'))+5);
    
    $toCalendar = new tc_calendar("toDate", true, false);
    $toCalendar->setIcon("calendar/images/iconCalendar.gif");
    $toCalendar->setDate(date('d', $toDate)
                         , date('m', $toDate)
                         , date('Y', $toDate));
    $toCalendar->dateAllow(date("Y-m-d", $fromDate), date("Y-m-d", $allowEndDate));
    $toCalendar->setPath("calendar/");
    $toCalendar->setYearInterval(date('Y', strtotime('now')), date('Y', strtotime('now'))+5);
    
    echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Availability &amp; Contact | Holiday In Sharm el Sheikh</title>
<meta name="description" content="To enquire, check availability or make a booking please complete the following form." />
<link rel="stylesheet" type="text/css" href="stylesheets/structure.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/screen.css" media="screen" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
<script language="javascript" src="calendar/calendar.js"></script>
<script type="text/javascript">
<!--
function initForm() {
document.getElementById('name').focus();
document.getElementById('sourceOther').disabled = true;
}

function checkSource() {
var source = document.getElementById('source').value;
if (source == 'other') {
document.getElementById('sourceOther').disabled = false;
document.getElementById('sourceOther').focus();
} else {
document.getElementById('sourceOther').disabled = true;
}
}

function validateAndSubmit() {
var name = document.getElementById('name').value;
var emailAddress = document.getElementById('emailAddress').value;
var phone = document.getElementById('phone').value;
if (name == '' || emailAddress == '' || phone == '') {
alert('Please complete the mandatory fields: Name, Email Address and Phone Number.');
return false;
}
var sharing = document.getElementById('sharing').value;
if (sharing == 'select') {
alert('Please tell us how many will be sharing.');
return false;
}
var source = document.getElementById('source').value;
if (source == 'select') {
alert('Please select a source.');
return false;
}
document.getElementById('contactForm').submit();
}
-->
</script>
</head>
<body id="contact" onload="initForm();">
<ul id="accessibility-links">
<li><a href="#content">Skip to content</a></li>
<li><a href="#navigation">Skip to navigation</a></li>
</ul>
<div id="wrapper">
<div id="banner">
<img id="logoScreen" src="images/hises_logo.gif" alt="HolidayInSharmElSheikh.com Logo" height="85" width="174" />
<img id="logoPrint" src="images/hises_logo_print.gif" alt="HolidayInSharmElSheikh.com Logo" height="85" width="174" />
<h1>Contact Us</h1>
</div>
<ul id="navigation">
<li id="nav-home" class="nav-white"><a href="index.html">Home</a></li>
<li id="nav-about"><a href="about.html">About</a></li>
<li id="nav-accommodation"><a href="accommodation.html">Accommodation</a></li>
<li id="nav-resort"><a href="resort.html">The Resort</a></li>
<li id="nav-area"><a href="area.html">The Local Area</a></li>
<li id="nav-pricing"><a href="pricing.html">Pricing</a></li>
<li id="nav-extras"><a href="extras.html">Extras</a></li>
<li id="nav-contact"><a href="contact.php">Availability &amp; Contact</a></li>
</ul>
<div id="content">
<?php if ($thanks) { ?>
<h2>Thank You!</h2>
<p>Thank you for your feedback. We will make every effort to respond as soon as possible although it may take 2 workings days to receive a response.</p>
<?php } else { ?>
<p>To enquire, check availability or make a booking please complete the following form:</p>
<?php if (count($errorMessages) > 0) { ?>
<div id="attention">
<h3>Attention</h3>
<ul>
<?php foreach($errorMessages as $errorMessage) { echo "<li>$errorMessage</li>\n"; } ?>
</ul>
</div>
<?php } ?>
<form id="contactForm" action="contact.php" method="post" onsubmit="validateAndSubmit(); return false;">
<fieldset>
<legend>Your Details</legend>
<p>
<label for="name">Your Name *:</label>
<input type="text" id="name" name="name" <?php if (isset($_POST['name'])) { echo 'value="'.$_POST['name'].'"'; } ?> />
</p>
<p>
<label for="emailAddress">Your Email Address *:</label>
<input type="text" id="emailAddress" name="emailAddress" <?php if (isset($_POST['emailAddress'])) { echo 'value="'.$_POST['emailAddress'].'"'; } ?> />
</p>
<p>
<label for="phone">Your Phone Number *:</label>
<input type="text" id="phone" name="phone" <?php if (isset($_POST['phone'])) { echo 'value="'.$_POST['phone'].'"'; } ?> />
</p>
<p>
<label for="fromDate">Date interested in: from</label>
<?php
$fromCalendar->writeScript();
?>
<label for="toDate" style="text-align: right;">to</label>
<?php
$toCalendar->writeScript();
?>
</p>
<p>
<label for="sharing">How many sharing *:</label>
<select id="sharing" name="sharing">
<option select="selected" value="select">Please Select...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
</p>
</fieldset>
<fieldset>
<legend>Address</legend>
<p>
<label for="street1">Street 1:</label>
<input type="text" id="street1" name="street1" <?php if (isset($_POST['street1'])) { echo 'value="'.$_POST['street1'].'"'; } ?> />
</p>
<p>
<label for="street2">Street 2:</label>
<input type="text" id="street2" name="street2" <?php if (isset($_POST['street2'])) { echo 'value="'.$_POST['street2'].'"'; } ?> />
</p>
<p>
<label for="town">Town:</label>
<input type="text" id="town" name="town" <?php if (isset($_POST['town'])) { echo 'value="'.$_POST['town'].'"'; } ?> />
</p>
<p>
<label for="county">County:</label>
<input type="text" id="county" name="county" <?php if (isset($_POST['county'])) { echo 'value="'.$_POST['county'].'"'; } ?> />
</p>
<p>
<label for="postcode">Postcode:</label>
<input type="text" id="postcode" name="postcode" <?php if (isset($_POST['postcode'])) { echo 'value="'.$_POST['postcode'].'"'; } ?> />
</p>
<p>
<label for="country">Country:</label>
<input type="text" id="country" name="country" <?php if (isset($_POST['country'])) { echo 'value="'.$_POST['country'].'"'; } ?> />
</p>
</fieldset>
<fieldset>
<legend>Your Message</legend>
<textarea id="message" name="message" cols="60" rows="20"><?php if (isset($_POST['message'])) { echo $_POST['message']; } ?></textarea>
</fieldset>
<fieldset>
<legend>How did you hear about us?</legend>
<p>
<label for="source">Source *:</label>
<select id="source" name="source" onchange="checkSource();">
<option select="selected" value="select">Please Select...</option>
<optgroup label="Internet Search">
<option value="google">Google</option>
<option value="yahoo">Yahoo</option>
<option value="msn">MSN</option>
</optgroup>
<option value="recommendation">Recommendation</option>
<option value="other">Other (Please Specify)</option>
</select>
</p>
<p>
<label for="sourceOther">If other, please provide details:</label>
<input type="text" id="sourceOther" name="sourceOther" <?php if (isset($_POST['sourceOther'])) { echo 'value="'.$_POST['sourceOther'].'"'; } ?> />
</p>
</fieldset>
<p>
<input type="reset" />
<input type="submit" name="submit" value="Send Enquiry" />
</p>
</form>
<p>* denotes mandatory field</p>
<?php } ?>
<p>Please read our <a href="privacy.html">privacy policy</a></p>
<p>Before booking please read our <a href="terms.html">Terms &amp; Conditions</a></p>
</div>
<div id="footer">
<p>Copyright &copy;2008-2009 Teresa Brown.</p>
<ul id="footer-links">
<li><a href="terms.html">Terms &amp; Conditions</a></li>
<li><a href="privacy.html">Privacy Policy</a></li>
</ul>
</div>
</div>
</body>
</html>