// JavaScript Document
function fmt (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);
}

function fmthours (num) {
  if(isNaN(num))
  	num = "0";

	
	return num.toFixed(0);
}

function CalcProductivity() {
  var DailySavings = document.productivity.employees.value * document.productivity.hourlywage.value * (document.productivity.timesaving.value/60);
  var HoursSaved = document.productivity.employees.value * (document.productivity.timesaving.value/60);

  document.productivity.HiddenMonthly.value = DailySavings * 20;
  
  document.productivity.HoursMonthly.value = fmthours(HoursSaved * 20);
  document.getElementById("divProdHoursMonthly").innerHTML = fmthours(HoursSaved * 20);
  document.productivity.HoursYearly.value = fmthours(HoursSaved * 240);
  document.getElementById("divProdHoursYearly").innerHTML = fmthours(HoursSaved * 240);
  document.productivity.HoursMonthly.value = fmthours(HoursSaved * 20);
  document.getElementById("divProdHoursMonthly2").innerHTML = fmthours(HoursSaved * 20);
  document.productivity.HoursYearly.value = fmthours(HoursSaved * 240);
  document.getElementById("divProdHoursYearly2").innerHTML = fmthours(HoursSaved * 240);
  
  document.productivity.Monthly.value = fmt(DailySavings * 20);
  document.getElementById("divProdMonthly").innerHTML = fmt(DailySavings * 20);
  document.productivity.Yearly.value = fmt(DailySavings * 240);
  document.getElementById("divProdYearly").innerHTML = fmt(DailySavings * 240);
  
   //print form variables
  document.printform.prod_employees.value = document.productivity.employees.value;
  document.printform.prod_hourlywage.value = document.productivity.hourlywage.value;
  document.printform.prod_timesaving.value = document.productivity.timesaving.value;
  document.printform.prod_HoursMonthly.value = document.productivity.HoursMonthly.value;
  document.printform.prod_HoursYearly.value = document.productivity.HoursYearly.value;
  document.printform.prod_Monthly.value = document.productivity.Monthly.value;
  document.printform.prod_Yearly.value = document.productivity.Yearly.value;
  
}

function CalcLostRevenue() {
  document.lostrevenue.HiddenMonthly.value = document.lostrevenue.estimatedhourlyrevenue.value * document.lostrevenue.HoursMonthly.value;
  document.lostrevenue.HoursMonthly.value = document.getElementById("divProdHoursMonthly2").innerHTML;
  document.lostrevenue.HoursYearly.value = document.getElementById("divProdHoursYearly2").innerHTML;
  document.lostrevenue.LostRevenueMonthly.value = fmt(document.lostrevenue.estimatedhourlyrevenue.value * document.lostrevenue.HoursMonthly.value);
  document.getElementById("divProdLostRevenueMonthly").innerHTML = fmt(document.lostrevenue.estimatedhourlyrevenue.value * document.lostrevenue.HoursMonthly.value);
  document.lostrevenue.LostRevenueYearly.value = fmt(document.lostrevenue.estimatedhourlyrevenue.value * document.lostrevenue.HoursYearly.value);
  document.getElementById("divProdLostRevenueYearly").innerHTML = fmt(document.lostrevenue.estimatedhourlyrevenue.value * document.lostrevenue.HoursYearly.value);
  
  //print form variables
  document.printform.lostrevenue_estimatedhourlyrevenue.value = document.lostrevenue.estimatedhourlyrevenue.value;
  document.printform.lostrevenue_HoursMonthly.value = document.lostrevenue.HoursMonthly.value;
  document.printform.lostrevenue_HoursYearly.value = document.lostrevenue.HoursYearly.value;
  document.printform.lostrevenue_LostRevenueMonthly.value = document.lostrevenue.LostRevenueMonthly.value;
  document.printform.lostrevenue_LostRevenueYearly.value = document.lostrevenue.LostRevenueYearly.value;
}

function CalcVehProductivity() {
  var DailySavings = document.vehproductivity.employees.value * document.vehproductivity.hourlyrevenue.value;
  
  document.vehproductivity.HiddenMonthly.value = DailySavings * 20;
  
  document.vehproductivity.TimeSaved.value = fmt(DailySavings);
  document.getElementById("divProdTimeSaved").innerHTML = fmt(DailySavings);
  //print form variables
  document.printform.vp_employees.value = document.vehproductivity.employees.value;
  document.printform.vp_hourlyrevenue.value = document.vehproductivity.hourlyrevenue.value;
  document.printform.vp_TimeSaved.value = document.vehproductivity.TimeSaved.value;
}

function CalcSideJobs() {
  var MonthlySavings = document.sidejobs.monthlyrevenue.value * document.sidejobs.moonlighting.value;

  document.sidejobs.HiddenMonthly.value = MonthlySavings;
  document.sidejobs.Monthly.value = fmt(MonthlySavings);
  document.getElementById("divSideMonthly").innerHTML =  fmt(MonthlySavings);
  document.sidejobs.Yearly.value = fmt(MonthlySavings * 12);
  document.getElementById("divSideYearly").innerHTML = fmt(MonthlySavings * 12);
  
  //print form variables
  document.printform.sj_monthlyrevenue.value = document.sidejobs.monthlyrevenue.value;
  document.printform.sj_moonlighting.value = document.sidejobs.moonlighting.value;
  document.printform.sj_Monthly.value = document.sidejobs.Monthly.value;
  document.printform.sj_Yearly.value = document.sidejobs.Yearly.value;
}


function CalcVOC() {
  var VOC = document.voc.vehicletype.value * document.voc.monthlymileage.value * document.voc.km_miles.value * document.voc.vehicles.value;
  var MonthlySavings = VOC * document.voc.mileagereduction.value;

  document.voc.HiddenMonthly.value = MonthlySavings;
  document.voc.Monthly.value = fmt(MonthlySavings);
  document.getElementById("divVOCMonthly").innerHTML =  fmt(MonthlySavings);
  document.voc.Yearly.value = fmt(MonthlySavings * 12);
  document.getElementById("divVOCYearly").innerHTML = fmt(MonthlySavings * 12);
  
  //print form variables
  document.printform.voc_vehicletype.value = document.voc.vehicletype.value;
  document.printform.voc_monthlymileage.value = document.voc.monthlymileage.value;
  document.printform.voc_km_miles.value = document.voc.km_miles.value;
  document.printform.voc_vehicles.value = document.voc.vehicles.value;
  document.printform.voc_mileagereduction.value = document.voc.mileagereduction.value;
  document.printform.voc_Monthly.value = document.voc.Monthly.value;
  document.printform.voc_Yearly.value = document.voc.Yearly.value;
  
}


function CalcInsurance() {
  var MonthlySavings = document.insurance.vehicles.value * document.insurance.cost.value * document.insurance.discount.value / 12;

  document.insurance.HiddenMonthly.value = MonthlySavings;
  document.insurance.Monthly.value = fmt(MonthlySavings);
  document.getElementById("divInsuranceMonthly").innerHTML =  fmt(MonthlySavings);
  document.insurance.Yearly.value = fmt(MonthlySavings * 12);
  document.getElementById("divInsuranceYearly").innerHTML = fmt(MonthlySavings * 12);
  
  //print form variables
  document.printform.insurance_vehicles.value = document.insurance.vehicles.value;
  document.printform.insurance_cost.value = document.insurance.cost.value;
  document.printform.insurance_discount.value = document.insurance.discount.value;
  document.printform.insurance_Monthly.value = document.insurance.Monthly.value;
  document.printform.insurance_Yearly.value = document.insurance.Yearly.value;
  
}

function CalcMaintenance() {
  var MonthlySavings = (document.maintenance.vehicles.value * document.maintenance.reduceddays.value * document.maintenance.revenue.value) / 12;

  document.maintenance.HiddenMonthly.value = MonthlySavings;
  document.maintenance.Monthly.value = fmt(MonthlySavings);
  document.getElementById("divMaintenanceMonthly").innerHTML =  fmt(MonthlySavings);
  document.maintenance.Yearly.value = fmt(MonthlySavings * 12);
  document.getElementById("divMaintenanceYearly").innerHTML = fmt(MonthlySavings * 12);

  
  //print form variables
  document.printform.maintenance_vehicles.value = document.maintenance.vehicles.value;
  document.printform.maintenance_reduceddays.value = document.maintenance.reduceddays.value;
  document.printform.maintenance_revenue.value = document.maintenance.revenue.value;
  document.printform.maintenance_Monthly.value = document.maintenance.Monthly.value;
  document.printform.maintenance_Yearly.value = document.maintenance.Yearly.value;
  
}

function CalcManagement() {
  var MonthlySavings = (document.management.salary.value * document.management.timepercent.value);

  document.management.HiddenMonthly.value = MonthlySavings;
  document.management.Monthly.value = fmt(MonthlySavings);
  document.getElementById("divManagementMonthly").innerHTML =  fmt(MonthlySavings);
  document.management.Yearly.value = fmt(MonthlySavings * 12);
  document.getElementById("divManagementYearly").innerHTML = fmt(MonthlySavings * 12);
  
  //print form variables
  document.printform.management_salary.value = document.management.salary.value;
  document.printform.management_timepercent.value = document.management.timepercent.value;
  document.printform.management_Monthly.value = document.management.Monthly.value;
  document.printform.management_Yearly.value = document.management.Yearly.value;

}

function CalcFuel() {
  var MonthlySavings = ((document.fuel.vehicles.value * document.fuel.weeklyfuel.value) * document.fuel.timesaved.value);

  document.fuel.HiddenMonthly.value = MonthlySavings;
  document.fuel.Monthly.value = fmt(MonthlySavings);
  document.getElementById("divFuelMonthly").innerHTML =  fmt(MonthlySavings);
  document.fuel.Yearly.value = fmt(MonthlySavings * 12);
  document.getElementById("divFuelYearly").innerHTML = fmt(MonthlySavings * 12);
  
  //print form variables
  document.printform.fuel_vehicles.value = document.fuel.vehicles.value;
  document.printform.fuel_weeklyfuel.value = document.fuel.weeklyfuel.value;
  document.printform.fuel_timesaved.value = document.fuel.timesaved.value;
  document.printform.fuel_Monthly.value = document.fuel.Monthly.value;
  document.printform.fuel_Yearly.value = document.fuel.Yearly.value;

}

function CalcEmpProductivity() {
  var MonthlySavings = (document.empproductivity.vehicles.value * document.empproductivity.timetojob.value * document.empproductivity.hourlywage.value);

  document.empproductivity.HiddenMonthly.value = MonthlySavings;
  document.empproductivity.Monthly.value = fmt(MonthlySavings);
  document.getElementById("divEmpproductivityMonthly").innerHTML =  fmt(MonthlySavings);
  document.empproductivity.Yearly.value = fmt(MonthlySavings * 12);
  document.getElementById("divEmpproductivityYearly").innerHTML = fmt(MonthlySavings * 12);
  
  //print form variables
  document.printform.empproductivity_vehicles.value = document.empproductivity.vehicles.value;
  document.printform.empproductivity_timetojob.value = document.empproductivity.timetojob.value;
  document.printform.empproductivity_hourlywage.value = document.empproductivity.hourlywage.value;
  document.printform.empproductivity_Monthly.value = document.empproductivity.Monthly.value;
  document.printform.empproductivity_Yearly.value = document.empproductivity.Yearly.value;
  }

function CalTraining(){

document.training.TrainingWeeksSaved.value = 2 * document.training.employees.value;
document.training.TrainingWagesMonth.value = 4*4*document.training.employees.value * document.training.TrainingHrlyWage.value;
document.training.TrainingWagesYear.value = 12*4*4*document.training.employees.value * document.training.TrainingHrlyWage.value;

var TrainingWeeksSaved1 = document.training.TrainingWeeksSaved.value;
var TrainingWagesMonth1 = document.training.TrainingWagesMonth.value;
var TrainingWagesYear1 = document.training.TrainingWagesYear.value;

document.training.HiddenMonthly.value = TrainingWagesMonth1;

document.getElementById("divTrainingWeeksSaved").innerHTML = fmt(TrainingWeeksSaved1);
document.getElementById("divTrainingWagesMonth").innerHTML = fmt(TrainingWagesMonth1);
document.getElementById("divTrainingWagesYear").innerHTML = fmt(TrainingWagesYear1);

//print form variables
document.printform.trainingweeksaving.value = TrainingWeeksSaved1;
document.printform.trainingmonthlywagesaving.value = TrainingWagesMonth1;
document.printform.trainingyearlywagesaving.value = TrainingWagesYear1;
}


function CalCellularCost(){

document.cellularcost.CellularMonthlySave.value = document.cellularcost.vans.value * document.cellularcost.monthlybill.value * document.cellularcost.perc.value/100;

var TotalCellularMonth = document.cellularcost.CellularMonthlySave.value;
document.cellularcost.HiddenMonthly.value = TotalCellularMonth;

document.getElementById("divCellularMonthly").innerHTML = fmt(TotalCellularMonth);


//print form variables
document.printform.cellularmonthlysaving.value = fmt(TotalCellularMonth);
document.printform.cellularyearlysaving.value = fmt(TotalCellularMonth*12);

}

function Calptofuel(){

document.ptofuel.PTOSavings.value = document.ptofuel.vans.value * document.ptofuel.monthhrs.value * 4 *15/100;

var TotalPTOMonth = document.ptofuel.PTOSavings.value;
document.ptofuel.HiddenMonthly.value = TotalPTOMonth;

document.getElementById("divPTOSaved").innerHTML = fmt(TotalPTOMonth);


//print form variables
document.printform.ptomonthly.value =fmt(TotalPTOMonth);
document.printform.ptoyearly.value = fmt(TotalPTOMonth*12);

}

function TotalAll() {
	
	CalcProductivity();
	CalcVehProductivity();
	CalcLostRevenue();
	CalcSideJobs();
	CalcVOC();
	CalcInsurance();
	CalcMaintenance();
	CalcManagement();
	CalcFuel();
	CalcEmpProductivity();
	CalTraining();
	CalCellularCost();
	Calptofuel();

	var MonthlyTotalSavings = parseInt(document.productivity.HiddenMonthly.value)
	                        + parseInt(document.lostrevenue.HiddenMonthly.value)
							+ parseInt(document.vehproductivity.HiddenMonthly.value)
							+ parseInt(document.sidejobs.HiddenMonthly.value)
							+ parseInt(document.voc.HiddenMonthly.value)
							+ parseInt(document.insurance.HiddenMonthly.value)
 							+ parseInt(document.maintenance.HiddenMonthly.value)
							+ parseInt(document.management.HiddenMonthly.value)
 							+ parseInt(document.fuel.HiddenMonthly.value)
							+ parseInt(document.empproductivity.HiddenMonthly.value)
							+ parseInt(document.training.HiddenMonthly.value)
							+ parseInt(document.cellularcost.HiddenMonthly.value)
							+ parseInt(document.ptofuel.HiddenMonthly.value);

//alert(MonthlyTotalSavings);
	document.totals.Monthly.value = fmt(MonthlyTotalSavings);
  document.getElementById("divTotalMonthly").innerHTML = fmt(MonthlyTotalSavings);
	document.totals.Yearly.value = fmt(MonthlyTotalSavings * 12);
		document.totals.Daily.value = fmt((MonthlyTotalSavings * 12)/365);
  document.getElementById("divTotalYearly").innerHTML = fmt(MonthlyTotalSavings * 12);
  document.getElementById("divTotalDaily").innerHTML = fmt((MonthlyTotalSavings * 12)/365);
  document.printform.totals_Monthly.value = document.totals.Monthly.value;
  document.printform.totals_Yearly.value = document.totals.Yearly.value;
  document.printform.totals_Daily.value = document.totals.Daily.value;
}