// constants
var KIWI_METER_WIDTH = (1 / 3);
var KIWI_FEET_WIDTH = 1.0936;
var TILE_PREVIEW_GRID_LENGTH = 20;

/*
 * Converts area to tiles.
 */
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 = "kiwi";
  document.areaForm.actArea.value = "Area you'd like to cover: " + 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 == "kiwi") {
      if (rnd == "up") {
        numTilesWidth = Math.ceil(width / KIWI_METER_WIDTH);
        numTilesLength = Math.ceil(length / KIWI_METER_WIDTH);
      } else { // round down
        numTilesWidth = Math.floor(width / KIWI_METER_WIDTH);
        numTilesLength = Math.floor(length / KIWI_METER_WIDTH);
      }
    } else { // tile type is gridmat
      // !!!
    }
    // special case round flooring to zero
    if (numTilesWidth == 0) numTilesWidth++;
    if (numTilesLength == 0) numTilesLength++;
    area = Math.round(numTilesWidth * numTilesLength * KIWI_METER_WIDTH * KIWI_METER_WIDTH * 100) / 100; // round to hundredths
  } else { // units are feet
    if (type == "kiwi") {
      if (rnd == "up") {
        numTilesWidth = Math.ceil(width / KIWI_FEET_WIDTH);
        numTilesLength = Math.ceil(length / KIWI_FEET_WIDTH);
      } else { // round down
        numTilesWidth = Math.floor(width / KIWI_FEET_WIDTH);
        numTilesLength = Math.floor(length / KIWI_FEET_WIDTH);
      }
    } else { // tile type is gridmat
      // !!!
    }
    // special case round flooring to zero
    if (numTilesWidth == 0) numTilesWidth++;
    if (numTilesLength == 0) numTilesLength++;
    area = Math.round(numTilesWidth * numTilesLength * KIWI_FEET_WIDTH * KIWI_FEET_WIDTH * 100) / 100; // round to hundredths
  }
  var numTiles = numTilesWidth * numTilesLength;
  document.areaForm.area.value = "Area covered by tiles: " + area + " sq. " + units;
  document.areaForm.tiles.value = "Number of tiles: " + numTiles + " total tiles (" + numTilesWidth + " across the width, " + numTilesLength + " along the length)";
}

/*
 * Converts tiles to area.
 */
function tilesToArea(width, length) {
  if(width <= 0 || length <= 0) {
    alert("Please enter the number of tiles.");
    return;
  }

  if(isNaN(width) || isNaN(length)) {
    alert("Invalid input. Please try again.");
    return;
  }
  
  var width = Math.round(width); // round to nearest tile
  var length = Math.round(length); // round to nearest tile
  var units = document.tileForm.units.value;
  var area = null;

  if (units == "meters") {
    area = Math.round(width * length * KIWI_METER_WIDTH * KIWI_METER_WIDTH * 100) / 100; // round to hundredths
  } else { // units are feet
    area = Math.round(width * length * KIWI_FEET_WIDTH * KIWI_FEET_WIDTH * 100) / 100; // round to hundredths
  }
  document.tileForm.result.value = "Area covered by tiles: " + area + " sq. " + units;
}
