// constants
var GB_METER_WIDTH = 4.93;
var GB_METER_HEIGHT = 15;
var GB_FEET_WIDTH = 1.5;
var GB_FEET_HEIGHT = 4.57;



/*
 * Square Coverage.
 */
function areaToTiles(width, length) {
  if(width <= 0 || length <= 0) {
    alert("Please enter the area dimensions.");
    return;
  }

  if(isNaN(width) || isNaN(length)) {
    alert("Invalid input. Please try again.");
    return;
  }
  
  // actual area desired
  var actArea = Math.round(width * length * 100) / 100; // round to hundredths
  var units = document.areaForm.units.value;
  var rnd = document.areaForm.rnd.value; // round up or down
  //var type = document.areaForm.type.value; // tile type
  var type = "GB";
  document.areaForm.actArea.value = "Desired Coverage: " + actArea + " sq. " + units;
  var numTilesWidth = null; // number of tiles across the width
  var numTilesLength = null; // number of tiles along the length
  var area = null; // area the tiles will cover

  if (units == "meters") {
    if (type == "GB") {
      if (rnd == "up") {
        numTilesWidth = Math.ceil(width * GB_METER_WIDTH);
        numTilesLength = Math.ceil(length * GB_METER_HEIGHT);
      } else { // round down
        numTilesWidth = Math.floor(width * GB_METER_WIDTH);
        numTilesLength = Math.floor(length * GB_METER_HEIGHT);
      }
    } else { // tile type is gridmat
      // !!!
    }
    // special case round flooring to zero
    if (numTilesWidth == 0) numTilesWidth++;
    if (numTilesLength == 0) numTilesLength++;
    area = Math.round(((numTilesWidth * numTilesLength * 2.625 * 8) / 1550) * 100) / 100; // round to hundredths
  } else { // units are feet
    if (type == "GB") {
      if (rnd == "up") {
        numTilesWidth = Math.ceil(width * GB_FEET_WIDTH);
        numTilesLength = Math.ceil(length * GB_FEET_HEIGHT);
      } else { // round down
        numTilesWidth = Math.floor(width * GB_FEET_WIDTH);
        numTilesLength = Math.floor(length * GB_FEET_HEIGHT);
      }
    } else { // tile type is gridmat
      // !!!
    }
    // special case round flooring to zero
    if (numTilesWidth == 0) numTilesWidth++;
    if (numTilesLength == 0) numTilesLength++;
    area = Math.round(((numTilesWidth * numTilesLength * 2.625 * 8) / 144) * 100) / 100; // round to hundredths
	
  }
  var numTiles = numTilesWidth * numTilesLength;
  document.areaForm.area.value = "Actual Coverage: " + area + " sq. " + units;
  document.areaForm.tiles.value = "Pieces: " + numTiles + " Brick (" + numTilesWidth + " Across, " + numTilesLength + " High)";
  document.areaForm.boxes.value = "Boxes: " + Math.ceil((numTiles / 63) * 1) / 1 + " Boxes @ 63 Pieces/Box ";
}

var CORNER_METER_WIDTH = 2.625;
var CORNER_FEET_WIDTH = 2.625;

/*
 * Converts area to tiles.
 */
function LengthToTiles(width, length) {
  if(width <= 0) {
    alert("Please enter the area dimensions.");
    return;
  }

  if(isNaN(width)) {
    alert("Invalid input. Please try again.");
    return;
  }
  
  // actual area desired
  var actArea = Math.round(width * 100) / 100; // round to hundredths
  var units = document.lengthForm.units.value;
  var rnd = document.lengthForm.rnd.value; // round up or down
  //var type = document.lengthForm.type.value; // tile type
  var type = "brick";
  document.lengthForm.actArea.value = "Desired Length: " + actArea + " ln. " + units;
  var numTilesWidth = null; // number of tiles across the width

  var area = null; // area the tiles will cover

  if (units == "meters") {
    if (type == "brick") {
      if (rnd == "up") {
        numTilesWidth = Math.ceil((width * 39.36) / CORNER_METER_WIDTH);

      } else { // round down
        numTilesWidth = Math.floor((width * 39.36) / CORNER_METER_WIDTH);

      }
    } else { // tile type is gridmat
      // !!!
    }
    // special case round flooring to zero
    if (numTilesWidth == 0) numTilesWidth++;

    area = Math.round(((numTilesWidth * CORNER_FEET_WIDTH) / 39.36) * 100) / 100; // round to hundredths
  } else { // units are feet
    if (type == "brick") {
      if (rnd == "up") {
        numTilesWidth = Math.ceil((width * 12) / CORNER_FEET_WIDTH);

      } else { // round down
        numTilesWidth = Math.floor((width * 12) / CORNER_FEET_WIDTH);

      }
    } else { // tile type is gridmat
      // !!!
    }
    // special case round flooring to zero
    if (numTilesWidth == 0) numTilesWidth++;

    area = Math.round(((numTilesWidth * CORNER_FEET_WIDTH) / 12) * 100) / 100; // round to hundredths
  }
  var numTiles = numTilesWidth;
  document.lengthForm.area.value = "Actual Length: " + area + " ln. " + units;
  document.lengthForm.tiles.value = "Pieces: " + numTiles + " Brick Corners ";
  document.lengthForm.boxes.value = "Boxes: " + Math.ceil((numTiles / 32) * 1) / 1 + " Boxes @ 32 Pieces/Box ";
}
