function lighten(SelectedItem) // OK
{
  document.getElementById(SelectedItem).style.color = '#0063D8';
};


function darken(SelectedItem) // OK
{
  document.getElementById(SelectedItem).style.color = '#000022';
};


function ShowPDF(s) // NOT TESTED
{
  var menu = document.getElementById(s);
  var pdf  = './' + menu.options[menu.selectedIndex].value;
  var DrawingWindow;
  DrawingWindow = window.open('', 'DrawingPreview', 'toolbar=0,scrollbars=0,resizable=0,menubar=0,width=1,height=1');
  DrawingWindow.close();
  DrawingWindow = window.open('', 'DrawingPreview', 'toolbar=0,scrollbars,resizable,menubar=0,width=800,height=600');
  DrawingWindow.document.write('<html><body bgcolor="#FFFFFF"><br><br><br><br><center> Loading drawing...</center></body></html>');
  DrawingWindow.document.URL = pdf;
  DrawingWindow.focus();
};


function CalcVolume() // OK
{
  if(parseInt(navigator.appVersion) >= 4)
  {
    var vesdiameter;
    var veslength;
    var volume;

    var diamunits;
    var lenunits;
    var volunits;

    if(navigator.appName == 'Netscape')
    {
      vesdiameter  = document.forms['Volume'].elements['VesDiameter'].value;
      veslength    = document.forms['Volume'].elements['VesLength'].value;

      diamunits    = document.forms['Volume'].elements['DiamUnits'].value;
      lenunits     = document.forms['Volume'].elements['LenUnits'].value;
      volunits     = document.forms['Volume'].elements['VolUnits'].value;

      volume       = eval((veslength * lenunits) * (Math.PI * Math.pow(eval((vesdiameter * diamunits) / 2) ,2)));

      document.forms['Volume'].elements['VesVolume'].value = Math.round(Math.abs(volume * volunits)*10) / 10;
    }
    else
    {
      vesdiameter  = eval(document.all.Volume.VesDiameter.value);
      veslength    = eval(document.all.Volume.VesLength.value);

      diamunits    = eval(document.all.Volume.DiamUnits.value);
      lenunits     = eval(document.all.Volume.LenUnits.value);
      volunits     = eval(document.all.Volume.VolUnits.value);

      volume       = eval((veslength * lenunits) * (Math.PI * Math.pow(eval((vesdiameter * diamunits) / 2), 2)));

      document.all.Volume.VesVolume.value = Math.round(Math.abs(volume * volunits) * 10) / 10;
    }
  }
};


function CalcPressureA() // OK
{
  if(parseInt(navigator.appVersion) >= 4)
  {
    var A;
    var Aunits;
    var Bunits;

    if(navigator.appName == 'Netscape')
    {
      A       = document.forms['Pressure'].elements['PressureA'].value * 1;
      Aunits  = document.forms['Pressure'].elements['AUnits'].value * 1;
      Bunits  = document.forms['Pressure'].elements['BUnits'].value * 1;

      document.forms['Pressure'].elements['PressureB'].value = Math.round(Math.abs((A / Aunits) * Bunits) * 10) / 10;
    }
    else
    {
      A       = document.all.Pressure.PressureA.value * 1;
      Aunits  = document.all.Pressure.AUnits.value * 1;
      Bunits  = document.all.Pressure.BUnits.value * 1;

      document.all.Pressure.PressureB.value = Math.round(Math.abs((A / Aunits) * Bunits) * 10) / 10;
    }
  }
};


function CalcPressureB() // OK
{
  if(parseInt(navigator.appVersion) >= 4)
  {
    var B;
    var Aunits;
    var Bunits;

    if(navigator.appName == 'Netscape')
    {
      B       = document.forms['Pressure'].elements['PressureB'].value * 1;
      Aunits  = document.forms['Pressure'].elements['AUnits'].value * 1;
      Bunits  = document.forms['Pressure'].elements['BUnits'].value * 1;

      document.forms['Pressure'].elements['PressureA'].value = Math.round(Math.abs((B / Bunits) * Aunits) * 10) / 10;
    }
    else
    {
      B       = document.all.Pressure.PressureB.value * 1;
      Aunits  = document.all.Pressure.AUnits.value * 1;
      Bunits  = document.all.Pressure.BUnits.value * 1;

      document.all.Pressure.PressureA.value = Math.round(Math.abs((B / Bunits) * Aunits) * 10) / 10;
    }
  }
};


function CalcCapacity() // OK
{
  if(parseInt(navigator.appVersion) >= 4)
  {
    var RUnits;
    var LUnits;
    var HUnits;

    var radius;
    var length;
    var height;
    var volunits;
    var capacity;

    if(navigator.appName == 'Netscape')
    {
      RUnits    = Math.abs(document.forms['Capacity'].elements['RUnits'].value);
      LUnits    = Math.abs(document.forms['Capacity'].elements['LUnits'].value);
      HUnits    = Math.abs(document.forms['Capacity'].elements['HUnits'].value);

      radius    = Math.abs(document.forms['Capacity'].elements['VesRadius'].value * RUnits);
      length    = Math.abs(document.forms['Capacity'].elements['VesLength'].value * LUnits);
      height    = Math.abs(document.forms['Capacity'].elements['LiqHeight'].value * HUnits);
      volunits  = Math.abs(document.forms['Capacity'].elements['VUnits'].value);
    }
    else
    {
      RUnits    = Math.abs(document.all.Capacity.RUnits.value);
      LUnits    = Math.abs(document.all.Capacity.LUnits.value);
      HUnits    = Math.abs(document.all.Capacity.HUnits.value);

      radius    = Math.abs(document.all.Capacity.VesRadius.value * RUnits);
      length    = Math.abs(document.all.Capacity.VesLength.value * LUnits);
      height    = Math.abs(document.all.Capacity.LiqHeight.value * HUnits);
      volunits  = Math.abs(document.all.Capacity.VUnits.value);
    }

    if(height > (radius * 2))
    {
      if(navigator.appName == 'Netscape')
      {
        height    = 2 * (document.forms['Capacity'].elements['VesRadius'].value / HUnits * RUnits);
        document.forms['Capacity'].elements['LiqHeight'].value = Math.round(height * 10) / 10;
        height    = height * HUnits;
        capacity  = length * (Math.pow(radius, 2) * Math.PI);
      }
      else
      {
        height    = 2 * (document.all.Capacity.VesRadius.value / HUnits * RUnits);
        document.all.Capacity.LiqHeight.value = Math.round(height * 10) / 10;
        height    = height * HUnits;
        capacity  = length * (Math.pow(radius, 2) * Math.PI);
      }
    }
    else if(height == (2 * radius))
    {
      capacity = length * (Math.pow(radius, 2) * Math.PI);
    }
    else if(height > radius)
    {
      capacity = length * (Math.pow(radius, 2) * Math.PI) - (length * Math.pow(radius, 2) / 2 * (2 * Math.acos((radius - ((2 * radius) - height)) / radius) - Math.sin(2 * Math.acos((radius - ((2 * radius) - height)) / radius))));
    }
    else if(height == radius)
    {
      capacity = length * (Math.pow(radius, 2) * Math.PI) / 2;
    }
    else if(height < radius)
    {
      capacity = length * Math.pow(radius, 2) / 2 * (2 * Math.acos((radius - height) / radius) - Math.sin(2 * Math.acos((radius - height) / radius)));
    }

    if(navigator.appName == 'Netscape')
    {
      document.forms['Capacity'].elements['VesVolume'].value = Math.round(capacity * volunits * 10) / 10;
    }
    else
    {
      document.all.Capacity.VesVolume.value = Math.round(capacity * volunits * 10) / 10;
    }
  }
};


function CalcTempA() // OK
{
  if(parseInt(navigator.appVersion) >= 4)
  {
    var A;
    var B;
    var AU;
    var BU;

    if(navigator.appName == 'Netscape')
    {

      A   = document.forms['Temperature'].elements['TempA'].value;
      B   = document.forms['Temperature'].elements['TempB'].value;
      AU  = document.forms['Temperature'].elements['AUnits'].value;
      BU  = document.forms['Temperature'].elements['BUnits'].value;

      if(AU == 'F' && BU == 'F')
      {
        document.forms['Temperature'].elements['TempB'].value = A;
      }
      if(AU == 'F' && BU == 'C')
      {
        document.forms['Temperature'].elements['TempB'].value = Math.round((eval(A - 32)  * .555555) * 100) / 100;
      }
      if(AU == 'F' && BU == 'K')
      {
        document.forms['Temperature'].elements['TempB'].value = Math.round((eval(A - 32)  * .555555 + 273.15) * 100) / 100;
      }
      if(AU == 'C' && BU == 'F')
      {
        F = (A * 1.8) + 32;
        document.forms['Temperature'].elements['TempB'].value = Math.round(F * 100) / 100;
      }
      if(AU == 'C' && BU == 'C')
      {
        document.forms['Temperature'].elements['TempB'].value = A;
      }
      if(AU == 'C' && BU == 'K')
      {
        document.forms['Temperature'].elements['TempB'].value = Math.round((A * 1 + 273.15) * 100) / 100;
      }
      if(AU == 'K' && BU == 'F')
      {
        C = A * 1 + 273.15;
        F = (C * 9 / 5) + 32;
        document.forms['Temperature'].elements['TempB'].value = Math.round(F * 100) / 100;
      }
      if(AU == 'K' && BU == 'C')
      {
        C = A * 1 + 273.15;
        document.forms['Temperature'].elements['TempB'].value = Math.round(C * 100) / 100;
      }
      if(AU == 'K' && BU == 'K')
      {
        document.forms['Temperature'].elements['TempB'].value = A;
      }
    }
    else
    {
      A   = document.all.Temperature.TempA.value;
      B   = document.all.Temperature.TempB.value;
      BU  = document.all.Temperature.BUnits.value;
      AU  = document.all.Temperature.AUnits.value;

      if(AU == 'F' && BU == 'F')
      {
        document.all.Temperature.TempB.value = A;
      }
      if(AU == 'F' && BU == 'C')
      {
        document.all.Temperature.TempB.value = Math.round((eval(A - 32)  * .555555) * 100) / 100;
      }
      if(AU == 'F' && BU == 'K')
      {
        document.all.Temperature.TempB.value = Math.round((eval(A - 32)  * .555555 + 273.15) * 100) / 100;
      }
      if(AU == 'C' && BU == 'F')
      {
        F = (A * 1.8) + 32;
        document.all.Temperature.TempB.value = Math.round(F * 100) / 100;
      }
      if(AU == 'C' && BU == 'C')
      {
        document.all.Temperature.TempB.value = A;
      }
      if(AU == 'C' && BU == 'K')
      {
        document.all.Temperature.TempB.value = Math.round((A * 1 + 273.15) * 100) / 100;
      }
      if(AU == 'K' && BU == 'F')
      {
        C = A * 1 + 273.15;
        F = (C * 9 / 5) + 32;
        document.all.Temperature.TempB.value = Math.round(F * 100) / 100;
      }
      if(AU == 'K' && BU == 'C')
      {
        C = A * 1 + 273.15;
        document.all.Temperature.TempB.value = Math.round(C * 100) / 100;
      }
      if(AU == 'K' && BU == 'K')
      {
        document.all.Temperature.TempB.value = A;
      }
    }
  }
};


function CalcTempB() // OK
{
  if(parseInt(navigator.appVersion) >= 4)
  {
    var A;
    var B;
    var AU;
    var BU; 

    if(navigator.appName == 'Netscape')
    {
      A   = document.forms['Temperature'].elements['TempA'].value;
      B   = document.forms['Temperature'].elements['TempB'].value;
      AU  = document.forms['Temperature'].elements['AUnits'].value;
      BU  = document.forms['Temperature'].elements['BUnits'].value;

      if(BU == 'F' && AU == 'F')
      {
        document.forms['Temperature'].elements['TempA'].value = B;
      }
      if(BU == 'F' && AU == 'C')
      {
        var f = (B - 32) * 5 / 9;
        document.forms['Temperature'].elements['TempA'].value = Math.round(f * 100) / 100;
      }
      if(BU == 'F' && AU == 'K')
      {
        document.forms['Temperature'].elements['TempA'].value = Math.round(eval((B * 1 - 32)  * .555555 + 273.15) * 100) / 100;
      }
      if(BU == 'C' && AU == 'F')
      {
        document.forms['Temperature'].elements['TempA'].value = Math.round(eval((B * 1.8 + 32) * 100)) / 100;
      }
      if(BU == 'C' && AU == 'C')
      {
        document.forms['Temperature'].elements['TempA'].value = B;
      }
      if(BU == 'C' && AU == 'K')
      {
        document.forms['Temperature'].elements['TempA'].value = Math.round(eval(B * 1 + 273.15) * 100) / 100;
      }
      if(BU == 'K' && AU == 'F')
      {
        var c = (B * 1 - 273.15);
        var f = (c * 1.8) + 32;
        document.forms['Temperature'].elements['TempA'].value = Math.round(f * 100) / 100;
      }
      if(BU == 'K' && AU == 'C')
      {
        document.forms['Temperature'].elements['TempA'].value = Math.round(eval(B * 1 - 273.15) * 100) / 100;
      }
      if(BU == 'K' && AU == 'K')
      {
        document.forms['Temperature'].elements['TempA'].value = B;
      }
    }
    else
    {
      A   = document.all.Temperature.TempA.value;
      B   = document.all.Temperature.TempB.value;
      AU  = document.all.Temperature.AUnits.value;
      BU  = document.all.Temperature.BUnits.value;

      if(BU == 'F' && AU == 'F')
      {
        document.all.Temperature.TempA.value = B;
      }
      if(BU == 'F' && AU == 'C')
      {
        var f = (B - 32) * 5 / 9;
        document.all.Temperature.TempA.value = Math.round(f * 100) / 100;
      }
      if(BU == 'F' && AU == 'K')
      {
        document.all.Temperature.TempA.value = Math.round(eval((B * 1 - 32)  * .555555 + 273.15) * 100) / 100;
      }
      if(BU == 'C' && AU == 'F')
      {
        document.all.Temperature.TempA.value = Math.round(eval((B * 1.8 + 32) * 100)) / 100;
      }
      if(BU == 'C' && AU == 'C')
      {
        document.all.Temperature.TempA.value = B;
      }
      if(BU == 'C' && AU == 'K')
      {
        document.all.Temperature.TempA.value = Math.round(eval(B * 1 + 273.15) * 100) / 100;
      }
      if(BU == 'K' && AU == 'F')
      {
        var c = (B * 1 - 273.15);
        var f = (c * 1.8) + 32;
        document.all.Temperature.TempA.value = Math.round(f * 100) / 100;
      }
      if(BU == 'K' && AU == 'C')
      {
        document.all.Temperature.TempA.value = Math.round(eval(B * 1 - 273.15) * 100) / 100;
      }
      if(BU == 'K' && AU == 'K')
      {
        document.all.Temperature.TempA.value = B;
      }
    }
  }
};


function CalcFlowA() // OK
{
  if(parseInt(navigator.appVersion) >= 4)
  {
    var A;
    var AU;
    var BU;

    if(navigator.appName == 'Netscape')
    {
      A   = document.forms['Flow'].elements['FlowA'].value;
      AU  = document.forms['Flow'].elements['AUnits'].value;
      BU  = document.forms['Flow'].elements['BUnits'].value;

      document.forms['Flow'].elements['FlowB'].value = Math.round((A/AU) * BU * 100) / 100;
    }
    else
    {
      A   = document.all.Flow.FlowA.value;
      AU  = document.all.Flow.AUnits.value;
      BU  = document.all.Flow.BUnits.value;

      document.all.Flow.FlowB.value = Math.round((A/AU) * BU * 100) / 100;
    }
  }
};


function CalcFlowB() // OK
{
  if(parseInt(navigator.appVersion) >= 4)
  {
    var B;
    var AU;
    var BU;

    if(navigator.appName == 'Netscape')
    {
      B   = document.forms['Flow'].elements['FlowB'].value;
      AU  = document.forms['Flow'].elements['AUnits'].value;
      BU  = document.forms['Flow'].elements['BUnits'].value;

      document.forms['Flow'].elements['FlowA'].value = Math.round((B / BU) * AU * 100) / 100;
    }
    else
    {
      B   = document.all.Flow.FlowB.value;
      AU  = document.all.Flow.AUnits.value;
      BU  = document.all.Flow.BUnits.value;

      document.all.Flow.FlowA.value = Math.round((B / BU) * AU * 100) / 100;
    }
  }
};


function SubmitQuote() // NOT TESTED
{
  if(navigator.appName == 'Netscape')
  {
    var QuoteForm = document.forms['Quote'];
  }
  else
  {
    var QuoteForm  = document.all.Quote;
    var sName      = QuoteForm.RequestedBy.value;
    var sCompany   = QuoteForm.Company.value;
    var sAddress   = QuoteForm.Address.value;
    var sCity      = QuoteForm.City.value;
    var sZip       = QuoteForm.Zip.value;
    var sPhone     = QuoteForm.Phone.value;
    var sDim       = QuoteForm.Dimensions.value;
    var sMatl      = QuoteForm.Material.value;

  }

  var missingdata = false;
  var errortext   = 'The following fields require data: \r\r';

  if(!sName)
  {
    missingdata = true;
    errortext  += 'Name \r';
  }
  if(!sCompany)
  {
    missingdata = true;
    errortext  += 'Company \r';
  }
  if(!sAddress)
  {
    missingdata = true;
    errortext  += 'Address \r';
  }
  if(!sCity)
  {
    missingdata = true;
    errortext  += 'City \r';
  }
  if(!sZip)
  {
    missingdata = true;
    errortext  += 'Zip \r';
  }
  if(!sPhone)
  {
    missingdata = true;
    errortext  += 'Phone \r';
  }
  if(!sDim)
  {
    missingdata = true;
    errortext  += 'Dimensions \r';
  }
  if(!sMatl)
  {
    missingdata = true;
    errortext  += 'Matl of Construction \r';
  }
  if(missingdata)
  {
    alert(errortext);
    return false;
  }
  else
  {
    var listvalue = QuoteForm.Industry.options[QuoteForm.Industry.selectedIndex].value;

    switch(listvalue)
    {
      case 'Industrial':
        QuoteForm.action = 'mailto:industrial@bendelcorp.com?Request for Quotation';
        break;
      case 'Petroleum':
        QuoteForm.action = 'mailto:petroleum@bendelcorp.com?Request for Quotation';
        break;
    }
    QuoteForm.submit();
    alert('Your request has been sent to' + QuoteForm.action);
  }
};


function SubmitInfoRequest() // NOT TESTED
{
};


function MakeTen(s) // NOT TESTED
{
  var n     = '';
  var strng = stripCharsInBag(s, '()[]/\.-extEXT ');

  if(strng.length < 10 && !strng == '')
  {
    alert('Phone number must have 10 digits. Please include your area code.');
  }
  else
  {
    for (i = 0; i < 10; i++)
    {
      n += strng.charAt(i);
    }
    return n;
  }
};


function stripCharsInBag(s, bag) // NOT TESTED
{
  var i;
  var returnString = '';

  for (i = 0; i < s.length; i++)
  {
    var c = s.charAt(i);
    if(bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
};


function FormatPhone(s) // NOT TESTED
{
  var p = '';
  var a = '(';
  var e = ')';
  var n = '-';

  if(s.length == 10)
  {
    for (i = 0; i < 10; i++)
    {
    if(i < 3)          {a += s.charAt(i);}
    if(i > 2 && i <6)  {e += s.charAt(i);}
    if(i > 5)          {n += s.charAt(i);}
  }

  p = a + e + n;
  return p;
  }
  else
  {
    return '';
  }
};


function Showlocator(loc) // NOT TESTED
{
  var locator;
  loc = eval(loc);

  if(loc > 7 && loc <13)
  {
    locator = document.all.locator2;
  }
  else
  {
    locator = document.all.locator1;
  }

  var map;
  map = document.all.map1;

  if(loc == 1)
  {
    locator.style.left = 178;
    locator.style.top  = 100;
  }
  if(loc == 2)
  {
    locator.style.left = 170;
    locator.style.top  = 128;
  }
  if(loc == 3)
  {
    locator.style.left = 148;
    locator.style.top  = 168;
  }
  if(loc == 4)
  {
    locator.style.left = 171;
    locator.style.top  = 119;
  }
  if(loc == 5)
  {
    locator.style.left = 178;
    locator.style.top  = 92;
  }
  if(loc == 6)
  {
    locator.style.left = 175;
    locator.style.top  = 99;
  }
  if(loc == 7)
  {
    locator.style.left = 175;
    locator.style.top  = 107;
  }
  if(loc == 8)
  {
    locator.style.left = 15;
    locator.style.top  = 120;
  }
  if(loc == 9)
  {
    locator.style.left = 34;
    locator.style.top  = 126;
  }
  if(loc == 10)
  {
    locator.style.left = 78;
    locator.style.top  = 198;
  }
  if(loc == 11)
  {
    locator.style.left = 30;
    locator.style.top  = 190;
  }
  if(loc == 12)
  {
    locator.style.left = 9;
    locator.style.top  = 119;
  }
  if(loc == 13)
  {
    locator.style.left = 52;
    locator.style.top  = 18;
  }
  if(loc == 14)
  {
    locator.style.left = 52;
    locator.style.top  = 13;
  }

   locator.style.visibility = 'visible';
   return true;
};


function Hidelocator() // NOT TESTED
{
  document.all.locator1.style.visibility = 'hidden';
  document.all.locator2.style.visibility = 'hidden';
};

/*       Horizontal Capacity
--------------------------------------------------------------*/
var VolumeParameters = null;
function ShowChartCapacityHorizontal()
{
  // Get amounts in inches, therefore * 12
  var length = Number($("#capLength").val()) * Math.roundTo($("#capLengthUnits").val() * 12, 4);
  var diameter = Number($("#capDiameter").val()) * Math.roundTo($("#capDiameterUnits").val() * 12, 4);
  diameter = Math.ceil(diameter); // since we are doing whole inch increments
  var volumeFactor = Number($("#capVolumeUnits").val()) / 1728.0; // in3 * 1/1728 ft3/in3

	VolumeParameters = { length: length, diameter: diameter, lengthDisplay: $("#capLength").val(), lengthLabel: $("#capLengthUnits :selected").text(),
    volumeFactor: volumeFactor, volumeLabel: $("#capVolumeUnits :selected").text() };
  CapacityWindow = window.open('CapacityChart.html', 'CapacityWindow');
}

function GenerateChart(length_in, diameter_in, lengthDisplay, lengthLabel, volumeFactor, volumeLabel)
{
	UpdateChartLabels(lengthDisplay, lengthLabel);
	
	var dimensions = { length: length_in, radius: diameter_in/2, height:1, VolumeFactor: volumeFactor, complete:false }
	
	var firstPageEntries = 50;
	var subsequentPageEntries = 50;
	
	// First Columns
	var results1 = GenerateNumberPairs(firstPageEntries, dimensions);
	var results2 = GenerateNumberPairs(firstPageEntries, dimensions);
	var results3 = GenerateNumberPairs(firstPageEntries, dimensions);
	var results4 = GenerateNumberPairs(firstPageEntries, dimensions);
	var tbl = GenerateTable(volumeLabel, '-on', results1, results2, results3, results4);
	$(".resultsarea").append(tbl);
	
	var generateRecursive = function (dim, OnCompleted)
	{
	  if (dim.complete) { OnCompleted(); return; }

	  var results1 = GenerateNumberPairs(subsequentPageEntries, dimensions);
	  var results2 = GenerateNumberPairs(subsequentPageEntries, dimensions);
	  var results3 = GenerateNumberPairs(subsequentPageEntries, dimensions);
	  var results4 = GenerateNumberPairs(subsequentPageEntries, dimensions);
	  var tbl = GenerateTable(volumeLabel, '-off', results1, results2, results3, results4);
	  $(".resultsarea").append("<br class='pagebreak' />").append(tbl);

	  setTimeout(function () { generateRecursive(dim, OnCompleted); }, 5);
	}
	generateRecursive(dimensions, RefreshNavigationLinks);
}

function FormatNumber(n)
{
	var s = (n-0).toFixed(0);
	if(s.length < 5) { return s; }
	
	var re = /(\d+)(\d{3})/;
	while (re.test(s))
	{
		s = s.replace(re, '$1' + ',' + '$2');
	}
	return s;
}

function GenerateNumberPairs(iterations, dimensions)
{
	var length = dimensions.length, 
		radius = dimensions.radius;
		
	var col = [];

	var increment = height;
	var startHeight = dimensions.height;
	for(i = dimensions.height; i <= (2 * radius) && i < (startHeight + iterations); i++)
	{
		var height = i, capacity;
	
		if(height > (radius * 2)) // this could never happen, right?
		{
			height   = 2 * (radius);
			height   = Math.round(height * 10) / 10;
			capacity = length * (Math.pow(radius, 2) * Math.PI);
		}
		if(height == (2 * radius)) // this is always going to get overwritten by the next condition, correct?
		{
			capacity = length * (Math.pow(radius, 2) * Math.PI);
		}
		if(height > radius)
		{
			capacity = length * (Math.pow(radius, 2) * Math.PI) - (length * Math.pow(radius, 2) / 2 * (2 * Math.acos((radius - ((2 * radius) - height))/radius) - Math.sin(2 * Math.acos((radius - ((2 * radius) - height))/radius))));
		}
		if(height == radius)
		{
			capacity = length * (Math.pow(radius, 2) * Math.PI) / 2;
		}
		if(height < radius)
		{
			capacity = length * Math.pow(radius, 2) / 2 * (2 * Math.acos((radius - height)/radius) - Math.sin(2 * Math.acos((radius - height)/radius)));
		}
	
		//...Convert capacity into selected units
    capacity = dimensions.VolumeFactor * capacity;
			
		//var record = $('<dl><dt>' + FormatNumber(height) + '</dt><dd>' + FormatNumber(capacity) + '</dd></dl>');
		//col.append(record);
		col.push({ height: FormatNumber(height), capacity: FormatNumber(capacity) });
	}
	dimensions.height = i;
	dimensions.complete = i > (2 * radius);
	return col;
}

function GenerateTable(capacityLabel, cssModifier)
{
  var makeColumn = function (results)
  {
    var useSmallCapacityLabel = capacityLabel.length >= 10;
    var capacityLabelHtml = "Volume<br />(" + capacityLabel + ")";
    if (capacityLabel.length >= 10)
    {
      capacityLabelHtml = capacityLabel.replace(/ /, "<br />");
    }
    if (!results.length) { return $("<div class='col-container'></div>"); }
    var rows = ["<dl class='col-header'><dt>Height<br />(in)</dt><dd" + (useSmallCapacityLabel ? " class='small'" : "") + ">" + capacityLabelHtml + "</dd></dl>"];
    for (var i = 0; i < results.length; i++)
    {
      rows.push("<dl" + (i % 2 == 0 ? " class='alt-row'" : "") + "><dt>" + results[i].height + "</dt><dd>" + results[i].capacity + "</dd></dl>");
    }

    return $("<div class='col-container'>" + rows.join('') + "</div>");
  };

  // Build the CSS columns
	var ret = $('<div class="page page' + cssModifier + '"></div>');
	for (var i = 2; i < arguments.length; i++)
	{
	  if (arguments[i] == null) { continue; }
	  var col = makeColumn(arguments[i]);
    if(i+1 === arguments.length)
    { col.addClass("last"); }
    ret.append(col);
	}

	return ret;
}

// Startup & Binding
$(function ()
{
  // Calculator form input bindings
  $("#capLength").add("#capDiameter").keyup(CalcVolumeHorizontal);
  $("#capVolume").blur(CalcVolumeHorizontal);
  $("#capLengthUnits").add("#capDiameterUnits").add("#capVolumeUnits").change(CalcVolumeHorizontal);
  $("#btnCalcVolumeH").click(ShowChartCapacityHorizontal);

  // Print page binding, only on the print page
  $("#navnext").click(function ()
  {
    $(".page-on, .page-on + br + .page-off").toggleClass("page-on page-off");
    RefreshNavigationLinks();
    return false;
  });

  $("#navprev").click(function ()
  {
    var currPage = $(".page-on");
    currPage.toggleClass("page-on page-off");
    currPage.prevAll(".page-off:first").toggleClass("page-on page-off");
    RefreshNavigationLinks();
    return false;
  });

  $("#navprint").click(function () { window.print(); return false; });

  // If we are the child window
  if (opener && opener.VolumeParameters)
  {
    GenerateChart(opener.VolumeParameters.length,
      opener.VolumeParameters.diameter,
      opener.VolumeParameters.lengthDisplay,
      opener.VolumeParameters.lengthLabel,
      opener.VolumeParameters.volumeFactor,
      opener.VolumeParameters.volumeLabel);
  }

});

// Print window functions
function RefreshNavigationLinks()
{
  var nextPageAvailable = $(".page-on + br + .page-off").length > 0;
  var prevPageAvailable = $(".page-on").prevAll(".page-off:first").length > 0;
  $("#navnextcontainer").css("visibility", nextPageAvailable ? "visible" : "hidden");
  $("#navprevcontainer").css("visibility", prevPageAvailable ? "visible" : "hidden");
}

function UpdateChartLabels(length, lengthLabel)
{
  var lengthText = length + ' ' + lengthLabel;
  $('#vLength').text(lengthText);
}




/*       Horizontal Volume
--------------------------------------------------------------*/
Math.roundTo = function (d, places)
{
  return Math.round(d * Math.pow(10, places)) / Math.pow(10, places);
};

// Calculation Functions
function CalcVolumeHorizontal()
{
  var diameter = Number($("#capDiameter").val());
  var diameterUnitMultiplier = Number($("#capDiameterUnits").val());
  var length = Number($("#capLength").val());
  var lengthUnitMultiplier = Number($("#capLengthUnits").val());
  var volumeUnitMultiplier = Number($("#capVolumeUnits").val());

  var volume = GetVolume(length * lengthUnitMultiplier, diameter * diameterUnitMultiplier);
  volume = Math.roundTo(volume * volumeUnitMultiplier, 2); // round to 2 digits

  $("#capVolume").val(volume);
}

function GetVolume(length, diameter)
{
  return length * Math.PI * Math.pow(diameter / 2, 2);
}

