-
Notifications
You must be signed in to change notification settings - Fork 394
/
trip.js
120 lines (109 loc) · 2.72 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
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
/*
* Create new trips and modify existing ones
*/
const 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 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_TRIP) {
editTrip(self.xmlHttpReq.responseText);
}
};
var query = "";
if (strURL == URL_TRIP) {
var form = document.forms["tripform"],
privacy;
for (var 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
* @param type
*/
function validate(type) {
var form = document.forms["tripform"];
if (form.name.value == "") {
showError(gt.gettext("Please enter a name for this trip."));
return;
}
document.getElementById("miniresultbox").innerHTML =
"<i>" + gt.gettext("Processing...") + "</i>";
xmlhttpPost(URL_TRIP, type);
}
/**
* Delete trip?
*/
function deleteTrip() {
if (
confirm(
gt.gettext(
"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>" + gt.gettext("Deleting trip cancelled.") + "</i>";
}
}
/**
* Check if trip creation/editing/deletion succeeded
* @param str {string}
*/
function editTrip(str) {
var code = str.split(";")[0],
trid = str.split(";")[1],
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>";
}