// JavaScript Document
function calctax(){
    var cost_of_equipment = document.getElementById("cost_of_equipment");
	var section_deduction = document.getElementById("section_deduction");
	var first_year_deduction = 0;/*document.getElementById("first_year_deduction");*/
	var total_first_year = document.getElementById("total_first_year");
	var cash_saving_on_your_equipment = document.getElementById("cash_saving_on_your_equipment");
	var cost_of_new_equipment = document.getElementById("cost_of_new_equipment");
	var bonus_depreciation = document.getElementById("bonus_depreciation");

	if(cost_of_equipment.value == ""){
		alert("Please enter a Cost of Equipment");
		return;
	}
	
	var totalCost = 0;
	try
	{
		var sValue = cost_of_equipment.value.replace("$", "");
		var sValue = sValue.replace(",", "");
	
		totalCost = parseFloat(sValue);
		
		if(isNaN(totalCost))
			throw("Number is not in a correct format: " + cost_of_equipment.value);
	}
	catch(e)
	{
		alert("Please enter a proper Dollar amount");            
		return;
	}
	
	var valueB4 = 0;
			
	if(totalCost >= 250000)
		valueB4 = 250000;
	else
		valueB4 = totalCost;  
	
	var valueB9 = 0;
				   
	if(totalCost >= 250000)
		valueB9 = ((totalCost - 250000) * .50);
	else
		valueB9 = 0;   
		
	var valueB5 = 0;
	valueB5 = ((totalCost - (valueB9 + valueB4)) * .20);
		
	section_deduction.value = formatCurrency(valueB4);
	bonus_depreciation.value = formatCurrency(valueB9);
	//first_year_deduction.value = formatCurrency(valueB5);
	total_first_year.value = formatCurrency(valueB4 + valueB5 + valueB9);                
	cash_saving_on_your_equipment.value = formatCurrency((valueB4 + valueB5 + valueB9) * .35);
	cost_of_new_equipment.value = formatCurrency(totalCost - ((valueB4 + valueB5 + valueB9) * .35));
}

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);
}

window.addEvent('domready', function(){
	$('calcbutton').addEvent('click', function(e){
	    e.stop();
		calctax();
	});
});
