﻿function SetCalc()
{
	SetNumeric();
	Calc();
}
var roomWidth = 0;
var unitWidth = 0;
var firstUnitWidth = 0;
var prefGap = 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 = "+ firstUnitWidth +" mm + "+ fullUnits +" units at "+ unitWidth +" mm ("+ gaps +" gaps at "+ gap.toFixed(1) +" mm)";
    else
        document.getElementById("spnResults").innerHTML = fullUnits +" units at "+ unitWidth +" mm ("+ gaps +" gaps at "+ gap.toFixed(1) +" mm)";
    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 = "+ firstUnitWidth +" mm + "+ fullUnits +" units at "+ unitWidth +" mm ("+ gaps +" gaps at "+ gap.toFixed(1) +" mm)";
    else
        document.getElementById("spnResults").innerHTML = fullUnits +" units at "+ unitWidth +" mm ("+ gaps +" gaps at "+ gap.toFixed(1) +" mm)";

    var totalUnits = gaps - 1;   
    DrawFit(roomWidth, firstUnitWidth, unitWidth, unitWidth, gap, totalUnits);
}
function CutToFit()
{
    if ( ! GetInput())
        return;
    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 = "+ firstUnitWidth +" + "+ wholeUnits +" full units + "+ lastCutWidth.toFixed(1) +" cut ("+ gaps +" gaps at "+ prefGap +" mm)";
    else
        document.getElementById("spnResults").innerHTML = wholeUnits +" full units + "+ lastCutWidth.toFixed(1) +" cut  ("+ gaps +" gaps at "+ prefGap +" mm)";

     DrawFit(roomWidth, firstUnitWidth, unitWidth, lastCutWidth, prefGap, totalUnits);
}
function EqualEnds()
{
    if ( ! GetInput())
        return;
    var unitAndGap = unitWidth + prefGap;
    roomWidth -= prefGap;
    var endCuts = ((roomWidth % unitAndGap) - (prefGap * 2)) / 2;
    var halfUnit = unitAndGap / 2;
    if (endCuts < halfUnit)
        endCuts += halfUnit;
    if (endCuts > 0)
        document.getElementById("txtFirstUnitWidth").value = endCuts;
    CutToFit();
}
function GetInput()
{
    roomWidth = parseInt(document.getElementById("txtWidth").value);
    if (roomWidth < 1000 || isNaN(roomWidth))
    {
        alert("Total Width must be greater than 1000");
        document.getElementById("txtWidth").focus();
        return false;
    }
    unitWidth = parseInt(document.getElementById("txtUnitWidth").value);
    if (unitWidth < 10 || isNaN(unitWidth))
    {
        alert("Unit Width must be greater than 10");
        document.getElementById("txtUnitWidth").focus();
        return false;
    }
    if (roomWidth < (unitWidth * 2))
    {
        alert("Total Width must be more than 2 x Unit Width");
        return false;
    }
    firstUnitWidth = parseInt(document.getElementById("txtFirstUnitWidth").value);
    if (firstUnitWidth < 10 || isNaN(firstUnitWidth))
    {
        alert("First Unit Width must be greater than 10");
        document.getElementById("txtFirstUnitWidth").focus();
        return false;
    }
    prefGap = parseInt(document.getElementById("txtPrefGap").value);
    if (prefGap < 0 || isNaN(prefGap))
    {
        alert("Gap Width must be greater than 1");
        document.getElementById("txtPrefGap").focus();
        return false;
    }
    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 CopyFirst(ctrl)
{
    document.getElementById("txtFirstUnitWidth").value = ctrl.value;
}
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);
    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 = "+ first;
    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 +" = "+ last;
    td = tr.insertCell(tr.cells.length);
    td.style.backgroundColor = gapColor;
    td.style.width = dGap;
}

