/* === KTY CALCULATOR JS — loaded on /crochet-calculators/ === */ (function(){ function fmt(n,d){d=d||0;if(isNaN(n)||!isFinite(n))return'0';return Number(n).toLocaleString('en-US',{minimumFractionDigits:d,maximumFractionDigits:d})} function $(id){return document.getElementById(id)} function val(id){var el=$(id);return el?parseFloat(el.value)||0:0} /* 1. YARN YARDAGE CALCULATOR */ var ycPresets={blanket:{w:50,l:60},scarf:{w:8,l:60},hat:{w:20,l:8},sweater:{w:44,l:26},amigurumi:{w:6,l:10}}; var ycWeightYards={fingering:1700,sport:1400,dk:1150,worsted:850,bulky:550,superbulky:350}; window.ycSetProject=function(){ var p=$('yc-project').value,pr=ycPresets[p]; if(pr){$('yc-width').value=pr.w;$('yc-length').value=pr.l} }; window.ycCalculate=function(){ var w=val('yc-width'),l=val('yc-length'),gauge=val('yc-gauge'),weight=$('yc-weight').value,mult=parseFloat($('yc-pattern').value)||1.0,ypg=ycWeightYards[weight]||850; if(w<=0||l<=0||gauge<=0){$('yc-result').textContent='Please enter width, length, and gauge.';$('yc-output').textContent='';return} var totalS=w*gauge*l*gauge,spy=gauge*36*mult,yards=totalS/spy,grams=(yards/ypg)*100; $('yc-result').textContent=''; $('yc-output').innerHTML=''+fmt(Math.round(yards))+' yards (~'+fmt(Math.round(grams))+' g / '+fmt((grams/28.3495).toFixed(1),1)+' oz)'; }; /* 2. STITCH & ROW COUNTER */ var scC={stitch:0,row:0,repeat:0}; window.scAdjust=function(t,d){scC[t]=Math.max(0,scC[t]+d);$('sc-'+t+'-display').textContent=scC[t]}; window.scReset=function(t){scC[t]=0;$('sc-'+t+'-display').textContent='0'}; /* 3. HAT SIZING CALCULATOR */ var hcS={15:{c:14,l:4.5},16:{c:15,l:5},17:{c:16,l:5.5},18:{c:17,l:6},19:{c:18,l:6.5},20:{c:19,l:7},21:{c:20,l:7.5},22:{c:21.5,l:7.5},23:{c:23,l:8},24:{c:24.5,l:8}}; window.hcSetPreset=function(){var v=parseFloat($('hc-preset').value);if(hcS[v]){$('hc-head').value=v;$('hc-negative').value=hcS[v].c}}; window.hcCalculate=function(){ var head=val('hc-head'),neg=val('hc-negative'); if(head<=0){$('hc-result').textContent='';$('hc-output').textContent='';return} var circ=neg>0?neg:head-(head*0.1),dia=circ/Math.PI,rounds=Math.ceil(dia/0.65),chains=Math.round(circ*2.5),yards=circ*rounds*1.2; $('hc-result').textContent=''; $('hc-output').innerHTML='
Hat circumference: '+fmt(circ,1)+'"
Crown diameter: '+fmt(dia,1)+'"
Rounds to crown: '+rounds+'
Starting chain/ring: '+chains+'
Est. yarn: ~'+fmt(Math.round(yards))+' yards
'; }; /* 4. BLANKET SIZING CALCULATOR */ var bcB={lovey:{w:12,l:12},baby:{w:30,l:40},crib:{w:36,l:52},throw:{w:50,l:60},twin:{w:66,l:90},full:{w:80,l:90},queen:{w:90,l:100},king:{w:108,l:100},'california-king':{w:104,l:108}}; var bcWY={fingering:1700,sport:1400,dk:1150,worsted:850,bulky:550,superbulky:350}; window.bcCalculate=function(){ var type=$('bc-type').value,gauge=val('bc-gauge'),weight=$('bc-weight').value,ypg=bcWY[weight]||850,w,l; if(type&&bcB[type]){w=bcB[type].w;l=bcB[type].l}else{w=0;l=0} if(w<=0||l<=0){$('bc-result').textContent='Please select a blanket type.';$('bc-output').textContent='';return} var area=w*l,spi=gauge>0?gauge:4,mult=1.3,yards=(w*spi*l*spi)/(spi*36*mult),grams=(yards/ypg)*100,chains=Math.round(w*spi); $('bc-result').textContent=''; $('bc-output').innerHTML='
Size: '+w+'" x '+l+'" ('+fmt(area)+' sq in)
Starting chains: ~'+chains+'
Est. yarn: ~'+fmt(Math.round(yards))+' yards (~'+fmt(Math.round(grams))+' g)
Est. skeins (worsted): ~'+fmt(Math.ceil(yards/200))+'
'; }; /* 5. STITCH & ROW CALCULATOR */ window.sruUpdateLabels=function(){ var unit=$('sru-unit').value,sz=unit==='cm'?10:4,lbl=unit==='cm'?'10cm':'4"'; if($('sru-stitch-label'))$('sru-stitch-label').textContent='Stitches per '+lbl+' swatch'; if($('sru-stitch-len-label'))$('sru-stitch-len-label').textContent='Width of piece ('+unit+')'; if($('sru-row-label'))$('sru-row-label').textContent='Rows per '+lbl+' swatch'; if($('sru-row-len-label'))$('sru-row-len-label').textContent='Length of piece ('+unit+')'; }; window.sruCalc=function(){ var unit=$('sru-unit').value,sz=unit==='cm'?10:4,spi=val('sru-spi'),sl=val('sru-stitch-len'),rpi=val('sru-rpi'),rl=val('sru-row-len'); if(spi>0&&sl>0){var s=Math.round(spi*(sl/sz));$('sru-stitch-output').innerHTML=''+fmt(s)+' stitches';$('sru-stitch-result').textContent=''} else{$('sru-stitch-output').textContent=spi<=0?'Enter stitches per swatch':'Enter piece width';$('sru-stitch-result').textContent=''} if(rpi>0&&rl>0){var r=Math.round(rpi*(rl/sz));$('sru-row-output').innerHTML=''+fmt(r)+' rows';$('sru-row-result').textContent=''} else{$('sru-row-output').textContent=rpi<=0?'Enter rows per swatch':'Enter piece length';$('sru-row-result').textContent=''} }; /* 6. PROJECT TIME ESTIMATOR */ window.teCalc=function(){ var min=val('te-minutes'),rows=val('te-rows'); if(min<=0||rows<=0){$('te-result').textContent='';$('te-output').textContent='';return} var mpr=min/rows,rph=60/mpr; $('te-result').textContent=''; $('te-output').innerHTML='
Time per row: '+fmt(mpr,1)+' min
Rows per hour: ~'+fmt(rph,1)+'
50-row project: ~'+fmt(50*mpr/60,1)+' hrs
100-row project: ~'+fmt(100*mpr/60,1)+' hrs
200-row project: ~'+fmt(200*mpr/60,1)+' hrs
'; }; /* 7. SKEINS NEEDED CALCULATOR */ window.snCalc=function(){ var yds=val('sn-yards'),yps=val('sn-yps'),price=val('sn-price'); if(yds<=0||yps<=0){$('sn-result').textContent='';$('sn-output').textContent='';return} var skeins=Math.ceil(yds/yps),left=(skeins*yps)-yds,cost=skeins*price; $('sn-result').textContent=''; $('sn-output').innerHTML='
Skeins needed: '+skeins+'
Leftover: ~'+fmt(Math.round(left))+' yards
'+(price>0?'
Total cost: $'+fmt(cost,2)+'
':''); }; /* 8. PRICING CALCULATOR — FULL */ window.pfCalc=function(){ var mat=val('pf-yarn-cost'),hrs=val('pf-hours'),rate=val('pf-rate'),fees=val('pf-fees'); var labor=hrs*rate,sub=mat+labor+fees,profit=sub*0.2,retail=sub+profit,whole=retail*0.5; $('pf-result').textContent=''; $('pf-output').innerHTML='
Materials: $'+fmt(mat,2)+'
Labor ('+hrs+'h @ $'+fmt(rate,2)+'): $'+fmt(labor,2)+'
Fees: $'+fmt(fees,2)+'
Subtotal: $'+fmt(sub,2)+'
+20% profit: $'+fmt(profit,2)+'
Retail: $'+fmt(retail,2)+'
Wholesale: $'+fmt(whole,2)+'
'; }; /* 9. PRICING CALCULATOR — QUICK */ window.pqCalc=function(){ var skeins=val('pq-skeins'),cost=val('pq-cost'),fees=val('pq-fees'); var mat=skeins*cost,base=mat*3,total=base+fees; $('pq-result').textContent=''; $('pq-output').innerHTML='
Material cost: $'+fmt(mat,2)+'
3x markup: $'+fmt(base,2)+'
'+(fees>0?'
+ Fees: $'+fmt(fees,2)+'
':'')+'
Suggested price: $'+fmt(total,2)+'
'; }; /* 10. YARN WEIGHT CALCULATOR */ window.ywUpdateLabels=function(){ var unit=$('yw-unit').value; if($('yw-proj-label'))$('yw-proj-label').textContent='Project weight ('+unit+')'; if($('yw-skein-label'))$('yw-skein-label').textContent='Full skein weight ('+unit+')'; }; window.ywCalc=function(){ var pw=val('yw-proj'),sw=val('yw-skein'),yps=val('yw-yps'); if(pw<=0||sw<=0){$('yw-result').textContent='';$('yw-output').textContent='';return} var used=pw/sw,partial=used%1,full=Math.floor(used),yds=Math.round(used*yps),left=yps-Math.round(partial*yps); $('yw-result').textContent=''; $('yw-output').innerHTML='
Skeins used: '+fmt(used,1)+' ('+full+' full + '+fmt(partial*100,0)+'% of one)
Yardage used: ~'+fmt(yds)+' yards
Remaining in partial: ~'+fmt(left)+' yards
'; }; /* Init */ sruUpdateLabels();ywUpdateLabels(); })();