$(document).ready(function(){
const sipDuration=document.querySelector("#rate");
sipDuration.addEventListener("change", function(e){
const value=this.value;
const maxValue=e.target.getAttribute("max");
let abs=Math.floor((value / maxValue) * 100);
abs=abs - 2.5;
});
const sipDurationVal=document.querySelector("#rateVal");
sipDurationVal.addEventListener("keyup", function(e){
const value=this.value;
const maxValue=e.target.getAttribute("max");
let abs=Math.floor((value / maxValue) * 100);
abs=abs - 2.5;
});
const comDuration=document.querySelector("#years");
comDuration.addEventListener("input", function(e){
const value=this.value;
const maxValue=e.target.getAttribute("max");
const abs=Math.floor((value / maxValue) * 100);
});
const comDurationVal=document.querySelector("#timeVal");
comDurationVal.addEventListener("keyup", function(e){
const value=this.value;
const maxValue=e.target.getAttribute("max");
const abs=Math.floor((value / maxValue) * 100);
});
const comDurationfcpa=document.querySelector("#withdrawal");
comDurationfcpa.addEventListener("input", function(e){
const value=this.value;
const maxValue=e.target.getAttribute("max");
const abs=Math.floor((value / maxValue) * 100);
});
const comDurationValfcpaTxt=document.querySelector("#withdrawlVal");
comDurationValfcpaTxt.addEventListener("input", function(e){
const value=this.value&&this.value.replaceAll(",", "");
const maxValue=e.target.getAttribute("max");
const abs=Math.floor((value / maxValue) * 100);
console.log("fcpatxt", value, maxValue, abs);
});
$(document).on("keypress",
"#total-investment,.variable-input-class ",
function(e){
var charCode=e.which ? e.which:e.keyCode;
if(charCode > 31&&(charCode < 48||charCode > 57)){
return false;
}}
);
$(document).on("keyup",
"#total-investment, .variable-input-class",
function(e){
var val=this.value;
val=val.replace(/,/g, "");
if(isNaN(val)||val==""){
val=0;
}
this.value=parseInt(val).toLocaleString("en-IN", {
maximumFractionDigits: 0,
minimumFractionDigits: 0,
});
}
);
$("#rate, #rateVal, #years, #timeVal, #withdrawal, #withdrawlVal").on("input", function (){
swpFixedCashFLowCalc();
});
var input=document.querySelector("#total-investment");
input.addEventListener("input", resizeInput);
resizeInput.call(input);
function resizeInput(){
this.style.width=this.value.length + "ch";
}
var inputCashFlow=document.querySelector("#withdrawlVal");
inputCashFlow.addEventListener("input", resizeInputCashFLow);
resizeInputCashFLow.call(inputCashFlow);
$("#withdrawal").on("input", function(event){
var inputCashFlow=document.querySelector("#withdrawlVal");
inputCashFlow.style.width=inputCashFlow.value.length + "ch";
});
function resizeInputCashFLow(){
this.style.width=this.value.length + "ch";
}
$("#total-investment").on("keyup", function(event){
swpFixedCashFLowCalc()
});
function bindInputEvents(inputId, outputId){
const inputElement=$(inputId);
const outputElement=$(outputId);
inputElement.on("input", function(event){
outputElement.val(inputElement.val());
swpFixedCashFLowCalc()
});
outputElement.on("input", function(event){
inputElement.val(outputElement.val());
swpFixedCashFLowCalc()
});
}
$(document).ready(function(){
bindInputEvents("#rate", "#rateVal");
bindInputEvents("#years", "#timeVal");
bindInputEvents("#withdrawal", "#withdrawlVal");
swpFixedCashFLowCalc()
});
function formatRupee(amount){
return new Intl.NumberFormat("en-IN", {
style: "currency",
currency: "INR",
minimumFractionDigits: 0,
}).format(amount);
}
function swpFixedCashFLowCalc(){
let swpInterest=$("#rate").val();
console.log("swpInterest", swpInterest);
const swpDuration=$("#years").val();
console.log("swpDuration", swpDuration);
let principalAmount=$("#total-investment")
.val()
.replaceAll(",", "");
console.log("principalAmount", principalAmount);
if(principalAmount.length==0){
principalAmount=0;
$("#total-investment").css("width", "2ch");
}
let swpInterestTxt=parseFloat($("#rateVal").val());
if(isNaN(swpInterestTxt)||swpInterestTxt <=1){
swpInterestTxt=1;
}
if(swpInterestTxt >=15){
swpInterestTxt=15;
}
let swpDurationTxt=parseInt($("#timeVal").val());
if(isNaN(swpDurationTxt)||swpDurationTxt <=1){
swpDurationTxt=1;
}
if(swpDurationTxt >=30){
swpDurationTxt=30;
}
let fcpaTXT=$("#withdrawlVal");
if(fcpaTXT.val()==""||fcpaTXT.val() <=10){
fcpaTXT.val(10);
$("#withdrawlVal").css("width", "2ch");
}
if(fcpaTXT.val() >=2000000){
fcpaTXT.val(2000000);
}
const swpInvestedAmount=$("#invested-amount");
const swpPresentValue=$("#total-value");
const swpNetPresentValue=$("#total-withdrawn");
let monthlyWithdrawl=parseFloat($("#withdrawlVal").val().replaceAll(",", ""));
console.log("monthlyWithdrawl", monthlyWithdrawl);
let n=12;
let r=swpInterestTxt / (n * 100);
let t=swpDurationTxt;
let balance=parseFloat(principalAmount);
for (let i=0; i < t * 12; i++){
balance=(balance - monthlyWithdrawl) * (1 + r);
if(balance < 0){
balance=0;
break;
}}
let result=Math.round(balance);
console.log("Result", result);
swpPresentValue.text(formatRupeeMinus(result));
let netPresentValue=(monthlyWithdrawl * n * t);
swpNetPresentValue.text(formatRupee(netPresentValue));
let formattedPrincipalAmount=formatRupee(principalAmount);
console.log("Formatted Principal Amount:", formattedPrincipalAmount);
$("#invested-amount").text(formattedPrincipalAmount);
}
function formatRupeeMinus(rupee=0){
if(rupee > 0){
return (
"₹" + rupee.toLocaleString(undefined, {
minimumFractionDigits: 0
})
);
}else{
return (
"-₹" +
Math.abs(rupee).toLocaleString(undefined, {
minimumFractionDigits: 0,
})
);
}}
});