﻿function createWallTileCalculator(addHeading) {
  try {
    document.write('<form class="extcalc" name="frmWallTileCalc" onsubmit="return calculateWallTiles(this);"><table border="0" cellpadding="0" cellspacing="0">');
    if (addHeading) document.write('<tr><td colspan="2"><h4>Wall Tile Calculator</h4></td></tr>');
    document.write('<tr><td colspan="2">Tile Dimensions (in centimetres)</td>');
    document.write('<tr><td>&nbsp; &nbsp; &nbsp; &nbsp;Tile Width</td><td><input type="text" size="5" maxlength="5" name="calcTileWidth" style="width:30px;" title="Tile Width in millimetres" onchange="clearWallTileResults();" /> <b>cm</b>');
    document.write('<tr><td>&nbsp; &nbsp; &nbsp; &nbsp;Tile Height</td><td><input type="text" size="5" maxlength="5" name="calcTileHeight" style="width:30px;" title="Tile Height in millimetres" onchange="clearWallTileResults();" /> <b>cm</b></td></tr>');
    document.write('<tr><td colspan="2">Room Dimensions (in metres)</td>');
    document.write('<tr><td>&nbsp; &nbsp; &nbsp; &nbsp;Room Width</td><td><input type="text" size="5" maxlength="5" name="calcTileRoomWidth" style="width:30px;" title="Room Width in metres" onchange="clearWallTileResults();" /> <b>m</b></td></tr>');
    document.write('<tr><td>&nbsp; &nbsp; &nbsp; &nbsp;Room Height</td><td><input type="text" size="5" maxlength="5" name="calcTileRoomHeight" style="width:30px;" title="Room Height in metres" onchange="clearWallTileResults();" /> <b>m</b></td></tr>');
    document.write('</table>');
    document.write('<div style="text-align:center;" id="calcWallTileResult"></div>');
    document.write('<p style="text-align:center;"><input type="submit" name="go" value="Calculate Tiles" /></p>');
    document.write('<p id="calcWallTileNote" style="display:none;"><small>Always remember, it is a really good idea to buy extra tiles to cover any breakage whilst tiling and spares in the event of future need. For example, I have recently tiled my bathroom and have now discovered that the recessed shower valve is faulty which means it will have to be replaced. Where is the valve you may ask? It\'s recessed in the tile covered wall!<br />Orchard Planning Solutions is not responsible for any disparity between the estimated cost and the actual cost paid by you.</small></p>');
    document.write('</form>');
  } catch(e) {}
}
function clearWallTileResults() {
  try {
    var elRes = document.getElementById("calcWallTileResult");
    var elNote = document.getElementById("calcWallTileNote");
    elRes.innerHTML=''; elNote.style.display='none';
  } catch(e) {}
  return true;
}
function calculateWallTiles(f) {
  try {
    var elRes = document.getElementById("calcWallTileResult");
    var elNote = document.getElementById("calcWallTileNote");
    elRes.innerHTML=''; elNote.style.display='none';
    var tw = f.calcTileWidth.value.toString();
    var th = f.calcTileHeight.value.toString();
    var rw = f.calcTileRoomWidth.value.toString();
    var rh = f.calcTileRoomHeight.value.toString();
    if (!(IsNumeric(tw)&&IsNumeric(th)&&IsNumeric(rw)&&IsNumeric(rh))) {
      alert('Please enter valid Tile and Room Dimension values');
      return false;
    } else if ( (tw < 2) || (th < 2) ) {
      alert('Please ensure you have entered the tile size in centimetres');
      return false;
    } else if ( (rw > 100) || (rh > 100) ) {
      alert('Please ensure you have entered the room size in metres');
      return false;
    }
    rw *= 100; rh *= 100; // multiply room size up to centimetres
    var totalH = Math.ceil(parseFloat(rh)/parseFloat(th));
    var totalW = Math.ceil(parseFloat(rw)/parseFloat(tw));
    var total = totalH * totalW;
    var res='<em>'+total.toString()+" tiles";
    res+=' required.</em>';
    elRes.innerHTML='<div class="whatthismeans">'+res+'</div>'; elNote.style.display='block';
  } catch(e) {alert('Please enter valid Tile and Room Dimension values: ' + e.toString());}
  return false;
}