// Enhanced form protection for ClientSurgeAI contact form
let formStartTime = null;
let submitButton = null;
let formElement = null;
function init() {
// Record when the form loads
formStartTime = Date.now();
formElement = document.querySelector('[data-landingsite-contact-form]');
submitButton = formElement?.querySelector('button[type="submit"]');
if (formElement) {
// Set the hidden start time field
const startTimeField = document.getElementById('form_start_time');
if (startTimeField) {
startTimeField.value = formStartTime.toString();
}
// Add form submit protection
formElement.addEventListener('submit', handleFormSubmit);
// Add real-time validation
addInputValidation();
}
}
function handleFormSubmit(event) {
const currentTime = Date.now();
const timeSpent = (currentTime - formStartTime) / 1000; // seconds
// Prevent submission if completed too quickly (likely a bot)
if (timeSpent < 10) {
event.preventDefault();
showError('Please take a moment to review your information before submitting.');
return false;
}
// Check honeypot field
const honeypot = formElement.querySelector('input[name="website"]');
if (honeypot && honeypot.value) {
event.preventDefault();
return false; // Silent fail for bots
}
// Validate required fields have meaningful content
if (!validateFormContent()) {
event.preventDefault();
return false;
}
// Disable submit button to prevent double submission
if (submitButton) {
submitButton.disabled = true;
submitButton.innerHTML = 'Submitting...';
}
return true;
}
function validateFormContent() {
const firstName = document.getElementById('firstName').value.trim();
const lastName = document.getElementById('lastName').value.trim();
const email = document.getElementById('email').value.trim();
const company = document.getElementById('company').value.trim();
const message = document.getElementById('message').value.trim();
// Check for obvious fake content
const suspiciousPatterns = [
/test/i,
/fake/i,
/spam/i,
/^[a-z]+$/, // all lowercase single word
/^[A-Z]+$/, // all uppercase single word
/^\d+$/, // only numbers
];
const fieldsToCheck = [firstName, lastName, company];
for (const field of fieldsToCheck) {
for (const pattern of suspiciousPatterns) {
if (pattern.test(field)) {
showError('Please provide legitimate business information.');
return false;
}
}
}
// Validate email domain
if (!isValidBusinessEmail(email)) {
showError('Please use a business email address.');
return false;
}
// Check message quality
if (message.length < 20 || /^[a-zA-Z\s]+$/.test(message) && message.split(' ').length < 5) {
showError('Please provide more details about your business needs.');
return false;
}
return true;
}
function isValidBusinessEmail(email) {
// Block common temporary/disposable email domains
const blockedDomains = [
'gmail.com', // Consider removing if you want to allow Gmail
'10minutemail.com',
'tempmail.org',
'guerrillamail.com',
'mailinator.com',
'temp-mail.org'
];
const domain = email.split('@')[1]?.toLowerCase();
// For now, let's allow Gmail but flag other suspicious domains
const suspiciousDomains = [
'10minutemail.com',
'tempmail.org',
'guerrillamail.com',
'mailinator.com',
'temp-mail.org',
'throwaway.email'
];
return !suspiciousDomains.includes(domain);
}
function addInputValidation() {
// Real-time validation for name fields
const nameFields = ['firstName', 'lastName'];
nameFields.forEach(fieldId => {
const field = document.getElementById(fieldId);
if (field) {
field.addEventListener('input', function() {
// Remove numbers and special characters
this.value = this.value.replace(/[^A-Za-z\s]/g, '');
// Limit consecutive spaces
this.value = this.value.replace(/\s+/g, ' ');
});
}
});
// Email validation
const emailField = document.getElementById('email');
if (emailField) {
emailField.addEventListener('blur', function() {
if (this.value && !isValidBusinessEmail(this.value)) {
this.setCustomValidity('Please use a business email address');
} else {
this.setCustomValidity('');
}
});
}
// Message field validation
const messageField = document.getElementById('message');
if (messageField) {
messageField.addEventListener('input', function() {
const remaining = 1000 - this.value.length;
const label = document.querySelector('label[for="message"]');
if (label && remaining < 100) {
label.textContent = `Tell us about your business and goals * (${remaining} characters remaining)`;
}
});
}
}
function showError(message) {
// Remove existing error message
const existingError = document.getElementById('form-error-message');
if (existingError) {
existingError.remove();
}
// Create error message
const errorDiv = document.createElement('div');
errorDiv.id = 'form-error-message';
errorDiv.className = 'bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg mb-4';
errorDiv.innerHTML = `${message}`;
// Insert before form
if (formElement) {
formElement.parentNode.insertBefore(errorDiv, formElement);
// Scroll to error
errorDiv.scrollIntoView({ behavior: 'smooth', block: 'center' });
// Remove error after 5 seconds
setTimeout(() => {
if (errorDiv.parentNode) {
errorDiv.remove();
}
}, 5000);
}
}
function teardown() {
if (formElement) {
formElement.removeEventListener('submit', handleFormSubmit);
}
}
// Export the required functions
export { init, teardown };