function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
function CalculateInterestRate(rate)
{
	var theForm;
	theForm = document.getElementById('calculatorForm');
	
	var intrate1, intrate2, totalrate;
	intrate1 = parseFloat(theForm.InterestRate.value.replace(",","."));
	intrate2 = parseFloat(rate);
	totalrate = (intrate1 + intrate2);
	totalrate = roundNumber(totalrate,2);
	
	if (totalrate>0)
	{
		totalrate = totalrate.toString().replace(".", ",");
		theForm.InterestTotalRate.value = totalrate+" %";
	}
	else
	{
		theForm.InterestTotalRate.value = "";
	}
} //end function CalculateInterestRate

function Calculate()
{
	var theForm;
	theForm = document.getElementById('calculatorForm');
	if (cmdCalc_Click(theForm))
	{
		var LoanAmount, InterestTotalRate, Installments, InstallmentAmount;
		InterestTotalRate = theForm.InterestTotalRate.value;
		LoanAmount = theForm.LoanAmount.value;
		Installments = theForm.Installments.value.toString().replace(".", ",");
		InstallmentAmount = calculatePayment(theForm);
		theForm.InstallmentAmount.value = InstallmentAmount.toString().replace(".", ",");
	} 
	else return false;
} //end function Calculate

function checkForZero(field)
{
    if (field.value == 0 || field.value.length == 0) {
        alert ("Η τιμή σε αυτό το πεδίο δεν μπορεί να είναι μηδέν!");
        field.focus(); }
    else
       return false; //calculatePayment(field.form);
}  //end function checkForZero

function cmdCalc_Click(theForm)
{
    if (theForm.LoanAmount.value == 0 || theForm.LoanAmount.value.length == 0) {
        alert ("Η τιμή σε αυτό το πεδίο δεν μπορεί να είναι μηδέν!");
        theForm.LoanAmount.focus();  
		return false;}
    else if (theForm.InterestTotalRate.value == 0 || theForm.InterestTotalRate.value.length == 0) {
        alert ("Η τιμή σε αυτό το πεδίο δεν μπορεί να είναι μηδέν!");
        theForm.InterestTotalRate.focus();  
		return false;}
    else if (theForm.Installments.value == 0 || theForm.Installments.value.length == 0) {
        alert ("Η τιμή σε αυτό το πεδίο δεν μπορεί να είναι μηδέν!");
        theForm.Installments.focus();  
		return false;}
    else
		return true;
}  //end function cmdCalc_Click

function calculatePayment(theForm)
{
    princ = theForm.LoanAmount.value.replace(".", "").replace(",", ".");
    intRate = (theForm.InterestTotalRate.value.replace(',','.').replace('%', '')/100) / 12;
    months = theForm.Installments.value;
    return Math.floor((princ*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100;
}  //end function calculatePayment
