// 600 mm archive range plus CEO-confirmed 450 mm range (2026-10-25); 200 mm height. export function estimateBlocks({length, height, openings, waste, width, unit, blockLength=600}) { const values=[length,height,openings,waste,width]; if(values.some(v=>typeof v!=='number'||!Number.isFinite(v))) throw new Error('Enter a number in every field.'); if(!['m','ft'].includes(unit)||![100,150,200].includes(width)||![450,600].includes(blockLength)) throw new Error('Choose a listed unit, block length and width.'); if(length<=0||height<=0||length>10000||height>10000) throw new Error('Wall length and height must be greater than zero and no more than 10,000.'); if(openings<0||waste<0||waste>30) throw new Error('Openings cannot be negative. Choose a wastage allowance from 0% to 30%.'); const scale=unit==='ft'?0.3048:1,gross=length*height*scale*scale,deduction=openings*scale*scale; if(deduction>gross+1e-9) throw new Error('The opening area cannot exceed the wall area.'); const faceArea=blockLength/1000*.2,net=Math.max(0,gross-deduction),raw=net/faceArea; const roundUp=n=>n===0?0:Math.max(1,Math.ceil(n-Number.EPSILON*Math.max(1,n)*8)); return {gross,net,base:roundUp(raw),total:roundUp(raw*(1+waste/100)),width,waste,blockLength,faceArea}; } export function estimateMessage(input) { const r=estimateBlocks(input); const dimension=n=>Number(n.toFixed(6)); return `Please advise on CELBLOC supply for this preliminary estimate.\n\nBlock size: ${r.blockLength} × 200 × ${r.width} mm\nWall: ${dimension(input.length)} × ${dimension(input.height)} ${input.unit}\nOpenings: ${dimension(input.openings)} ${input.unit}²\nNet wall area: ${r.net.toFixed(2)} m²\nWastage allowance: ${r.waste}%\nEstimated quantity: ${r.total.toLocaleString('en-GB')} blocks\n\nBasis: ${r.faceArea.toFixed(2)} m² per block; joints excluded; total rounded up after wastage. Please confirm the project quantity, specification and availability.`; } if(typeof document!=='undefined') { const form=document.querySelector('#clc-calculator'); if(form){ const error=document.querySelector('#calc-error'),result=document.querySelector('#calc-result'),link=document.querySelector('#calc-enquire'); let currentUnit='m'; const converted=new Map(); const numeric=k=>{const v=form.elements.namedItem(k).value,prior=converted.get(k);return prior&&prior.display===v?prior.raw:(v===''?NaN:Number(v));}; const input=()=>Object.fromEntries(['length','height','openings','waste','width','unit','blockLength'].map(k=>[k,k==='unit'?form.elements.namedItem(k).value:numeric(k)])); const invalidate=()=>{result.hidden=true;error.textContent='';form.querySelectorAll('[aria-invalid]').forEach(e=>e.removeAttribute('aria-invalid'));}; form.addEventListener('input',event=>{converted.delete(event.target.name);invalidate();}); form.elements.unit.addEventListener('change',()=>{ const next=form.elements.unit.value,factor=next==='ft'?1/0.3048:0.3048; if(next!==currentUnit){for(const k of ['length','height','openings']){const f=form.elements.namedItem(k),value=numeric(k);if(f.value!==''&&Number.isFinite(value)){const raw=value*Math.pow(factor,k==='openings'?2:1),display=String(Number(raw.toFixed(6)));f.value=display;converted.set(k,{raw,display});}}currentUnit=next;} form.querySelectorAll('[data-length-unit]').forEach(el=>el.textContent=next); document.querySelector('[data-area-unit]').textContent=next+'²';invalidate(); }); function updateBasis(){const blockLength=Number(form.elements.namedItem('blockLength').value),area=(blockLength/1000*.2).toFixed(2);document.querySelector('#calc-face-length').textContent=blockLength;document.querySelectorAll('[data-face-area]').forEach(el=>el.textContent=area);} form.addEventListener('change',()=>{invalidate();updateBasis();}); updateBasis(); form.addEventListener('submit',event=>{ event.preventDefault();invalidate(); try{ const data=input(),r=estimateBlocks(data); document.querySelector('#calc-total').textContent=r.total.toLocaleString('en-GB'); document.querySelector('#calc-net').textContent=r.net.toFixed(2)+' m²'; document.querySelector('#calc-gross').textContent=r.gross.toFixed(2)+' m²'; document.querySelector('#calc-base').textContent=r.base.toLocaleString('en-GB'); document.querySelector('#calc-waste').textContent=r.waste+'%'; document.querySelector('#calc-size').textContent=r.blockLength+' × 200 × '+r.width+' mm'; const params=new URLSearchParams(Object.entries(data).map(([k,v])=>['calc_'+k,String(v)])); link.href='enquire.html?'+params+'#enquiry';result.hidden=false;document.querySelector('#calc-result-heading').focus(); }catch(err){error.textContent=err.message;error.focus();} }); form.hidden=false; } const enquiry=document.querySelector('[data-seed-enquiry]'); if(enquiry){ const q=new URLSearchParams(location.search); if(q.has('calc_length'))try{ const data=Object.fromEntries(['length','height','openings','waste','width','unit','blockLength'].map(k=>[k,k==='unit'?q.get('calc_'+k):(q.get('calc_'+k)===null?(k==='blockLength'?600:NaN):Number(q.get('calc_'+k)))])); const message=enquiry.elements.message; if(!message.value){message.value=estimateMessage(data);enquiry.elements.family.value='CLC / Celbloc';document.querySelector('#estimate-carried').hidden=false;} }catch{/* Ignore malformed estimate links; keep the regular enquiry available. */} } }