// JavaScript Document
function idValue(theID)
{
	var tmp = Math.round(document.getElementById(theID).value);
	return tmp;
}
function idHTML(theID)
{
	var tmp = document.getElementById(theID);
	return tmp;
}
function checkNAN(myField)
{
	if (isNaN(myField)) {
		myField = 0;
	}else if (myField == Infinity) {
		myField = 0;
	} else {
		myField = myField;
	}
	return myField;
}

function calculate_compare(){
	
	/******************************************************************************************/
	//SECTION 1 VALUES
		var rental_rate = idValue("rental_rate");
		var days_per_year = idValue("days_per_year");
		
		var acq_cost = idValue("acq_cost");
		var deprec_years = Math.floor(idValue("deprec_years"));
		var resale = idValue("resale") / 100;
		var deprec_cost = (acq_cost - (acq_cost * resale)) / deprec_years;
			deprec_cost = deprec_cost.toFixed(2);
			idHTML("deprec_cost").innerHTML = checkNAN(deprec_cost);
			//TOTAL DEPRECIATION COST
			
		var total_financed = idValue("total_financed");
		var yrs_financed = idValue("yrs_financed");
		var num_payments = 0;
			num_payments = (yrs_financed * 12);
			idHTML("num_payments").innerHTML = checkNAN(num_payments);
		var finance_rate = idValue("finance_rate") / 100;
		var annual_interest = (total_financed * finance_rate);
			annual_interest = annual_interest.toFixed(2);
			//annual_interest = 0;
			idHTML("annual_interest").innerHTML = checkNAN(annual_interest);
			//ANNUAL INTEREST CALCULATION
			
		var prep_cost = idValue("prep_cost");
		var prep_cost_total = "";
		if(prep_cost!==0){prep_cost_total = (prep_cost / deprec_years);}
		else{prep_cost_total = 0;}
			prep_cost_total = prep_cost_total.toFixed(2);
			idHTML("prep_cost_total").innerHTML = checkNAN(prep_cost_total);
		//TOTAL PREP COST
		
		var section_one_total= (Math.abs(deprec_cost) + Math.abs(annual_interest) + Math.abs(prep_cost_total));
			section_one_total = section_one_total.toFixed(2);
			idHTML("section_one_total").innerHTML = checkNAN(section_one_total);
		
	/******************************************************************************************/
	//SECTION 2
		var annual_main_cost = idValue("annual_main_cost") / 100;
		var annual_main_cost_total = (acq_cost * annual_main_cost);
			annual_main_cost_total = checkNAN(annual_main_cost_total.toFixed(2));
			idHTML("annual_main_cost_total").innerHTML = annual_main_cost_total;
			idHTML("annual_main_cost_total").innerHTML = annual_main_cost_total;
			idHTML("section_two_total").innerHTML = annual_main_cost_total;
			//TOTAL ANNUAL MAINTENANCE COST
		
	/******************************************************************************************/
	//SECTION 3
		var inspections = idValue("inspections");
		var insurance = idValue("insurance");
		var accidents_year = idValue("accidents_year");
		var excess_accidents = idValue("excess_accidents");
		
		var replace_cost = (Math.abs(rental_rate) * Math.abs(1.2));
			replace_cost = replace_cost.toFixed(2);
			idHTML("replace_cost").innerHTML = checkNAN(replace_cost);
		
		var excess_accidents_total = (Math.abs(accidents_year) * Math.abs(excess_accidents));
			excess_accidents_total = excess_accidents_total.toFixed(2);
			idHTML("excess_accidents_total").innerHTML = checkNAN(excess_accidents_total);
			
		var replace_days = idValue("replace_days");
		var replace_cost_total = (Math.abs(replace_days) * Math.abs(replace_cost));
			replace_cost_total = replace_cost_total.toFixed(2);
			idHTML("replace_cost_total").innerHTML = checkNAN(replace_cost_total);
		
		var section_three_total = (Math.abs(inspections) + Math.abs(insurance) + Math.abs(excess_accidents_total) + Math.abs(replace_cost_total));
			section_three_total = section_three_total.toFixed(2);
			idHTML("section_three_total").innerHTML = checkNAN(section_three_total);
		//TOTAL OTHER COST
		
	/******************************************************************************************/
	//SECTION 4
		var gen_purchasing = idValue("gen_purchasing");
		var gen_maintenance = idValue("gen_maintenance");
		var gen_insurance = idValue("gen_insurance");
		var gen_transport_cost = idValue("gen_transport_cost");
		var gen_accounting = idValue("gen_accounting");
		var sum_cost = (Math.abs(gen_maintenance) + Math.abs(gen_insurance) + Math.abs(gen_transport_cost) + Math.abs(gen_accounting));
			sum_cost = sum_cost.toFixed(2);
		
		var gen_total_hours =(Math.abs(sum_cost) + (Math.abs(gen_purchasing) / Math.abs(deprec_years)));
		idHTML("gen_total_hours").innerHTML = checkNAN(Math.round(gen_total_hours));
		
		var gen_cost_per_hour = idValue("gen_cost_per_hour");

		var section_four_total = (Math.abs(gen_cost_per_hour) * Math.round(gen_total_hours));
			section_four_total = section_four_total.toFixed(2);
			idHTML("section_four_total").innerHTML = checkNAN(section_four_total);
		//TOTAL GENERAL ADMINISTRATIVE COST
		
	/******************************************************************************************/
	//SECTION 5 TOTAL
		var total_expenses = (Math.abs(section_one_total) + Math.abs(annual_main_cost_total) + Math.abs(section_three_total) + Math.abs(section_four_total));
			total_expenses = total_expenses.toFixed(2);
			idHTML("total_expenses").innerHTML = checkNAN(total_expenses);
			idHTML("total_utilization").innerHTML = checkNAN(Math.abs(days_per_year));
			
		var total_owning_cost = (Math.abs(total_expenses) / Math.abs(days_per_year));
			total_owning_cost = total_owning_cost.toFixed(2);
			idHTML("total_owning_cost").innerHTML = checkNAN(Math.round(total_owning_cost));
			idHTML("total_rent_cost").innerHTML = checkNAN(Math.abs(rental_rate));
		
		var comparative_retults = (rental_rate / total_owning_cost - 1) * 100;
			comparative_retults = Math.round(comparative_retults);
			idHTML("comparative_retults").innerHTML = checkNAN(comparative_retults) +"%";
			
			var negNum = (10 - (10 * 2));
			
			if (section_four_total!==0 && section_four_total>=0) {
				if(comparative_retults >= -10){
					idHTML("comparisson").innerHTML = '<br><h3 class="titoloSezione">YOU BETTER BUY (BUT DID YOU REALLY THINK OF ALL YOUR COSTS?)</h3>';
				}else{
					idHTML("comparisson").innerHTML = '<br><h3 class="titoloSezione">RENTAL IS THE BEST SOLUTION!!</h3>';
				}
			}
			
			//alert("Total: "+ comparative_retults);
	
}

function run_calculations(){
	calculate_compare();
}

function numbersonly(myfield, e, dec){
var key;
var keychar;

if (window.event){
   key = window.event.keyCode;
} else if (e){
   key = e.which;
   calculate_compare();
} else {
   return true;
}
keychar = String.fromCharCode(key);

	// control keys
	if ((key===null) || (key===0) || (key===8) || (key===9) || (key===13) || (key===27) )
		{return true;}
	// numbers
	else if ((("0123456789.").indexOf(keychar) > -1))
		{return true;}
	// decimal point jump
	else if (dec && (keychar == "."))
	   {
	   myfield.form.elements[dec].focus();
	   return false;
	   }
	else {
	   return false;
	}
}
