forked from jpatokal/openflights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alsearch.js
351 lines (321 loc) · 10.9 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
/*
* Search for airline in database(s)
*/
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]) {
keys = args[1].split('&');
if(keys[0].split('=')[0] == "name") {
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 xmlHttpReq = false;
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) {
if(strURL == URL_ALSEARCH) {
if(action == "SEARCH") {
searchResult(self.xmlHttpReq.responseText);
}
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 == "") {
if(! 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 == "") {
if(! 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 == "") {
desc = name + ", " + country +
" (IATA: " + (iata == "" ? "N/A" : iata) + ", ICAO: " + (icao == "" ? "N/A" : icao) + ")";
if(! confirm(Gettext.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 = "";
if(! parent.opener || ! parent.opener.addNewAirport) {
guest = true;
} else {
guest = false;
}
if(warning) {
table += "<tr><td colspan=2><i><font color='red'>" + warning + "</font></i></td></tr>";
warning = null;
}
for(a in airlines) {
var col = airlines[a].split(";");
// First line contains header info
if(a == 0) {
offset = parseInt(col[0]);
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>" + Gettext.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 = jsonParse(airlines[a]);
if(a % 2 == 1) {
bgcolor = "#fff";
} else {
bgcolor = "#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) {
if(col["al_uid"] == "own") {
label = gt.gettext("Edit");
} else {
label = 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 search result into form
function loadAirline(data) {
var col = jsonParse(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"];
country = col["country"];
var country_select = form.country;
for(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'];
name = form.name.value;
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;
disabled = (mode != "F");
form.iata.disabled = disabled;
form.icao.disabled = disabled;
form.callsign.disabled = disabled;
form.iatafilter.disabled = disabled;
}
// Clear form -- everything *except* 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 main window and close this
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 + '.html', 'OpenFlights Help: ' + context, 'width=500,height=400,scrollbars=yes');
}