-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.js
More file actions
290 lines (229 loc) · 10.5 KB
/
book.js
File metadata and controls
290 lines (229 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*global $*/
var book = (function() {
"use strict";
var vehicles;
var size;
var pickupDate, dropoffDate;
var registration_number;
var name;
var phone;
var pub = {};
/* This function starts the booking process by displaying the sizes of the cars to the user. */
function startBooking() {
size = "";
$("#createBooking").empty();
$("#createBooking").append("<p>Please choose a car size.</p>");
$("#createBooking").append("<div class='size'>" +
"<img src='images/smallCar.jpg' alt='small car'>" +
"<figcaption>Small Car</figcaption></div>" +
"<div class='size'>" +
"<img src='images/mediumCar.jpg' alt='medium car'>" +
"<figcaption>Medium Car</figcaption></div>" +
"<div class='size'>" +
"<img src='images/largeCar.jpg' alt='large car'>" +
"<figcaption>Large Car</figcaption></div>" +
"<div class='size'>" +
"<img src='images/luxuryCar.jpg' alt='luxury car'>" +
"<figcaption>Luxury Car</figcaption></div>");
$(".size").find("img").each(function() {
$(this).click(chooseDates);
$(this).css('cursor', 'pointer');
});
}
/* This function allows the user to choose the dates they want to book */
function chooseDates() {
if (size.length === 0) {
size = $(this).attr("src");
size = size.replace("images/", "");
size = size.replace(".jpg", "");
size = size.replace("Car", "");
size = size.charAt(0).toUpperCase() + size.substring(1, size.length);
}
$("#createBooking").empty();
$("#createBooking").append("<h3>Pickup and Dropoff Dates</h3>");
$("#createBooking").append("<p>Please confirm your pickup and dropoff dates</p>");
$("#createBooking").append("<form id='dates'><div id='pickup'>" +
"<label for='pickupDate'>Pickup Date:</label>" +
"<input type='date' id='pickupDate' name='pickupDate'>" +
"</div>" +
"<div id='dropoff'>" +
"<label for='dropoffDate'>Dropoff Date:</label>" +
"<input type='date' id='dropoffDate' name='dropoffDate'>" +
"</div>" +
"<button id='confirm' type='button'>Confirm</button>" +
"</form>" +
"<div id='errormessage'></div>");
$("#confirm").click(checkDates);
$("#createBooking").append("<button id='back' type='button'>Go Back</button>");
$("#back").click(startBooking);
}
/* This function checks if the dates are valid */
function checkDates() {
$("#errormessage").empty();
pickupDate = new Date($("#pickupDate").val());
dropoffDate = new Date($("#dropoffDate").val());
var todayDate = new Date();
var valid = true;
if (pickupDate.toString() === "Invalid Date") {
$("#errormessage").append("<p>Please input a pickup date</p>");
valid = false;
}
if (dropoffDate.toString() === "Invalid Date") {
$("#errormessage").append("<p>Please input a dropoff date</p>");
valid = false;
}
if (pickupDate < todayDate && pickupDate.setHours(0 , 0, 0, 0) != todayDate.setHours(0, 0, 0, 0)) {
$("#errormessage").append("<p>Your pickup date can't be in the past</p>");
valid = false;
}
if (pickupDate.setHours(0, 0, 0, 0) === todayDate.setHours(0, 0, 0, 0)) {
$("#errormessage").append("<p>Your pickup date can't be today");
valid = false;
}
if (dropoffDate < pickupDate) {
$("#errormessage").append("<p>Your dropoff date can't be before your pickup date");
valid = false;
}
if (dropoffDate.setHours(0, 0, 0, 0) === todayDate.setHours(0, 0, 0, 0)) {
$("#errormessage").append("<p>Your dropoff date can't be today");
valid = false;
}
if (valid === true) {
displayChoices();
}
}
/* This function displays the vehicles the user can choose, it will check against the booking.json file to see if any vehicle is booked
at the dates that user requested.
*/
function displayChoices() {
$("#createBooking").empty();
$.ajax ({
type: "GET",
url: "./Resources/bookings.json",
dataType: 'json',
cache: false,
success: function(data) {
var bookedVehicles = [];
$("#createBooking").append("<h3>" + size + " Cars</h3>");
$("#createBooking").append("<hr>");
$(data.bookings.booking).each(function() {
var bookedPickup = new Date(this.pickup.month + "/" + this.pickup.day + "/" + this.pickup.year);
var bookedDropoff = new Date(this.dropoff.month + "/" + this.dropoff.day + "/" + this.dropoff.year);
if ((pickupDate <= bookedDropoff) && (bookedPickup <= dropoffDate)) {
bookedVehicles.push(this.number);
}
});
$(vehicles.fleet.vehicle).each(function() {
if (bookedVehicles.includes(this.registration) === false) {
if (this.vehicleType === size) {
var imageURL = "images/" + this.registration + ".jpg";
$("#createBooking").append("<div class ='vehicleItem'>" +
"<ul>" +
"<li class='registration'>Registration: " + this.registration + "</li>" +
"<li>Vehicle Type: " + this.vehicleType + "</li>" +
"<li>Vehicle Description: " + this.description + "</li>" +
"<li class='price'>Vehicle Price per Day: $" + this.pricePerDay + "</li>" +
"</ul>" +
"<img src='" + imageURL + "' alt='car picture'>" +
"<button class='confirm' type='button'>Book this car</button>" +
"</div>" +
"<hr>");
}
}
});
if ($(".vehicleItem").length >= 1) {
$(".confirm").click(confirmBooking);
} else {
$("#createBooking").append("Sorry, no cars are currently available");
}
$("#createBooking").append("<button id='back' type='button'>Go Back</button>");
$("#back").click(chooseDates);
},
error: function() {
$("#createBooking").html("Sorry, something has gone wrong on our end.");
}
});
}
/* This function is the final confirmation confirming the booking with the user */
function confirmBooking() {
registration_number = $(this).parent().find(".registration").text();
var price = $(this).parent().find(".price").text();
registration_number = registration_number.replace("Registration: ", "");
price = price.replace("Vehicle Price per Day: $", "");
price = parseFloat(price);
var days = Math.round((dropoffDate - pickupDate) / (1000*60*60*24));
var totalPrice = price * days;
var item = $(this).parent()[0].outerHTML;
$(this).remove();
$("#createBooking").empty();
$("#createBooking").append("<h3>Confirm Booking</h3><hr>");
$("#createBooking").append(item);
$(".confirm").remove();
$("#createBooking").append("<hr>");
$("#createBooking").append("<p>You have booked this car from " +
pickupDate.getDate() + "/" + pickupDate.getMonth() + "/" + pickupDate.getFullYear() + " till the " +
dropoffDate.getDate() + "/" + dropoffDate.getMonth() + "/" + dropoffDate.getFullYear() + "</p>");
$("#createBooking").append("<p>The total price of your booking is: $" + totalPrice.toFixed(2) + "NZD</p>");
$("#createBooking").append("<p>Please provide your details below</p>" +
"<form id='contact'>" +
"<label for='name'>Name:</label>" +
"<input type='text' id='name' name='name'>" +
"<label for='phone'>Phone:</label>" +
"<input type='text' id='phone' name='phone'>" +
"</form>");
$("#createBooking").append("<div id='errormessage'></div>");
$("#createBooking").append("<button id='back' type='button'>Go Back</button>");
$("#back").click(displayChoices);
$("#createBooking").append("<button id='complete' type='button'>Complete Booking</button>");
$("#complete").click(checkDetails);
}
/* This function checks if the details provided are valid */
function checkDetails() {
name = $("#name").val();
phone = $("#phone").val();
var valid = true;
$("#errormessage").empty();
if (name < 1) {
$("#errormessage").append("<p>Please provide a name</p>");
valid = false;
}
if (phone < 1) {
$("#errormessage").append("<p>Please provide a phone number</p>");
valid = false;
}
if (isNaN(phone) === true) {
$("#errormessage").append("<p>Please don't provide letters in your phone number");
valid = false;
}
if (valid === true) {
bookCar();
}
}
/* This function confirms the booking and sends the booking off making it final */
function bookCar() {
var booking = {number: registration_number, name: name, pickup: pickupDate, dropoff: dropoffDate};
var output = JSON.stringify(booking);
window.sessionStorage.setItem("booking", output);
$("#createBooking").empty();
$("#createBooking").append("<h3>Booking Successful</h3>");
$("#createBooking").append("<p>Thanks for booking with us</p>");
}
/* Setup function gets the data from vehicles.json ready for the webiste to use */
pub.setup = function() {
$.ajax ({
type: "GET",
url: "./Resources/vehicles.json",
dataType: 'json',
cache: false,
success: function(data) {
vehicles = data;
startBooking();
},
error: function() {
$("#createBooking").html("Sorry, something has gone wrong on our end.");
}
});
}
return pub;
}());
$(document).ready(book.setup);