function pmt(interest, months, principal) {
    	base = interest + 1;
    	t = Math.pow(base, months);
    	result = principal * t * interest / (t - 1);
    	return result;
}

function getMonthlyRate(totalPrice, yearInterest, months) {
	TAX = 1.08;
	capital = (totalPrice * TAX) / months;	
	sumaFinantata = totalPrice * TAX;
	interest = yearInterest / 12;

	principal = sumaFinantata;
	pmtValue = pmt(interest, months, principal);    	
	monthlyInterest = ((pmtValue * months) - totalPrice * TAX) / months;
    	rataLunara = capital + monthlyInterest;

	return Math.round(rataLunara * 100) / 100;
}

function displayRates(totalPriceRon, euro) {
	ronYearInterest = 0.2555;
	euroYearInterest = 0.18225;
	months = new Array(12, 18, 24, 36, 48, 60);
	for (i=0; i < months.length; i++) {		
		rateRon = getMonthlyRate(totalPriceRon, ronYearInterest, months[i]);
		rateEuro = getMonthlyRate(totalPriceRon / euro, euroYearInterest, months[i]);
		document.getElementById('ron' + months[i]).innerHTML = rateRon;
		document.getElementById('eur' + months[i]).innerHTML = rateEuro;
	}	
}

function displayRONRate(price, months) {
        ronYearInterest = 0.2555;
        rate = getMonthlyRate(price, ronYearInterest, months);
	document.getElementById('ron-rate').innerHTML = rate;
}

function displayEURORate(price, months, euro) {
        euroYearInterest = 0.18225;
        rate = getMonthlyRate(price / euro, euroYearInterest, months);
	document.getElementById('euro-rate').innerHTML = rate;
}

function isDigit(num) {
	if (num.length > 1) {
		return false;
	}

	var string="1234567890";
	if (string.indexOf(num) != -1) {
		return true;
	}

	return false;
}

function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
		}
	return true;
}

function isInteger(val){
	if (isBlank(val)) {
		return false;
	}
	
	for(var i = 0; i < val.length; i++){
		if(!isDigit(val.charAt(i))) { 
			return false;
		}
	}
	return true;
}

function displayCnRate(totalPrice, months) {
	if (!isInteger(totalPrice)) {
		alert("Valoare introdusa in campul \"Valoare credit\" trebuie sa fie un numar intreg.");
	}

	TAX = 1.06;
	yearInterest = 0.33;
	capital = (totalPrice * TAX) / months;	
	sumaFinantata = totalPrice * TAX;
	interest = yearInterest / 12;

	principal = sumaFinantata;
	pmtValue = pmt(interest, months, principal);	
	monthlyInterest = ((pmtValue * months) - totalPrice * TAX) / months;
    	rataLunara = capital + monthlyInterest;

	document.getElementById("rate").innerHTML = Math.round(rataLunara * 100) / 100 + ' RON';
}
