/***************************************
* ROI Calculator
* Version: 1.0
***************************************/



/*
 * calcROC
 *
 * Calculates the reduced operational cost
 *
 * INPUTS: roc_opcostpermile, roc_numvehicles, roc_nonbizmiles, roc_avgbusdays
 * UPDATES: roc_costreduction
 * RETURNS: cost reduction per month
 */
function calcROC() {
  var roc_opcostpermile = document.getElementById("roc_opcostpermile").value;
  var roc_numvehicles = document.getElementById("roc_numvehicles").value;
  var roc_nonbizmiles = document.getElementById("roc_nonbizmiles").value;
  var roc_avgbusdays = document.getElementById("roc_avgbusdays").firstChild.nodeValue;
  
  var roc_costreduction = roc_opcostpermile * roc_numvehicles * roc_nonbizmiles* roc_avgbusdays;
  
  document.getElementById("roc_costreduction").firstChild.nodeValue = formatCurrency(roc_costreduction);
  
  return roc_costreduction; 
}

/*
 * calcRFC
 *
 * Calculates the reduced fuel cost
 *
 * INPUTS: rfc_currentidle, rfc_idlegoal, rfc_fuelprice, rfc_vehicles, rfc_avgbusdays
 * UPDATES: rfc_currentcostidle, rfc_monthlysavings
 * RETURNS: monthly savings at goal
 */
function calcRFC() {
  var rfc_currentidle = document.getElementById("rfc_currentidle").value;
  var rfc_idlegoal = document.getElementById("rfc_idlegoal").value;
  var rfc_fuelprice = document.getElementById("rfc_fuelprice").value;
  //var rfc_fuelprice = document.getElementById("rfc_fuelprice").firstChild.nodeValue;
  var rfc_vehicles = document.getElementById("rfc_vehicles").value;
  var rfc_avgbusdays = document.getElementById("rfc_avgbusdays").firstChild.nodeValue;

  var rfc_currentcostidle = (rfc_currentidle/60) * rfc_fuelprice * rfc_vehicles * rfc_avgbusdays;
  var rfc_monthlysavings = rfc_currentcostidle - ((rfc_idlegoal/60) * rfc_fuelprice * rfc_vehicles * rfc_avgbusdays);
  
  document.getElementById("rfc_currentcostidle").firstChild.nodeValue = formatCurrency(rfc_currentcostidle);
  document.getElementById("rfc_monthlysavings").firstChild.nodeValue = formatCurrency(rfc_monthlysavings);
  
  return rfc_monthlysavings;
}

/*
 * calcRO
 *
 * Calculates the reduced overtime
 *
 * INPUTS: ro_overtimerate, ro_overtimeperdriver, ro_numdrivers, ro_avgbusdays
 * UPDATES: ro_potentialsavings
 * RETURNS: potential savings / month
 */
function calcRO() {
  var ro_overtimerate = document.getElementById("ro_overtimerate").value;
  var ro_overtimeperdriver = document.getElementById("ro_overtimeperdriver").value;
  var ro_numdrivers = document.getElementById("ro_numdrivers").value;
  var ro_avgbusdays = document.getElementById("ro_avgbusdays").firstChild.nodeValue;

  var ro_potentialsavings = ro_overtimerate * ro_overtimeperdriver * ro_numdrivers * ro_avgbusdays;

  document.getElementById("ro_potentialsavings").firstChild.nodeValue = formatCurrency(ro_potentialsavings);
  
  return ro_potentialsavings;
}

/*
 * calcIR
 *
 * Calculates the increased revenue
 *
 * INPUTS: ir_avgprofit, ir_numvehicles, ir_numadditionaljobs, ir_avgbusdays
 * UPDATES: ir_potentialrevenueincrease
 * RETURNS: potential revenue increase/month
 */
function calcIR() {
  var ir_avgprofit = document.getElementById("ir_avgprofit").value;
  var ir_numvehicles = document.getElementById("ir_numvehicles").value;
  var ir_numadditionaljobs = document.getElementById("ir_numadditionaljobs").value;
  var ir_avgbusdays = document.getElementById("ir_avgbusdays").firstChild.nodeValue;

  var ir_potentialrevenueincrease = ir_avgprofit * ir_numvehicles * ir_numadditionaljobs * ir_avgbusdays;

  document.getElementById("ir_potentialrevenueincrease").firstChild.nodeValue = formatCurrency(ir_potentialrevenueincrease);
  
  return ir_potentialrevenueincrease;
}

/*
 * calcCF
 *
 * Calculates the carbon footprint
 *
 * INPUTS: cf_currentidle, cf_idlegoal, cf_co2emissions, cf_vehicles, cf_avgbusdays
 * UPDATES: cf_currentemissions, cf_co2reduction
 */
function calcCF() {
  var cf_currentidle = document.getElementById("cf_currentidle").value;
  var cf_idlegoal = document.getElementById("cf_idlegoal").value;
  var cf_vehicles = document.getElementById("cf_vehicles").value;
  var cf_co2emissions = document.getElementById("cf_co2emissions").firstChild.nodeValue;
  var cf_avgbusdays = document.getElementById("cf_avgbusdays").firstChild.nodeValue;

  var cf_currentemissions = (cf_currentidle / 60) * cf_vehicles * cf_co2emissions * cf_avgbusdays;
  var cf_co2reduction = cf_currentemissions - ((cf_idlegoal / 60) * cf_vehicles * cf_co2emissions * cf_avgbusdays);

  document.getElementById("cf_currentemissions").firstChild.nodeValue = Math.round(cf_currentemissions*10)/10;
  document.getElementById("cf_co2reduction").firstChild.nodeValue = Math.round(cf_co2reduction*10)/10;
}

/*
 * estimateTotals
 *
 * Calculates the estimated totals.  Whenever any value is edited, *everything* is recalculated in order to
 * arrive at the correct totals.
 *
 * INPUTS: calcROC, calcRFC, calcRO, calcIR
 * UPDATES: et_costsavings, et_increasedrevenue, et_totalpotentialvalue
 */
function estimateTotals() {
  var roc = calcROC();
  var rfc = calcRFC();
  var ro = calcRO();
  var ir = calcIR();
  
  var et_costsavings = roc + rfc + ro;
  var et_increasedrevenue = ir;
  var et_totalpotentialvalue = et_costsavings + et_increasedrevenue;

  document.getElementById("et_costsavings").firstChild.nodeValue = formatCurrency(et_costsavings);
  document.getElementById("et_increasedrevenue").firstChild.nodeValue = formatCurrency(et_increasedrevenue);
  document.getElementById("et_totalpotentialvalue").firstChild.nodeValue = formatCurrency(et_totalpotentialvalue);
}

/*
 * formatCurrency
 *
 * Format a string to US currency.  Add decimals, commas, etc.
 * From: http://javascript.internet.com/forms/currency-format.html
 *
 * INPUTS: num - the value to format
 * RETURNS: the formatted string
 */
function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  
  if(isNaN(num))
    num = "0";

  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  
  if(cents<10)
    cents = "0" + cents;
  
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
  
  num.substring(num.length-(4*i+3));
  
  return (((sign)?'':'-') + '$' + num + '.' + cents);
}

/*
 * initPage
 *
 * Setup the event handlers on the editable form fields, and calculate the initial totals
 * based on the default values on the fields.
 *
 */
function initPage() {
  document.getElementById("roc_opcostpermile").onchange = estimateTotals;
  document.getElementById("roc_numvehicles").onchange = estimateTotals;
  document.getElementById("roc_nonbizmiles").onchange = estimateTotals;

  document.getElementById("rfc_currentidle").onchange = estimateTotals;
  document.getElementById("rfc_idlegoal").onchange = estimateTotals;
  document.getElementById("rfc_fuelprice").onchange = estimateTotals;
  document.getElementById("rfc_vehicles").onchange = estimateTotals

  document.getElementById("ro_overtimerate").onchange = estimateTotals;
  document.getElementById("ro_overtimeperdriver").onchange = estimateTotals;
  document.getElementById("ro_numdrivers").onchange = estimateTotals;

  document.getElementById("ir_avgprofit").onchange = estimateTotals;
  document.getElementById("ir_numvehicles").onchange = estimateTotals;
  document.getElementById("ir_numadditionaljobs").onchange = estimateTotals;

  document.getElementById("cf_currentidle").onchange = calcCF;
  document.getElementById("cf_idlegoal").onchange = calcCF;
  document.getElementById("cf_vehicles").onchange = calcCF;

  estimateTotals();
  calcCF();
}
