-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscripts.js
194 lines (162 loc) · 7.03 KB
/
scripts.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
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
// Function to show selected tab content
function showContent(tabName) {
const contents = document.querySelectorAll('.tab-content');
contents.forEach(content => {
content.style.display = content.id === tabName ? 'block' : 'none';
});
}
//Data Management
////------------------------------------------------------------------------------------------------------------
// Validate limits to ensure non-negative values and save to local storage
function validateLimits() {
const dataLimit = document.getElementById("dataLimit").value;
const callLimit = document.getElementById("callLimit").value;
if (dataLimit < 0 || callLimit < 0) {
alert("Monthly limits for data and calls cannot be negative. Please enter valid numbers.");
return false;
}
// Save to local storage
localStorage.setItem("monthlyDataLimit", dataLimit);
localStorage.setItem("monthlyCallLimit", callLimit);
// Save notification preferences
saveNotificationPreferences();
alert("Settings saved!");
return true;
}
// Save notification preferences to localStorage
function saveNotificationPreferences() {
const notifications = ["notify5", "notify10", "notify20", "notify50"];
notifications.forEach(id => {
const checkbox = document.getElementById(id);
localStorage.setItem(id, checkbox.checked);
});
}
// Load saved limits and notification preferences on page load
window.onload = function() {
// Load data and call limits
const savedDataLimit = localStorage.getItem("monthlyDataLimit");
const savedCallLimit = localStorage.getItem("monthlyCallLimit");
if (savedDataLimit !== null) {
document.getElementById("dataLimit").value = savedDataLimit;
}
if (savedCallLimit !== null) {
document.getElementById("callLimit").value = savedCallLimit;
}
// Load notification preferences
loadNotificationPreferences();
};
// Load notification preferences from localStorage
function loadNotificationPreferences() {
const notifications = ["notify5", "notify10", "notify20", "notify50"];
notifications.forEach(id => {
const isChecked = localStorage.getItem(id) === "true";
document.getElementById(id).checked = isChecked;
});
}
//Register and Login Management
//------------------------------------------------------------------------------------------------------------
// Function to register a user
function registerUser() {
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
const companyName = document.getElementById("companyname").value;
// Save email and password and companyname to localStorage (for demo purposes; in real apps, use a secure backend)
if (email && password && companyName) {
localStorage.setItem("userEmail", email); //Storing email
localStorage.setItem("userCompany", companyName); //Storing company's name
sendConfirmationEmail(email); // Send confirmation email
alert("Registration successful! Check your email for confirmation.");
window.location.href = "login.html"; // Redirect to login page
return true;
} else {
alert("Please enter a valid email, company name and password.");
return false;
}
}
// Function to send a confirmation email
function sendConfirmationEmail(email) {
const templateParams = {
to_email: email,
subject: "Registration Confirmation",
message: "Thank you for registering with our Telecom App!"
};
emailjs.send("YOUR_SERVICE_ID", "YOUR_TEMPLATE_ID", templateParams)
.then((response) => {
console.log('Email sent successfully!', response.status, response.text);
}, (err) => {
console.error('Failed to send email. Error:', err);
});
}
// Function to log telecom usage and send notification if below threshold
function logTelecomRemain() {
const dataRemain = parseInt(document.getElementById("dataRemain").value, 10); // Get data usage input
const email = localStorage.getItem("userEmail"); // Retrieve stored email
// Check if the data usage is below the threshold
if (dataRemain <= 100 && email) {
sendUsageNotificationEmail(email, Remain); // Send notification email
alert("You are low in data and notification email sent!");
} else if (dataRemain > 100 ){
alert("Usage logged successfully. You have sufficient amount of data.");
}
}
// Function to send a usage notification email
function sendUsageNotificationEmail(email, dataRemain) {
const templateParams = {
to_email: email,
subject: "Data Usage Notification",
message: `Your current data usage is ${dataRemain}. Please be mindful that it's below the threshold of 100.`
};
emailjs.send("YOUR_SERVICE_ID", "YOUR_TEMPLATE_ID", templateParams)
.then((response) => {
console.log('Usage notification email sent successfully!', response.status, response.text);
}, (err) => {
console.error('Failed to send usage notification email. Error:', err);
});
}
// Function to log in a user
function loginUser() {
const enteredEmail = document.getElementById("loginEmail").value;
const enteredPassword = document.getElementById("loginPassword").value;
// Retrieve stored email and password
const storedEmail = localStorage.getItem("userEmail");
const storedPassword = localStorage.getItem("userPassword");
// Check if entered credentials match stored credentials
if (enteredEmail === storedEmail && enteredPassword === storedPassword) {
alert("Login successful!");
window.location.href = "index.html"; // Redirect to homepage
return true;
} else {
alert("Incorrect email or password. Please try again.");
return false;
}
}
// Function to check login status on the homepage
function checkLoginStatus() {
const userEmail = localStorage.getItem("userEmail");
if (userEmail) {
document.getElementById("welcome-section").innerHTML = `<h2>Welcome back, ${userEmail}!</h2>`;
} else {
document.getElementById("welcome-section").innerHTML = "<h2>Welcome to Telecom App! Please log in to access your account.</h2>";
}
}
// Function to log out
function logoutUser() {
localStorage.removeItem("userEmail"); // Remove email from local storage
alert("You have been logged out.");
window.location.reload(); // Refresh the page to update UI
}
/*
// Function to suggest closest limit based on user input
function suggestLimit(inputElement, limitArray, suggestionElement) {
const inputValue = parseInt(inputElement.value, 10);
suggestionElement.textContent = ''; // Clear previous suggestion
if (!isNaN(inputValue) && inputValue >= 0) {
// Find closest limit
const closestLimit = limitArray.reduce((prev, curr) => {
return (Math.abs(curr - inputValue) < Math.abs(prev - inputValue) ? curr : prev);
});
// Display suggestion
suggestionElement.textContent = `Did you mean: ${closestLimit}?`;
}
}
*/