var totalWidth,totalLength,optGap,boardWidth,boardAndGap,boards,allGaps,actualGap,linealBoards,minLast,boardLength,joistSpacing,boardThick;function SetDeck(){SetNumeric();minLast=25;CalcDeck()}
function CalcDeck(){totalWidth=parseFloat(document.getElementById("txtTotalWidth").value);if(totalWidth<100||totalWidth>1E5||isNaN(totalWidth))alert("Deck Width must be between 1000 and 100000"),document.getElementById("txtTotalWidth").focus();else{totalLength=parseFloat(document.getElementById("txtTotalLength").value);if(isNaN(totalLength))document.getElementById("txtTotalLength").value="0",totalLength=0;optGap=parseFloat(document.getElementById("_txtOptGap").value);optGap<1||optGap>20||isNaN(optGap)?
(alert("Optimal Gap must be between 1 and 20"),document.getElementById("_txtOptGap").focus()):(boardWidth=parseFloat(document.getElementById("txtBoardWidth").value),boardWidth<20||boardWidth>600||isNaN(boardWidth)?(alert("Board Width must be between 20 and 600"),document.getElementById("txtBoardWidth").focus()):(boardThick=parseFloat(document.getElementById("txtBoardThick").value),boardThick<19||boardThick>100||isNaN(boardThick)?(alert("Board Thickness must be between 19 and 100"),document.getElementById("txtBoardThick").focus()):
(boardLength=parseFloat(document.getElementById("txtBoardLength").value),boardLength<1E3||boardLength>2E4||isNaN(boardLength)?(alert("Board Length must be between 1000 and 20000"),document.getElementById("txtBoardLength").focus()):(joistSpacing=parseFloat(document.getElementById("txtJoistSpacing").value),joistSpacing<200||joistSpacing>1E3||isNaN(joistSpacing)?(alert("Joist Spacing must be between 200 and 1000"),document.getElementById("txtJoistSpacing").focus()):(boards=Math.floor((totalWidth+optGap)/
(boardWidth+optGap)),allGaps=totalWidth-boards*boardWidth,actualGap=allGaps/(boards-1),boardAndGap=boardWidth+actualGap,document.getElementById("btnSubBoard").disabled=!1,document.getElementById("btnAddBoard").disabled=!1,document.getElementById("btnCutLast").disabled=!1,UpdateResults(0))))))}}
function Board(a){if(a==1)if(allGaps>boardWidth)boards++;else{document.getElementById("btnAddBoard").disabled=!0;alert("Won't Fit");return}else if(actualGap<20)boards--;else{document.getElementById("btnSubBoard").disabled=!0;alert("Gaps are too large");return}allGaps=totalWidth-boards*boardWidth;actualGap=allGaps/(boards-1);boardAndGap=boardWidth+actualGap;linealBoards=boards*totalLength;document.getElementById("btnSubBoard").disabled=!1;document.getElementById("btnAddBoard").disabled=!1;document.getElementById("btnCutLast").disabled=
!1;UpdateResults(0)}
function UpdateResults(a){DoRun();linealBoards=boards*totalLength;document.getElementById("spnBoards").innerHTML=boards;document.getElementById("spnGap").innerHTML=RoundTo(actualGap,1);document.getElementById("spnLineal").innerHTML=CommaFormat(linealBoards);var c=parseFloat(document.getElementById("ddWaste").value),e=linealBoards+linealBoards*c/100;document.getElementById("spnLinealPlus").innerHTML=CommaFormat(e);document.getElementById("spnPercentWaste").innerHTML=c;document.getElementById("spnBoardLength").innerHTML=boardLength;
e=Math.ceil(e/boardLength);document.getElementById("spnTotalBoards").innerHTML=e;var f=e*boardLength*boardWidth*boardThick*1.0E-9;document.getElementById("spnDeckVolume").innerHTML=RoundTo(f,3);var b=Math.ceil(totalLength/joistSpacing)+1;document.getElementById("spnJoists").innerHTML=b;b=b*boards*2;totalLength>boardLength&&(b+=e*2);document.getElementById("spnFasteners").innerHTML=b;b="";a==0?b=boards+" Members @ "+boardWidth+" wide, with "+(boards-1)+" gaps of "+RoundTo(actualGap,1)+" mm":(b=boards-
1+" Members @ "+boardWidth+" wide + 1 @ "+a+" wide, with "+(boards-1)+" gaps of "+RoundTo(actualGap,1)+"mm",document.getElementById("spnBoards").innerHTML+=" (Last Board = "+a+")");b+="<br />"+e+" Boards @ "+CommaFormat(boardLength)+" X "+boardWidth+" X "+boardThick+" (includes "+c+"% waste) = "+RoundTo(f,3)+"m&sup3;";document.getElementById("spnDesc").innerHTML=b}
function DoRun(){for(var a="",c=0,e=document.getElementById("cbEveryBoard").checked,f=document.getElementById("ddEveryBoard").value,b=0;b<boards-1;b++)c+=boardAndGap,b%f==f-1?a+='<span class="CalcRes2">'+Math.round(c)+"</span>, ":e||(a+=Math.round(c)+", ");a=a.substring(0,a.length-2);document.getElementById("spnBoardRun").innerHTML=a}
function CutLast(){var a=Math.round(totalWidth%(boardWidth+optGap));a>0&&(boards=Math.ceil(totalWidth/boardAndGap));if(a<minLast)if(confirm("With "+optGap+"mm gaps, the last board width is too small ("+Math.round(a)+"mm)\n\r\n\rWould you like to adjust gaps for min last board = "+minLast+"mm ?"))actualGap-=(minLast-a)/(boards-1),boardAndGap=boardWidth+actualGap,a=minLast;else return;actualGap=optGap;boardAndGap=boardWidth+actualGap;document.getElementById("btnAddBoard").disabled=!0;document.getElementById("btnCutLast").disabled=
!0;UpdateResults(Math.round(a))}function Reset(){document.getElementById("btnSubBoard").disabled=!0;document.getElementById("btnAddBoard").disabled=!0;document.getElementById("btnCutLast").disabled=!0}function RoundTo(a,c){c=Math.pow(10,c);return d=Math.round(a*c)/c}function GetQR(a){BuildQR(a,"Centres "+RoundTo(boardAndGap,1))}function RunTalk(){RunTalkQR("RunTalk - Metric",boardAndGap,0)};
