/*
 *	by tGDA, t_gda$_AT_$hotmail.com
 *	20060916
 */
 
var ProfitRate=0.0725;
var TotalAmount, DPercentage, DPercentage_100, Tenor;
var DownPayment, FinancingAmount, MonthlyInstallment, MonthlyIncome, TotalProfitAmount;

function calc_new_vals() {
	//preparing the total amount
	TotalAmount=parseInt(document.getElementById("input_total_amount").value);
	if(isNaN(TotalAmount)) {
		TotalAmount=1;
	}
	//preparing the down payment percentage
	DPercentage=parseInt(document.getElementById("input_dp_percentage").value);
	if(isNaN(DPercentage)) {
		DPercentage=1;
	}
	DPercentage_100=DPercentage/100;
	//preparing the tenor
	Tenor=parseInt(document.getElementById("input_tenor").value);
	if(isNaN(Tenor)) {
		Tenor=1;
	}
	
	//calculating the new results...
	//>Down Payment
	DownPayment=Math.round(TotalAmount*DPercentage_100);
	set_output('output_down_payment', DownPayment);
	//>FinancingAmount
	FinancingAmount=Math.round(TotalAmount-DownPayment);
	set_output('output_financing_amount', FinancingAmount);
	//>MonthlyInstallment
	MonthlyInstallment=Math.round(((FinancingAmount*ProfitRate*Tenor)+FinancingAmount)/(Tenor*12));
	set_output('output_monthly_installment', MonthlyInstallment);
	//>MonthlyIncome
	MonthlyIncome=Math.round(MonthlyInstallment*2.5);
	set_output('output_monthly_income', MonthlyIncome);
	//>MonthlyIncome
	TotalProfitAmount=Math.ceil(FinancingAmount*ProfitRate*Tenor);
	set_output('output_total_profit_amount', TotalProfitAmount);
	
}
var ThousandGrouping=true;
function thousandGroup(num) {
	var isNeg=num<0;
	num=Math.abs(num)+"";
	var resultNum="";
	for(var l=num.length; l>=0; l--) {
		if(l!=(num.length-1) && (l-num.length+1)%3==0) {
			resultNum=","+resultNum;
		}
		resultNum=num.charAt(l)+resultNum;
	}
	if(isNeg) {
		resultNum="-"+resultNum;
	}
	return resultNum;
}
function set_output(elemId, newVal) {
	if (isNaN(newVal) || !isFinite(newVal)) {
		return;
	}
	if(ThousandGrouping) {
		newVal=thousandGroup(newVal);
	}
	document.getElementById(elemId).innerHTML=newVal;
}

function is_num(evt) {
	evt = evt ? evt : event;
	var charCode=evt.which? evt.which : evt.keyCode;
	//alert(charCode);
	
	return (charCode>=96 && charCode<=105) || (charCode!=32) && (charCode<=57);
}