    var sum=0.01;var firstSumPercent=0.01;var sumUnderTax=0.01;var period=0.01;var payCounts=0.01;var taxRate=0.01;var htmlToinsert;var rowToInsert;var tableHeader;var tableFooter;var firstSum=0.01;var periodPay=0.01;

    var sumA = 0;var sumB = 0;var sumC = 0;var sumD = 0;var sumE = 0;var sumF = 0;var i = 0;
    tableHeader = ' <table id="tableForView" > <tr style="font-family: Arial Narrow; font-size: 13px; font-weight: bold; font-style: normal; font-variant: small-caps; background-color: #808080; color: #FFFFFF; table-layout: fixed; border-collapse: collapse; empty-cells: hide; caption-side: top; text-align: center;"><td width="40px"> &#1053;&#1086;&#1084;&#1077;&#1088; &#1087;&#1083;&#1072;&#1090;&#1077;&#1078;&#1072; </td><td width="120px"> &#1057;&#1091;&#1084;&#1084;&#1072; &#1082; &#1074;&#1099;&#1087;&#1083;&#1072;&#1090;&#1077; &#1074; &#1085;&#1072;&#1095;&#1072;&#1083;&#1077; &#1087;&#1077;&#1088;&#1080;&#1086;&#1076;&#1072;</td><td width="120px"> &#1055;&#1083;&#1072;&#1090;&#1077;&#1078; &#1087;&#1086; &#1089;&#1091;&#1084;&#1084;&#1077; &#1088;&#1072;&#1089;&#1089;&#1088;&#1086;&#1095;&#1082;&#1080;</td><td width="120px"> &#1055;&#1088;&#1086;&#1094;&#1077;&#1085;&#1090; &#1087;&#1086; &#1088;&#1072;&#1089;&#1089;&#1088;&#1086;&#1095;&#1082;&#1077;</td><td width="120px"> &#1054;&#1089;&#1090;&#1072;&#1090;&#1086;&#1082; &#1089;&#1091;&#1084;&#1084;&#1099; &#1074; &#1082;&#1086;&#1085;&#1094;&#1077; &#1087;&#1077;&#1088;&#1080;&#1086;&#1076;&#1072;</td><td width="120px"> &#1057;&#1091;&#1084;&#1084;&#1072; &#1082; &#1091;&#1087;&#1083;&#1072;&#1090;&#1077;</td></tr>';
    tableFooter = ' </table> ';

    function drawTable() {
        htmlToinsert = tableHeader;

        sum = document.getElementById("sum").value;
        firstSumPercent = document.getElementById("firstSumPercent").value;
        sumUnderTax = document.getElementById("sumUnderTax").value;
        period = document.getElementById("period").value;
        payCounts = document.getElementById("payCounts").value;
        taxRate = document.getElementById("taxRate").value;
        firstSum = Math.round(sum * firstSumPercent,0)/100;
        sumUnderTax = sum - firstSum;
        document.getElementById("firstSum").value = firstSum.toFixed(2);
        document.getElementById("sumUnderTax").value = sumUnderTax.toFixed(2);
        periodPay = Math.round(sumUnderTax*10000/ payCounts)/10000;

         sumA = 0;sumB = 0;sumC = 0;sumD = 0;sumE = 0;sumF = 0;
      
        for (i = 1; i < payCounts; i++) {
            rowToInsert = '<tr id="row' + i + '" style="font-family: Arial; font-size: 12px; font-weight: bold; font-style: normal; font-variant: small-caps; background-color: #FFFFFF; color: #000000; table-layout: fixed; border-collapse: collapse; empty-cells: hide; caption-side: top; text-align: right;"> <td>' + i + '</td><td id="sumA' + i + '">' + (sumUnderTax - periodPay * (i - 1)) + '</td>' + '<td id="sumB' + i + '">' + periodPay + '</td>' + '<td id="sumC' + i + '">' + Math.round((taxRate * (period / payCounts) * (sumUnderTax - periodPay * (i - 1))) / 100, 2) + '</td>' + '<td id="sumD' + i + '">' + (sumUnderTax - (periodPay * i)) + '</td>' + '<td id="sumE' + i + '">' + (Math.round((taxRate * (period / payCounts) * (sumUnderTax - periodPay * (i - 1))) / 100, 2) + periodPay) + '</td>';
            htmlToinsert += rowToInsert;
            sumB = sumB + periodPay;
            sumC = sumC + Math.round((taxRate  * (sumUnderTax - periodPay * (i - 1)))/100 , 2);
            sumE = sumE +(Math.round((taxRate * (sumUnderTax - periodPay * (i - 1)))/100 , 2) + periodPay);
        }

        rowToInsert = '<tr id="row' + i + '" style="font-family: Arial; font-size: 12px; font-weight: bold; font-style: normal; font-variant: small-caps; background-color: #FFFFFF; color: #000000; table-layout: fixed; border-collapse: collapse; empty-cells: hide; caption-side: top; text-align: right;"> <td>' + i + '</td><td id="sumA' + i + '">' + (sumUnderTax - periodPay * (i - 1)) + '</td>' + '<td id="sumB' + i + '">' + (sumUnderTax - periodPay * (i - 1)) + '</td>' + '<td id="sumC' + i + '">' + Math.round((taxRate * (period / payCounts) * (sumUnderTax - periodPay * (i - 1))) / 100, 2) + '</td>' + '<td id="sumD' + i + '">' + /*(sumUnderTax - (periodPay * i))*/0 + '</td>' + '<td id="sumE' + i + '">' + (Math.round((taxRate * (period / payCounts) * (sumUnderTax - periodPay * (i - 1))) / 100, 2) + (sumUnderTax - periodPay * (i - 1))) + '</td>';
        htmlToinsert += rowToInsert;

          
        sumB += (sumUnderTax - periodPay * (i - 1));
        sumC += Math.round((taxRate *  (sumUnderTax - periodPay * (i - 1)))/100 , 2);
        sumE += (Math.round((taxRate * (sumUnderTax - periodPay * (i - 1)))/100 , 2) + (sumUnderTax - periodPay * (i - 1)));


        rowToInsert = '<tr id="row' + i + '" style="font-family: Arial Narrow; font-size: 14px; font-weight: bold; font-style: normal; font-variant: small-caps; background-color: #808080; color: #FFFFFF; table-layout: fixed; border-collapse: collapse; empty-cells: hide; caption-side: top; text-align: right;"> <td>' + "&#1048;&#1090;&#1086;&#1075;&#1086;" + '</td><td id="sumA' + i + '">' + '</td>' + '<td id="sumB' + i + '">' + sumB + '</td>' + '<td id="sumC' + i + '">' + sumC + '</td>' + '<td id="sumD' + i + '">' + sumD + '</td>' + '<td id="sumE' + i + '">' + sumE + '</td>';
        htmlToinsert += rowToInsert;

        htmlToinsert += tableFooter;
        document.getElementById("monthPay").value = (Math.round(sumE*100/payCounts,0)/100).toFixed(2);
        document.getElementById("totalSum").value = ((((Math.round(sumE*100/payCounts,0)/100)*payCounts)*100 + firstSum*100)/100).toFixed(2);
        //document.getElementById("sum").value = Number(sum).toFixed(2);

        //document.getElementById("tablePlace").innerHTML = htmlToinsert;
    }

    function makeReCalc() {
        
        sum = document.getElementById("sum").value;
        firstSumPercent = document.getElementById("firstSumPercent").value;
        sumUnderTax = document.getElementById("sumUnderTax").value;
        period = document.getElementById("period").value;
        payCounts = document.getElementById("payCounts").value;
        taxRate = document.getElementById("taxRate").value;
        firstSum = Math.round(sum * firstSumPercent,0)/100;
        sumUnderTax = sum - firstSum;
        document.getElementById("firstSum").value = firstSum;
        document.getElementById("sumUnderTax").value = sumUnderTax;
        periodPay = Math.round(sumUnderTax*10000/ payCounts)/10000;

        drawTable();
       

    }
   
