| Current Path : /home/users/unlimited/www/nigeria.codeskitter.site/assets/js/ |
| Current File : /home/users/unlimited/www/nigeria.codeskitter.site/assets/js/main.js |
// Main JavaScript for gexneural academy Website
document.addEventListener('DOMContentLoaded', function() {
// Add to cart functionality
const addToCartButtons = document.querySelectorAll('.add-to-cart');
addToCartButtons.forEach(button => {
button.addEventListener('click', function() {
const courseId = this.getAttribute('data-course-id');
addToCart(courseId);
});
});
// Testimonial rating stars
const ratingStars = document.querySelectorAll('.rating-stars input');
ratingStars.forEach(star => {
star.addEventListener('change', function() {
const rating = this.value;
updateRatingDisplay(rating);
});
});
});
function addToCart(courseId) {
fetch('add_to_cart.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `course_id=${courseId}&action=add`
})
.then(response => response.json())
.then(data => {
if(data.success) {
showAlert('Course added to cart successfully!', 'success');
updateCartCount();
} else {
showAlert(data.message || 'Error adding course to cart', 'error');
}
})
.catch(error => {
console.error('Error:', error);
showAlert('An error occurred. Please try again.', 'error');
});
}
function updateCartCount() {
// This would typically make an API call to get the current cart count
// For now, we'll just increment a counter stored in localStorage
let cartCount = parseInt(localStorage.getItem('cartCount') || '0');
cartCount++;
localStorage.setItem('cartCount', cartCount);
const cartBadge = document.querySelector('.cart-count');
if(cartBadge) {
cartBadge.textContent = cartCount;
cartBadge.style.display = 'inline-block';
}
}
function showAlert(message, type) {
const alertDiv = document.createElement('div');
alertDiv.className = `alert alert-${type} alert-dismissible fade show`;
alertDiv.innerHTML = `
${message}
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
`;
// Add to the top of the page
document.body.insertBefore(alertDiv, document.body.firstChild);
// Auto remove after 5 seconds
setTimeout(() => {
if(alertDiv.parentNode) {
alertDiv.parentNode.removeChild(alertDiv);
}
}, 5000);
}
function updateRatingDisplay(rating) {
const stars = document.querySelectorAll('.rating-stars .form-check-label');
stars.forEach((star, index) => {
if(index < rating) {
star.classList.add('text-warning');
} else {
star.classList.remove('text-warning');
}
});
}
// Search functionality
function searchCourses() {
const searchTerm = document.getElementById('searchInput').value.toLowerCase();
const courseCards = document.querySelectorAll('.course-card');
courseCards.forEach(card => {
const title = card.querySelector('.card-title').textContent.toLowerCase();
const description = card.querySelector('.card-text').textContent.toLowerCase();
if(title.includes(searchTerm) || description.includes(searchTerm)) {
card.parentElement.style.display = 'block';
} else {
card.parentElement.style.display = 'none';
}
});
}
// Filter courses by category
function filterCourses(category) {
const courseCards = document.querySelectorAll('.course-card');
courseCards.forEach(card => {
const courseCategory = card.getAttribute('data-category');
if(category === 'all' || courseCategory === category) {
card.parentElement.style.display = 'block';
} else {
card.parentElement.style.display = 'none';
}
});
}