var a = 50000.00;
var p = 11;
var t = 10;
var d = 0;
var htmlCode = "";
var tStr = "";

function Init()
{
  document.forms.mainform["amount"].value = a.toString();
  document.forms.mainform["percent"].value = p.toString();

  onChangeParameter();
}

function roundPrcs(value, precision)
{
  var val = Math.round(value*Math.pow(10,precision));

  val=val<0 ? "" : val.toString();

  val = val.substring(0,val.length-precision)+"."+val.substring(val.length-precision, val.length);

  return val;
}

function onChangeParameter()
{
  switch (document.forms.mainform["calcscheme"].value)
  {
    case "decr" :
      document.getElementById("pay_header").innerText="Платеж в первый месяц";
      break;
    case "equ" :
      document.getElementById("pay_header").innerText="Ежемесячный платеж";
      break;
    default :
      document.getElementById("pay_header").innerText="";
  }

  calcPay();
}

function calcPay()
{
  a = Number(document.forms.mainform["amount"].value);
  p = Number(document.forms.mainform["percent"].value);
  t = Number(document.forms.mainform["term"].value);
  d = Number(document.forms.mainform["pay"].value);

  switch (document.forms.mainform["calcscheme"].value)
  {
    case "decr" :
      d = a/t+a*p/1200;
      break;
    case "equ" :
      d = a*p/1200/(1-Math.pow(1+p/1200,-t));
      break;
    default :
      d=-1;
  }

  document.forms.mainform["pay"].value = roundPrcs(d, 2);
}

function genTable()
{

  a = Number(document.forms.mainform["amount"].value);
  p = Number(document.forms.mainform["percent"].value);
  t = Number(document.forms.mainform["term"].value);
  var scheme="";
  var db, dp, d;
  var sdb=0, sdp=0, sd=0;

//  document.getElementById("pay_table").innerText="A long time ago";

  for(i=0;i<document.forms.mainform["calcscheme"].options.length;i++)
  {
    if(document.forms.mainform["calcscheme"].options.item(i).selected)
    {
      scheme=document.forms.mainform["calcscheme"].options.item(i).innerText;
      break;
    }
  }
  tStr ="";
  tStr = tStr + "<p class='title2'>Условия кредитования:</p>";
  tStr = tStr + "Схема выплат: "+scheme+".<br>";
  tStr = tStr + "Сумма кредита: "+roundPrcs(a,2)+ "<br>";
  tStr = tStr + "Срок кредитования: "+t+" мес. Процентная ставка: "+roundPrcs(p,2)+"%.<br><br>";
  tStr = tStr + "<p class='title2'>График возврата кредита и уплаты процентов за пользование кредитом:</p>";
  tStr = tStr + "<table border=1 width=90% cellspacing=0 cellpadding=5 bgcolor=#сссссс style='border:1px solid white'>";
  tStr = tStr + "<tr bgcolor='#007aee' style='color:white'><td>Период (год)</td><td>Возвращаемая часть кредита</td>";
  tStr = tStr + "<td>Проценты по кредиту</td><td>Общая сумма кредита и процентов</td><td>Задолженность</td></tr>";

  db=Number(roundPrcs(a/t,2));
  d=Number(roundPrcs(a*p/1200/(1-Math.pow(1+p/1200,-t)),2));
  var tyear = 0;
  for(i=1;i<t;i++)
  {
    switch (document.forms.mainform["calcscheme"].value)
    {
      case "decr" :
        dp=Number(roundPrcs(a*p/1200,2));
        d=Number(roundPrcs(db+dp,2));
        a=a-db;
        break;
      case "equ" :
        dp=Number(roundPrcs(a*p/1200,2));
        db=Number(roundPrcs(d-dp,2));
        a=a-db;
        break;
      default :
        dp="";
    }
    if ((i-1)%12 == 0) tyear++;
    tStr = tStr + "<tr  bgcolor='#FEF1CB'><td><nobr>"+i+" мес. (" + tyear + ")</nobr></td><td>"+roundPrcs(db,2)+"</td><td>"+roundPrcs(dp,2)+"</td><td>"+roundPrcs(d,2)+"</td><td>"+roundPrcs(a,2)+"</td></tr>";
    sdp+=dp;
    sdb+=db;
    sd+=d;
  }//for

  db=a;
  dp=Number(roundPrcs(db*p/1200,2));
  d=Number(roundPrcs(db+dp,2));
  sdp+=dp;
  sdb+=db;
  sd+=d;

  tStr = tStr + "<tr bgcolor='#FEF1CB' ><td>"+i+" мес. (" + tyear + ") </td><td>"+roundPrcs(db,2)+"</td><td>"+roundPrcs(dp,2)+"</td><td>"+roundPrcs(d,2)+"</td><td>"+roundPrcs(a,2)+"</td></tr>";

  tStr = tStr + "<tr bgcolor='#007aee' style='color:white'><td><b>Всего:</b></td><td><b>"+roundPrcs(sdb,2)+"</b></td><td><b>"+roundPrcs(sdp,2)+"</b></td><td><b>"+roundPrcs(sd,2)+"</b></td><td bgcolor='white'></td></tr>";

  tStr = tStr + "</table><br>";

  document.getElementById("table_data").innerHTML = "";
  document.getElementById("table_data").innerHTML = tStr;

}
  htmlCode = '';

  htmlCode = htmlCode + '<form id="mainform">';
  htmlCode = htmlCode + '<table border=0 cellspacing=0 celpadding=0><tr><td width=200>Схема выплат</td><td>';
  htmlCode = htmlCode + '  <select class="input" name="calcscheme" onclick="onChangeParameter()" onkeyup="onChangeParameter()">';
  htmlCode = htmlCode + '  <option id="decr" value="decr">Убывающими платежами</option>';
  htmlCode = htmlCode + '  <option selected id="edu" value="equ">Аннуитентными платежами</option>';
  htmlCode = htmlCode + '  </select>  </td></tr><tr><td>Сумма кредита</td><td>';
  htmlCode = htmlCode + '<input  class="input" type="text" size=10 name="amount" value="" onkeyup="onChangeParameter()">';
  htmlCode = htmlCode + '  </td></tr><tr>  <td>    Годовая ставка  </td>';
  htmlCode = htmlCode + '<td><input  class="input" type="text" size=10 name="percent" value="" onkeyup="onChangeParameter()"> %';
  htmlCode = htmlCode + '</td></tr><tr>  <td>  Срок погашения  </td>  <td>';
  htmlCode = htmlCode + '  <select  class="input" name="term" onclick="onChangeParameter()" onkeyup="onChangeParameter()">';
  htmlCode = htmlCode + '<option ';
  if (t == 1) { htmlCode = htmlCode + ' selected '; }
  htmlCode = htmlCode +  ' value="12">1 год</option>';

  var aMonth = 0;

  for(i=2;i<5;i++)
  {
    aMonth = i*12;
    htmlCode = htmlCode + '<option ';
    if (t == i) { htmlCode = htmlCode + ' selected '; }
    htmlCode = htmlCode + ' value="' + aMonth.toString() + '">' + i.toString() + ' года</option>';
  }

  for(i=5;i<21;i++)
  {
    aMonth = i*12;
    htmlCode = htmlCode + '<option ';
    if (t == i) { htmlCode = htmlCode + ' selected '; }
    htmlCode = htmlCode + ' value="' + aMonth.toString() + '">' + i.toString() + ' лет</option>';
  }
  htmlCode = htmlCode + '<option ';
  if (t == 21) { htmlCode = htmlCode + ' selected '; }
  htmlCode = htmlCode + ' value="252">21 год</option>';
  for(i=22;i<25;i++)
  {
    aMonth = i*12;
    htmlCode = htmlCode + '<option ';
    if (t == i) { htmlCode = htmlCode + ' selected '; }
    htmlCode = htmlCode + ' value="' + aMonth.toString() + '">' + i.toString() + ' года</option>';
  }

  for(i=25;i<31;i++)
  {
    aMonth = i*12;
    htmlCode = htmlCode + '<option ';
    if (t == i) { htmlCode = htmlCode + ' selected '; }
    htmlCode = htmlCode + ' value="' + aMonth.toString() + '">' + i.toString() + ' лет</option>';
  }

  htmlCode = htmlCode + '  </select>  </td></tr><tr>  <td>';
  htmlCode = htmlCode + '<span id="pay_header">Платеж в первый месяц</span>  </td>  <td>';
  htmlCode = htmlCode + '<input  class="input" type="text" size=10 name="pay" disabled value="" onkeyup="onChangeParameter()">';
  htmlCode = htmlCode + '<span id="ccy2"></span>  </td></tr></table>';
  htmlCode = htmlCode + '<br><input class="button" type="button" value="Построить график погашения" onclick="genTable()"></form>';
