]*>[\s\S]*?<\/style>/gi, ''); /* If description is plain text (no HTML tags), wrap in paragraph */ if(descContent && !/<[a-z]/i.test(descContent)){ descContent = '

'+descContent.replace(/\n\n/g,'

').replace(/\n/g,'
')+'

'; } if(!descContent.trim()) descContent = '

No description available.

'; tabsSection.innerHTML='' +'
' +'' +'' +'
' +'
' +'
' +'
' +'
' +'
' +(specRowsHtml||'

No details available. Add specs to Short Description in WooCommerce as Key: Value format.

') +'
' +'
'; /* Set description HTML safely via innerHTML */ document.querySelector('.spd-tab-desc-inner').innerHTML = descContent; /* Description tab prose styling */ var style=document.createElement('style'); style.textContent= '.spd-tab-desc-inner p{font-size:14.5px;color:#6b7a8d;line-height:1.9;margin-bottom:14px;}' +'.spd-tab-desc-inner h1,.spd-tab-desc-inner h2,.spd-tab-desc-inner h3{font-weight:800;color:var(--dark);margin:18px 0 8px;}' +'.spd-tab-desc-inner h2{font-size:18px;}.spd-tab-desc-inner h3{font-size:16px;}' +'.spd-tab-desc-inner ul,.spd-tab-desc-inner ol{margin:10px 0 14px 20px;}' +'.spd-tab-desc-inner li{font-size:14px;color:#6b7a8d;line-height:1.7;margin-bottom:5px;}' +'.spd-tab-desc-inner strong,.spd-tab-desc-inner b{color:var(--dark);font-weight:700;}' +'.spd-tab-desc-inner a{color:var(--green);}' +'.spd-tab-desc-inner table{width:100%;border-collapse:collapse;margin-bottom:14px;}' +'.spd-tab-desc-inner td,.spd-tab-desc-inner th{padding:8px 12px;border:1px solid #edf0f3;font-size:13.5px;color:#6b7a8d;}' +'.spd-tab-desc-inner th{background:#f7f9fb;font-weight:700;color:var(--dark);}'; document.getElementById('spdTabDesc').appendChild(style); tabsSection.querySelectorAll('.spd-tab-btn').forEach(function(btn){ btn.addEventListener('click',function(){ tabsSection.querySelectorAll('.spd-tab-btn').forEach(function(b){b.classList.remove('active');}); tabsSection.querySelectorAll('.spd-tab-panel').forEach(function(pn){pn.classList.remove('active');}); this.classList.add('active'); var tabId='spdTab'+this.dataset.tab.charAt(0).toUpperCase()+this.dataset.tab.slice(1); document.getElementById(tabId).classList.add('active'); }); }); /* Right details */ var reg=parseFloat(p.regular_price||p.price||0); var sale=p.sale_price?parseFloat(p.sale_price):null; var msrp=parseFloat(p.regular_price||0); var savings=sale&&msrp?msrp-sale:0; var html='
' +''+catName+'' +(brandName?''+brandName+'':'') +'
' +'

'+name+'

' +buildStars(p.average_rating,p.rating_count) /* Price block */ +'
' +'
Cash Price
' +'
'+buildPrice(p)+'
' +(msrp?'
MSRP: $'+msrp.toLocaleString()+'
'+(savings>0?'
Savings: $'+savings.toLocaleString()+'
':'')+'
':'') +'
' +(p.on_sale?'Sale':'') +(p.featured?'Featured':'') +'
' +'
' /* Stock info */ +'
' /* Action buttons */ +'
' +'' +'' +'
' /* Finance calculator */ +'
' +'
Finance Calculator
' +'
' +'
' +'
' +'
Est. Monthly Payment
' +'

* Estimate only. Final finance terms subject to credit approval. Ask us about flexible payment plans.

' +'
' +'
' +'
5-Year Warranty
' +'
Nationwide Delivery
' +'
Safety Certified
' +'
'; document.getElementById('spdSkel').style.display='none'; var ct=document.getElementById('spdContent'); ct.innerHTML=html; ct.style.display='block'; /* Stock info rows from short description key:value pairs */ var infoRows=document.getElementById('spdInfoRows'); if(shortDesc){ var raw=shortDesc.replace(/<[^>]+>/g,'\n').replace(/ /g,' '); var lines=raw.split('\n').map(function(l){return l.trim();}).filter(function(l){return l.length>2;}); var infoHtml=''; lines.slice(0,6).forEach(function(line){ var m=line.match(/^([^:]+):\s*(.+)$/); if(m) infoHtml+='
'+m[1]+':'+m[2]+'
'; }); infoRows.innerHTML=infoHtml; } /* Finance calculator logic */ function calcPayment(){ var totalPrice=parseFloat(p.sale_price||p.regular_price||p.price||0); var down=parseFloat(document.getElementById('spdCalcDown').value)||0; var loan=totalPrice-down; var apr=parseFloat(document.getElementById('spdCalcCredit').value)/100; var months=parseInt(document.getElementById('spdCalcTerm').value); var rate=apr/12; var monthly=rate>0?loan*(rate*Math.pow(1+rate,months))/(Math.pow(1+rate,months)-1):loan/months; document.getElementById('spdCalcResult').textContent=loan>0?'$'+Math.round(monthly).toLocaleString()+'/mo':'—'; } calcPayment(); ['spdCalcDown','spdCalcTerm','spdCalcCredit'].forEach(function(id){ document.getElementById(id).addEventListener('input',calcPayment); document.getElementById(id).addEventListener('change',calcPayment); }); /* Populate enquiry section */ var enqImg=document.getElementById('spdEnqImg'); if(imgs.length){ enqImg.src=imgs[0].src; enqImg.alt=name; } else { document.querySelector('.spd-enq-sum-img').style.background='#f0f3f6'; } document.getElementById('spdEnqName').textContent=name; document.getElementById('spdEnqPrice').innerHTML=buildPrice(p)+'asking price'; /* Finance */ if(price>0) calcFinance(price); /* Specs in summary */ var specHtml=buildSpecs(shortDesc,5).replace(/spd-spec-row/g,'spd-enq-sum-spec').replace(/spd-spec-k/g,'spd-enq-sum-spec-k').replace(/spd-spec-v/g,'spd-enq-sum-spec-v').replace(/spd-specs/g,'spd-enq-sum-specs'); document.getElementById('spdEnqSpecs').innerHTML=specHtml; /* Show enquiry section */ document.getElementById('spdEnquirySection').style.display='block'; /* Auto-fill CF7 / WPForms hidden fields with product info */ setTimeout(function(){ var fields={ 'trailer' : name, 'trailer-name': name, 'product' : name, 'price' : currentProduct.price, 'trailer-price': currentProduct.price, 'interest' : selectedInterest, 'enquiry-type': selectedInterest, 'subject' : 'Enquiry: '+name+' ('+currentProduct.price+')' }; Object.keys(fields).forEach(function(key){ var el=document.querySelector('input[name="'+key+'"],textarea[name="'+key+'"]'); if(el) el.value=fields[key]; }); }, 800); /* slight delay to let CF7 render */ /* Scroll to enquiry on button click */ ['spdBuyBtn','spdEnqBtn'].forEach(function(id){ document.getElementById(id).addEventListener('click',function(){ document.getElementById('spdEnquirySection').scrollIntoView({behavior:'smooth',block:'start'}); }); }); } /* Interest tabs — update a hidden CF7 field if it exists */ document.querySelectorAll('.spd-itab').forEach(function(tab){ tab.addEventListener('click',function(){ document.querySelectorAll('.spd-itab').forEach(function(t){t.classList.remove('active');}); this.classList.add('active'); selectedInterest=this.dataset.interest; /* Try to fill CF7 hidden/text field named "interest" or "enquiry-type" */ var f=document.querySelector('input[name="interest"],input[name="enquiry-type"],input[name="enquiry_type"]'); if(f) f.value=selectedInterest; }); }); /* Related products */ function loadRelated(catId){ var grid=document.getElementById('spdRelated'); for(var i=0;i<4;i++) grid.innerHTML+='
'; fetch(api('products?per_page=4&status=publish&_fields=id,name,permalink,images,price,regular_price,sale_price')+(catId?'&category='+catId:'')) .then(function(r){return r.json();}) .then(function(prods){ grid.innerHTML=''; (prods||[]).forEach(function(p){ var n=p.name||'Trailer',img=(p.images&&p.images.length)?''+n+'':'
'; var reg=parseFloat(p.regular_price||p.price||0),sale=p.sale_price?parseFloat(p.sale_price):null; var ph=sale&&sale$'+reg.toLocaleString()+'$'+sale.toLocaleString()+'':''+(reg?'$'+reg.toLocaleString():'Contact Us')+''; grid.innerHTML+='
'+img+'
'+n+'
'+ph+'
'; }); }).catch(function(){grid.innerHTML='';}); } /* Init */ var pid=getProductId(); var fields='id,name,permalink,images,price,regular_price,sale_price,average_rating,rating_count,categories,brands,pwb_brand,yith_brands,description,short_description,on_sale,featured'; var url=pid?api('products/'+pid+'?_fields='+fields):api('products?per_page=1&status=publish&_fields='+fields); fetch(url) .then(function(r){return r.json();}) .then(function(data){ var p=Array.isArray(data)?data[0]:data; if(!p||p.id===undefined) throw new Error('No product'); renderProduct(p); var catId=(p.categories&&p.categories.length)?p.categories[0].id:null; loadRelated(catId); }) .catch(function(){ document.getElementById('spdSkel').innerHTML='

Could not load product. Please refresh the page.

'; }); })();