-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
368 lines (311 loc) · 15.5 KB
/
Copy pathscript.js
File metadata and controls
368 lines (311 loc) · 15.5 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
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
document.addEventListener('DOMContentLoaded', () => {
// Handle Navigation and Tabs (Common to all pages)
const whatIsThisTab = document.getElementById('what-is-this-tab');
const contributorsTab = document.getElementById('contributors-tab');
if (whatIsThisTab && contributorsTab) {
// Switch between tabs if the elements exist on the current page
whatIsThisTab.addEventListener('click', function() {
setActiveTab('what-is-this-tab');
document.getElementById('content').innerHTML = '<p>This section explains what AIT Wiki is all about.</p>';
});
contributorsTab.addEventListener('click', function() {
setActiveTab('contributors-tab');
document.getElementById('content').innerHTML = '<p>This section shows the list of contributors to AIT Wiki.</p>';
});
}
// Function to set active tab
function setActiveTab(tabId) {
const tabs = document.querySelectorAll('.nav-tab');
tabs.forEach(tab => tab.classList.remove('active'));
document.getElementById(tabId).classList.add('active');
}
// Handle Login Button (Only on pages where it exists, like index.html)
const loginButton = document.querySelector('.sign-up-btn');
if (loginButton) {
loginButton.addEventListener('click', () => {
window.location.href = 'login.html';
});
}
// Signup Form Handling (Only on signup.html)
const regForm = document.getElementById('regForm');
if (regForm) {
regForm.addEventListener('submit', function (e) {
e.preventDefault(); // Prevent the default form submission behavior
// Collect the form data
const userData = {
name: document.getElementById('name').value,
regno: document.getElementById('regnp').value,
branch: document.querySelector('select[name="dropdown_name"]').value,
number: document.getElementById('number').value,
email: document.getElementById('email').value,
password: document.getElementById('description').value,
rememberMe: document.getElementById('remember').checked
};
// Check if all required fields are filled
if (!userData.name || !userData.regno || !userData.email || !userData.password) {
alert('Please fill in all required fields.');
return; // Stop the process if fields are missing
}
// Store the data in localStorage or sessionStorage as needed
if (userData.rememberMe) {
localStorage.setItem('userData', JSON.stringify(userData));
localStorage.setItem('loggedIn', 'true');
} else {
sessionStorage.setItem('userData', JSON.stringify(userData));
sessionStorage.setItem('loggedIn', 'true');
}
alert('Registration successful! You will be redirected to the login page.');
// Redirect to login page
window.location.href = 'login.html';
});
}
// Login Form Handling (Only on login.html)
const loginForm = document.getElementById('loginForm');
if (loginForm) {
loginForm.addEventListener('submit', function (e) {
e.preventDefault(); // Prevent the form from submitting
const email = document.getElementById('loginEmail').value;
const password = document.getElementById('loginPassword').value;
// Retrieve stored user data from localStorage
const storedUserData = JSON.parse(localStorage.getItem('userData'));
if (storedUserData) {
// Check if the entered email and password match the stored data
if (email === storedUserData.email && password === storedUserData.password) {
alert(`Welcome back, ${storedUserData.name}!`);
// Check if "Remember Me" was selected
if (storedUserData.rememberMe) {
localStorage.setItem('loggedIn', 'true'); // Remember logged in state
} else {
sessionStorage.setItem('loggedIn', 'true'); // Session-based login
}
window.location.href = 'chat.html'; // Redirect to chat.html
} else {
alert('Invalid email or password.');
}
} else {
alert('No user data found. Please register first.');
}
});
}
// -------------------------------------------------------------------
const submitButton = document.getElementById('submit-query');
const chatbox = document.getElementById('chatbox');
// const responseDiv = document.getElementById('response');
const chatHistory = document.getElementById('chat-history');
const newChatButton = document.getElementById('new-chat');
// Function to load a JSON dataset (for clubs, timetable, holidays)
async function loadDataset(filename) {
try {
const response = await fetch(filename);
if (!response.ok) {
throw new Error(`Failed to load ${filename}`);
}
const data = await response.json();
console.log(`Loaded ${filename}:`, data); // Debugging: log loaded data
return data;
} catch (error) {
console.error(`Error loading ${filename}:`, error);
return null;
}
}
function appendMessage(message, sender) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message', sender); // 'message' class for common styling, 'user' or 'bot' class for distinction
messageDiv.innerText = message;
chatHistory.appendChild(messageDiv);
chatHistory.scrollTop = chatHistory.scrollHeight; // Auto-scroll to the latest message
}
// if (loginButton) {
// loginButton.addEventListener('click', () => {
// window.location.href = 'login.html';
// });
// }
if (newChatButton) {
newChatButton.addEventListener('click', () => {
chatHistory.innerHTML = ''; // Clear all chat history
}); }
// Function to find the matching club by name or alias
function findClubByName(userQuery, clubs) {
const queryLower = userQuery.toLowerCase(); // Normalize user query to lowercase
// Iterate over each club in the clubs object
for (const [clubName, clubData] of Object.entries(clubs)) {
// Check if the main club name matches the query
if (queryLower.includes(clubName.toLowerCase())) {
return clubName;
}
// Check if any of the club's aliases match the query
if (clubData.aliases) {
for (const alias of clubData.aliases) {
if (queryLower.includes(alias.toLowerCase())) {
return clubName; // Return the main club name if an alias matches
}
}
}
}
return null; // No matching club found
}
// Get the current day, time, and date
function getCurrentDateTime() {
const now = new Date();
const day = now.toLocaleString('en-US', { weekday: 'long' });
const time = now.toTimeString().split(' ')[0].substring(0, 5); // Get time in HH:MM format
const date = now.toISOString().split('T')[0]; // Get date in YYYY-MM-DD format
return { day, time, date };
}
// Check if today is a holiday (Saturday, Sunday, or national holiday)
function isHoliday(holidays, date) {
const { day } = getCurrentDateTime();
return day === "Saturday" || day === "Sunday" || holidays.includes(date);
}
// Find the current class for the user based on timetable
function getCurrentClass(timetable, branch, day, time, queryType) {
const todayClasses = timetable[branch][day];
if (!todayClasses || todayClasses.length === 0) {
return "You have no classes today.";
}
switch (queryType) {
case 'next':
// Find the next class after the current time
for (const cls of todayClasses) {
if (time < cls.startTime) {
return `Your next class is ${cls.subject} (${cls.startTime} - ${cls.endTime}).`;
}
}
return "You have no more classes today.";
case 'first':
// Find the first class of the day
const firstClass = todayClasses[0];
return `Your first class today was ${firstClass.subject} (${firstClass.startTime} - ${firstClass.endTime}).`;
case 'last':
// Find the last class of the day
const lastClass = todayClasses[todayClasses.length - 1];
return `Your last class today is ${lastClass.subject} (${lastClass.startTime} - ${lastClass.endTime}).`;
case 'previous':
// Find the previous class before the current time
let previousClass = null;
for (const cls of todayClasses) {
if (time > cls.endTime) {
previousClass = cls;
} else {
break;
}
}
return previousClass
? `Your previous class was ${previousClass.subject} (${previousClass.startTime} - ${previousClass.endTime}).`
: "You haven't had any classes yet today.";
default:
return "Sorry, I don't understand your query.";
}
}
// Handle user query submission
if (submitButton){
submitButton.addEventListener('click', async () => {
console.log("Submit button clicked"); // Debugging: Check if event listener works
const userQuery = chatbox.value.trim(); // Convert to lowercase for easier matching
console.log("User Query:", userQuery); // Debugging: Log the user query
if (!userQuery) return;
appendMessage(userQuery, 'user'); // Display the user's message
chatbox.value = '';
const { day, time, date } = getCurrentDateTime();
// Load external datasets
const clubs = await loadDataset('clubs.json'); // Load clubs data
const timetable = await loadDataset('timetable.json'); // Load timetable data
const holidays = await loadDataset('holidays.json'); // Load holidays data
// Check for errors in loading datasets
if (!clubs || !timetable || !holidays) {
appendMessage("Error loading required data. Please try again later.", 'AIT-wiki');
return;
}
const lowerCaseQuery = userQuery.toLowerCase()
// Club-related query handling
const clubMatch = lowerCaseQuery.match(/who (is|are|who's) the (\w+) of (.+) club\?/i); // Updated regex to capture everything after "of"
if (clubMatch) {
const position = clubMatch[2].toLowerCase(); // Normalize position (e.g., secretary)
const userClubQuery = clubMatch[3].toLowerCase().trim(); // Extract the part after "of"
// Use the function to find the club name (main or alias)
const matchedClub = findClubByName(userClubQuery, clubs);
if (matchedClub && clubs[matchedClub][position]) {
const people = clubs[matchedClub][position].join(', ');
appendMessage(`The ${position} of ${matchedClub} is/are: ${people}.`, 'AIT-wiki');
} else {
appendMessage(`Sorry, I don't have information about the ${position} of ${userClubQuery}.`, 'AIT-wiki');
}
return;
}
// Check for current class query
if (lowerCaseQuery.includes("current class") || lowerCaseQuery.includes("my current class")) {
const userBranch = JSON.parse(localStorage.getItem('userData')).branch; // Get user's branch from stored data
console.log("User Branch:", userBranch); // Debugging: Log the user's branch
// Check if it's a holiday
if (isHoliday(holidays.holidays, date)) {
appendMessage("Today is a holiday. No classes.");
} else {
// Get the current class for the user's branch
const currentClass = getCurrentClass(timetable, userBranch, day, time);
appendMessage(currentClass);
}
return;
}
appendMessage("Sorry, I don't understand your query.");
});
}
// -------------------------------------------
const passwordField = document.getElementById('description');
const rePasswordField = document.getElementById('re-password');
const passwordMatchMessage = document.getElementById('password-match-message');
const lengthCriteria = document.getElementById('length-criteria');
const uppercaseCriteria = document.getElementById('uppercase-criteria');
const lowercaseCriteria = document.getElementById('lowercase-criteria');
const numberCriteria = document.getElementById('number-criteria');
const specialCriteria = document.getElementById('special-criteria');
const passwordCriteriaList = document.getElementById('password-criteria'); // Criteria container
// Hide criteria on page load
passwordCriteriaList.style.display = 'none';
passwordMatchMessage.style.display = 'none';
// Listen for input in the password field
passwordField.addEventListener('input', () => {
// Show the criteria list only when the user starts typing
passwordCriteriaList.style.display = 'block';
const password = passwordField.value;
// Check length
if (password.length >= 8) {
lengthCriteria.style.display = 'none'; // Hide when satisfied
} else {
lengthCriteria.style.display = 'list-item'; // Show if unsatisfied
}
// Check for uppercase letter
if (/[A-Z]/.test(password)) {
uppercaseCriteria.style.display = 'none'; // Hide when satisfied
} else {
uppercaseCriteria.style.display = 'list-item'; // Show if unsatisfied
}
// Check for lowercase letter
if (/[a-z]/.test(password)) {
lowercaseCriteria.style.display = 'none'; // Hide when satisfied
} else {
lowercaseCriteria.style.display = 'list-item'; // Show if unsatisfied
}
// Check for number
if (/[0-9]/.test(password)) {
numberCriteria.style.display = 'none'; // Hide when satisfied
} else {
numberCriteria.style.display = 'list-item'; // Show if unsatisfied
}
// Check for special character
if (/[@#$%^&*(),.?":{}|<>]/.test(password)) {
specialCriteria.style.display = 'none'; // Hide when satisfied
} else {
specialCriteria.style.display = 'list-item'; // Show if unsatisfied
}
rePasswordField.addEventListener('input', () => {
const password = passwordField.value;
const rePassword = rePasswordField.value;
// Show message if passwords don't match, hide if they do
if (password !== rePassword) {
passwordMatchMessage.style.display = 'block'; // Show message
} else {
passwordMatchMessage.style.display = 'none'; // Hide message
}
})
})
});