var currency = '';

//
// Do the calculation. 
//
function calculatePayment(form) {

	// P = Lr(1+r/100)^n/[100{(1+r/100)^n - 1}]
	// P is monthly payment
	// L is loan amount
	// r is interest rate
	// n is number of months

	L = 1 * form.amount.value;
	r = 1 * form.interest.value;
	n = 1 * form.months.value;
	
	// String to capture output...
	var str = '';
	
	// Do calculations and build output...
	for (i=0; i < form.type.length; i++) {
		if (form.type[i].checked) {
			val = form.type[i].value
			if (val == 'both' || val =='flat')  {
				P = doFlatRateCalculation(L, r, n);
				str += 
					"<h5>Ta&#351;&#305;t ve &#304;htiya&ccedil; i&ccedil;in</h5>" + 
					"<strong>Toplam &ouml;denecek:</strong> " + formatMoney(n*P) +
					"<strong>Ayl&#305;k &ouml;denecek:</strong> " + formatMoney(P);
			}
			if (val == 'both' || val =='compound')  {
				P = doCompoundCalculation(L, r, n);
				str = str +
					"<h5>Vergisiz </h5>" + 
					"<strong>Toplam &ouml;denecek:</strong> " + formatMoney(n*P) +
					"<strong>Ayl&#305;k &ouml;denecek:</strong> " + formatMoney(P);
			}
			if (val == 'both' || val =='serkan')  {
				P = doSerkanCalculation(L, r, n);
				str = str +
					"<h5>Konut kredisi i&ccedil;in </h5>" + 
					"<strong>Toplam &ouml;denecek:</strong> " + formatMoney(n*P) +
					"<strong>Ayl&#305;k &ouml;denecek:</strong> " + formatMoney(P);
			}
			
		}
	}
	
	document.getElementById('result').innerHTML = str;
	
}

//
// Calculate payment using a compounded rate. 
//
function doCompoundCalculation(L, r, n) {
	// L is loan amount
	// r is APR
	// n is number of months

	P = L * r * Math.pow(1+r/100,n) / (100 * (Math.pow(1+r/100,n) - 1));
	return P;
}

//
// Calculate payment using a flat rate (no compounding). 
//
function doFlatRateCalculation(L, r, n) {
	// L is loan amount
	// r is flat rate
	// n is number of months
	
	P = L * r * 1.20 * Math.pow((1+(r/100)*1.20),n) / (100 * (Math.pow((1+(r/100)*1.20),n) - 1));
	return P;
}
//
// Calculate payment using a flat rate (no compounding). 
//
function doSerkanCalculation(L, r, n) {
	// L is loan amount
	// r is flat rate
	// n is number of months
	
	P = L * r * 1.05 * Math.pow((1+(r/100)*1.05),n) / (100 * (Math.pow((1+(r/100)*1.05),n) - 1));
	return P;
}
//
// Only allow digits and decimal point.
//
function setNumericOnly(object) {
	value = object.value;
	object.value = value.replace(/[^\d\.]/g, "");
}

//
// Format money to 2dp and add commas...
// Based on script by: Cyanide_7 (leo7278@hotmail.com)
// See: http://javascript.internet.com/forms/currency-format.html
// 
function formatMoney(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	dec = num%100;
	num = Math.floor(num/100).toString();
	if(dec<10) dec = "0" + dec;
	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 currency + (((sign)?'':'-') + num + '.' + dec);
}
