function KalkulatorKredytowy(formname,resultdiv) {

	this.formname = formname;
	this.resultdiv = resultdiv;

	this.Round = function (value) {
		return (Math.round(value*Math.pow(10,2)))/Math.pow(10,2);
	}
	
	this.Calc = function () {
		this.form = document[this.formname];
		if (!this.CheckFormValues()) return false;

		this.data = new Array();

		this.rate = (this.form.kwota.value / this.form.okres.value);

		this.data[1] = new Array();
	 
		
		this.data[1].intrest = (this.form.kwota.value * (this.form.oproc.value/100/12));

		this.data[1].subtotal = (this.rate + this.data[1].intrest);
		

		for(var i = 2; i <= this.form.okres.value; i++) {
			this.data[i] = new Array();
			this.data[i].intrest = (this.form.kwota.value - (this.rate * ( i - 1) ) ) *  (this.form.oproc.value/100/12);
			this.data[i].subtotal = (this.data[i-1].subtotal + this.rate + this.data[i].intrest);
		}
	
		this.equal = (this.form.oproc.value /100 / 12) * this.form.kwota.value * ( 1 + 1 / ( Math.pow( (this.form.oproc.value /100 / 12) + 1, this.form.okres.value) -1));	
		this.DisplayResults();
	}

	this.CheckFormValues = function () {
		this.form.kwota.value = this.form.kwota.value.replace(",",".");		
		this.form.oproc.value = this.form.oproc.value.replace(",",".");
		this.form.okres.value = this.form.okres.value.replace(",",".");
		this.form.kwota.value = this.Round(this.form.kwota.value);
			
		errorMessage = "";	
		
		if (!IsNumeric(this.form.kwota.value) || this.form.kwota.value <= 0)
			{
				errorMessage = errorMessage + "<li>Kwota musi byc liczba i nie moze byc rowna, badz mniejsza niz 0";
				this.form.kwota.focus();
			}
		if (!IsNumeric(this.form.oproc.value) || this.form.oproc.value <= 0)
			{
				errorMessage = errorMessage + "<li>Oprocentowanie musi byc liczba i nie moze byc rowne, badz mniejsze niz 0";
				this.form.oproc.focus();
			}
		if (!IsNumeric(this.form.okres.value) || this.form.okres.value <= 1)
			{
				errorMessage = errorMessage + "<li>Okres musi byc liczba miesiecy i nie moze byc rowny, badz mniejszy niz 1 miesiac";
				this.form.okres.focus();
			}
	

		if (errorMessage.length > 0)
			{
				errorMessage ="<ul class=\"errorMessage\">" + errorMessage + "</ul>";
		
				document.getElementById(this.resultdiv).innerHTML = errorMessage;
				return false;
			}
		return true;
		
	}

	this.DisplayResults = function () {
		tabelka = "<li>Stala miesieczna rata: "+this.Round(this.equal) + "zl"+
				"<li>Raty malejace: (od "+ this.Round(this.rate + this.data[1].intrest) + "zl do " + this.Round(this.rate + this.data[this.form.okres.value].intrest) + "zl)" +		
				"<table class=\"calcTable\">";
		tabelka = tabelka + 
				'<tr><th>LP' +
				'</th><th>Saldo przed splata' + 
				'</th><th>Rata' + 
				'</th><th>Odsetki' + 
				'</th><th>Rata + Odsetki' + 

				'</th><th>Pozostale saldo' + 
				'</th><th>Suma' +
				'</th></tr>';
		for(var i = 1; i <= this.form.okres.value; i++) {
			tabelka = tabelka + 
				'<tr><td>' + i +
				'</td><td>' + this.Round(this.form.kwota.value - (this.rate * (i - 1))) + 
				'</td><td>' + this.Round(this.rate) + 
				'</td><td>' + this.Round(this.data[i].intrest) + 
				'</td><td>' + this.Round(this.rate + this.data[i].intrest) +
				'</td><td>' + this.Round(this.form.kwota.value - (this.rate * i )) +
				'</td><td>' + this.Round(this.data[i].subtotal) +
				'</td></tr>';

		}		
		tabelka = tabelka + "</table>"		

		document.getElementById(this.resultdiv).innerHTML = tabelka;
		
	}
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
