var totalWidth,totalLength,optGap,boardWidth,boardAndGap,boards,allGaps,actualGap,linealBoards,minLast,boardLength,joistSpacing;function Setup(){minLast=32;RestoreFromQs();CalcDeck();SetInput()}
function CalcDeck(){totalWidth=ConvertTo32nds(document.getElementById("txtTotalWidth").value);if(totalWidth<1152||totalWidth>38400||isNaN(totalWidth)){alert("Deck width must be between 3' and 100'");document.getElementById("txtTotalWidth").focus()}else{totalLength=ConvertTo32nds(document.getElementById("txtTotalLength").value);if(totalLength<1152||totalWidth>38400||isNaN(totalLength)){alert("Deck length must be between 3' and 100'");document.getElementById("txtTotalLength").focus()}else{optGap=ConvertTo32nds(document.getElementById("txtOptGap").value);
if(optGap<1||optGap>20||isNaN(optGap)){alert('Optimal Gap must be between 1/32" and 1"');document.getElementById("txtOptGap").focus()}else{boardWidth=ConvertTo32nds(document.getElementById("txtBoardWidth").value);if(boardWidth<32||boardWidth>768||isNaN(boardWidth)){alert("Board width must be between 1\" and 2'");document.getElementById("txtBoardWidth").focus()}else{boardLength=ConvertTo32nds(document.getElementById("txtBoardLength").value);if(boardLength<2304||boardLength>7680||isNaN(boardLength)){alert("Board width must be between 6' and 20'");
document.getElementById("txtBoardLength").focus()}else{joistSpacing=parseInt(document.getElementById("ddJoistSpacingInches").value)*32;boards=Math.floor((totalWidth+optGap)/(boardWidth+optGap));allGaps=totalWidth-boards*boardWidth;actualGap=allGaps/(boards-1);boardAndGap=boardWidth+actualGap;document.getElementById("btnSubBoard").disabled=false;document.getElementById("btnAddBoard").disabled=false;document.getElementById("btnCutLast").disabled=false;UpdateResults(0)}}}}}}
function Board(a){if(a==1)if(allGaps>boardWidth)boards++;else{document.getElementById("btnAddBoard").disabled=true;alert("Won't Fit");return}else if(actualGap<20)boards--;else{document.getElementById("btnSubBoard").disabled=true;alert("Gaps are too large");return}allGaps=totalWidth-boards*boardWidth;actualGap=allGaps/(boards-1);boardAndGap=boardWidth+actualGap;linealBoards=boards*totalLength;document.getElementById("btnSubBoard").disabled=false;document.getElementById("btnAddBoard").disabled=false;
document.getElementById("btnCutLast").disabled=false;UpdateResults(0)}
function UpdateResults(a){DoRun();linealBoards=boards*totalLength;document.getElementById("spnBoards").innerHTML=boards;document.getElementById("spnGap").innerHTML=ConEng(actualGap,false)+" ("+RoundTo(actualGap/32,2)+"&quot;)";document.getElementById("spnLineal").innerHTML=ConEng(linealBoards,true);var d=parseFloat(document.getElementById("ddWaste").value),c=linealBoards+linealBoards*d/100;if(c%384>0)c+=384-c%384;document.getElementById("spnLinealPlus").innerHTML=ConEng(c,true);document.getElementById("spnPercentWaste").innerHTML=
d;c=Math.ceil(c/boardLength);document.getElementById("spnTotalBoards").innerHTML=c;document.getElementById("spnBoardLength").innerHTML=ConEng(boardLength,true);var b=Math.ceil(totalLength/joistSpacing)+1;document.getElementById("spnJoists").innerHTML=b;b=b*boards*2;if(totalLength>boardLength)b+=c*2;document.getElementById("spnFasteners").innerHTML=b;document.getElementById("spnBoardLength").innerHTML=ConEng(boardLength,true);b="";if(a==0)b=boards+" Members @ "+ConEng(boardWidth,false)+" wide, with "+
(boards-1)+" gaps of "+ConEng(actualGap,false);else{b=boards-1+" Members @ "+boardWidth+" wide + 1 @ "+a+" wide, with "+(boards-1)+" gaps of "+RoundTo(actualGap,1);document.getElementById("spnBoards").innerHTML+=" (Last Board = "+ConEng(a,false)+")"}b+="<br />"+c+" Boards @ "+ConEng(boardLength,true)+" long (includes "+d+" % waste)";document.getElementById("spnDesc").innerHTML=b}
function DoRun(){for(var a="",d=0,c=document.getElementById("cbEveryBoard").checked,b=document.getElementById("ddEveryBoard").value,e=0;e<boards-1;e++){d+=boardAndGap;if(e%b==b-1)a+='<span class="deck">'+ConEng(d,true)+"</span>, ";else c||(a+=ConEng(d,true)+", ")}a=a.substring(0,a.length-2);document.getElementById("spnBoardRun").innerHTML=a}
function CutLast(){var a=Math.round(totalWidth%(boardWidth+optGap));if(a>0)boards=Math.ceil(totalWidth/boardAndGap);if(a<minLast)if(confirm("With "+ConEng(optGap,false)+" gaps, the last board width is too small ("+ConEng(a,false)+")\n\r\n\rWould you like to adjust gaps for min last board = "+ConEng(minLast,false)+" ?")){actualGap-=(minLast-a)/(boards-1);boardAndGap=boardWidth+actualGap;a=minLast}else return;actualGap=optGap;boardAndGap=boardWidth+actualGap;document.getElementById("btnAddBoard").disabled=
true;document.getElementById("btnCutLast").disabled=true;UpdateResults(Math.round(a))}function Reset(){document.getElementById("btnSubBoard").disabled=true;document.getElementById("btnAddBoard").disabled=true;document.getElementById("btnCutLast").disabled=true};
