-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptForEnrollment.js
More file actions
297 lines (238 loc) · 9.56 KB
/
Copy pathscriptForEnrollment.js
File metadata and controls
297 lines (238 loc) · 9.56 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
/// Check if childCount exists in local storage, initialize it to 0 if not
if (!localStorage.getItem('childCount')) {
localStorage.setItem('childCount', '0');
}
// Retrieve childCount from local storage
var childCount = parseInt(localStorage.getItem('childCount'));
// Create an array to store children's names
var childrenNames = [];
// Check if childCount is 0 localStorage is empty
if (childCount === 0 ) {
//Create a sample child
var sampleChild = {
firstName: 'Khalid',
lastName: 'Abdullah',
};
var sampleChild2 = {
firstName: 'Mona',
lastName: 'Abdullah',
};
// Add the sample child to the childrenNames array
childrenNames.push(sampleChild.firstName + ' ' + sampleChild.lastName);
childrenNames.push(sampleChild2.firstName + ' ' + sampleChild2.lastName);
}
// Check if childCount is 2 there are no children registered
else if (childCount === 2) {
for (var i = 1; i <= childCount; i++) {
var firstName = localStorage.getItem(`childFirstName${i}`);
var lastName = localStorage.getItem(`childLastName${i}`);
childrenNames.push(firstName + ' ' + lastName);
}
}
// there is children have registered
else {
// Loop through childCount and retrieve children's names from local storage
for (var i = 3; i <= childCount; i++) {
var firstName = localStorage.getItem(`childFirstName${i}`);
var lastName = localStorage.getItem(`childLastName${i}`);
childrenNames.push(firstName + ' ' + lastName);
}
}
// Get the dropdown element
var dropdown = document.getElementById('dropdown');
// Clear existing options in the dropdown
dropdown.innerHTML = '';
// Create the "Select a child" option
var selectOption = document.createElement('option');
selectOption.disabled = true;
selectOption.selected = true;
selectOption.textContent = 'Select a child';
dropdown.appendChild(selectOption);
// Populate the dropdown with children's names
childrenNames.forEach(function(name) {
var option = document.createElement('option');
option.textContent = name;
dropdown.appendChild(option);
});
// Create a multidimensional array for courses
var courses = [
['Robotics', 'Mariam Alyami'],
['Python Programming', 'Ahmad Alghamdi'],
['Web Development', 'Ahmad Alghamdi'],
['Website Design Fundamentals', 'Waleed Alqahtani'],
['Games Development', 'Mariam Alyami'],
['Scratch Programming', 'Waleed Alqahtani'],
['Problem Solving', 'Majedah Altamimi'],
['Programming Fundamentals', 'Majedah Altamimi'],
['Game Design', 'Reema Alsubai'],
['Mobile App Development', 'Reema Alsubai'],
['Animation Design', 'Salem Albuaij'],
['Electronics', 'Salem Albuaij']
];
// Add prerequisite courses
courses[0].push('Electronics');
courses[1].push('Programming Fundamentals');
courses[2].push('Website Design Fundamentals');
courses[4].push('Game Design');
courses[7].push('Problem Solving');
courses[8].push('Scratch Programming');
courses[9].push('Programming Fundamentals');
// Populate the filter options from the array
function populateFilterOptions() {
var tutors = [];
var prerequisites = [];
for (var i = 0; i < courses.length; i++) {
var tutor = courses[i][1];
var prerequisite = courses[i][2];
if (tutors.indexOf(tutor) === -1) {
tutors.push(tutor);
}
if (prerequisite && prerequisites.indexOf(prerequisite) === -1) {
prerequisites.push(prerequisite);
}
}
var tutorSelect = document.querySelector('optgroup[label="Tutors"]');
var prerequisiteSelect = document.querySelector('optgroup[label="Prerequisite"]');
var coursesSelect = document.querySelector('optgroup[label="Courses"]');
tutors.forEach(function (tutor) {
var option = document.createElement('option');
option.value = tutor;
option.textContent = tutor;
tutorSelect.appendChild(option);
});
prerequisites.forEach(function (prerequisite) {
var option = document.createElement('option');
option.value = prerequisite;
option.textContent = prerequisite;
prerequisiteSelect.appendChild(option);
});
var showAllOption = document.createElement('option');
showAllOption.value = 'all';
showAllOption.textContent = 'Show all courses';
coursesSelect.appendChild(showAllOption);
}
// Filter the courses based on the selected tutor, prerequisite, or show all option
function filterCourses() {
var tutorFilter = document.querySelector('#select optgroup[label="Tutors"] option:checked');
var prerequisiteFilter = document.querySelector('#select optgroup[label="Prerequisite"] option:checked');
var showAllFilter = document.querySelector('#select optgroup[label="Courses"] option:checked');
var tutor = tutorFilter ? tutorFilter.value : null;
var prerequisite = prerequisiteFilter ? prerequisiteFilter.value : null;
var showAll = showAllFilter && showAllFilter.value === 'all';
// Check if no filters are selected
if (!tutorFilter && !prerequisiteFilter && !showAllFilter) {
showAll = true; // Show all courses
}
var coursesContainer = document.getElementById('courses');
coursesContainer.innerHTML = '';
for (var i = 0; i < courses.length; i++) {
var course = courses[i][0];
var courseTutor = courses[i][1];
var coursePrerequisite = courses[i][2];
if ((tutor && courseTutor === tutor) || (prerequisite && coursePrerequisite === prerequisite) || showAll) {
var div = document.createElement('div');
div.className = 'responsive';
var gallery = document.createElement('div');
gallery.className = 'gallery';
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.name = "Child's course";
checkbox.value = course;
var img = document.createElement('img');
img.src = 'images/' + course.toLowerCase().replace(/ /g, '-') + '.jpg';
img.alt = 'course image';
var desc = document.createElement('div');
desc.className = 'desc';
desc.textContent = course;
gallery.appendChild(checkbox);
gallery.appendChild(img);
gallery.appendChild(desc);
div.appendChild(gallery);
coursesContainer.appendChild(div);
}
}
}
// Function to handle form submission
function handleSubmit(event) {
event.preventDefault(); // Prevent form submission
// Retrieve the selected child and course
var childSelect = document.getElementById('dropdown');
var courseSelect = document.getElementsByName("Child's course");
var selectedChild = childSelect.value;
// Check if at least one course is selected
var selectedCourses = [];
for (var i = 0; i < courseSelect.length; i++) {
if (courseSelect[i].checked) {
selectedCourses.push(courseSelect[i].value);
}
}
// Check if both a child and at least one course are selected
if (selectedChild === 'Select a child' && selectedCourses.length === 0) {
alert('Please select a child and at least one course.');
return; // Exit the function if the form is not valid
}
// Check if a child is selected
if (selectedChild === 'Select a child') {
alert('Please select a child.');
return; // Exit the function if the form is not valid
}
// Check if at least one course is selected
if (selectedCourses.length === 0) {
alert('Please select at least one course.');
return; // Exit the function if the form is not valid
}
// Remove previous information
var childInfoContainer = document.getElementById('childInfo');
childInfoContainer.innerHTML = '';
// Display the child's information
var childInfoHeading = document.createElement('h2');
childInfoHeading.textContent = 'Child Information';
childInfoContainer.appendChild(childInfoHeading);
var NameHeading = document.createElement('h3');
NameHeading.textContent = 'Child Name: ' ;
childInfoContainer.appendChild(NameHeading);
var childNameParagraph = document.createElement('p');
childNameParagraph.textContent = selectedChild;
childInfoContainer.appendChild(childNameParagraph);
if (selectedCourses.length > 0) {
var coursesHeading = document.createElement('h3');
coursesHeading.textContent = 'Selected Courses with Tutors';
childInfoContainer.appendChild(coursesHeading);
var coursesList = document.createElement('ul');
for (var j = 0; j < selectedCourses.length; j++) {
var courseItem = document.createElement('li');
var tutor = getCourseTutor(selectedCourses[j]);
courseItem.textContent = selectedCourses[j] + ' - Tutor: ' + tutor;
coursesList.appendChild(courseItem);
}
childInfoContainer.appendChild(coursesList);
}
// Clear the form
childSelect.selectedIndex = 0;
for (var k = 0; k < courseSelect.length; k++) {
courseSelect[k].checked = false;
}
// Re-select and display all courses
var select = document.getElementById('select');
select.selectedIndex = 0;
filterCourses();
}
// Function to get the tutor name for a course
function getCourseTutor(course) {
for (var i = 0; i < courses.length; i++) {
if (courses[i][0] === course) {
return courses[i][1];
}
}
return '';
}
// Add event listener to the submit button
var submitBtn = document.getElementById('submitBtn');
submitBtn.addEventListener('click', handleSubmit);
// Load the filter options and apply event listeners
window.addEventListener('DOMContentLoaded', function () {
populateFilterOptions();
var select = document.getElementById('select');
select.addEventListener('change', filterCourses);
filterCourses();
});