-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflight.js
340 lines (292 loc) · 14.1 KB
/
flight.js
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Load the XML data (assuming the XML file is hosted on the server)
async function loadFlightsXML() {
const response = await fetch('flights.xml');
const xmlText = await response.text();
const parser = new DOMParser();
return parser.parseFromString(xmlText, 'application/xml');
}
// Toggle return date field visibility based on trip type
function toggleReturnDate(show) {
const returnDateField = document.getElementById("returnDate");
const returnDateContainer = document.getElementById("returnDateContainer");
if (show) {
returnDateContainer.style.display = "block";
returnDateField.setAttribute("required", true);
} else {
returnDateContainer.style.display = "none";
returnDateField.removeAttribute("required");
}
}
// Show passenger form when the icon is clicked
function showPassengerForm() {
const passengerForm = document.getElementById("passengerForm");
const isVisible = passengerForm.style.display === "block";
passengerForm.style.display = isVisible ? "none" : "block";
// Get passenger input fields
const adults = document.getElementById("adults");
const children = document.getElementById("children");
const infants = document.getElementById("infants");
// Set required attributes based on form visibility
if (isVisible) {
adults.removeAttribute("required");
children.removeAttribute("required");
infants.removeAttribute("required");
} else {
adults.setAttribute("required", true);
children.setAttribute("required", true);
infants.setAttribute("required", true);
}
}
let cityData = { cities: [] }; // Default structure in case fetch fails
// Fetch cities data
fetch("./cities.json")
.then((response) => response.json())
.then((data) => {
cityData = data;
const originDropdown = document.getElementById("origin");
const destinationDropdown = document.getElementById("destination");
// Populate both origin and destination dropdowns with cities
data.cities.forEach((city) => {
const originOption = document.createElement("option");
originOption.value = city;
originOption.textContent = city;
originDropdown.appendChild(originOption);
const destinationOption = document.createElement("option");
destinationOption.value = city;
destinationOption.textContent = city;
destinationDropdown.appendChild(destinationOption);
});
})
.catch((error) => {
console.error("Error loading city data:", error);
});
// Validate and search for flights based on form inputs
async function validateFlightForm() {
const origin = document.getElementById("origin").value.trim().toLowerCase();
const destination = document
.getElementById("destination")
.value.trim()
.toLowerCase();
const departureDate = document.getElementById("departureDate").value;
const returnDateElement = document.getElementById("returnDate");
const returnDate = returnDateElement && returnDateElement.value ? returnDateElement.value : null;
const tripType = document.querySelector('input[name="tripType"]:checked').value;
const adults = parseInt(document.getElementById("adults").value) || 0;
const children = parseInt(document.getElementById("children").value) || 0;
const infants = parseInt(document.getElementById("infants").value) || 0;
const totalPassengers = adults + children + infants;
localStorage.setItem('totalPassengers', JSON.stringify(totalPassengers));
localStorage.setItem('adults', JSON.stringify(adults))
localStorage.setItem('children', JSON.stringify(children))
localStorage.setItem('infants', JSON.stringify(infants))
// Validate number of passengers
if (totalPassengers === 0) {
alert("Please enter at least one passenger.");
return;
}
if (adults < 1) {
alert("At least one adult is required for booking.");
return;
}
// Validate origin and destination
const originCity = document.getElementById("origin").value;
const destinationCity = document.getElementById("destination").value;
const validCities = cityData.cities;
if (!validCities.includes(originCity)) {
alert("Origin must be a city in Texas or California.");
return;
}
if (!validCities.includes(destinationCity)) {
alert("Destination must be a city in Texas or California.");
return;
}
if (origin === destination) {
alert("Origin and destination must be different.");
return;
}
// Validate departure date
const startDate = new Date("2024-09-01");
const endDate = new Date("2024-12-01");
const depDateObj = new Date(departureDate);
if (depDateObj < startDate || depDateObj > endDate) {
alert("Departure date must be between Sep 1, 2024, and Dec 1, 2024.");
return;
}
// Validate return date if round trip
let returnDateObj;
if (tripType === "roundtrip") {
if (!returnDate) {
alert("Please select a return date for a round trip.");
return;
}
returnDateObj = new Date(returnDate);
if (returnDateObj <= depDateObj) {
alert("Return date must be after the departure date.");
return;
}
}
// Load and search flights in XML
const xmlDoc = await loadFlightsXML();
const flights = xmlDoc.getElementsByTagName('flight');
// Search for departing flights
const departingFlights = searchFlights(flights, origin, destination, departureDate, totalPassengers);
// Handle round-trip search for return flights if applicable
let returningFlights = [];
if (tripType === "roundtrip") {
returningFlights = searchFlights(flights, destination, origin, returnDate, totalPassengers);
}
// Display results
if (tripType === "oneway") {
displayFlights(departingFlights, "Departing Flights");
} else {
displayRoundTripFlights(departingFlights, returningFlights);
}
}
// Function to search for flights based on input criteria
function searchFlights(flights, origin, destination, date, totalPassengers) {
flightsContainer.innerHTML="<h3>Available Flights</h3>";
const results = [];
const searchDate = new Date(date);
for (let i = 0; i < flights.length; i++) {
const flight = flights[i];
const flightOrigin = flight.getElementsByTagName('origin')[0].textContent.toLowerCase();
const flightDestination = flight.getElementsByTagName('destination')[0].textContent.toLowerCase();
const flightDate = flight.getElementsByTagName('departure-date')[0].textContent;
const availableSeats = parseInt(flight.getElementsByTagName('available-seats')[0].textContent);
const flightDateObj = new Date(flightDate);
const diffInDays = Math.abs((flightDateObj - searchDate) / (1000 * 60 * 60 * 24));
if (
flightOrigin === origin &&
flightDestination === destination &&
diffInDays <= 3 &&
availableSeats >= totalPassengers
) {
results.push(flight);
}
}
return results;
}
// Function to display round-trip flight options
function displayRoundTripFlights(departingFlights, returningFlights) {
const flightsContainer = document.getElementById('flightsContainer');
flightsContainer.innerHTML = ''; // Clear previous results
const departingHeader = document.createElement('h4');
departingHeader.textContent = "Available Departing Flights";
flightsContainer.appendChild(departingHeader);
displayFlights(departingFlights, "Departing Flights");
const returningHeader = document.createElement('h4');
returningHeader.textContent = "Available Returning Flights";
flightsContainer.appendChild(returningHeader);
displayFlights(returningFlights, "Returning Flights");
// Add the unified "Select Both Flights" button if flights are displayed
if (departingFlights.length > 0 && returningFlights.length > 0) {
const selectBothButton = document.createElement('button');
selectBothButton.id = 'selectBothFlightsButton';
selectBothButton.textContent = 'Select Both Flights';
selectBothButton.className = 'book-button';
selectBothButton.onclick = selectBothFlights;
flightsContainer.appendChild(selectBothButton);
}
}
// Function to display flights
function displayFlights(flights, type) {
const flightsContainer = document.getElementById('flightsContainer');
if (flights.length === 0) {
const noFlightsMsg = document.createElement('p');
noFlightsMsg.textContent = `No available ${type.toLowerCase()} found for your criteria.`;
flightsContainer.appendChild(noFlightsMsg);
return;
}
flights.forEach(flight => {
const flightId = flight.getElementsByTagName('flight-id')[0].textContent;
const origin = flight.getElementsByTagName('origin')[0].textContent;
const destination = flight.getElementsByTagName('destination')[0].textContent;
const departureDate = flight.getElementsByTagName('departure-date')[0].textContent;
const arrivalDate = flight.getElementsByTagName('arrival-date')[0].textContent;
const departureTime = flight.getElementsByTagName('departure-time')[0].textContent;
const arrivalTime = flight.getElementsByTagName('arrival-time')[0].textContent;
const availableSeats = flight.getElementsByTagName('available-seats')[0].textContent;
const price = flight.getElementsByTagName('price')[0].textContent;
const flightCard = document.createElement('div');
flightCard.className = 'flight-card';
flightCard.setAttribute('data-type', type); // Add data-type attribute for distinguishing flights
flightCard.innerHTML = `
<p><strong>Flight ID:</strong> ${flightId}</p>
<p><strong>Origin:</strong> ${origin}</p>
<p><strong>Destination:</strong> ${destination}</p>
<p><strong>Departure Date:</strong> ${departureDate}</p>
<p><strong>Arrival Date:</strong> ${arrivalDate}</p>
<p><strong>Departure Time:</strong> ${departureTime}</p>
<p><strong>Arrival Time:</strong> ${arrivalTime}</p>
<p><strong>Available Seats:</strong> ${availableSeats}</p>
<p><strong>Price:</strong> $${price}</p>
`;
// Add a "Select Flight" button for one-way trips
const tripType = document.querySelector('input[name="tripType"]:checked')?.value;
if (tripType !== "roundtrip") {
const selectButton = document.createElement('button');
selectButton.textContent = "Select Flight";
selectButton.className = 'book-button';
selectButton.onclick = () => selectOneWayFlight(flightId);
flightCard.appendChild(selectButton);
}
flightsContainer.appendChild(flightCard);
});
}
let selectedDepartingFlight = null;
let selectedReturningFlight = null;
function selectOneWayFlight(flightId) {
const flightsContainer = document.getElementById('flightsContainer');
const selectedCard = Array.from(flightsContainer.getElementsByClassName('flight-card'))
.find(card => card.innerText.includes(`Flight ID: ${flightId}`));
const selectedFlight = {
flightId,
origin: selectedCard.querySelector('p:nth-child(2)').textContent.split(': ')[1],
destination: selectedCard.querySelector('p:nth-child(3)').textContent.split(': ')[1],
departureDate: selectedCard.querySelector('p:nth-child(4)').textContent.split(': ')[1],
arrivalDate: selectedCard.querySelector('p:nth-child(5)').textContent.split(': ')[1],
departureTime: selectedCard.querySelector('p:nth-child(6)').textContent.split(': ')[1],
arrivalTime: selectedCard.querySelector('p:nth-child(7)').textContent.split(': ')[1],
price: parseFloat(selectedCard.querySelector('p:nth-child(9)').textContent.split('$')[1]),
};
// Store the selected flight in localStorage
localStorage.setItem('selectedDepartingFlight', JSON.stringify(selectedFlight));
alert(`Departing flight ${flightId} selected!`);
// Redirect to the cart page
window.location.href = 'cart.html';
}
function selectBothFlights() {
// Retrieve the first displayed departing and returning flights
const departingCard = document.querySelector('.flight-card[data-type="Departing Flights"]');
const returningCard = document.querySelector('.flight-card[data-type="Returning Flights"]');
if (!departingCard || !returningCard) {
alert('Please select both departing and returning flights.');
return;
}
// Extract and store the details of the departing flight
const selectedDepartingFlight = {
flightId: departingCard.querySelector('p:nth-child(1)').textContent.split(': ')[1],
origin: departingCard.querySelector('p:nth-child(2)').textContent.split(': ')[1],
destination: departingCard.querySelector('p:nth-child(3)').textContent.split(': ')[1],
departureDate: departingCard.querySelector('p:nth-child(4)').textContent.split(': ')[1],
arrivalDate: departingCard.querySelector('p:nth-child(5)').textContent.split(': ')[1],
departureTime: departingCard.querySelector('p:nth-child(6)').textContent.split(': ')[1],
arrivalTime: departingCard.querySelector('p:nth-child(7)').textContent.split(': ')[1],
price: parseFloat(departingCard.querySelector('p:nth-child(9)').textContent.split('$')[1]),
};
localStorage.setItem('selectedDepartingFlight', JSON.stringify(selectedDepartingFlight));
// Extract and store the details of the returning flight
const selectedReturningFlight = {
flightId: returningCard.querySelector('p:nth-child(1)').textContent.split(': ')[1],
origin: returningCard.querySelector('p:nth-child(2)').textContent.split(': ')[1],
destination: returningCard.querySelector('p:nth-child(3)').textContent.split(': ')[1],
departureDate: returningCard.querySelector('p:nth-child(4)').textContent.split(': ')[1],
arrivalDate: returningCard.querySelector('p:nth-child(5)').textContent.split(': ')[1],
departureTime: returningCard.querySelector('p:nth-child(6)').textContent.split(': ')[1],
arrivalTime: returningCard.querySelector('p:nth-child(7)').textContent.split(': ')[1],
price: parseFloat(returningCard.querySelector('p:nth-child(9)').textContent.split('$')[1]),
};
localStorage.setItem('selectedReturningFlight', JSON.stringify(selectedReturningFlight));
alert('Both departing and returning flights have been selected!');
window.location.href = 'cart.html';
}