function doClear(language)
{
	document.loanForm.ammount.value = '';
	document.loanForm.rate.value = '';
	document.loanForm.length.value = 0;
	document.loanForm.monthly.value = '';
	document.loanForm.total.value = '';
}
function doLoan(language)
{
	var length = "";
	var ammount = "";
	var rate = "";
	var monthly = 0.0;
	var monthlyRate = 0.0;

	if (1 == 1) {
		replaceComma(document.loanForm.ammount);
		replaceComma(document.loanForm.rate);
		ammount = document.loanForm.ammount.value;
		rate = document.loanForm.rate.value;
		length = document.loanForm.length.value;

		if (ammount * 1 != ammount) {
			if (language == 'el') alert('Εισάγετε τιμή για το ποσό');
			else alert('Please type a loan amount');

		} else if (rate * 1 != rate) {
			if (language == 'el') alert('Εισάγετε τιμή για το επιτόκιο');
			else if (language == 'en') alert('Please type a loan rate');

		} else if (length == 0)	{
			if (language == 'el') alert('Επιλέξτε περίοδο εξόφλησης');
			else if (language == 'en') alert('Please select the loan term');

		} else if (ammount > 0 && rate > 0)	{
			rate = rate / 100.0;
			monthlyRate = rate / 12.0;
			monthly = ammount * monthlyRate / (1.0 - calculate(monthlyRate, -length));

			document.loanForm.monthly.value = formatNumber(monthly,2,true,false);
			total = formatNumber(monthly,2,true,false) * length;
			document.loanForm.total.value = formatNumber(total,2,true,false);
		} else {
			if (language == 'el') alert('Εισάγετε θετικές αριθμητικές τιμές για το το ποσό και το επιτόκιο');
			else if (language == 'en') alert('Type positive values for the loan amount and the rate');
		}
	}
	return (true);
}

function replaceComma(obj) {
	if (obj.value.indexOf(',') != -1) {
		obj.value = obj.value.substring(0,obj.value.indexOf(',') ) + '.' + obj.value.substring(obj.value.indexOf(',') + 1);
	}
}

function calculate(a, n) {
var i;
var sum, pow, term, cof;

	if(n < 0) return 1.0 / calculate(a, -n);

	sum = 1.0;
	pow = n;
	term = 1;
	cof = 1.0
	for (i = 1; i < 10; i++) {
		cof = cof * pow / i;
		pow = pow - 1.0;
		term = term * a;
		sum = sum + cof * term;
	}

	return sum;
}

function formatNumber(num, decimalNum, bolLeadingZero, bolParens)
/* IN - num:            the number to be formatted
decimalNum:     the number of decimals after the digit
bolLeadingZero: true / false to use leading zero
bolParens:      true / false to use parenthesis for - num

RETVAL - formatted number
*/
{
var tmpNum = num;

// Return the right number of decimal places
tmpNum *= Math.pow(10,decimalNum);
tmpNum = Math.floor(tmpNum);
tmpNum /= Math.pow(10,decimalNum);

var tmpStr = new String(tmpNum);

	// See if we need to hack off a leading zero or not
	if (!bolLeadingZero && num < 1 && num > -1 && num !=0)
	if (num > 0)
		tmpStr = tmpStr.substring(1,tmpStr.length);
	else
		// Take out the minus sign out (start at 2)
		tmpStr = "-" + tmpStr.substring(2,tmpStr.length);

	// See if we need to put parenthesis around the number
	if (bolParens && num < 0)
		tmpStr = "(" + tmpStr.substring(1,tmpStr.length) + ")";
	return tmpStr;
}
