-
Notifications
You must be signed in to change notification settings - Fork 394
/
alsearch.js
491 lines (461 loc) · 12.4 KB
/
alsearch.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* Search for airline in database(s)
*/
const URL_ALSEARCH = "/php/alsearch.php";
var warning;
var gt;
window.onload = function init() {
gt = new Gettext({ domain: "messages" });
// ...?name=x&mode=y
// 0 1 2 3 4
var args = window.location.href.split("?");
if (args[1]) {
var keys = args[1].split("&");
if (keys[0].split("=")[0] == "name") {
var form = document.forms["searchform"];
form.name.value = unescape(keys[0].split("=")[1]);
selectInSelect(form.mode, keys[1].split("=")[1]);
selectInSelect(form.active, "Y");
changeMode();
}
}
};
function doSearch(offset) {
xmlhttpPost(URL_ALSEARCH, offset, "SEARCH");
}
function doRecord(offset) {
xmlhttpPost(URL_ALSEARCH, offset, "RECORD");
}
function xmlhttpPost(strURL, offset, action) {
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open("POST", strURL, true);
self.xmlHttpReq.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
self.xmlHttpReq.onreadystatechange = function () {
if (self.xmlHttpReq.readyState == 4 && strURL == URL_ALSEARCH) {
if (action == "SEARCH") {
searchResult(self.xmlHttpReq.responseText);
} else if (action == "RECORD") {
recordResult(self.xmlHttpReq.responseText);
}
}
};
var query = "";
if (strURL == URL_ALSEARCH) {
var form = document.forms["searchform"];
var name = form.name.value;
var country = form.country[form.country.selectedIndex].text;
var iata = form.iata.value;
var icao = form.icao.value;
var alias = form.alias.value;
var callsign = form.callsign.value;
var mode = form.mode.value;
var active = form.active.value;
var alid = form.alid.value;
if (iata != "" && iata.length != 2) {
alert(gt.gettext("IATA codes must be exactly two letters."));
form.iata.focus();
return;
} else {
iata = iata.toUpperCase();
form.iata.value = iata;
}
if (icao != "" && icao.length != 3) {
alert(gt.gettext("ICAO codes must be exactly three letters."));
form.icao.focus();
return;
} else {
icao = icao.toUpperCase();
form.icao.value = icao;
}
if (["XXX", "YYY", "ZZZ"].indexOf(icao) > -1) {
alert(gt.gettext("Invalid ICAO code."));
form.iata.focus();
return;
}
if (action == "RECORD") {
if (!parent.opener || !parent.opener.addNewAirline) {
alert(
gt.gettext(
"Sorry, you have to be logged into OpenFlights to use this."
)
);
return;
}
if (name == "") {
alert(gt.gettext("Please enter a name."));
form.name.focus();
return;
} else {
name = name.substring(0, 1).toUpperCase() + name.substring(1);
form.name.value = name;
}
if (country == "ALL") {
alert(gt.gettext("Please select a country."));
form.country.focus();
return;
}
if (active == "") {
alert(
gt.gettext(
"Please select Yes for airlines that are still operating, or No for inactive airlines."
)
);
form.active.focus();
return;
}
if (mode == "F") {
if (
iata == "" &&
!confirm(
gt.gettext(
"You have not entered an IATA/FAA code. Are you sure the airline does not have one and you wish to proceed?"
)
)
) {
return;
}
if (
icao == "" &&
!confirm(
gt.gettext(
"You have not entered an ICAO code. Are you sure the airline does not have one and you wish to proceed?"
)
)
) {
return;
}
}
// Last check for new airlines only
if (alid == "") {
var desc =
name +
", " +
country +
" (" +
gt.gettext("IATA") +
": " +
(iata == "" ? gt.gettext("N/A") : iata) +
", " +
gt.gettext("ICAO") +
": " +
(icao == "" ? gt.gettext("N/A") : icao) +
")";
if (
!confirm(
gt.strargs(
gt.gettext(
"Are you sure you want to add %1 as a new operator? Please double-check the name and any airline codes before confirming."
),
[desc]
)
)
) {
document.getElementById("miniresultbox").innerHTML =
"<i>" + gt.gettext("Cancelled.") + "</i>";
return;
}
}
}
query =
"name=" +
encodeURIComponent(name) +
"&" +
"alias=" +
encodeURIComponent(alias) +
"&" +
"iata=" +
encodeURIComponent(iata) +
"&" +
"icao=" +
encodeURIComponent(icao) +
"&" +
"country=" +
encodeURIComponent(country) +
"&" +
"callsign=" +
encodeURIComponent(callsign) +
"&" +
"mode=" +
encodeURIComponent(mode) +
"&" +
"active=" +
encodeURIComponent(active) +
"&" +
"offset=" +
offset +
"&" +
"iatafilter=" +
form.iatafilter.checked +
"&" +
"alid=" +
alid +
"&" +
"action=" +
action;
document.getElementById("miniresultbox").innerHTML =
"<i>" +
(action == "SEARCH"
? gt.gettext("Searching...")
: gt.gettext("Recording...")) +
"</i>";
}
self.xmlHttpReq.send(query);
}
/**
* Display results of search
*/
function searchResult(str) {
var airlines = str.split("\n");
var table = "<table width=95% cellspacing=0>";
var offset, sql;
var disclaimer = "";
var guest = !parent.opener || !parent.opener.addNewAirport;
if (warning) {
table +=
"<tr><td colspan=2><i><font color='red'>" +
warning +
"</font></i></td></tr>";
warning = null;
}
for (var a in airlines) {
var col = airlines[a].split(";");
// The first line contains header info
if (a == 0) {
offset = parseInt(col[0]);
var max = col[1];
sql = col[2];
if (max == 0) {
table +=
"<tr><td><i>" + gt.gettext("No matches found.") + "</i></td></td>";
break;
}
table +=
"<tr><td><b>" +
gt.strargs(gt.gettext("Results %1 to %2 of %3"), [
offset + 1,
Math.min(offset + 10, max),
max,
]) +
"</b><br></td>";
if (max > 10) {
table += '<td style="text-align: right"><nobr>';
if (offset - 10 >= 0) {
table +=
'<input id="b_back" type="button" value="<" onClick="doSearch(' +
(offset - 10) +
')">';
} else {
table += '<input type="button" value="<" disabled>';
}
if (offset + 10 < max) {
table +=
'<input id="b_fwd" type="button" value=">" onClick="doSearch(' +
(offset + 10) +
')">';
} else {
table += '<input type="button" value=">" disabled>';
}
table += "</nobr></td>";
}
table += "</tr>";
continue;
}
// Meat of the table
var col = JSON.parse(airlines[a]);
var bgcolor = a % 2 == 1 ? "#fff" : "#ddd";
switch (col["al_uid"]) {
case "user":
bgcolor = "#fdd";
disclaimer =
"<br><span style='background-color: " +
bgcolor +
"'>" +
gt.gettext(
"Operators in pink have been added by users of OpenFlights."
) +
"</span>";
break;
case "own":
bgcolor = "#ddf";
disclaimer =
"<br><span style='background-color: " +
bgcolor +
"'>" +
gt.gettext(
"Operators in blue have been added by you and can be edited."
) +
"<span>";
break;
}
table +=
"<tr><td style='background-color: " +
bgcolor +
"'>" +
col["al_name"] +
"</td>";
// id = alid
table +=
"<td style='text-align: right; background-color: " +
bgcolor +
"'><input type='button' value='" +
gt.gettext("Select") +
"' onClick='selectAirline(\"" +
col["alid"] +
'","' +
encodeURIComponent(col["al_name"]) +
'","' +
col["mode"] +
"\")'></td>";
if (col["al_uid"] == "own" || guest) {
var label =
col["al_uid"] == "own" ? gt.gettext("Edit") : gt.gettext("Load");
table +=
"<td style='text-align: right; background-color: " +
bgcolor +
"'><input type='button' value='" +
label +
"' onClick='loadAirline(\"" +
encodeURIComponent(airlines[a]) +
"\")'></td>";
}
table += "</tr>";
}
table += "</table>";
table += disclaimer;
document.getElementById("miniresultbox").innerHTML = table;
}
/**
* Load data from the search results into the form
* @param data {string}
*/
function loadAirline(data) {
var col = JSON.parse(unescape(data));
var b_back = document.getElementById("b_back");
var b_fwd = document.getElementById("b_fwd");
if (b_back) b_back.disabled = true;
if (b_fwd) b_fwd.disabled = true;
var form = document.forms["searchform"];
form.name.value = col["name"];
if (col["alias"] != "null") {
form.alias.value = col["alias"];
}
if (col["iata"] != "null") {
form.iata.value = col["iata"];
}
if (col["icao"] != "null") {
form.icao.value = col["icao"];
}
if (col["callsign"] != "null") {
form.callsign.value = col["callsign"];
}
form.mode.value = col["mode"];
var country = col["country"];
var country_select = form.country;
for (var index = 0; index < country_select.length; index++) {
if (
country_select[index].value == country ||
country_select[index].text == country
) {
country_select.selectedIndex = index;
}
}
var active_select = form.active;
for (index = 0; index < active_select.length; index++) {
if (active_select[index].value == col["active"]) {
active_select.selectedIndex = index;
}
}
if (col["alid"]) {
form.alid.value = col["alid"];
document.getElementById("b_add").style.display = "none";
document.getElementById("b_edit").style.display = "inline";
} else {
form.alid.value = "";
document.getElementById("b_add").style.display = "inline";
document.getElementById("b_edit").style.display = "none";
}
}
// Did we manage to record the airline?
function recordResult(str) {
var col = str.split(";");
// Error?
if (col[0] != "1") {
document.getElementById("miniresultbox").innerHTML = col[1];
} else {
document.getElementById("miniresultbox").innerHTML = col[2];
// Select newly minted airline and return to main
// 1;alid
var form = document.forms["searchform"];
var name = form.name.value;
var mode = form.mode.value;
if (mode == "F") {
var iata = form.iata.value;
name += " (" + (iata != "" ? iata : form.icao.value) + ")";
}
selectAirline(col[1], name, mode);
}
}
/**
* Enable IATA,ICAO,callsign only for flights
*/
function changeMode() {
var form = document.forms["searchform"];
var mode = form.mode.value;
var disabled = mode != "F";
form.iata.disabled = disabled;
form.icao.disabled = disabled;
form.callsign.disabled = disabled;
form.iatafilter.disabled = disabled;
}
/**
* Clear form -- everything *except* the database
*/
function clearSearch() {
var form = document.forms["searchform"];
form.name.value = "";
form.country.selectedIndex = 0;
form.active.selectedIndex = 0;
form.mode.selectedIndex = 0;
changeMode();
form.iata.value = "";
form.icao.value = "";
form.alias.value = "";
form.callsign.value = "";
form.iatafilter.checked = true;
form.alid.value = "";
}
/**
* Airline selected, kick it back to the main window and close this
* @param data
* @param name
* @param mode
*/
function selectAirline(data, name, mode) {
if (!parent.opener || !parent.opener.addNewAirline) {
alert(
gt.gettext("Sorry, you have to be logged into OpenFlights to do this.")
);
}
parent.opener.addNewAirline(data, unescape(name), mode);
window.close();
}
// A dupe from openflights.js...
function help(context) {
window.open(
"/help/" + context + ".php",
// TODO: Localise?
"OpenFlights Help: " + context,
"width=500,height=400,scrollbars=yes"
);
}