/* ===== Live Viewers & Delivery Widget - Multi-Lang ===== */ document.addEventListener("DOMContentLoaded", function () { const isEn = document.documentElement.lang === 'en' || document.documentElement.dir === 'ltr' || window.location.href.indexOf('/en') > -1; const minViewers = 27, maxViewers = 48; let viewers = Math.floor(Math.random() * (maxViewers - minViewers + 1)) + minViewers; if (document.getElementById("cy-delivery-live-widget")) return; const widget = document.createElement("div"); widget.innerHTML = `
${viewers} ${isEn ? 'are looking at this product right now' : 'يشاهدون هذا المنتج الآن'}
🚚 ${isEn ? 'Fast delivery for your order' : 'توصيل سريع لطلبك'}
${isEn ? 'Delivery Time' : 'مدة التوصيل'} ${isEn ? '1 to 4 Days' : 'من يوم إلى 4 أيام'}
${isEn ? 'Jeddah & Makkah' : 'جدة ومكة'} ${isEn ? 'Same Day' : 'بنفس اليوم'}
`; let targetElement = null; const allElements = document.querySelectorAll("span, small, p, div"); for (let el of allElements) { const text = (el.textContent || "").replace(/\s+/g, " ").trim(); if ( (text.includes("السعر شامل الضريبه") || text.includes("السعر شامل الضريبة") || text.includes("شامل الضريبة") || text.includes("شامل الضريبه")) && el.children.length === 0 ) { targetElement = el.tagName.toLowerCase() === "span" ? el.parentElement : el; break; } } if (!targetElement) { targetElement = document.querySelector(".product-price") || document.querySelector(".price-wrapper") || document.querySelector(".product-details__price") || document.querySelector(".s-product-price"); } if (targetElement && targetElement.parentNode) { targetElement.parentNode.insertBefore(widget, targetElement.nextSibling); setInterval(function () { viewers += Math.floor(Math.random() * 5) - 2; if (viewers < minViewers) viewers = minViewers + 1; if (viewers > maxViewers) viewers = maxViewers - 1; const viewerEl = document.getElementById("cy-view-count"); if (viewerEl) viewerEl.innerText = viewers; }, 6000); } }); /* ===== Salla Luxury Size Guide & Dynamic Panel - V8 (Optimized & Responsive) ===== */ (function () { "use strict"; if (window.__premiumSizeGuideLoaded__) return; window.__premiumSizeGuideLoaded__ = true; var isEn = document.documentElement.lang === 'en' || document.documentElement.dir === 'ltr' || window.location.href.indexOf('/en') > -1; var txt = { title: isEn ? "Size Guide" : "دليل المقاسات", unitIn: isEn ? "Inches" : "بالإنش", unitCm: isEn ? "Centimeters" : "بالسم", btnSmart: isEn ? "Use Smart Fitting Room" : "استخدمي غرفة القياس الذكية", sizeLabel: isEn ? "Size" : "المقاس", bust: isEn ? "Bust" : "الصدر", waist: isEn ? "Waist" : "الخصر", hip: isEn ? "Hip" : "الأرداف", note: isEn ? "* All numbers above are approximate and depend on fabric and design." : "* جميع الأرقام أعلاه تقريبية وتعتمد على نوع القماش والتصميم.", smartTitle: isEn ? "Smart Fitting Room" : "غرفة القياس الذكية", modIn: isEn ? "In" : "إنش", modCm: isEn ? "Cm" : "سم", lblBust: isEn ? "Bust Circumference" : "محيط الصدر", lblWaist: isEn ? "Waist Circumference" : "محيط الخصر", lblHip: isEn ? "Hip Circumference" : "محيط الأرداف", placeholder: isEn ? "Enter measurement..." : "أدخلي القياس هنا...", lblPref: isEn ? "How do you prefer the fit?" : "كيف تفضلين ارتداء القطعة؟", prefReg: isEn ? "Regular (True to size)" : "مضبوط (مطابق للجدول)", prefRel: isEn ? "Relaxed (Slightly loose)" : "مريح (واسع قليلاً)", prefSnug: isEn ? "Snug (Body-hugging)" : "ضيق (محدد للجسم)", btnCalc: isEn ? "Calculate Recommended Size" : "احسبي مقاسي الموصى به", btnSave: isEn ? "Save Measurements" : "حفظ قياساتي", btnLoad: isEn ? "Load Measurements" : "استرجاع القياس", resSmart: isEn ? "Smart Result:" : "النتيجة الذكية:", dpTitle1: isEn ? "Measurements for Size" : "قياسات قطعة", dpTitle2: isEn ? "" : "بالتفصيل", dpToCm: isEn ? "Switch to CM" : "تحويل إلى سم", dpToIn: isEn ? "Switch to Inches" : "تحويل للإنش", alertCalc: isEn ? "Please calculate your size first" : "الرجاء حساب المقاس أولاً", alertSave: isEn ? "Measurements saved successfully." : "تم حفظ القياسات بنجاح.", alertLoad: isEn ? "No saved measurements found." : "لا توجد قياسات محفوظة مسبقاً." }; var SIZES = [ { hip: 36, waist: 26, bust: 32, size: 'S', uk: '6' }, { hip: 38, waist: 28, bust: 34, size: 'M', uk: '8' }, { hip: 40, waist: 30, bust: 36, size: 'L', uk: '10' }, { hip: 42, waist: 32, bust: 38, size: 'XL', uk: '12' }, { hip: 44, waist: 34, bust: 40, size: 'XXL', uk: '14' }, { hip: 46, waist: 36, bust: 42, size: '3XL', uk: '16' }, { hip: 48, waist: 38, bust: 44, size: '4XL', uk: '18' }, { hip: 50, waist: 40, bust: 46, size: '5XL', uk: '20' }, { hip: 52, waist: 42, bust: 48, size: '6XL', uk: '22' }, { hip: 54, waist: 44, bust: 50, size: '7XL', uk: '24' } ]; if (!window.Salla || !Salla.onReady) return; Salla.onReady(function () { injectGlobalStyles(); setTimeout(function() { initDynamicPanel(); }, 500); if (!Salla.product || !Salla.product.event || !Salla.product.event.onSizeGuideFetched) return; Salla.product.event.onSizeGuideFetched(function (sizes, productId) { waitForSizeGuideContainer(initPremiumUI, { retries: 50, interval: 120 }, sizes, productId); }); }); function injectGlobalStyles() { if(document.getElementById('lux-global-styles')) return; var style = document.createElement('style'); style.id = 'lux-global-styles'; style.innerHTML = ` :root { --lux-dark: var(--color-primary, #111111); --lux-white: #ffffff; --lux-accent: var(--color-secondary, #888888); --lux-bg: #fdfdfd; --lux-border: #eaeaea; --lux-text-mut: #666666; } #lux-sizes { font-family: inherit; line-height: 1.6; color: var(--lux-dark); scroll-margin-top: 72px; direction: ${isEn ? 'ltr' : 'rtl'}; } #lux-sizes * { box-sizing: border-box; } #lux-sizes .lux-container { max-width: 1000px; margin: 0 auto; padding: 15px 0; } .lux-head-wrap { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 12px; margin-bottom: 20px; } .lux-title { font-size: 1.25rem; font-weight: 800; margin: 0; display: flex; align-items: center; gap: 8px; } .lux-title::before { content: ""; display: inline-block; width: 4px; height: 18px; background-color: var(--lux-dark); border-radius: 4px; } .lux-toggle-pill { display: inline-flex; background: #f4f4f4; border-radius: 50px; padding: 4px; border: 1px solid #ebebeb; } .lux-toggle-pill input { display: none; } .lux-toggle-pill label { padding: 6px 20px; font-size: 0.85rem; font-weight: 700; cursor: pointer; border-radius: 50px; color: var(--lux-text-mut); transition: all 0.25s ease; margin: 0; } .lux-toggle-pill input:checked + label { background: var(--lux-dark); color: var(--lux-white); box-shadow: 0 4px 8px rgba(0,0,0,0.15); } .lux-btn-smart { display: inline-flex; align-items: center; gap: 8px; width: 100%; justify-content: center; background: linear-gradient(135deg, var(--lux-dark), #2a2a2a); color: var(--lux-white); border: none; padding: 12px 20px; border-radius: 14px; margin-bottom: 24px; font-size: 0.95rem; font-weight: 700; cursor: pointer; transition: transform 0.2s, box-shadow 0.2s; box-shadow: 0 6px 15px rgba(0,0,0,0.08); } .lux-btn-smart:active { transform: scale(0.98); } .lux-btn-smart svg { width: 18px; height: 18px; fill: currentColor; } .lux-badge { background: #d4af37; color: #fff; font-size: 0.65rem; padding: 2px 8px; border-radius: 20px; letter-spacing: 0.5px; font-weight: 800;} .lux-table-wrapper { background: var(--lux-white); border: 1px solid var(--lux-border); border-radius: 16px; overflow-x: auto; box-shadow: 0 4px 20px rgba(0,0,0,0.02); -webkit-overflow-scrolling: touch;} #lux-sizes table { width: 100%; border-collapse: collapse; text-align: ${isEn ? 'left' : 'center'}; table-layout: auto; min-width: 400px; } #lux-sizes th { background: #fafafa; color: var(--lux-dark); padding: 14px 10px; font-weight: 800; font-size: 0.85rem; border-bottom: 2px solid var(--lux-border); text-align: center; white-space: nowrap; } #lux-sizes td { padding: 14px 10px; border-bottom: 1px solid var(--lux-border); font-size: 0.9rem; color: var(--lux-text-mut); font-weight: 600; text-align: center; } #lux-sizes tbody tr:nth-child(even) { background-color: #fdfdfd; } #lux-sizes tbody tr:hover { background-color: #f5f5f5; } #lux-sizes .lux-note { font-size: 0.75rem; color: var(--lux-text-mut); margin-top: 12px; text-align: center; font-weight: 500; } .lux-modal-overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.5); backdrop-filter: blur(4px); z-index: 99999; display: none; align-items: center; justify-content: center; opacity: 0; transition: opacity 0.3s ease; } .lux-modal-overlay.show { display: flex; opacity: 1; } .lux-modal-content { background: var(--lux-white); width: 100%; max-width: 450px; border-radius: 24px; padding: 25px; max-height: 90vh; overflow-y: auto; box-shadow: 0 20px 40px rgba(0,0,0,0.2); transform: translateY(20px); transition: transform 0.3s ease; direction: ${isEn ? 'ltr' : 'rtl'}; } .lux-modal-overlay.show .lux-modal-content { transform: translateY(0); } .lux-modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; } .lux-modal-title { font-size: 1.2rem; font-weight: 800; margin: 0; color: var(--lux-dark); } .lux-btn-close { background: #f4f4f4; border: none; width: 34px; height: 34px; border-radius: 50%; font-size: 1.2rem; display: flex; align-items: center; justify-content: center; cursor: pointer; color: var(--lux-dark); transition: 0.2s; } .lux-btn-close:hover { background: #e0e0e0; color: #000; transform: scale(1.05); } .lux-form-row { margin-bottom: 16px; text-align: ${isEn ? 'left' : 'right'}; } .lux-form-row label { display: block; font-size: 0.85rem; font-weight: 700; margin-bottom: 8px; color: var(--lux-dark); } .lux-input { width: 100%; padding: 12px 16px; border: 1px solid #ddd; border-radius: 12px; font-size: 0.95rem; outline: none; transition: all 0.3s; background: #fafafa; color: var(--lux-dark); font-family: inherit; font-weight: 600; } .lux-input:focus { border-color: var(--lux-dark); background: var(--lux-white); box-shadow: 0 0 0 3px rgba(0,0,0,0.05); } .lux-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 25px; } .lux-btn { padding: 14px 10px; border-radius: 12px; font-size: 0.85rem; font-weight: 700; cursor: pointer; transition: 0.2s; text-align: center; border: none; font-family: inherit; } .lux-btn-primary { background: var(--lux-dark); color: var(--lux-white); grid-column: span 2; box-shadow: 0 4px 12px rgba(0,0,0,0.1); } .lux-btn-primary:hover { opacity: 0.9; transform: translateY(-1px); } .lux-btn-secondary { background: #f8f8f8; color: var(--lux-dark); border: 1px solid #e0e0e0; } .lux-btn-secondary:hover { background: #eee; } .lux-result-box { margin-top: 25px; border: 2px dashed #e0e0e0; background: #fafafa; padding: 20px; border-radius: 16px; text-align: center; } .lux-rec-val { font-size: 2rem; font-weight: 900; color: var(--lux-dark); margin: 5px 0; } .lux-kpi-list { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; margin-top: 12px; } .lux-kpi-item { background: var(--lux-white); border: 1px solid #eee; padding: 6px 14px; border-radius: 20px; font-size: 0.8rem; font-weight: 700; color: var(--lux-text-mut); } /* Dynamic Panel Styles */ .lux-dp-wrapper { margin: 24px 0; background: var(--lux-white); border: 1px solid #f0f0f0; border-radius: 16px; padding: 20px; display: none; direction: ${isEn ? 'ltr' : 'rtl'}; font-family: inherit; box-shadow: 0 8px 24px rgba(0,0,0,0.04); position: relative; overflow: hidden; } .lux-dp-wrapper::before { content: ""; position: absolute; top: 0; ${isEn ? 'left: 0;' : 'right: 0;'} width: 5px; height: 100%; background: var(--lux-dark); border-radius: ${isEn ? '16px 0 0 16px' : '0 16px 16px 0'}; } .lux-dp-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; flex-wrap: wrap; gap: 15px; border-bottom: 1px solid #f4f4f4; padding-bottom: 15px; } .lux-dp-title { font-size: 1.1rem; font-weight: 800; color: var(--lux-dark); margin: 0; display: flex; align-items: center; gap: 6px; } .lux-dp-sz-hl { background: var(--lux-dark); color: #fff; padding: 4px 12px; border-radius: 8px; font-family: monospace; font-size: 1.1rem; font-weight: bold;} .lux-dp-tools { display: flex; align-items: center; } .lux-dp-unit-toggle { background: #f0f0f0; border: none; color: var(--lux-dark); padding: 8px 20px; border-radius: 10px; font-size: 0.85rem; font-weight: 700; cursor: pointer; transition: 0.2s; font-family: inherit; } .lux-dp-unit-toggle:hover { background: #e4e4e4; } .lux-dp-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; } .lux-dp-card { background: #fbfbfb; border: 1px solid #f0f0f0; padding: 16px 10px; text-align: center; border-radius: 14px; transition: transform 0.2s, background 0.2s; } .lux-dp-card:hover { transform: translateY(-2px); background: #f4f4f4; border-color: #e8e8e8; } .lux-dp-label { display: block; font-size: 0.75rem; font-weight: 700; color: var(--lux-text-mut); margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.5px;} .lux-dp-value { display: block; font-size: 1.35rem; font-weight: 900; color: var(--lux-dark); } @media (max-width: 768px) { .lux-dp-header { flex-direction: column; align-items: stretch; gap: 12px; } .lux-dp-tools { justify-content: stretch; } .lux-dp-unit-toggle { width: 100%; padding: 10px; } } @media (max-width: 480px) { .lux-head-wrap { flex-direction: column; align-items: stretch; } .lux-toggle-pill { justify-content: center; } .lux-modal-content { height: 100vh; max-height: 100vh; border-radius: 0; padding: 25px 20px; } .lux-dp-card { padding: 12px 6px; } .lux-dp-label { font-size: 0.7rem; margin-bottom: 6px; } .lux-dp-value { font-size: 1.15rem; } } `; document.head.appendChild(style); } function waitForSizeGuideContainer(cb, opts, sizes, productId) { var selectors = ['.s-product-size-guide-wrapper salla-tabs', '.s-product-size-guide-wrapper .s-product-size-guide', '.s-product-size-guide', '[data-size-guide-target]', '.product-size-guide, .size-guide']; var wrapperSel = '.s-product-size-guide-wrapper'; function find() { var wrapper = document.querySelector(wrapperSel); for (var i = 0; i < selectors.length; i++) { var el = wrapper ? wrapper.querySelector(selectors[i]) : document.querySelector(selectors[i]); if (el) return el; } return null; } var tries = 0; var maxRetries = (opts && opts.retries) || 40; var timer = setInterval(function () { var host = find(); if (host) { clearInterval(timer); cb(host, sizes, productId); } else if (++tries >= maxRetries) { clearInterval(timer); var wrapper = document.querySelector(wrapperSel) || document.body; var fallback = document.createElement('div'); fallback.className = 's-product-size-guide-fallback'; wrapper.appendChild(fallback); cb(fallback, sizes, productId); } }, (opts && opts.interval) || 100); } function initDynamicPanel() { if(document.getElementById('lux-dynamic-panel')) return; var panel = document.createElement('div'); panel.id = 'lux-dynamic-panel'; panel.className = 'lux-dp-wrapper'; panel.innerHTML = `

${txt.dpTitle1} ${txt.dpTitle2}

${txt.bust}
${txt.waist}
${txt.hip}
`; var optionsWrap = document.querySelector('salla-product-options') || document.querySelector('.s-product-options-wrapper'); if (optionsWrap) { optionsWrap.parentNode.insertBefore(panel, optionsWrap.nextSibling); } else { setTimeout(function() { optionsWrap = document.querySelector('salla-product-options') || document.querySelector('.s-product-options-wrapper'); if(optionsWrap) optionsWrap.parentNode.insertBefore(panel, optionsWrap.nextSibling); }, 1500); } var currentMatch = null; var showInCm = false; function renderPanel() { if(!currentMatch) return; panel.style.display = 'block'; document.getElementById('dp-sz').textContent = currentMatch.uk + ' / ' + currentMatch.size; var valFmt = function(v) { return showInCm ? Math.round(v * 2.54) + (isEn ? ' cm' : ' سم') : v + (isEn ? ' in' : ' إنش'); }; document.getElementById('dp-bust').textContent = valFmt(currentMatch.bust); document.getElementById('dp-waist').textContent = valFmt(currentMatch.waist); document.getElementById('dp-hip').textContent = valFmt(currentMatch.hip); document.getElementById('dp-toggle-unit').textContent = showInCm ? txt.dpToIn : txt.dpToCm; } document.body.addEventListener('click', function(e) { if(e.target && e.target.id === 'dp-toggle-unit') { e.preventDefault(); showInCm = !showInCm; renderPanel(); } }); /* معالجة قراءة المقاسات: يدعم: XL 12 XL-12 XL/12 12 XL 12-XL 7XL 24 7XL-24 */ function normalizeSizeText(text) { return String(text || '') .toUpperCase() .replace(/\u00A0/g, ' ') .replace(/[–—−]/g, '-') .replace(/\s*-\s*/g, '-') .replace(/\s*\/\s*/g, '/') .replace(/\s+/g, ' ') .trim(); } function compactSizeText(text) { return normalizeSizeText(text).replace(/[^A-Z0-9]/g, ''); } function getSelectedOptionText(target) { if (!target) return ''; var selectEl = null; if (target.tagName && target.tagName.toLowerCase() === 'select') { selectEl = target; } else if (target.closest) { selectEl = target.closest('select'); } if (selectEl && selectEl.options && selectEl.selectedIndex >= 0) { var selectedOption = selectEl.options[selectEl.selectedIndex]; return selectedOption ? (selectedOption.textContent || selectedOption.value || '') : ''; } if (target.tagName && target.tagName.toLowerCase() === 'input' && target.type === 'radio') { var lbl = document.querySelector('label[for="' + target.id + '"]'); if (lbl) return lbl.textContent || ''; return target.value || ''; } return target.textContent || target.value || ''; } function isPlaceholderOption(text) { var t = normalizeSizeText(text); return ( t === '' || t === 'اختر' || t === 'اختار' || t === 'اختيار' || t === 'SELECT' || t === 'CHOOSE' || t === 'PLEASE SELECT' ); } function checkSelectedOption(elementText) { if (!elementText) return; if (isPlaceholderOption(elementText)) return; var compactText = compactSizeText(elementText); var match = null; for (var i = 0; i < SIZES.length; i++) { var s = SIZES[i]; var sizeName = String(s.size || '').toUpperCase(); var ukNumber = String(s.uk || '').toUpperCase(); var pattern1 = compactSizeText(sizeName + ukNumber); // XL12 var pattern2 = compactSizeText(sizeName + '-' + ukNumber); // XL-12 var pattern3 = compactSizeText(sizeName + ' ' + ukNumber); // XL 12 var pattern4 = compactSizeText(sizeName + '/' + ukNumber); // XL/12 var pattern5 = compactSizeText(ukNumber + sizeName); // 12XL var pattern6 = compactSizeText(ukNumber + '-' + sizeName); // 12-XL var pattern7 = compactSizeText(ukNumber + ' ' + sizeName); // 12 XL var pattern8 = compactSizeText(ukNumber + '/' + sizeName); // 12/XL if ( compactText === pattern1 || compactText === pattern2 || compactText === pattern3 || compactText === pattern4 || compactText === pattern5 || compactText === pattern6 || compactText === pattern7 || compactText === pattern8 ) { match = s; break; } } if (match) { currentMatch = match; renderPanel(); } } function isInsideProductOptions(target) { if (!target || !target.closest) return false; return !!( target.closest('salla-product-options') || target.closest('.product-options') || target.closest('.s-product-options-wrapper') ); } document.addEventListener('change', function(e) { if (isInsideProductOptions(e.target)) { setTimeout(function() { var text = getSelectedOptionText(e.target); checkSelectedOption(text); }, 80); } }); document.addEventListener('click', function(e) { if (isInsideProductOptions(e.target)) { setTimeout(function() { var text = getSelectedOptionText(e.target); checkSelectedOption(text); }, 120); } }); setTimeout(function() { var selectEl = document.querySelector('salla-product-options select') || document.querySelector('.product-options select') || document.querySelector('.s-product-options-wrapper select'); if (selectEl && selectEl.selectedIndex >= 0) { checkSelectedOption(getSelectedOptionText(selectEl)); return; } var activeEl = document.querySelector('salla-product-options .s-option.active') || document.querySelector('salla-product-options input[type="radio"]:checked') || document.querySelector('.product-options .active') || document.querySelector('.s-product-options-wrapper input[type="radio"]:checked'); if(activeEl) { checkSelectedOption(getSelectedOptionText(activeEl)); } }, 1000); } function initPremiumUI(sizesHost) { sizesHost.style.display = 'block'; sizesHost.style.width = '100%'; sizesHost.innerHTML = `

${txt.title}

UK ${txt.sizeLabel} ${txt.bust} ${txt.waist} ${txt.hip}
${txt.note}

${txt.smartTitle}

${txt.resSmart}
${txt.bust}:
${txt.waist}:
${txt.hip}:
`; function toCm(v) { return Math.round(v * 2.54); } function fmt(v, u) { return u === 'cm' ? (toCm(v) + (isEn ? ' cm' : ' سم')) : (v + '"'); } function parseNum(x) { if (typeof x === 'string') x = x.replace(/[^\d.\-]/g, '').replace(/,/g, '.'); var n = parseFloat(x); return Number.isFinite(n) ? n : NaN; } var tbody = document.getElementById('luxTableBody'); var uIn = document.getElementById('u-in'), uCm = document.getElementById('u-cm'); var mIn = document.getElementById('m-in'), mCm = document.getElementById('m-cm'); function renderTable(unit) { var html = ""; for (var i = 0; i < SIZES.length; i++) { var r = SIZES[i]; html += '' + r.uk + '' + r.size + '' + '' + fmt(r.bust, unit) + '' + fmt(r.waist, unit) + '' + '' + fmt(r.hip, unit) + ''; } tbody.innerHTML = html; } function syncUnits(source) { var activeUnit = source === 'page' ? (uCm.checked ? 'cm' : 'in') : (mCm.checked ? 'cm' : 'in'); uCm.checked = activeUnit === 'cm'; uIn.checked = activeUnit === 'in'; mCm.checked = activeUnit === 'cm'; mIn.checked = activeUnit === 'in'; renderTable(activeUnit); } uIn.addEventListener('change', function() { syncUnits('page'); }); uCm.addEventListener('change', function() { syncUnits('page'); }); mIn.addEventListener('change', function() { syncUnits('modal'); }); mCm.addEventListener('change', function() { syncUnits('modal'); }); renderTable('in'); var modal = document.getElementById('luxFitModal'); document.getElementById('btnOpenModal').addEventListener('click', function() { modal.classList.add('show'); }); document.getElementById('btnCloseModal').addEventListener('click', function() { modal.classList.remove('show'); }); modal.addEventListener('click', function(e) { if(e.target === modal) modal.classList.remove('show'); }); var PADS = { regular: 0, relaxed: 1, snug: -0.5 }; function pickSize(userIn, pref) { var pad = Math.max(-1, Math.min(2, PADS[pref || 'regular'] || 0)); var need = { bust: userIn.bust + pad, waist: userIn.waist + pad, hip: userIn.hip + pad }; for (var i = 0; i < SIZES.length; i++) { if (SIZES[i].bust >= need.bust && SIZES[i].waist >= need.waist && SIZES[i].hip >= need.hip) return SIZES[i]; } return SIZES[SIZES.length - 1]; } var form = document.getElementById('luxFitForm'); form.addEventListener('submit', function (e) { e.preventDefault(); var unit = mCm.checked ? 'cm' : 'in'; var pref = document.getElementById('inpPref').value; var b = parseNum(document.getElementById('inpBust').value); var w = parseNum(document.getElementById('inpWaist').value); var h = parseNum(document.getElementById('inpHip').value); if (!(b>0) || !(w>0) || !(h>0)) return; var bIn = unit === 'cm' ? b / 2.54 : b; var wIn = unit === 'cm' ? w / 2.54 : w; var hIn = unit === 'cm' ? h / 2.54 : h; var rec = pickSize({bust: bIn, waist: wIn, hip: hIn}, pref); document.getElementById('resSize').textContent = 'UK ' + rec.uk + ' / ' + rec.size; var outFmt = function(v) { return unit === 'cm' ? Math.round(v) + (isEn ? ' cm' : ' سم') : v.toFixed(1) + '"'; }; document.getElementById('resBust').textContent = outFmt(b); document.getElementById('resWaist').textContent = outFmt(w); document.getElementById('resHip').textContent = outFmt(h); try { localStorage.setItem('luxLastSize', JSON.stringify({unit: unit, pref: pref, b: b, w: w, h: h})); } catch(err){} }); document.getElementById('btnSave').addEventListener('click', function() { if(document.getElementById('resSize').textContent === '—') return alert(txt.alertCalc); try { var data = localStorage.getItem('luxLastSize'); if(data) localStorage.setItem('luxSavedSize', data); alert(txt.alertSave); } catch(err){} }); document.getElementById('btnLoad').addEventListener('click', function() { try { var saved = JSON.parse(localStorage.getItem('luxSavedSize') || localStorage.getItem('luxLastSize')); if(!saved) return alert(txt.alertLoad); (saved.unit === 'cm' ? mCm : mIn).checked = true; document.getElementById('inpBust').value = saved.b; document.getElementById('inpWaist').value = saved.w; document.getElementById('inpHip').value = saved.h; document.getElementById('inpPref').value = saved.pref; syncUnits('modal'); } catch(err){} }); } })();