// the following values are used to calculate pricing in the calculations below
var newBuildingBase      = 250;
var newBuildingPerFoot   = 0.11;
var newBuildingSelfServe = 99;

var exstPlusAdtnBase      = 275;
var exstPlusAdtnPerFoot   = 0.11;
var exstPlusAdtnSelfServe = 129;

var adtnOnlyBase      = 185;
var adtnOnlyPerFoot   = 0.11;
var adtnOnlySelfServe = 99;



function validateNumber(anInput) {
  if (window.RegExp) {
    var reg = new RegExp("^[0-9]*$","g");
    if (!reg.test(anInput)) {
      return(false);
    }
  }
  return(true);
}

/**
 ***********************************************************************
 * This function is called after the user 'touches' any of the inputs of
 * the 'New Building' compliance method line.
 ***********************************************************************
 */
function doNewBuilding(anElement) {
  var f = anElement.form;

  // if we clicked on the 'New Building' radio button
  // copy any addition values into the newFloor area,
  if (anElement.name == 'method') {
    if (f.adtnFloor.value > 0) {
      f.newFloor.value  = f.adtnFloor.value;
    }
    if (f.adtnOnlyFloor.value > 0) {
      f.newFloor.value  = f.adtnOnlyFloor.value;
    }
  }

  // if the user clicked in the area input form
  // make sure that the 'New Building' radio is selected
  if (anElement.name == 'newFloor') {
    f.method[0].checked = true;
  }

  // clear the other area inputs
  f.exstFloor.value = "";
  f.adtnFloor.value = "";
  f.adtnOnlyFloor.value = "";

  if (validateNumber(f.newFloor.value) == false) {
    alert("Floor areas must be whole numbers");
    f.newFloor.focus();
    f.newFloor.select();
    return;
  }

  // calculate the estimate
  if (f.newFloor.value > 0) {
    f.lowEstimate.value = Math.round(Math.max( newBuildingBase, (f.newFloor.value * newBuildingPerFoot) ) - 10 );
    f.hiEstimate.value  = Math.round(Math.max( newBuildingBase, (f.newFloor.value * newBuildingPerFoot) ) + 10 );
    f.selfServe.value = newBuildingSelfServe;
  }
  else {
    f.lowEstimate.value = '0';
    f.hiEstimate.value  = '0';
    f.selfServe.value   = '0';
  }
} // doNewBuilding

/**
 ***********************************************************************
 * This function is triggered when the user 'touches' the
 * Existing-plus-addition line
 ***********************************************************************
 */
function doExstPlusAdtn(anElement) {
  var f = anElement.form;

  // if we clicked on the 'Existing Plus Addition' radio button
  // copy any addition values into the newFloor area,
  if (anElement.name == 'method') {
    if (f.newFloor.value > 0) {
      f.exstFloor.value  = f.newFloor.value;
    }
    if (f.adtnOnlyFloor.value > 0) {
      f.adtnFloor.value  = f.adtnOnlyFloor.value;
    }
  }

  // if the user clicked in one of the area inputs
  // make sure that the 'Existing-Plus-Addition' radio is selected
  if (anElement.name == 'exstFloor' || anElement.name == 'adtnFloor') {
    f.method[1].checked = true;
  }

  // clear the other area inputs
  f.newFloor.value = "";
  f.adtnOnlyFloor.value = "";

  if (validateNumber(f.exstFloor.value) == false) {
    alert("Floor areas must be whole numbers");
    f.exstFloor.focus();
    f.exstFloor.select();
    return;
  }

  if (validateNumber(f.adtnFloor.value) == false) {
    alert("Floor areas must be whole numbers");
    f.adtnFloor.focus();
    f.adtnFloor.select();
    return;
  }

  // calculate the estimate
  if (f.exstFloor.value > 0 && f.adtnFloor.value > 0) {
    var sumAreas = f.exstFloor.value * 1 + f.adtnFloor.value * 1;
    f.lowEstimate.value = Math.round(Math.max( exstPlusAdtnBase, (sumAreas * exstPlusAdtnPerFoot) ) - 10 );
    f.hiEstimate.value  = Math.round(Math.max( exstPlusAdtnBase, (sumAreas * exstPlusAdtnPerFoot) ) + 10 );
    f.selfServe.value   = exstPlusAdtnSelfServe;
  }
  else {
    f.lowEstimate.value = '0';
    f.hiEstimate.value  = '0';
    f.selfServe.value   = '0';
  }
} // end doExstPlusAdtn


/**
 ***********************************************************************
 * This function is called after the user 'touches' any of the inputs of
 * the 'Treat Addition as New Building' compliance method line.
 ***********************************************************************
 */
function doAdditionOnly(anElement) {
  var f = anElement.form;

  // if we clicked on the 'New Bulding' radio button
  // copy any addition values into the newFloor area,
  if (anElement.name == 'method') {
    if (f.newFloor.value > 0) {
      f.adtnOnlyFloor.value  = f.newFloor.value;
    }
    if (f.adtnFloor.value > 0) {
      f.adtnOnlyFloor.value  = f.adtnFloor.value;
    }
  }

  // if the user clicked in the area input form
  // make sure that the 'New Building' radio is selected
  if (anElement.name == 'adtnOnlyFloor') {
    f.method[2].checked = true;
  }

  // clear the other area inputs
  f.newFloor.value  = "";
  f.exstFloor.value = "";
  f.adtnFloor.value = "";

  if (validateNumber(f.adtnOnlyFloor.value) == false) {
    alert("Floor areas must be whole numbers");
    f.adtnOnlyFloor.focus();
    f.adtnOnlyFloor.select();
    return;
  }

  // calculate the estimate
  if (f.adtnOnlyFloor.value > 0) {
    f.lowEstimate.value = Math.round(Math.max( adtnOnlyBase, (f.adtnOnlyFloor.value * adtnOnlyPerFoot) ) - 10 );
    f.hiEstimate.value  = Math.round(Math.max( adtnOnlyBase, (f.adtnOnlyFloor.value * adtnOnlyPerFoot) ) + 10 );
    f.selfServe.value   = newBuildingSelfServe;
  }
  else {
    f.lowEstimate.value = '0';
    f.hiEstimate.value  = '0';
    f.selfServe.value   = '0';
  }
} // end doAdditionOnly


function recalculate(anElement) {
  var f = anElement.form
  if (f.newFloor.value != "") doNewBuilding(f.newFloor);
  if (f.exstFloor.value != "" && f.adtnFloor.value != "") doExstPlusAdtn(f.exstFloor);
  if (f.adtnOnlyFloor.value != "") doAdditionOnly(f.adtnOnlyFloor);

  if ( (f.method[1].checked == true ) &&
       (f.exstFloor.value == "" || f.adtnFloor.value == "") ) {
    var msg = "To use the Existing-Plus-Addition method \n";
    msg    += "you must provide areas for both: \n";
    msg    += "- The Existing Building Conditioned Floor, and \n";
    msg    += "- The Addition Conditioned Floor";
    alert(msg);
    if (f.exstFloor.value == "") {
      f.exstFloor.focus();
      f.exstFloor.select();
    }
    else {
      f.adtnFloor.focus();
      f.adtnFloor.select();
    }
  }
}

