// JavaScript Document
var cellwidthdefault = 350;
var celllengthdefault = 350;
function clearForm() {
  // Clear Check Boxes
  document.getElementById('width_mtr').value = '';
  document.getElementById('length_mtr').value = '';
  document.getElementById('width_ft').value = '';
  document.getElementById('length_ft').value = '';
  document.getElementById('width_in').value = '';
  document.getElementById('length_in').value = '';
  document.getElementById('error_margin').value = 5;
  document.getElementById('floor_cell').width = cellwidthdefault;
  document.getElementById('floor_cell').height = celllengthdefault;
  document.getElementById('display_width').value = '';
  document.getElementById('display_length').value = '';
}
function doCalc() {
  // Initialise the variables
  var msg = '';
  var totalsqmtr = 0;
  var totalpacks = 0;
  var disptotalpacks = 0;
  var width_mtr = 0;
  var length = 0;
 var w_mtr = document.getElementById('width_mtr').value;
 var l_mtr = document.getElementById('length_mtr').value;
 var w_ft = document.getElementById('width_ft').value;
 var l_ft = document.getElementById('length_ft').value;
 var w_in = document.getElementById('width_in').value;
 var l_in = document.getElementById('length_in').value;
 var sqmtr_per_pack = document.getElementById('sqmtr_per_pack').value;
 var em = document.getElementById('error_margin').value;
 var cellwidth = cellwidthdefault;
 var celllength = celllengthdefault;
  // Check fields
  if (validate(document.getElementById('width_mtr')) == "no") {
    msg = msg + "The Width in Meters must be numeric and greater then 0.\n";
  }
  if (validate(document.getElementById('length_mtr')) == "no") {
    msg = msg + "The Length in Meters must be numeric and greater then 0.\n";
  }
  if (validate(document.getElementById('width_ft')) == "no") {
    msg = msg + "";
  }
  if (validate(document.getElementById('length_ft')) == "no") {
    msg = msg + "";
  }
  if (validate(document.getElementById('width_in')) == "no") {
    msg = msg + "";
  }
  if (validate(document.getElementById('length_in')) == "no") {
    msg = msg + "";
  }
  if (validate(document.getElementById('sqmtr_per_pack')) == "no") {
    msg = msg + "Sorry the calculator is unavailable at the moment.\n";
  }
  if (validate(document.getElementById('error_margin')) == "no") {
    msg = msg + "Sorry the calculator is unavailable at the moment.\n";
  }
  // Check Errors
  if (msg.length != 0) {
    alert(msg);
    return;
  }
  // Set Feet or Inches to zero if used
  if ((w_ft != 0) && (w_ft.length != 0) && (w_in.length == 0)) {
    w_in = 0;
    document.getElementById('width_in').value = 0;
  }
  if ((w_in != 0) && (w_in.length != 0) && (w_ft.length == 0)) {
    w_ft = 0;
    document.getElementById('width_ft').value = 0;
  }
  if ((l_ft != 0) && (l_ft.length != 0) && (l_in.length == 0)) {
    l_in = 0;
    document.getElementById('length_in').value = 0;
  }
  if ((l_in != 0) && (l_in.length != 0) && (l_ft.length == 0)) {
    l_ft = 0;
    document.getElementById('length_ft').value = 0;
  }
  // Calculate Width
  if ((w_ft.length != 0) && (w_in.length != 0)) {
    width = (((12 * w_ft) + (w_in * 1)) * 0.0254);
  } else if (w_mtr.length != 0) {
    width = (w_mtr * 1);
  } else {
    msg = msg + "Please enter a Width value\n";
  }
  // Calculate Length
  if ((l_ft.length != 0) && (l_in.length != 0)) {
    length = (((12 * l_ft) + (l_in * 1)) * 0.0254);
  } else if (l_mtr.length != 0) {
    length = (l_mtr * 1);
  } else {
    msg = msg + "Please enter a Length value.\n";
  }
  if (sqmtr_per_pack.length == 0) {
    msg = msg + "Sorry the completey calculator is unavailable for this product.\n";
  }
  if ((em.length == 0) || (em < 0) || (em > 100)) {
    msg = msg + "Sorry the completely calculator is unavailable for this product.\n";
    document.getElementById('error_margin').value = 5;
  }
  // Check Errors
  if (msg.length != 0) {
    alert(msg);
    return;
  }
  // Check for negative values
  if ((width <= 0) || (length <= 0) || (sqmtr_per_pack <= 0)) {
    msg = msg + "Entered values must not be negative.\n";
  }
  // Check Errors
  if (msg.length != 0) {
    alert(msg);
    return;
  }
  // Update the display
  totalsqmtr = (width * length) * (1.0 + (em / 100.0));
//  totalpacks = ((totalsqmtr / ((sqmtr_per_pack * 1.0) + 0.0001)) + 0.5);
  totalpacks = ((totalsqmtr / ((sqmtr_per_pack * 1.0) + 0.0001)) + 0.5);
  disptotalpacks = Math.round(totalpacks);
  document.getElementById('total_sqmtr').value = totalsqmtr;
//  document.getElementById('total_packs').value = totalpacks.toFixed(0);
  document.getElementById('total_packs').value = disptotalpacks ;
  document.getElementById('qty').value = disptotalpacks ;
 
// Added to include upading of cart quatity
// document.getElementById('quantity').value = disptotalpacks ;
  
  // Check for an error in the calculation
  if (isNaN(totalsqmtr)) {
    document.getElementById('total_sqmtr').value = "Error";
  } else {
    // Alter Room Display
    if (width < length) {
        cellwidth = (cellwidth * (width / length)).toFixed(0);
    } else {
        celllength = (celllength * (length / width)).toFixed(0);
    }
 //   document.getElementById('floor_cell').width = cellwidth;
 //   document.getElementById('floor_cell').height = celllength;
//    document.getElementById('display_width').value = (width.toFixed(4)) + " m";
 //   document.getElementById('display_length').value = (length.toFixed(4)) + " m";
  }
  if (isNaN(totalpacks)) {
    document.getElementById('total_packs').value = "Error";
  }
}
function validate(field) {
  var valid = " .-0123456789";
  var ok = "yes";
  var temp;
  for (var i=0; i<field.value.length; i++) {
    temp = "" + field.value.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") ok = "no";
  }
  return ok;
}