251 lines
7.9 KiB
JavaScript
251 lines
7.9 KiB
JavaScript
(function() {
|
|
'use strict';
|
|
|
|
// Language Toggle Functionality
|
|
var currentLang = localStorage.getItem('est-trade-lang') || 'he';
|
|
var langToggle = document.getElementById('langToggle');
|
|
var langToggleText = langToggle ? langToggle.querySelector('.lang-toggle-text') : null;
|
|
|
|
function setLanguage(lang) {
|
|
currentLang = lang;
|
|
localStorage.setItem('est-trade-lang', lang);
|
|
|
|
var htmlEl = document.documentElement;
|
|
|
|
if (lang === 'en') {
|
|
htmlEl.setAttribute('lang', 'en');
|
|
htmlEl.setAttribute('dir', 'ltr');
|
|
if (langToggleText) langToggleText.textContent = 'עב';
|
|
} else {
|
|
htmlEl.setAttribute('lang', 'he');
|
|
htmlEl.setAttribute('dir', 'rtl');
|
|
if (langToggleText) langToggleText.textContent = 'EN';
|
|
}
|
|
|
|
var translatableElements = document.querySelectorAll('[data-lang-he][data-lang-en]');
|
|
translatableElements.forEach(function(el) {
|
|
var heText = el.getAttribute('data-lang-he');
|
|
var enText = el.getAttribute('data-lang-en');
|
|
el.textContent = lang === 'en' ? enText : heText;
|
|
});
|
|
|
|
updatePageTitle(lang);
|
|
}
|
|
|
|
function updatePageTitle(lang) {
|
|
var titleEl = document.querySelector('title');
|
|
if (!titleEl) return;
|
|
|
|
var currentTitle = titleEl.textContent;
|
|
|
|
if (window.location.pathname.includes('contact')) {
|
|
titleEl.textContent = lang === 'en' ? 'Contact Us | Est Trade' : 'צור קשר | Est Trade';
|
|
} else {
|
|
titleEl.textContent = lang === 'en' ? 'Est Trade | Professional Import & Export' : 'Est Trade | יבוא ויצוא מקצועי לעסקים';
|
|
}
|
|
}
|
|
|
|
if (langToggle) {
|
|
langToggle.addEventListener('click', function() {
|
|
var newLang = currentLang === 'he' ? 'en' : 'he';
|
|
setLanguage(newLang);
|
|
});
|
|
}
|
|
|
|
// Initialize language on page load
|
|
if (currentLang) {
|
|
setLanguage(currentLang);
|
|
}
|
|
|
|
// Mobile Navigation Toggle
|
|
var navToggle = document.querySelector('.nav-toggle');
|
|
var navLinks = document.querySelector('.nav-links');
|
|
|
|
if (navToggle && navLinks) {
|
|
navToggle.addEventListener('click', function() {
|
|
var isExpanded = this.getAttribute('aria-expanded') === 'true';
|
|
this.setAttribute('aria-expanded', !isExpanded);
|
|
navLinks.classList.toggle('is-open');
|
|
});
|
|
|
|
// Close menu when clicking a link
|
|
navLinks.querySelectorAll('a').forEach(function(link) {
|
|
link.addEventListener('click', function() {
|
|
navToggle.setAttribute('aria-expanded', 'false');
|
|
navLinks.classList.remove('is-open');
|
|
});
|
|
});
|
|
|
|
// Close menu when clicking outside
|
|
document.addEventListener('click', function(e) {
|
|
if (!navToggle.contains(e.target) && !navLinks.contains(e.target)) {
|
|
navToggle.setAttribute('aria-expanded', 'false');
|
|
navLinks.classList.remove('is-open');
|
|
}
|
|
});
|
|
}
|
|
|
|
// Scroll Reveal Animation
|
|
var prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
|
|
if (!prefersReducedMotion) {
|
|
var revealElements = document.querySelectorAll(
|
|
'.about-content, .about-visual, .service-card, .advantage-item, ' +
|
|
'.testimonial-card, .partner-logo, .section-header, .hero-content, ' +
|
|
'.hero-stats, .info-card, .contact-form'
|
|
);
|
|
|
|
revealElements.forEach(function(el) {
|
|
el.classList.add('reveal');
|
|
});
|
|
|
|
var observerOptions = {
|
|
root: null,
|
|
rootMargin: '0px 0px -50px 0px',
|
|
threshold: 0.1
|
|
};
|
|
|
|
var revealObserver = new IntersectionObserver(function(entries) {
|
|
entries.forEach(function(entry) {
|
|
if (entry.isIntersecting) {
|
|
entry.target.classList.add('is-visible');
|
|
revealObserver.unobserve(entry.target);
|
|
}
|
|
});
|
|
}, observerOptions);
|
|
|
|
revealElements.forEach(function(el) {
|
|
revealObserver.observe(el);
|
|
});
|
|
}
|
|
|
|
// Contact Form Handling with Validation
|
|
var contactForm = document.getElementById('contactForm');
|
|
var formSuccess = document.getElementById('formSuccess');
|
|
|
|
if (contactForm && formSuccess) {
|
|
var validators = {
|
|
name: {
|
|
validate: function(value) { return value.trim().length >= 2; },
|
|
messageHe: 'נא להזין שם מלא',
|
|
messageEn: 'Please enter your full name'
|
|
},
|
|
email: {
|
|
validate: function(value) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value); },
|
|
messageHe: 'נא להזין כתובת אימייל תקינה',
|
|
messageEn: 'Please enter a valid email address'
|
|
},
|
|
phone: {
|
|
validate: function(value) { return /^[\d\-\+\s]{9,}$/.test(value); },
|
|
messageHe: 'נא להזין מספר טלפון תקין',
|
|
messageEn: 'Please enter a valid phone number'
|
|
},
|
|
message: {
|
|
validate: function(value) { return value.trim().length >= 10; },
|
|
messageHe: 'נא להזין הודעה (לפחות 10 תווים)',
|
|
messageEn: 'Please enter a message (at least 10 characters)'
|
|
}
|
|
};
|
|
|
|
function validateField(field) {
|
|
var validator = validators[field.name];
|
|
if (!validator) return true;
|
|
|
|
var errorEl = field.parentElement.querySelector('.form-error');
|
|
var isValid = validator.validate(field.value);
|
|
|
|
if (!isValid) {
|
|
field.classList.add('is-invalid');
|
|
if (errorEl) {
|
|
errorEl.textContent = currentLang === 'en' ? validator.messageEn : validator.messageHe;
|
|
}
|
|
} else {
|
|
field.classList.remove('is-invalid');
|
|
if (errorEl) errorEl.textContent = '';
|
|
}
|
|
|
|
return isValid;
|
|
}
|
|
|
|
// Real-time validation on blur
|
|
Object.keys(validators).forEach(function(fieldName) {
|
|
var field = contactForm.querySelector('[name="' + fieldName + '"]');
|
|
if (field) {
|
|
field.addEventListener('blur', function() {
|
|
validateField(this);
|
|
});
|
|
field.addEventListener('input', function() {
|
|
if (this.classList.contains('is-invalid')) {
|
|
validateField(this);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
contactForm.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
var isFormValid = true;
|
|
|
|
Object.keys(validators).forEach(function(fieldName) {
|
|
var field = contactForm.querySelector('[name="' + fieldName + '"]');
|
|
if (field && !validateField(field)) {
|
|
isFormValid = false;
|
|
}
|
|
});
|
|
|
|
if (!isFormValid) {
|
|
var firstInvalid = contactForm.querySelector('.is-invalid');
|
|
if (firstInvalid) firstInvalid.focus();
|
|
return;
|
|
}
|
|
|
|
var submitBtn = contactForm.querySelector('button[type="submit"]');
|
|
var originalText = submitBtn.textContent;
|
|
submitBtn.textContent = currentLang === 'en' ? 'Sending...' : 'שולח...';
|
|
submitBtn.disabled = true;
|
|
|
|
setTimeout(function() {
|
|
contactForm.hidden = true;
|
|
formSuccess.hidden = false;
|
|
}, 1000);
|
|
});
|
|
}
|
|
|
|
// Smooth scroll for anchor links with header offset
|
|
document.querySelectorAll('a[href^="#"]').forEach(function(anchor) {
|
|
anchor.addEventListener('click', function(e) {
|
|
var targetId = this.getAttribute('href');
|
|
if (targetId === '#') return;
|
|
|
|
var targetElement = document.querySelector(targetId);
|
|
if (targetElement) {
|
|
e.preventDefault();
|
|
var headerHeight = document.querySelector('.site-header').offsetHeight;
|
|
var targetPosition = targetElement.getBoundingClientRect().top + window.pageYOffset - headerHeight - 20;
|
|
|
|
window.scrollTo({
|
|
top: targetPosition,
|
|
behavior: prefersReducedMotion ? 'auto' : 'smooth'
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
// Header background enhancement on scroll
|
|
var header = document.querySelector('.site-header');
|
|
|
|
if (header) {
|
|
window.addEventListener('scroll', function() {
|
|
var currentScroll = window.pageYOffset;
|
|
|
|
if (currentScroll > 100) {
|
|
header.style.background = 'rgba(10, 22, 40, 0.98)';
|
|
} else {
|
|
header.style.background = 'rgba(10, 22, 40, 0.95)';
|
|
}
|
|
}, { passive: true });
|
|
}
|
|
|
|
})();
|