<?php
include_once('inc.function.php');
include_once('confdb.php');
include_once('db.functions.php');

$MESSAGE_VALID =   '<blockquote>
                      <h1>
                          <div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-remove-sign"></span> Please input a valid email address.</div>
                      </h1>
                      <br>
                      <br>
                    </blockquote>';

    if (isset($_POST['CLIENT_EMAIL'])) {
        $email = filter_var($_POST['CLIENT_EMAIL'], FILTER_SANITIZE_EMAIL);
        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $_SESSION['CLIENT_EMAIL'] = $email;
        } else {
            $_SESSION['MESSAGE'] = $MESSAGE_VALID;
            header('Location: booking_contact.php');
            die();
        }
    }

//$_SESSION['CLIENT_EMAIL'] = filter_var($_POST['CLIENT_EMAIL'], FILTER_SANITIZE_EMAIL);
$_SESSION['TYPE_CUSTOMER'] = $_POST['TYPE_CUSTOMER'];


$MESSAGE_EXIST =   '<blockquote>
                      <h1>
                          <div class="alert alert-success" role="alert"><span class="glyphicon glyphicon-ok"></span> You are already registered with us!</div>
                      </h1>
                      <em>You may proceed with booking, we will email you your username and password for your future reference.</em>
                      <br>
                      <br>
                    </blockquote>';

$row=DB::queryFirstRow( "SELECT * FROM customers INNER JOIN config_countries ON `customers`.`CUST_COUNTRY` = `config_countries`.`COUNTRY_SHORT` INNER JOIN `config_states` ON `customers`.`CUST_STATE` = `config_states`.`STATES_CODE` WHERE `CUST_EMAIL`= %s ORDER BY `CUST_ID` DESC LIMIT 1", $_POST['CLIENT_EMAIL']);

$counter = DB::count();

//$result = mysqli_query($con,"SELECT * FROM customers INNER JOIN config_countries ON `customers`.`CUST_COUNTRY` = `config_countries`.`COUNTRY_SHORT` INNER JOIN `config_states` ON `customers`.`CUST_STATE` = `config_states`.`STATES_CODE` WHERE `CUST_EMAIL`='$_POST[CLIENT_EMAIL]' ORDER BY `CUST_ID` DESC LIMIT 1");

//if(mysqli_num_rows($result) == 0) {
if($counter == 0) {
	 $_SESSION['EXISTING'] = 0;
     header('Location: booking_address.php');
} else {
	$_SESSION['EXISTING'] = 1;
	//$row = mysqli_fetch_assoc($result);
	$_SESSION['CUST_ID'] = $row['CUST_ID'];
	$_SESSION['CLIENT_NAME'] = $row['CUST_NAME'];
	$_SESSION['CLIENT_TELEPHONE'] = $row['CUST_TEL_1'];
  $_SESSION['CLIENT_TELEPHONE2'] = $row['CUST_TEL_2'];
	$_SESSION['CLIENT_FAX'] = $row['CUST_FAX_1'];
	$_SESSION['CLIENT_EMAIL'] = $row['CUST_EMAIL'];
	$_SESSION['CUST_COUNTRY_CALLING_CODE'] = $row['CUST_TEL_1_CODE'];
	$_SESSION['ADDRESS_1'] = $row['CUST_ADD_1'];
	$_SESSION['ADDRESS_2'] = $row['CUST_ADD_2'];
	$_SESSION['CUST_COUNTRY_NAME'] = $row['COUNTRY_NAME'];
	$_SESSION['CUST_STATES_NAME'] = $row['STATES_NAME'];
	$_SESSION['CUST_CITY'] = $row['CUST_CITY'];
	$_SESSION['CUST_POSTCODE'] = $row['CUST_POSTCODE'];
	$_SESSION['COMPANY_NAME'] = $row['CUST_COMPANY'];
	$_SESSION['MESSAGE'] = $MESSAGE_EXIST;
  header('Location: booking_submission.php');
}


?>