<?php
$invoice_ref = time().rand(10,5000);
$client_key = '4T7y1vuKPkdhRYUQdH6msueW9hqV2wpcwnSk0Qwxkvwf7TjQn40qymPW7IJb66gUC1w1505cDMkj9IR82juQEnIp4/8nd0nDhwYDSDoqROtNBWmkqtqYmF6F4qVGMUq3HpJrY3y2kdSljCFfWzWc7LIEn4G5XY2IZAPgAO5+mhwvYrricmqwLhqzV4FBUII5AwTvJZl4awrdul4vLWkS73vJuW/XrXHseGK/nqB3oSRluZ1jKp2qDgVTwrXYEbx4rwwS42X2w0C9tLKywxf27WD2CCK8ftSeQBimVqFI0r6VuRXFVaOn6SQTOD9V/TsCfPk5Zh6/PWcfapyVKS+l1w==';

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Fare Estimator</title>

    <!-- Bootstrap -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <link rel="stylesheet" href="assets/picker/default.css"  crossorigin="anonymous">
    <link rel="stylesheet" href="assets/picker/date.theme.css"  crossorigin="anonymous">

    <link rel="stylesheet" href="assets/picker/default.time.css"  crossorigin="anonymous">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        #map {
            height:300px;
        }
        body{
            background-color: #d9d9d9;
        }
.lat_error_pickup {
display:none;
color:#a94442;
margin-bottom: 10px;
margin-top: 5px;
}

    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-xs-12 ">
            <h3></h3>
        </div>
    </div>
</div>
<div class="container">
    <form role="search" id="form1" method="POST" action="thankyou.php" data-eway-encrypt-key="<?=$client_key?>">
    <input type="hidden" name="submit_form" value="1">
    <input type="hidden" name="hidden_ref" value="<?=$invoice_ref?>">
    <input type="hidden" id="redirecturl" name="redirecturl" value="">
    <input type="hidden" id="lat1_value" name="lat1_value" value="">
    <input type="hidden" id="lat2_value" name="lat2_value" value="">
            <div class="row">
                <div class="col-xs-12 col-md-6 no-padding">
                    <input type="text" required data-validation="required"  class="form-control" placeholder="Name" name="name" id="name">
                </div>
                <div class="col-xs-12 col-md-6">
                    <input type="number" required class="form-control" data-validation="required"  placeholder="Mobile Number" name="phone" id="phone">
                </div>
            </div>
            <div class="row" style="margin-top: 10px">
                <div class="col-xs-12 col-md-6">
                    <input type="text" id="pickup" autocomplete="off" data-validation="required"  required class="form-control" placeholder="Pickup Address"><div class="lat_error_pickup">Please enter valid address</div>
                </div>
                <div class="col-xs-12 col-md-6">
                    <input type="text" id="dropoff"  autocomplete="off" data-validation="required"  required class="form-control" placeholder="Drop Off Address">
                </div>
            </div>
            <div class="row" style="margin-top: 10px">
                <div class="col-xs-12 col-md-6">
                    <input type="text" required class="datepicker form-control"   name="date" id="date" placeholder="Date">
                </div>
                <div class="col-xs-12 col-md-6">
                    <input type="text" required class="timepicker form-control"    name="time" id="time" placeholder="Time">
                </div>
            </div>
            <div class="row" style="margin-top: 10px">
                <div class="col-xs-12 col-md-6">
                    <input type="text" required class=" form-control" id="EWAY_CARDNUMBER" placeholder="Credit Card Number" maxlength="20" autocomplete="off" name="EWAY_CARDNUMBER" data-eway-encrypt-name="EWAY_CARDNUMBER">
                </div>
                <div class="col-xs-12 col-md-6">
                    <input type="text" required class=" form-control" id="EWAY_CARDCVN" placeholder="CVC" maxlength="4" autocomplete="off" name="EWAY_CARDCVN" data-eway-encrypt-name="EWAY_CARDCVN">
                </div>
            </div>

           <div class="row" style="margin-top: 10px">
                <div class="col-xs-12 col-md-6">
        <select id="EWAY_CARDEXPIRYMONTH" name="EWAY_CARDEXPIRYMONTH">

            <option value="01">Jan</option>

            <option value="02">Feb</option>

            <option value="03">Mar</option>

            <option value="04">Apr</option>

            <option value="05">May</option>

            <option value="06">Jun</option>

            <option value="07">Jul</option>

            <option value="08">Aug</option>

            <option value="09">Sep</option>

            <option value="10">Oct</option>

            <option value="11">Nov</option>

            <option value="12">Dec</option>

        </select>

        <select id="EWAY_CARDEXPIRYYEAR" name="EWAY_CARDEXPIRYYEAR">
            <option value="17">2017</option>
            <option value="18">2018</option>
            <option value="19">2019</option>
            <option value="20">2020</option>
            <option value="21">2021</option>
            <option value="22">2022</option>
            <option value="23">2023</option>
            <option value="24">2024</option>
            <option value="25">2025</option>
            <option value="26">2026</option>
            <option value="27">2027</option>
        </select>
        
                </div>

            </div>


        <div class="row" style="margin-top: 10px">
            <div class="col-xs-12 col-md-6">
                <input type="email class="form-control" name="email" id="email" placeholder="Email">
            </div>
            <div class="col-xs-6 col-md-3">
                        <h4 >Quote : $<span id="totalPrice"><strong>0</strong></span></h4>
                        <!--<h4 >Quote : $<input type="text class="form-control" name="new_price" id="new_price" placeholder="Amount" value="0"></h4>-->
           
            </div>
            <div class="col-xs-6 col-md-3 ">
                <input type="hidden" name="totalPrice2" class="totalPrice2">
                <span id="dd" class="pull-right" >
                    <button style="background: rgb(2, 117, 216) none repeat scroll 0% 0%;padding-top:10px;" class="eway-button" id="testLatLng"><span style="display: block; min-height: 30px; background: rgb(2, 117, 216) none repeat scroll 0% 0%;color:#fff;">Submit</span></button>
                </span>
            </div>
        </div>

            <div class="row" style="margin-top: 10px">
                <div class="col-xs-12">
                    <div id="map"></div>
                </div>
            </div>

    </form>
    <script src="https://secure.ewaypayments.com/scripts/eCrypt.min.js"></script>

</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="assets/picker/picker.js"></script>
<script src="assets/picker/picker.date.js"></script>
<script src="assets/picker/picker.time.js"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.23/jquery.form-validator.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
    $("#name").on("keyup", function(){
            var id = $("#name").val();
            var data = 'name=' + id;
            $.ajax({
                type: "POST",
                url: "a_name_address.php",
                data: data,
                dataType: 'json',
                success: function (data) {
                      $("#phone").val(data.mobile);
                      $("#dropoff").val(data.pickup);
                      $("#email").val(data.email);
                } // success
            }); // ajax
    });
    $("#testLatLng").on("click", function(){

        var latlngVal = /^-?([0-8]?[0-9]|90)\.[0-9]{1,6},-?((1?[0-7]?|[0-9]?)[0-9]|180)\.[0-9]{1,6}$/;
        var latlng1 = $("input#lat1_value").val();
        var latlng2 = $("input#lat2_value").val();
        var invalid_latlng = 'Latitude and Longitude are not correct';
        
        // Validate Latitude and Longitude
        if (latlng1 != '' && latlng1 <= 200 && latlng1 >= -200) {

        } else{
             $('.lat_error_pickup').show();
             //alert(invalid_latlng+''+latlng1);
             return false;;
        }   
    });
});

    $('.datepicker').pickadate();
    $('.timepicker').pickatime({
        interval:15
    });

    $('#pickup, #dropoff, .datepicker, .timepicker, #name, #phone, #email').on('change', function () {
        distance();
    })

    map = null ;
    pickUp = null ;
    dropOff = null;
    directionsDisplay = null ;
    directionsService = null ;


    function initMap() {

        if(map == null)
        {
            map = new google.maps.Map(document.getElementById('map'), {
                zoom: 12,
                center: {lat: -31.9535959, lng: 115.8570118}
            });
        }
        var geocoder = new google.maps.Geocoder();

        if($('#pickup').val() != "")
            geocodeAddress(geocoder, map);

        if($('#dropoff').val() != "")
            geocodeAddress2(geocoder, map);

        if(directionsDisplay != null)
            directionsDisplay.setDirections({routes: []});

        directionsService = new google.maps.DirectionsService;
        directionsDisplay = new google.maps.DirectionsRenderer;
        directionsDisplay.setMap(map);

        calculateAndDisplayRoute(directionsService, directionsDisplay);
        distance();
    }

    function geocodeAddress(geocoder, resultsMap) {
        var address = document.getElementById('pickup').value;
        geocoder.geocode({'address': address}, function(results, status) {
            if (status === google.maps.GeocoderStatus.OK) {resultsMap.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({map: resultsMap,position: results[0].geometry.location});
					$lat1 = results[0].geometry.location.lat();
					$lng1 = results[0].geometry.location.lng();
            }
        });
    }

    function geocodeAddress2(geocoder, resultsMap) {
        var address = document.getElementById('dropoff').value;
        geocoder.geocode({'address': address}, function(results, status) {
            if (status === google.maps.GeocoderStatus.OK) {resultsMap.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({map: resultsMap,position: results[0].geometry.location});
					$lat2 = results[0].geometry.location.lat();
					$lng2 = results[0].geometry.location.lng();
            }
        });
    }

    function calculateAndDisplayRoute(directionsService, directionsDisplay) {
        directionsService.route({
            origin: document.getElementById('pickup').value,
            destination: document.getElementById('dropoff').value,
            travelMode: google.maps.TravelMode.DRIVING
        }, function(response, status) {
            if (status === google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            } else {
                // window.alert('Directions request failed due to ' + status);
            }
        });
    }

    // This example displays an address form, using the autocomplete feature
    // of the Google Places API to help users fill in the information.

    var placeSearch, dropoff , pickup ;

    function initAutocomplete() {
        pickup = new google.maps.places.Autocomplete((document.getElementById('pickup')),{types: ['geocode','establishment'],
            componentRestrictions:{country:"au"}});
        pickup.addListener();
        pickup.addListener('place_changed', function() {
            initMap();
        });
        dropoff = new google.maps.places.Autocomplete((document.getElementById('dropoff')),{types: ['geocode', 'establishment'],
            componentRestrictions:{country:"au"}});
        dropoff.addListener();
        dropoff.addListener('place_changed', function() {
            initMap();
        });
        initMap();
    }

    function distance() {
        var pickup = $('#pickup').val();
        var dropoff = $('#dropoff').val();
        var phone=$('#phone').val();
        var name=$('#name').val();
        var email=$('#email').val();
        var date=$('#date').val();
        var time=$('#time').val();
        var lat1 = $lat1;
        var lng1 = $lng1;
        var lat2 = $lat2;
        var lng2 = $lng2;
        $('#lat1_value').val($lat1);
        $('#lat2_value').val($lat2);

        if(pickup != "" && dropoff != "")
        {
            var dataString = 'point1=' + pickup + "&point2=" + dropoff;

            $.post("11.php", dataString, function (data) {
                console.log(data);
                $("#distance").text(data);
                if(isNumber(data))
                {
                    console.log(data);
                    var result = Math.ceil( ( ( data.split(" ")[0].replace("," , "") ) * 2.5) + 39 );
                    $('#totalPrice').text( result );
                    $('#totalPrice2').val( result );
                }
                $('#pay_button').attr('data-phone',phone);
                $('#pay_button').attr('data-email',email);
                url=encodeURIComponent('email='+email+'&name='+name+'&pickup='+pickup+'&dropoff='+dropoff+'&phone='+phone+'&date='+date+'&time='+time+'&lat1='+lat1+'&lng1='+lng1+'&lat2='+lat2+'&lng2='+lng2+'&price='+result);
		$('#urlCheck').text( url );
                $('#redirecturl').val( url );
            });
        }
    }

    function isNumber(obj) { return !isNaN(parseFloat(obj)) }


</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCzDCB1H9ZvIgT4NHGqCXajVQlRXKwTVY4&libraries=places,geometry&callback=initAutocomplete"   async defer></script>
<script>
    $.validate({});
</script>
</body>
</html>