-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
237 lines (204 loc) · 7.94 KB
/
script.js
File metadata and controls
237 lines (204 loc) · 7.94 KB
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
// ===============================
// Validation Utility
// ===============================
const Validator = {
validateEmail: (email) => {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(String(email).toLowerCase());
},
validatePassword: (password) => {
return password.length >= 6;
}
};
// ===============================
// Session Manager
// ===============================
const SessionManager = {
createSession: (user) => {
localStorage.setItem("session", JSON.stringify(user));
},
getSession: () => {
return JSON.parse(localStorage.getItem("session"));
},
clearSession: () => {
localStorage.removeItem("session");
}
};
// ===============================
// Login Handler
// ===============================
const Login = {
handleSubmit: (event) => {
event.preventDefault();
const formData = {
email: document.getElementById("email").value,
password: document.getElementById("password").value
};
// 1. Check registered users first
const users = JSON.parse(localStorage.getItem("users") || "[]");
const foundUser = users.find(
(u) => u.email === formData.email && u.password === formData.password
);
if (foundUser) {
SessionManager.createSession(foundUser);
alert(`Welcome, ${foundUser.firstName}! Redirecting...`);
window.location.href = "index.html"; // normal user dashboard
return;
}
// 2. Check admin users (hardcoded)
const adminUsers = [
{ email: "admin@redcross.org.ph", password: "Admin123!", role: "admin" },
{ email: "manager@redcross.org.ph", password: "Admin123!", role: "manager" },
{ email: "staff@redcross.org.ph", password: "Admin123!", role: "staff" },
{ email: "volunteer@redcross.org.ph", password: "Admin123!", role: "volunteer" }
];
const adminUser = adminUsers.find(
(u) => u.email === formData.email && u.password === formData.password
);
if (adminUser) {
SessionManager.createSession(adminUser);
alert(`Welcome, ${adminUser.role}! Redirecting...`);
window.location.href = "admin.html"; // admin dashboard
return;
}
// 3. Invalid credentials
alert("Invalid email or password. Please try again.");
}
};
// ===============================
// Registration Handler
// ===============================
document.addEventListener("DOMContentLoaded", () => {
const registerForm = document.getElementById("registerForm");
if (registerForm) {
registerForm.addEventListener("submit", (e) => {
e.preventDefault();
// Collect form values
const password = document.getElementById("password").value;
const confirmPassword = document.getElementById("confirmPassword").value;
// Validate passwords
if (password !== confirmPassword) {
alert("Passwords do not match!");
return;
}
if (password.length < 8) {
alert("Password must be at least 8 characters long!");
return;
}
const userData = {
surname: document.getElementById("surname").value,
firstName: document.getElementById("firstName").value,
middleName: document.getElementById("middleName").value,
age: document.getElementById("age").value,
sex: document.getElementById("sex").value,
birthdate: document.getElementById("birthdate").value,
address: document.getElementById("address").value,
mobile: document.getElementById("mobile").value,
email: document.getElementById("email").value,
bloodType: document.getElementById("bloodType").value,
maritalStatus: document.getElementById("maritalStatus").value,
occupation: document.getElementById("occupation").value,
password: password,
paid: false
};
// Get existing users
let users = JSON.parse(localStorage.getItem("users")) || [];
// Prevent duplicate by email
if (users.find(u => u.email === userData.email)) {
alert("Email already registered!");
return;
}
// Save new user
users.push(userData);
localStorage.setItem("users", JSON.stringify(users));
// Create session
localStorage.setItem("session", JSON.stringify({ email: userData.email }));
// Redirect to payment
alert("Registration successful! Please proceed to payment.");
window.location.href = "payment.html";
});
}
});
// ===============================
// Payment Handler
// ===============================
document.addEventListener("DOMContentLoaded", () => {
const paymentForm = document.getElementById("paymentForm");
if (paymentForm) {
const methodRadios = paymentForm.querySelectorAll("input[name='paymentMethod']");
const cardDetails = document.getElementById("cardDetails");
const gcashDetails = document.getElementById("gcashDetails");
const mayaDetails = document.getElementById("mayaDetails");
const bankDetails = document.getElementById("bankDetails");
// Hide all detail sections
const hideAll = () => {
[cardDetails, gcashDetails, mayaDetails, bankDetails].forEach(section => {
section.classList.add("hidden");
});
};
// Show selected method section
methodRadios.forEach(radio => {
radio.addEventListener("change", () => {
hideAll();
if (radio.checked) {
if (radio.value === "card") cardDetails.classList.remove("hidden");
if (radio.value === "gcash") gcashDetails.classList.remove("hidden");
if (radio.value === "maya") mayaDetails.classList.remove("hidden");
if (radio.value === "bank") bankDetails.classList.remove("hidden");
}
});
});
// Handle payment submission
paymentForm.addEventListener("submit", (e) => {
e.preventDefault();
const chosen = [...methodRadios].find(r => r.checked);
if (!chosen) {
alert("Please select a payment method.");
return;
}
const session = JSON.parse(localStorage.getItem("session"));
if (!session) {
alert("Session expired. Please register or login again.");
window.location.href = "login.html";
return;
}
// Get users and mark payment
let users = JSON.parse(localStorage.getItem("users")) || [];
let user = users.find(u => u.email === session.email);
if (user) {
user.paid = true;
localStorage.setItem("users", JSON.stringify(users));
alert(`Payment successful via ${chosen.value}!`);
window.location.href = "index.html"; // redirect after payment
} else {
alert("User not found. Please register again.");
window.location.href = "register.html";
}
});
}
});
// ===============================
// Attach Event Listeners
// ===============================
document.addEventListener("DOMContentLoaded", () => {
const loginForm = document.getElementById("loginForm");
if (loginForm) {
loginForm.addEventListener("submit", Login.handleSubmit);
}
const registerForm = document.getElementById("registerForm");
if (registerForm) {
registerForm.addEventListener("submit", Registration.handleSubmit);
}
const paymentForm = document.getElementById("paymentForm");
if (paymentForm) {
paymentForm.addEventListener("submit", Payment.handleSubmit);
}
const logoutBtn = document.getElementById("logoutBtn");
if (logoutBtn) {
logoutBtn.addEventListener("click", () => {
SessionManager.clearSession();
alert("You have been logged out.");
window.location.href = "login.html";
});
}
});