// Global flag to detect form submission
window.formSubmitted = false;
// Opens the checkout popup using product details from the clicked card
function openPopup(button) {
var productCard = button.closest('.product-card');
var productName = productCard.getAttribute("data-name");
var productPrice = productCard.getAttribute("data-price");
var productImage = productCard.getAttribute("data-image");
// Get selected size and color from dropdowns (if present)
var selectedSize = productCard.querySelector(".size-select") ? productCard.querySelector(".size-select").value : "";
var selectedColor = productCard.querySelector(".color-select") ? productCard.querySelector(".color-select").value : "";
document.getElementById("checkout-product-name").innerText = productName;
document.getElementById("checkout-product-price").innerText = productPrice +
(selectedSize ? " | Size: " + selectedSize : "") +
(selectedColor ? " | Color: " + selectedColor : "");
document.getElementById("checkout-product-image").src = productImage;
// Save product info in a hidden field for form submission
var productInfo = "Product: " + productName + " | Price: " + productPrice;
if (selectedSize) productInfo += " | Size: " + selectedSize;
if (selectedColor) productInfo += " | Color: " + selectedColor;
document.getElementById("product-info-field").value = productInfo;
document.getElementById("popup").style.display = "block";
document.getElementById("overlay").style.display = "block";
}
// Closes all popups and the overlay
function closeAllPopups() {
document.getElementById("popup").style.display = "none";
document.getElementById("form-popup").style.display = "none";
document.getElementById("success-popup").style.display = "none";
document.getElementById("overlay").style.display = "none";
}
// Opens the form popup and injects payment-specific fields if needed
function openForm(method) {
document.getElementById("payment-method").value = method;
if (method === 'Easypaisa') {
document.getElementById("payment-fields").innerHTML = `
`;
} else {
document.getElementById("payment-fields").innerHTML = `
Cash on Delivery Selected
`;
}
document.getElementById("form-popup").style.display = "block";
document.getElementById("popup").style.display = "none";
}
// Closes the form popup
function closeForm() {
document.getElementById("form-popup").style.display = "none";
document.getElementById("overlay").style.display = "none";
}
// Handle form submission; once submitted, the hidden iframe load event will show the success popup
function handleFormSubmit() {
window.formSubmitted = true;
closeForm();
return true;
}
// When the hidden iframe loads after form submission, show the success popup
document.getElementById("hidden_iframe").onload = function() {
if (window.formSubmitted) {
window.formSubmitted = false;
document.getElementById("success-popup").style.display = "block";
document.getElementById("overlay").style.display = "block";
}
};