﻿function SetCalc()
{
	SetNumeric();
	Calc();
}
var roomWidth = 0;
var unitWidth = 0;
var firstUnitWidth = 0;
var justGaps = 0;
var justUnits = 0;
var fullUnits = 0;
var isFirstCut = false;
function Calc()
{    
    if ( ! GetInput())
        return;
    var totalUnits = 0;
    isFirstCut = (firstUnitWidth != unitWidth);
    if (isFirstCut)
    {
        fullUnits = Math.floor((roomWidth - firstUnitWidth) / unitWidth);
        justUnits = firstUnitWidth + (fullUnits * unitWidth);
        totalUnits = fullUnits + 1;
    }
    else
    {
        justUnits = Math.floor(roomWidth / unitWidth) * unitWidth;
        fullUnits = justUnits / unitWidth;
        totalUnits = fullUnits;
    }
    justGaps = roomWidth - justUnits;
    gaps = totalUnits + 1;
    gap = justGaps / gaps;
    if (isFirstCut)
        document.getElementById("spnResults").innerHTML = "First unit = "+ ConEng(firstUnitWidth, false) +" + "+ fullUnits +" units at "+ ConEng(unitWidth, false) +" ("+ gaps +" gaps at "+ ConEng(gap, false) +")";
    else
        document.getElementById("spnResults").innerHTML = fullUnits +" units at "+ ConEng(unitWidth, false) +" ("+ gaps +" gaps at "+ ConEng(gap, false) +")";
    DrawFit(roomWidth, firstUnitWidth, unitWidth, unitWidth, gap, totalUnits);
}
function AddRemove()
{    
    if (isFirstCut)
    {
        justUnits = firstUnitWidth + (fullUnits * unitWidth);
        gaps = fullUnits + 2;
    }
    else
    {
        justUnits = fullUnits * unitWidth;
        gaps = fullUnits + 1;
    }
    justGaps = roomWidth - justUnits;
    gap = justGaps / gaps;
    if (gap < 0 || fullUnits < 2)
    {
        alert("Gaps too small or not enough units");
        fullUnits = oldUnits;
        return;
    }
    if (isFirstCut)
        document.getElementById("spnResults").innerHTML = "First unit = "+ ConEng(firstUnitWidth, false)+" + "+ fullUnits +" units at "+ ConEng(unitWidth, false) +" ("+ gaps +" gaps at "+ ConEng(gap, false) +")";
    else
        document.getElementById("spnResults").innerHTML = fullUnits +" units at "+ ConEng(unitWidth, false) +" ("+ gaps +" gaps at "+ ConEng(gap, false) +")";

    var totalUnits = gaps - 1;   
    DrawFit(roomWidth, firstUnitWidth, unitWidth, unitWidth, gap, totalUnits);
}
function CutToFit()
{
    if ( ! GetInput())
        return;
    var prefGap = parseInt(document.getElementById("ddPrefInches").value) * 32 + parseInt(document.getElementById("ddPrefFrac").value);
    isFirstCut = (firstUnitWidth != unitWidth)
    var wholeUnits = 0;
    var lastCutWidth = 0;
    var totalUnits = 0;
    var remainder = 0;
    var unitAndGap = unitWidth + prefGap;
    var roomAdj = 0;
    if (isFirstCut)
        roomAdj = roomWidth - firstUnitWidth - prefGap;
    else
        roomAdj = roomWidth;
    roomAdj -= prefGap; // take out the END 1 gap
    wholeUnits = Math.floor(roomAdj / unitAndGap); 
    var remainder = roomAdj % unitAndGap; // to the far end of the last full unit
    lastCutWidth = remainder - prefGap; // the second last gap'
    if (lastCutWidth <= 0)
    {
        alert("End unit to small\n\rClick 'Even Gaps' to adjust gaps");
        return;
    }
    if (isFirstCut)
        totalUnits = wholeUnits + 2;
    else
        totalUnits = wholeUnits + 1; // extra for first + last cuts
    gaps = totalUnits + 1;
    if (isFirstCut)
        document.getElementById("spnResults").innerHTML = "First unit = "+ ConEng(firstUnitWidth, false) +" + "+ wholeUnits +" full units + "+ ConEng(lastCutWidth, false) +" cut ("+ gaps +" gaps at "+ ConEng(prefGap, false) +")";
    else
        document.getElementById("spnResults").innerHTML = wholeUnits +" full units + "+ ConEng(lastCutWidth, false) +" cut ("+ gaps +" gaps at "+ ConEng(prefGap, false) +")";

     DrawFit(roomWidth, firstUnitWidth, unitWidth, lastCutWidth, prefGap, totalUnits);
}
function EqualEnds()
{
    if ( ! GetInput())
        return;
    var prefGap = parseInt(document.getElementById("ddPrefInches").value) * 32 + parseInt(document.getElementById("ddPrefFrac").value);
    var unitAndGap = unitWidth + prefGap;
    roomWidth -= prefGap;
    var endCuts = ((roomWidth % unitAndGap) - (prefGap * 2)) / 2;
    var halfUnit = unitAndGap / 2;
    if (endCuts < halfUnit)
        endCuts += halfUnit;
    document.getElementById("ddFirstInches").selectedIndex = Math.floor(endCuts / 32);
    document.getElementById("ddFirstFrac").selectedIndex = endCuts % 32;
    CutToFit();
}
function GetInput()
{
    roomWidth = parseInt(document.getElementById("ddLengthFeet").value) * 384 + parseInt(document.getElementById("ddLengthInches").value) * 32 + parseInt(document.getElementById("ddLengthFrac").value);
    unitWidth = parseInt(document.getElementById("ddUnitInches").value) * 32 + parseInt(document.getElementById("ddUnitFrac").value);
    if (roomWidth < (unitWidth * 2))
    {
        alert("Total Width must be more than 2 x Unit Width");
        return false;
    }
    firstUnitWidth = parseInt(document.getElementById("ddFirstInches").value) * 32 + parseInt(document.getElementById("ddFirstFrac").value);
    return true;
}
var oldUnits = 0;
function SubUnit()
{
    oldUnits = fullUnits;
    fullUnits --;
    AddRemove();
}
function AddUnit()
{
    oldUnits = fullUnits;
    fullUnits ++;
    AddRemove();
}
function ScaleTable(d)
{
    var t = document.getElementById("tblFit");
    var w = parseInt(t.style.width);
    if (d == 1)
        t.style.width = (w + 40) +"px";
    else
    {
        if (w > 200)
            t.style.width = (w - 40) +"px";
        else
        {
            alert("Too small");
            return;
        }
    }
}
function CopyFirstInch(ctrl)
{
    document.getElementById("ddFirstInches").selectedIndex = ctrl.selectedIndex;
}
function CopyFirstFrac(ctrl)
{
    document.getElementById("ddFirstFrac").selectedIndex = ctrl.selectedIndex;
}
function DrawFit(ln, first, unit, last, gap, totalUnits)
{
    if (totalUnits < 2)
    {
        alert("Cannot draw");
        return;
    }
    var units = totalUnits - 2;
    if (units > 100)
    {
        alert("Cannont draw more than 100 units");
        return;
    }
    var gapColor = "Black";
    var unitBg = "url(images/bluetile.jpg)";
    var tbl = document.getElementById("tblFit");
    var tw = parseInt(tbl.style.width);
    var dUnit = tw / (ln / unit); // +"px";
    if (dUnit < 400)
        tbl.style.height = dUnit +"px";
    dUnit += "px";
    var dGap = tw / (ln / gap) +"px";
    var dFirst = tw / (ln / first);
    var dLast = tw / (ln / last);
    tbl.deleteRow(0);
    var tr = tbl.insertRow(0);
    var td = null;
    td = tr.insertCell(tr.cells.length);
    td.style.backgroundColor = gapColor;
    td.style.width = dGap;
    td = tr.insertCell(tr.cells.length);
    td.style.backgroundImage = unitBg;
    td.title = "1 = "+ ConEng(first, false);
    td.style.width = dFirst +"px";
    for (var i=0; i< units; i++)
    {
        td = tr.insertCell(tr.cells.length);
        td.style.backgroundColor = gapColor;
        td.style.width = dGap;
        td = tr.insertCell(tr.cells.length);
        td.style.backgroundImage = unitBg;
        td.style.width = dUnit;
        //td.title = (i + 2) +" = "+ unit;
    }
    td = tr.insertCell(tr.cells.length);
    td.style.backgroundColor = gapColor;
    td.style.width = dGap;
    td = tr.insertCell(tr.cells.length);
    td.style.backgroundImage = unitBg;
    td.style.width = dLast +"px";
    td.title = totalUnits +" = "+ ConEng(last, false);
    td = tr.insertCell(tr.cells.length);
    td.style.backgroundColor = gapColor;
    td.style.width = dGap;
}

