var timecost1_total = 0.00;
var timecost2_total = 0.00;
var supplycost1_total = 0.00;
var supplycost2_total = 0.00;
var supplycost3_total = 0.00;
var supplycost4_total = 0.00;
var supplycost5_total = 0.00;
var supplycost6_total = 0.00;
var timecost_total = 0.00;
var supplycost_total = 0.00;
var total_yearly_investment = 0.00;
var total_monthly_investment = 0.00;
var efc_cost = 0; 
var roi = 0.00;

function formatCurrency(num,returnFloat) {
	num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num))  num = '0.00';
	
	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++)
	if(!returnFloat) num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	
	var currency = (((sign)?'':'-') + num + '.' + cents);
	if(returnFloat)  return parseFloat(currency);
	else return currency;
}

function formatInt(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))  num = '0';
	
	num = parseInt(num);
	
	return num;
}

function timecost1Total(){
	if(document.efc_roi_calculator.timecost1_paid_hour.value == '') document.efc_roi_calculator.timecost1_paid_hour.value = '';
	else document.efc_roi_calculator.timecost1_paid_hour.value = '$' + formatCurrency(document.efc_roi_calculator.timecost1_paid_hour.value, false);
	
	if(document.efc_roi_calculator.timecost1_employees.value == '') document.efc_roi_calculator.timecost1_employees.value = '';
	else document.efc_roi_calculator.timecost1_employees.value = formatInt(document.efc_roi_calculator.timecost1_employees.value);
	document.efc_roi_calculator.timecost1_hours_day.value = formatCurrency(document.efc_roi_calculator.timecost1_hours_day.value, true);
	document.efc_roi_calculator.timecost1_days_year.value = formatInt(document.efc_roi_calculator.timecost1_days_year.value);
	timecost1_total = document.efc_roi_calculator.timecost1_employees.value * document.efc_roi_calculator.timecost1_hours_day.value * document.efc_roi_calculator.timecost1_days_year.value * formatCurrency(document.efc_roi_calculator.timecost1_paid_hour.value, true);
	document.efc_roi_calculator.timecost1_total.value = '$' + formatCurrency(timecost1_total, false);
}

function timecost2Total(){
	document.efc_roi_calculator.timecost2_paid_hour.value = '$' + formatCurrency(document.efc_roi_calculator.timecost2_paid_hour.value, false);
	document.efc_roi_calculator.timecost2_employees.value = formatInt(document.efc_roi_calculator.timecost2_employees.value);
	document.efc_roi_calculator.timecost2_hours_day.value = formatCurrency(document.efc_roi_calculator.timecost2_hours_day.value, true);
	document.efc_roi_calculator.timecost2_days_year.value = formatInt(document.efc_roi_calculator.timecost2_days_year.value);
	timecost2_total = document.efc_roi_calculator.timecost2_employees.value * document.efc_roi_calculator.timecost2_hours_day.value * document.efc_roi_calculator.timecost2_days_year.value * formatCurrency(document.efc_roi_calculator.timecost2_paid_hour.value, true);
	document.efc_roi_calculator.timecost2_total.value = '$' + formatCurrency(timecost2_total, false);
	
}

function timeCostTotal() {
	timecost1Total();
	timecost_total = timecost1_total + timecost2_total;
	document.efc_roi_calculator.timecost_total.value = '$' + formatCurrency(timecost_total, false);	
}

function supplycost1Total(){
	document.efc_roi_calculator.supplycost1_price.value = '$' + formatCurrency(document.efc_roi_calculator.supplycost1_price.value, false);
	document.efc_roi_calculator.supplycost1_quantity.value = formatInt(document.efc_roi_calculator.supplycost1_quantity.value);
	supplycost1_total =  formatCurrency(document.efc_roi_calculator.supplycost1_price.value, true) * document.efc_roi_calculator.supplycost1_quantity.value;
	document.efc_roi_calculator.supplycost1_total.value = '$' + formatCurrency(supplycost1_total, false);
}

function supplycost2Total(){
	document.efc_roi_calculator.supplycost2_price.value = '$' + formatCurrency(document.efc_roi_calculator.supplycost2_price.value, false);
	document.efc_roi_calculator.supplycost2_quantity.value = formatInt(document.efc_roi_calculator.supplycost2_quantity.value);
	supplycost2_total =  formatCurrency(document.efc_roi_calculator.supplycost2_price.value, true) * document.efc_roi_calculator.supplycost2_quantity.value;
	document.efc_roi_calculator.supplycost2_total.value = '$' + formatCurrency(supplycost2_total, false);
}

function supplycost3Total(){
	document.efc_roi_calculator.supplycost3_price.value = '$' + formatCurrency(document.efc_roi_calculator.supplycost3_price.value, false);
	document.efc_roi_calculator.supplycost3_quantity.value = formatInt(document.efc_roi_calculator.supplycost3_quantity.value);
	supplycost3_total =  formatCurrency(document.efc_roi_calculator.supplycost3_price.value, true) * document.efc_roi_calculator.supplycost3_quantity.value;
	document.efc_roi_calculator.supplycost3_total.value = '$' + formatCurrency(supplycost3_total, false);
}

function supplycost4Total(){
	document.efc_roi_calculator.supplycost4_price.value = '$' + formatCurrency(document.efc_roi_calculator.supplycost4_price.value, false);
	document.efc_roi_calculator.supplycost4_quantity.value = formatInt(document.efc_roi_calculator.supplycost4_quantity.value);
	supplycost4_total =  formatCurrency(document.efc_roi_calculator.supplycost4_price.value, true) * document.efc_roi_calculator.supplycost4_quantity.value;
	document.efc_roi_calculator.supplycost4_total.value = '$' + formatCurrency(supplycost4_total, false);
}

function supplycost5Total(){
	document.efc_roi_calculator.supplycost5_price.value = '$' + formatCurrency(document.efc_roi_calculator.supplycost5_price.value, false);
	document.efc_roi_calculator.supplycost5_quantity.value = formatInt(document.efc_roi_calculator.supplycost5_quantity.value);
	supplycost5_total =  formatCurrency(document.efc_roi_calculator.supplycost5_price.value, true) * document.efc_roi_calculator.supplycost5_quantity.value;
	document.efc_roi_calculator.supplycost5_total.value = '$' + formatCurrency(supplycost5_total, false);
}

function supplycost6Total(){
	//supplycost6_total = formatCurrency(document.efc_roi_calculator.supplycost6_total.value, true);
	//document.efc_roi_calculator.supplycost6_total.value = '$' + formatCurrency(supplycost6_total, false);
	document.efc_roi_calculator.supplycost6_price.value = '$' + formatCurrency(document.efc_roi_calculator.supplycost6_price.value, false);
	document.efc_roi_calculator.supplycost6_quantity.value = formatInt(document.efc_roi_calculator.supplycost6_quantity.value);
	supplycost6_total =  formatCurrency(document.efc_roi_calculator.supplycost6_price.value, true) * document.efc_roi_calculator.supplycost5_quantity.value;
	document.efc_roi_calculator.supplycost6_total.value = '$' + formatCurrency(supplycost6_total, false);
}

function supplycostTotal() {
	supplycost1Total();
	supplycost2Total();
	supplycost3Total();
	supplycost4Total();
	supplycost5Total();
	supplycost6Total();
	supplycost_total = supplycost1_total + supplycost2_total + supplycost3_total + supplycost4_total + supplycost5_total + supplycost6_total;
	document.efc_roi_calculator.supplycost_total.value = '$' + formatCurrency(supplycost_total, false);	
}

function efcCost() {
	var x = 0;
	if(document.efc_roi_calculator.timecost1_employees.value > 0 && document.efc_roi_calculator.timecost1_employees.value <= 25)  x = 150;
	else if(document.efc_roi_calculator.timecost1_employees.value <= 45)  x = 135;
	else if(document.efc_roi_calculator.timecost1_employees.value <= 65)  x = 120;
	else if(document.efc_roi_calculator.timecost1_employees.value <= 85)  x = 105;
	else if(document.efc_roi_calculator.timecost1_employees.value > 85)  x = 90;
	
	if(document.efc_roi_calculator.timecost1_employees.value > 5)  efc_cost = (((document.efc_roi_calculator.timecost1_employees.value - 5) * x) + (1995)) * 1.18;
	else efc_cost = 1995 * 1.18;
	//alert(efc_cost);
}

function totalYearlyInvestment() {
	timeCostTotal();
	supplycostTotal();
	if(document.efc_roi_calculator.timecost1_employees.value != '' && document.efc_roi_calculator.timecost1_paid_hour.value != '') {
		total_yearly_investment = formatCurrency(timecost_total + supplycost_total, true);
		total_monthly_investment = formatCurrency(total_yearly_investment / 12, true);
		
		
		efcCost();
		document.efc_roi_calculator.total_yearly_investment.value = '$' + formatCurrency(total_yearly_investment, false);
		document.efc_roi_calculator.total_monthly_investment.value = '$' + formatCurrency(total_monthly_investment, false);
		document.efc_roi_calculator.efc_cost.value = '$' + formatCurrency(efc_cost, false);
		
		if(total_monthly_investment > 0) {
			roi = formatCurrency((efc_cost / total_monthly_investment), true);
			document.efc_roi_calculator.roi.value = formatCurrency(roi, false) + ' Months';
		} else {
			document.efc_roi_calculator.roi.value = '0.00'; 
		}
	} else {
		document.efc_roi_calculator.total_yearly_investment.value = '$0.00';
		document.efc_roi_calculator.total_monthly_investment.value = '$0.00';
		document.efc_roi_calculator.efc_cost.value = '$0.00';
		document.efc_roi_calculator.roi.value = '0.00';
		
	}
}

function resetROICalculator() {
	document.efc_roi_calculator.timecost1_employees.value = '';
	document.efc_roi_calculator.timecost1_paid_hour.value = '';
	document.efc_roi_calculator.timecost1_days_year.value = 250;
	document.efc_roi_calculator.supplycost1_price.value = '$4.00'
	document.efc_roi_calculator.supplycost1_quantity.value = '36';
	document.efc_roi_calculator.supplycost2_price.value = '$70.00'
	document.efc_roi_calculator.supplycost2_quantity.value = '3';
	document.efc_roi_calculator.supplycost3_price.value = '$170.00'
	document.efc_roi_calculator.supplycost3_quantity.value = '1';
	document.efc_roi_calculator.supplycost4_price.value = '$50.00'
	document.efc_roi_calculator.supplycost4_quantity.value = '12';
	document.efc_roi_calculator.supplycost5_price.value = '$100.00'
	document.efc_roi_calculator.supplycost5_quantity.value = '12';
	document.efc_roi_calculator.supplycost6_price.value = '$50.00'
	document.efc_roi_calculator.supplycost6_quantity.value = '12';
	totalYearlyInvestment(); 
	//document.efc_roi_calculator.total_yearly_investment.value = '';
	//document.efc_roi_calculator.total_monthly_investment.value = '';
	//document.efc_roi_calculator.efc_cost.value = '';
	//document.efc_roi_calculator.roi.value = 'Enter Data';
}