
        function round(liczba, dokladnosc) {
          
          if (!dokladnosc)
            dokladnosc = 2;

          dokladnosc10 = Math.pow(10, dokladnosc);
          
          if (liczba < 0) {
            liczba = -liczba;
            sign = "-";
          } else {
            sign = "";
          }
          
          liczbaafter = liczba * dokladnosc10;
          liczbaafter = Math.ceil(liczbaafter - 0.5);
          j = -1;
          retstring = "";
          while  ((liczbaafter > 0) || (dokladnosc > 0)) {
            cyfra = (liczbaafter % 10);
            retstring = cyfra + retstring;
            liczbaafter = liczbaafter / 10;
            liczbaafter = Math.floor(liczbaafter);
            if (j >= 0) {
              j++;
              if (((j % 3) == 0) && liczbaafter)
                retstring = " " + retstring;
            } else {
              dokladnosc--;
              if (dokladnosc == 0) {
                retstring = "." + retstring;
                j = 0;
              }
            }
          }
          if (retstring != 0)
            return sign + retstring;
          else
            return retstring;
        }


  function calculate() {
  	pattern=/^[0-9]+((\.|,)[0-9]+)?$/;
    document.kalkulator.kwota.value = document.kalkulator.kwota.value.replace(/,/,".");
    document.kalkulator.stopa.value = document.kalkulator.stopa.value.replace(/,/,".");
    document.kalkulator.okres.value = document.kalkulator.okres.value.replace(/,/,".");
    if ( document.kalkulator.kwota.value==0 || pattern.test(document.kalkulator.kwota.value)==false ) 
	alert('Podaj prawidłową wartość kwoty kredytu.');    
    else {
	if ( document.kalkulator.stopa.value==0 || pattern.test(document.kalkulator.stopa.value)==false ) 
	    alert('Podaj prawidłową wartość oprocentowania nominalnego.');
	else {
	    pattern=/^[0-9]+$/; 
	    if ( document.kalkulator.okres.value==0 || pattern.test(document.kalkulator.okres.value)==false ) 
		alert('Podaj prawidłową wartość okresu kredytowania.');
	    else {
    
		if (document.kalkulator.raty[0].checked) {
    		    calculate1();
		} else {
    		    calculate2();
		}
	    }
	}
    }
  }

  function calculate1() {
    // b6 - dlugosc okresu (w latach)
    if (document.kalkulator.okres_rodzaj[0].checked) {
      okres = document.kalkulator.okres.value;
// alert("wwww" + document.kalkulator.okres.value);

    } else {
      okres = document.kalkulator.okres.value / 12;
// alert(okres);
    }

    // b7 - liczba rat w ciagu roku
    if (document.kalkulator.kapitalizacja[0].checked) {
      liczba_rat = 12;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkulator.kapitalizacja[1].checked) {
      liczba_rat = 4;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkulator.kapitalizacja[2].checked) {
      liczba_rat = 1;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkulator.kapitalizacja[3].checked) {
      liczba_rat = 1 / okres;
      lrattot = 1;
    }
    pom = (1 - 1 / Math.pow(1 + ((document.kalkulator.stopa.value / 100) / liczba_rat), lrattot)) / ((document.kalkulator.stopa.value / 100) / liczba_rat);
// alert(pom);
    document.kalkulator.rwplaty.value = round((document.kalkulator.kwota.value / pom) * lrattot);
    document.kalkulator.rodsetki.value = round(((document.kalkulator.kwota.value / pom) * lrattot) - document.kalkulator.kwota.value);

    out_html = "<TABLE width=100% class=tb1><tr><td colspan=4 class=hd3><table border=0 cellspacing=0 width=100%><tr class=\"trh\"><td class=\"tdm ac\"><h2>Plan sp�aty</h2></td></tr></table></td></tr>\n";
    out_html += "<TR class=trc>\n";
    out_html += "  <TD align=center width=25%>L.p.</TD>\n";
    out_html += "  <TD align=center width=25%>Rata</TD>\n";
    out_html += "  <TD align=center width=25%>Odsetki</TD>\n";
    out_html += "  <TD align=center width=25%>Kapita�</TD>\n";
    out_html += "</TR>\n"
    rsplata = document.kalkulator.kwota.value / pom;
    plat_total = 0;
    suma_ods = 0;
    kredyt = 0;
    for (i = 1 ; i <= lrattot ; i++) {
      if (i % 2) {
        out_html += "<TR class=trl>\n";
      } else {
        out_html += "<TR class=trd>\n";
      }
      out_html += "<TD align=center width=25%>" + i + ".</TD>\n";
//alert(Math.pow(1 + (document.kalkulator.stopa.value / 100), (lrattot - i) + 1));

      rata_kap = (1 / (Math.pow(1 + ((document.kalkulator.stopa.value / 100) / liczba_rat), (lrattot - i) + 1))) * rsplata;
      odsetki = rsplata-rata_kap;
      suma_ods += odsetki;
      plat_total += rsplata;
      kredyt += rata_kap;
      out_html += "<TD align=center width=25%><B>" + round(rsplata) + "</B></TD>";
      out_html += "<TD align=center width=25%>" + round(odsetki) + "</TD>";
      out_html += "<TD align=center width=25%>" + round(rata_kap) + "</TD>\n";
      out_html += "</TR>\n";
    }
    out_html += "<TR class=trc>\n";
    out_html += "<TD  align=center width=25% valign=bottom><B>w sumie:</B></TD>\n";
    plat_total = Number(kredyt) + Number(suma_ods);
    out_html += "  <TD  align=center width=25% valign=bottom><B>" + round(plat_total) + "</B></TD>\n";
    out_html += "  <TD  align=center width=25% valign=bottom><B>" + round(suma_ods) + "</B></TD>\n";
    out_html += "  <TD  align=center width=25% valign=bottom><B>" + round(kredyt) + "</B></TD>\n";
    out_html += "</TR>\n";
    out_html += "</TABLE>\n";
    //document.kalkulator.rstopa.value = round(((suma_ods / (document.kalkulator.kwota.value * (1 - (document.kalkulator.prowizja.value / 100)))) / okres) * 100);
      document.getElementById('plan').innerHTML = out_html;
  }

  function calculate2() {
    stopa = document.kalkulator.stopa.value;
    if (document.kalkulator.okres_rodzaj[0].checked) {
      okres = document.kalkulator.okres.value;
    } else {
      okres = document.kalkulator.okres.value / 12;
    }
    if (document.kalkulator.kapitalizacja[0].checked) {
      lrat = 12;
    } else {
      lrat = 4;
    }
    kredyt = document.kalkulator.kwota.value;

//    lrattot = okres * lrat;
    if (document.kalkulator.kapitalizacja[0].checked) {
      liczba_rat = 12;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkulator.kapitalizacja[1].checked) {
      liczba_rat = 4;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkulator.kapitalizacja[2].checked) {
      liczba_rat = 1;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkulator.kapitalizacja[3].checked) {
      liczba_rat = 1 / okres;
      lrattot = 1;
    }

    w_raty = kredyt / lrattot;
    suma_ods = 0;
    out_html = "<TABLE width=100% cellpadding=3 cellspacing=1 class=tb1><tr><td colspan=4 class=hd3><h2>Plan sp�aty</h2></td></tr>\n";
    out_html += "<TR class=trc>\n";
    out_html += "  <TD align=center width=25%>L.p.</TD>\n";
    out_html += "  <TD align=center width=25%>Rata</TD>\n";
    out_html += "  <TD align=center width=25%>Odestki</TD>\n";
    out_html += "  <TD align=center width=25%>Kapital</TD>\n";
    out_html += "</TR>\n"
    for (i = 1 ; i <= lrattot ; i++) {
      if (i % 2) {
        out_html += "<TR class=trl>\n";
      } else {
        out_html += "<TR class=trd>\n";
      }
      out_html += "<TD align=center width=25%>" + i + ".</TD>\n";
      odsetki = ((stopa / 100) / lrat) * (kredyt - (w_raty * (i - 1)));
      suma_ods += odsetki;
      do_zaplaty = w_raty + odsetki;
      out_html += "<TD align=center width=25%><B>" + round(do_zaplaty) + "</B></TD>";
      out_html += "<TD align=center width=25%>" + round(odsetki) + "</TD>";
      out_html += "<TD align=center width=25%>" + round(w_raty) + "</TD>\n";
      out_html += "</TR>\n"
    }
    out_html += "<TR class=trc>\n";
    out_html += "<TD  align=center width=25% valign=bottom><B>w sumie:</B></TD>\n";
    plat_total = Number(kredyt) + Number(suma_ods);
    out_html += "  <TD  align=center width=25% valign=bottom><B>" + round(plat_total) + "</B></TD>\n";
    out_html += "  <TD  align=center width=25% valign=bottom><B>" + round(suma_ods) + "</B></TD>\n";
    out_html += "  <TD  align=center width=25% valign=bottom><B>" + round(kredyt) + "</B></TD>\n";
    out_html += "</TR>\n";
    out_html += "</TABLE>\n";
    document.kalkulator.rwplaty.value = round(plat_total);
    document.kalkulator.rodsetki.value = round(suma_ods);
//    document.kalkulator.rstopa.value = round((plat_total / (document.kalkulator.kwota.value * (1 - (document.kalkulator.prowizja.value / 100))) - 1) * 100);
//    document.kalkulator.rstopa.value = round(((plat_total / (document.kalkulator.kwota.value * (1 - (document.kalkulator.prowizja.value / 100))) - 1) / okres) * 100);
//    document.kalkulator.rstopa.value = round(((suma_ods / (document.kalkulator.kwota.value * (1 - (document.kalkulator.prowizja.value / 100)))) / okres) * 100);

      document.getElementById('plan').innerHTML = out_html;
  }


