forked from cryptopaths/cryptopaths.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
186 lines (160 loc) · 7.21 KB
/
script.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
document.addEventListener('DOMContentLoaded', function () {
const downArrow = document.getElementById('down-arrow');
const studentButton = document.getElementById('scroll-to-student');
const aboutUsContainer = document.querySelector('.about-us');
const studentSection = document.querySelector('.students');
const contact_button = document.getElementById('scroll-to-contact-us');
const contactHeader = document.querySelector('.name-contact');
// Scroll to the next section when the down arrow is clicked
downArrow.addEventListener('click', function () {
aboutUsContainer.scrollIntoView({ behavior: 'smooth' });
});
// Scroll to the student section when the student button is clicked
studentButton.addEventListener('click', function () {
studentSection.scrollIntoView({ behavior: 'smooth' });
});
contact_button.addEventListener('click', function() {
contactHeader.scrollIntoView({ behavior: 'smooth' });
});
// Form submission logic
const form = document.getElementById('myform');
const submitButton = document.getElementById('submit_button');
const error_message = document.getElementById('error_message');
submitButton.addEventListener('click', function (e) {
e.preventDefault();
console.log("Form submitted Successfully !");
validate(); // Call the validate function before sending the email
});
function validate() {
let form_name = document.getElementById("form_name").value.trim();
let subject = document.getElementById("subject").value.trim();
let phone = document.getElementById("phone").value.trim();
let email = document.getElementById("email").value.trim();
let message = document.getElementById("message").value.trim();
error_message.style.padding = "10px";
let errors = [];
if (form_name.length < 3) {
errors.push("Please enter a Valid Name");
}
if (subject.length < 5) {
errors.push("Please enter a correct Subject");
}
if (isNaN(phone) || phone.length <= 6) {
errors.push("Please enter a valid Phone Number");
}
if (!/^\S+@\S+\.\S+$/.test(email)) {
errors.push("Please enter a valid Email");
}
if (message.length <= 20) {
errors.push("Please enter a message of more than 20 characters");
}
if (errors.length > 0) {
// Show the error message and set its content
error_message.style.display = "block";
error_message.innerHTML = errors.join("<br>");
return false;
} else {
// Hide the error message if there are no errors
error_message.style.display = "none";
// Disable the submit button to prevent multiple submissions
submitButton.disabled = true;
// Send email only if validation passes
sendEmail();
return true;
}
}
function sendEmail() {
const result = document.getElementById('result');
const formData = new FormData(form);
const object = Object.fromEntries(formData);
const json = JSON.stringify(object);
result.innerHTML = "Please wait ..."
fetch('https://api.web3forms.com/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: json
})
.then(async (response) => {
let jsonResponse = await response.json();
if (response.status == 200) {
result.innerHTML = jsonResponse.message;
window.alert(jsonResponse.message);
} else {
console.log(response);
result.innerHTML = jsonResponse.message;
window.alert(jsonResponse.message);
}
})
.catch(error => {
console.log(error);
result.innerHTML = "Something went wrong!";
window.alert("Something went wrong !");
})
.then(function () {
form.reset();
setTimeout(() => {
result.style.display = "none";
submitButton.disabled = false; // Re-enable submit button after a timeout
}, 3000);
});
}
var buttons = document.querySelectorAll('.email-button');
// buttons.forEach(function(button) {
// var email = button.getAttribute('data-email');
// button.addEventListener('click', function () {
// var confirmed = confirm("Would you like to send mail to " + email + "?");
// if (confirmed) {
// window.location.href = "mailto:" + email;
// } else {
// var copyButton = document.createElement('button');
// copyButton.textContent = "Copy Email";
// copyButton.addEventListener('click', function () {
// navigator.clipboard.writeText(email).then(function () {
// alert("Email copied to clipboard: " + email);
// }, function (err) {
// console.error('Could not copy text: ', err);
// });
// });
// var cancelButton = document.createElement('button');
// cancelButton.textContent = "Cancel";
// cancelButton.addEventListener('click', function () {
// alert("Email copy cancelled.");
// });
// var messageDiv = document.createElement('div');
// messageDiv.textContent = "Email: " + email;
// messageDiv.appendChild(copyButton);
// messageDiv.appendChild(cancelButton);
// alert("Would you like to copy the email instead?");
// document.body.appendChild(messageDiv);
// }
// });
// });
buttons.forEach(function(button) {
button.addEventListener('click', function () {
var email = this.getAttribute('data-email'); // Assuming the email is stored in a data attribute
var confirmed = confirm("Would you like to send mail to " + email + "?");
if (confirmed) {
window.location.href = "mailto:" + email;
} else {
var copyConfirmed = confirm("Would you like to copy the email instead?");
navigator.clipboard.writeText(email).then(function () {
alert("Email copied to clipboard: " + email);
});
}
});
});
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/service-worker.js')
.then(registration => {
console.log('Service Worker registered with scope:', registration.scope);
})
.catch(error => {
console.error('Service Worker registration failed:', error);
});
});
}
});