
        function round(num, mp) {
          if (mp == null)
            mp = 2;

          mpm = Math.pow(10, mp);
          if (num < 0) {
            num = -num;
            sign = "-";
          } else {
            sign = "";
          }
          pomnum = num * mpm;
          pomnum = Math.ceil(pomnum - 0.5);
          j = -1;
          retstring = "";
          while  ((pomnum > 0) || (mp > 0)) {
            cyfra = (pomnum % 10);
            retstring = cyfra + retstring;
            pomnum = pomnum / 10;
            pomnum = Math.floor(pomnum);
            if (j >= 0) {
              j++;
              if (((j % 3) == 0) && pomnum)
                retstring = " " + retstring;
            } else {
              mp--;
              if (mp == 0) {
                retstring = "." + retstring;
                j = 0;
              }
            }
          }
          if (retstring != 0)
            return sign + retstring;
          else
            return retstring;
        }


  function calculate() {
  	pattern=/^[0-9]+((\.|,)[0-9]+)?$/;
    document.kalkul.kwota.value = document.kalkul.kwota.value.replace(/,/,".");
    document.kalkul.stopa.value = document.kalkul.stopa.value.replace(/,/,".");
    document.kalkul.okres.value = document.kalkul.okres.value.replace(/,/,".");
    if ( document.kalkul.kwota.value==0 || pattern.test(document.kalkul.kwota.value)==false ) 
	alert('Podaj prawidłową wartość kwoty kredytu.');    
    else {
	if ( document.kalkul.stopa.value==0 || pattern.test(document.kalkul.stopa.value)==false ) 
	    alert('Podaj prawidłową wartość oprocentowania nominalnego.');
	else {
	    pattern=/^[0-9]+$/; 
	    if ( document.kalkul.okres.value==0 || pattern.test(document.kalkul.okres.value)==false ) 
		alert('Podaj prawidłową wartość okresu kredytowania.');
	    else {
    
		if (document.kalkul.raty[0].checked) {
    		    calculate1();
		} else {
    		    calculate2();
		}
	    }
	}
    }
  }

  function calculate1() {
    // b6 - dlugosc okresu (w latach)
    if (document.kalkul.okres_rodzaj[0].checked) {
      okres = document.kalkul.okres.value;
// alert("wwww" + document.kalkul.okres.value);

    } else {
      okres = document.kalkul.okres.value / 12;
// alert(okres);
    }

    // b7 - liczba rat w ciagu roku
    if (document.kalkul.kapitalizacja[0].checked) {
      liczba_rat = 12;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkul.kapitalizacja[1].checked) {
      liczba_rat = 4;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkul.kapitalizacja[2].checked) {
      liczba_rat = 1;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkul.kapitalizacja[3].checked) {
      liczba_rat = 1 / okres;
      lrattot = 1;
    }
    pom = (1 - 1 / Math.pow(1 + ((document.kalkul.stopa.value / 100) / liczba_rat), lrattot)) / ((document.kalkul.stopa.value / 100) / liczba_rat);
// alert(pom);
    document.kalkul.rwplaty.value = round((document.kalkul.kwota.value / pom) * lrattot);
    document.kalkul.rodsetki.value = round(((document.kalkul.kwota.value / pom) * lrattot) - document.kalkul.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\">Plan spłaty</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.kalkul.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 trld\">\n";
      } else {
        out_html += "<TR class=\"trd trld\">\n";
      }
      out_html += "<TD align=center width=25%>" + i + ".</TD>\n";
//alert(Math.pow(1 + (document.kalkul.stopa.value / 100), (lrattot - i) + 1));

      rata_kap = (1 / (Math.pow(1 + ((document.kalkul.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.kalkul.rstopa.value = round(((suma_ods / (document.kalkul.kwota.value * (1 - (document.kalkul.prowizja.value / 100)))) / okres) * 100);
      document.getElementById('plan').innerHTML = out_html;
  }

  function calculate2() {
    stopa = document.kalkul.stopa.value;
    if (document.kalkul.okres_rodzaj[0].checked) {
      okres = document.kalkul.okres.value;
    } else {
      okres = document.kalkul.okres.value / 12;
    }
    if (document.kalkul.kapitalizacja[0].checked) {
      lrat = 12;
    } else {
      lrat = 4;
    }
    kredyt = document.kalkul.kwota.value;

//    lrattot = okres * lrat;
    if (document.kalkul.kapitalizacja[0].checked) {
      liczba_rat = 12;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkul.kapitalizacja[1].checked) {
      liczba_rat = 4;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkul.kapitalizacja[2].checked) {
      liczba_rat = 1;
      lrattot = okres * liczba_rat;
    }
    if (document.kalkul.kapitalizacja[3].checked) {
      liczba_rat = 1 / okres;
      lrattot = 1;
    }

    w_raty = kredyt / lrattot;
    suma_ods = 0;
    out_html = "<TABLE width=100% cellpadding=0 cellspacing=0 class=tb1><tr><td colspan=4 class=hd3><table border=0 cellspacing=0 width=100%><tr class=\"trh\"><td class=\"tdm ac\">Plan spłaty</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%>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.kalkul.rwplaty.value = round(plat_total);
    document.kalkul.rodsetki.value = round(suma_ods);
//    document.kalkul.rstopa.value = round((plat_total / (document.kalkul.kwota.value * (1 - (document.kalkul.prowizja.value / 100))) - 1) * 100);
//    document.kalkul.rstopa.value = round(((plat_total / (document.kalkul.kwota.value * (1 - (document.kalkul.prowizja.value / 100))) - 1) / okres) * 100);
//    document.kalkul.rstopa.value = round(((suma_ods / (document.kalkul.kwota.value * (1 - (document.kalkul.prowizja.value / 100)))) / okres) * 100);

      document.getElementById('plan').innerHTML = out_html;
  }

