forked from jpatokal/openflights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrip.js
93 lines (84 loc) · 2.51 KB
/
trip.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
/*
* Create new trips and modify existing ones
*/
var URL_TRIP = "/php/trip.php";
var trid = 0;
var type = "NEW";
window.onload = function init(){
gt = new Gettext({ 'domain' : 'messages' });
}
function xmlhttpPost(strURL, type) {
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_TRIP) {
editTrip(self.xmlHttpReq.responseText);
}
}
}
var query = "";
if(strURL == URL_TRIP) {
var form = document.forms['tripform'];
var privacy;
for (r=0; r < tripform.privacy.length; r++){
if (tripform.privacy[r].checked) {
privacy = tripform.privacy[r].value;
}
}
query = 'type=' + type + '&' +
'name=' + encodeURIComponent(form.name.value) + '&' +
'url=' + encodeURIComponent(form.url.value) + '&' +
'privacy=' + encodeURIComponent(privacy);
if(type == 'EDIT' || type == 'DELETE') {
query += '&trid=' + form.trid.value;
}
}
self.xmlHttpReq.send(query);
}
// Validate form
function validate(type) {
var form = document.forms['tripform'];
if(form.name.value == "") {
showError("Please enter a name for this trip.");
return;
}
document.getElementById("miniresultbox").innerHTML = "<i>Processing...</i>";
xmlhttpPost(URL_TRIP, type);
}
// Delete trip?
function deleteTrip() {
if(confirm("Are you sure you want to delete this trip? (Flights in this trip will NOT be deleted.)")) {
xmlhttpPost(URL_TRIP, "DELETE");
} else {
document.getElementById("miniresultbox").innerHTML = "<i>Deleting trip cancelled.</i>";
}
}
// Check if trip creation/editing/deletion succeeded
function editTrip(str) {
var code = str.split(";")[0];
var trid = str.split(";")[1];
var message = str.split(";")[2];
// Operation successful
if(code != "0") {
document.getElementById("miniresultbox").innerHTML = message;
var form = document.forms['tripform'];
parent.opener.newTrip(code, trid, form.name.value, form.url.value);
window.close();
} else {
showError(trid);
}
}
function showError(err) {
document.getElementById("miniresultbox").innerHTML = "<font color=red>" + err + "</font>";
}