'Constants
const PI = 3.1415926535
const SheetLength = 2.5
const SheetHeight = 1.25

'Private
dim AllowUpdate
dim BackMark
dim DeadwaterType
dim Fractions
dim Freeboard
dim HorizontalLap
dim SheetsHigh
dim SheetsRound
dim Suction
dim TankType
dim VerticalLap

'Properties
function Area()
   Area = cdbl(PI * Diameter * Diameter / 4)
end function

function Axial()
   Axial = cdbl(Head * Radius * 9810)
end function

function Deadwater()
   if DeadwaterType = 0 then
      select case cdbl(Suction)
         case 0.10, 0.15
            Deadwater = cdbl(0.1)
         case 0.20, 0.25
            Deadwater = cdbl(0.15)
         case 0.30, 0.35, 0.40
            Deadwater = cdbl(0.2)
         case 0.45
            Deadwater = cdbl( 0.225)
         case else
            Deadwater = 0
      end select
   elseif DeadwaterType = 1 then
      select case cdbl(Suction)
         case 0
            Deadwater = cdbl(0.03)
         case 0.10, 0.15
            Deadwater = cdbl(0.2)
         case 0.20
            Deadwater = cdbl(0.25)
         case 0.25, 0.3
            Deadwater = cdbl(0.3)
         case 0.35, 0.4
            Deadwater = cdbl(0.4)
         case 0.45
            Deadwater = cdbl(0.45)
         case else 
            Deadwater = 0
      end select
   elseif DeadwaterType = 2 then
      select case cdbl(Suction)
         case 0.10, 0.15
            Deadwater = cdbl(0.1 + 2 * Suction)
         case 0.20, 0.25
            Deadwater = cdbl(0.15 + 2 * Suction)
         case 0.30, 0.35, 0.40
            Deadwater = cdbl(0.2 + 2 * Suction)
         case 0.45
            Deadwater = cdbl(0.225 + 2 * Suction)
         case else
            Deadwater = 0
      end select   
   end if
end function

function Diameter()
    Diameter =  cdbl(round(SheetsRound * (SheetLength - VerticalLap) / PI, 2))
end function

function Head
    Head = cdbl(Height - Freeboard - BackMark / 2)
end function

function Height()
    dim Full, Cut
    Full = int(SheetsHigh)
	Cut = SheetsHigh - Full
	if Cut > 0 then Cut = (SheetHeight * Cut) - HorizontalLap
	Height = cdbl(round(Full * (SheetHeight - HorizontalLap) + Cut + BackMark, 3))
end function

function Radius
   Radius = cdbl(Diameter / 2)
end function

function Size()
   Size = FormatNumber(Diameter, 3) & "m Diameter x " & FormatNumber(Height, 3) & "m High"
end function

function Volume(VolType)
	select Case clng(VolType)
		case 0
		   Volume = cdbl(Area * (Height - Freeboard - Deadwater))
		case 1
		   Volume = cdbl(Area * (Height - Freeboard))
		case 2
		   Volume = cdbl(Area * Height)
		case 3
		   Volume = cdbl(Area * Deadwater)
	end select
end function

'Methods
sub Initialise
	Fractions = false
	AllowUpdate = false
	BackMark = 0.08
	HorizontalLap = 0.05
	VerticalLap = 0.1
	frmCapacity.scrDiameter.Value = 224
	frmCapacity.scrHeight.value = 52
	frmCapacity.scrFreeboard.Value = 85	
    scrDiameter_Change
	scrHeight_Change
	scrFreeboard_Change
	cboSuction_Change
	AllowUpdate = true
	Refresh
end sub

sub Refresh()
    tdSize.innerHTML = Size
    tdArea.innerHTML = FormatNumber(Area, 2) & " m<sup>2</sup>"
	divDiameter.innerHTML = FormatNumber(Diameter, 2)
	divHeight.innerHTML = FormatNumber(Height, 3)
	divFreeboard.innerHTML = FormatNumber(Freeboard, 2)
	tdEffective.innerHTML = FormatNumber(Volume(0), 1) & " m<sup>3</sup>"
	tdActual.innerHTML = FormatNumber(Volume(1), 1) & " m<sup>3</sup>"
	tdDeadwater.innerHTML = FormatNumber(Volume(3), 1) & " m<sup>3</sup>"
	tdDeadwaterLevel.innerHTML = FormatNumber(Deadwater, 3) & " m"
	tdRimHeight.innerHTML = FormatNumber(Height, 3) & " m"
	tdWaterLevel.innerHTML = FormatNumber(Height - Freeboard, 3) & " m"
	tdRoofHeight.innerHTML = FormatNumber(Height + 0.04, 3) & " m"
	tdOverallHeight.innerHTML = FormatNumber(Height + 0.835) & " m"
	
	if TankType = 0  then
	   if Axial > 815806 then lblInvalid.style.display = "" else lblInvalid.style.display = "None"
	elseif TankType = 1 then 
	   if Axial > 815806 then lblInvalid.style.display = "" else lblInvalid.style.display = "None"
	elseif TankType = 2 then 
		if Axial > 648353 then lblInvalid.style.display = "" else lblInvalid.style.display = "None"
	elseif TankType = 3 then
		if Axial > 815806 then lblInvalid.style.display = "" else lblInvalid.style.display = "None"
	elseif TankType = 4 then
		if Axial > 1509367 then lblInvalid.style.display = "" else lblInvalid.style.display = "None"
	end if 
end sub

'Events
sub scrDiameter_Change()
	SheetsRound = cdbl((frmCapacity.scrDiameter.max - frmCapacity.scrDiameter.value + frmCapacity.scrDiameter.min) / 8)
	frmCapacity.txtNumberRound.value = SheetsRound
	if AllowUpdate then Refresh
end sub

sub scrFreeboard_Change()
	Freeboard = cdbl((frmCapacity.scrFreeboard.max - frmCapacity.scrFreeboard.value + frmCapacity.scrFreeboard.min) / 100)
	frmCapacity.txtFreeboard.value = Freeboard
	if AllowUpdate then Refresh
end sub

sub scrHeight_Change()
	SheetsHigh = cdbl((frmCapacity.scrHeight.max - frmCapacity.scrHeight.value + frmCapacity.scrHeight.min) / 4)
	frmCapacity.txtNumberHigh.value = SheetsHigh
	if AllowUpdate then Refresh
end sub

sub cboDeadwater_Change()
   DeadwaterType = clng(frmCapacity.cboDeadwater.options(frmCapacity.cboDeadwater.selectedIndex).value)
   if AllowUpdate then Refresh
end sub

sub cboSuction_Change()
	Suction = cdbl(frmCapacity.cboSuction.options(frmCapacity.cboSuction.selectedIndex).value)
   if AllowUpdate then Refresh
end sub

sub cboType_Change()
    TankType = clng(frmCapacity.cboType.options(frmCapacity.cboType.selectedIndex).value)
    if TankType = 0 or TankType = 1 or TankType = 3 then
	    HorizontalLap = 0.05
	    VerticalLap = 0.1
	    BackMark = 0.08
	    frmCapacity.scrDiameter.Min = 32
	    frmCapacity.scrDiameter.Max = 224
	    frmCapacity.scrDiameter.Value = 224
	    frmCapacity.scrHeight.Max = 48	
    elseif TankType = 2 then
	    HorizontalLap = 0.051
	    VerticalLap = 0.1
	    BackMark = 0.08
	    frmCapacity.scrDiameter.Min = 32
	    frmCapacity.scrDiameter.Max = 224
	    frmCapacity.scrDiameter.Value = 224
	    frmCapacity.scrHeight.Max = 48
    elseif TankType = 4 then
	    HorizontalLap = 0.08
	    VerticalLap = 0.15
	    BackMark = 0.094
	    frmCapacity.scrDiameter.Min = 32
	    frmCapacity.scrDiameter.Max = 376
	    frmCapacity.scrDiameter.Value = 376
	    frmCapacity.scrHeight.Max = 64
    end if
	if AllowUpdate then Refresh
end sub
