$(document).ready(function() { if ( location.href.indexOf( 'language=fr' ) > 0 ){ var floating = ","; }else{ var floating = "."; } var type = "shares"; var amount = 1; $('.date-sel').find(':selected').each(function(){ let value = $(this).text(); $(this).parent().next().text(value); }); $('.date-sel').change(function(){ let value = $(this).find(':selected').text(); $(this).next().text(value); }); $('.sharesInput').change(function(){ amount = this.value; getData(startDate,endDate,amount,type); }); $('.investInput').change(function(){ amount = this.value; getData(startDate,endDate,amount,type); }); $('.sharesInput').focus(function(){ $("#shares").prop("checked", "checked" ); $(".sharesRow").css("background-color", "#F5F5F5"); $(".investRow").css("background-color", "#FFFFFF"); $(".sharesLeft").css("color", "#002364"); $(".investLeft").css("color", "#8e8c8a"); $(".investInput").css({"color" : "#8e8c8a","border" : "1px solid #ccc"}); $(".sharesInput").css({"color" : "#000","border" : "1px solid #a3a3a3"}); type = "shares"; }); $('.investInput').focus(function(){ $("#invest").prop("checked", "checked" ); $(".sharesRow").css("background-color", "#FFFFFF"); $(".investRow").css("background-color", "#F5F5F5"); $(".sharesLeft").css("color", "#8e8c8a"); $(".investLeft").css("color", "#002364"); $(".sharesInput").css({"color" : "#8e8c8a","border" : "1px solid #ccc"}); $(".investInput").css({"color" : "#000","border" : "1px solid #a3a3a3"}); type = "money"; }); if($("#shares").prop("checked")) { $(".sharesRow").css("background-color", "#F5F5F5"); $(".investRow").css("background-color", "#FFFFFF"); $(".sharesLeft").css("color", "#002364"); $(".investLeft").css("color", "#8e8c8a"); $(".investInput").css({"color" : "#8e8c8a","border" : "1px solid #ccc"}); $(".sharesInput").css({"color" : "#000","border" : "1px solid #a3a3a3"}); }; if($("#invest").prop("checked")) { $(".sharesRow").css("background-color", "#FFFFFF"); $(".investRow").css("background-color", "#F5F5F5"); $(".sharesLeft").css("color", "#8e8c8a"); $(".investLeft").css("color", "#002364"); $(".sharesInput").css({"color" : "#8e8c8a","border" : "1px solid #ccc"}); $(".investInput").css({"color" : "#000","border" : "1px solid #a3a3a3"}); }; $('input[type=radio][name=typeselect]').change(function() { if (this.value == 'shares') { type = "shares"; $(".sharesRow").css("background-color", "#F5F5F5"); $(".investRow").css("background-color", "#FFFFFF"); $(".sharesLeft").css("color", "#002364"); $(".investLeft").css("color", "#8e8c8a"); $(".investInput").css({"color" : "#8e8c8a","border" : "1px solid #ccc"}); $(".sharesInput").css({"color" : "#000","border" : "1px solid #a3a3a3"}); } else { type = "money"; $(".sharesRow").css("background-color", "#FFFFFF"); $(".investRow").css("background-color", "#F5F5F5"); $(".sharesLeft").css("color", "#8e8c8a"); $(".investLeft").css("color", "#002364"); $(".sharesInput").css({"color" : "#8e8c8a","border" : "1px solid #ccc"}); $(".investInput").css({"color" : "#000","border" : "1px solid #a3a3a3"}); } }); var startDay = $('#jour_deb').val(); var startMonth = $('#mois_deb').val(); var startYear = $('#annee_deb').val(); var startMonthStr = $('#mois_deb').find(":selected").text(); var endDay = $('#jour_fin').val(); var endMonth = $('#mois_fin').val(); var endYear = $('#annee_fin').val(); var endMonthStr = $('#mois_deb').find(":selected").text(); var startDate = Number(startYear + "" + startMonth + "" + startDay); var endDate = Number(endYear + "" + endMonth + "" + endDay); var startDateStr = startDay + " " + startMonthStr + " " + startYear; var endDateStr = endDay + " " + endMonthStr + " " + endYear; $('.inlineDateStart').text(startDateStr); $('.inlineDateEnd').text(endDateStr); $('#jour_deb').change(function(){ startDay = this.value; startDate = Number(startYear + "" + startMonth + "" + startDay); startDateStr = startDay + " " + startMonthStr + " " + startYear; $('.inlineDateStart').text(startDateStr); if( compareDate ( startDate, endDate ) ){ getData(startDate,endDate,amount,type); } }); $('#mois_deb').change(function(){ startMonthStr = $('#mois_deb').find(":selected").text(); startMonth = this.value; startDate = Number(startYear + "" + startMonth + "" + startDay); startDateStr = startDay + " " + startMonthStr + " " + startYear; $('.inlineDateStart').text(startDateStr); if( compareDate ( startDate, endDate ) ){ getData(startDate,endDate,amount,type); } }); $('#annee_deb').change(function(){ startYear = this.value; startDate = Number(startYear + "" + startMonth + "" + startDay); startDateStr = startDay + " " + startMonthStr + " " + startYear; $('.inlineDateStart').text(startDateStr); if( compareDate ( startDate, endDate ) ){ getData(startDate,endDate,amount,type); } }); $('#jour_fin').change(function(){ endDay = this.value; endDate = Number(endYear + "" + endMonth + "" + endDay); endDateStr = endDay + " " + endMonthStr + " " + endYear; $('.inlineDateEnd').text(endDateStr); if( compareDate ( startDate, endDate ) ){ getData(startDate,endDate,amount,type); } }); $('#mois_fin').change(function(){ endMonthStr = $('#mois_fin').find(":selected").text(); endMonth = this.value; endDate = Number(endYear + "" + endMonth + "" + endDay); endDateStr = endDay + " " + endMonthStr + " " + endYear; $('.inlineDateEnd').text(endDateStr); if( compareDate ( startDate, endDate ) ){ getData(startDate,endDate,amount,type); } }); $('#annee_fin').change(function(){ endYear = this.value; endDate = Number(endYear + "" + endMonth + "" + endDay); endDateStr = endDay + " " + endMonthStr + " " + endYear; $('.inlineDateEnd').text(endDateStr); if( compareDate ( startDate, endDate ) ){ getData(startDate,endDate,amount,type); } }); var startDateShown = false; $('.inlineDateStart').click(function(){ if (!startDateShown) { $('.startDateDiv').css('display','block'); startDateShown = !startDateShown; } else { $('.startDateDiv').css('display','none'); startDateShown = !startDateShown; } }); var endDateShown = false; $('.inlineDateEnd').click(function(){ if (!endDateShown) { $('.endDateDiv').css('display','block'); endDateShown = !endDateShown; } else { $('.endDateDiv').css('display','none'); endDateShown = !endDateShown; } }); function getData(startDate, endDate, amountSum, type){ $.ajax({ type: "GET", url: "./service_calculator.php", data: { ticker : "CNP", pays : "FR", fromDate : startDate, toDate : endDate, amount : amountSum, type : type, includeDividendes: true}, dataType:'JSON', success: function(response){ $('.investInput').val((Number(response.amountSharesStartPrice) - Number(response.differencePriceEuro) ).toFixed(2).replace(".", floating)); $('.sharesInput').val(Number(response.amountSharesStart).toFixed(2).replace(".", floating)); $('.valueShares').text(Number(response.amountSharesStart).toFixed(2).replace(".", floating)); $('.valueInvest').text((Number(response.amountSharesStartPrice) - Number(response.differencePriceEuro) ).toFixed(2).replace(".", floating) + "€"); $('.valueAmount').text(Number(response.amountSharesStartPrice).toFixed(2).replace(".", floating) + "€"); $('.valueDiv').text(Number(response.amountDiv).toFixed(2).replace(".", floating) + "€"); $('.valueDifEuro').text(Number(response.differencePriceEuro).toFixed(2).replace(".", floating) + "€"); $('.valueDifPer').text(Number(response.differencePricePercent).toFixed(2).replace(".", floating) + "%"); $('.valueDifAn').text(Number(response.annualPercent).toFixed(2).replace(".", floating) + "%"); if((response.differencePriceEuro).toString().includes("-")){ $('.valueDifEuro').addClass("negativeP"); $('.valueDifEuro').removeClass("positiveP"); } else { $('.valueDifEuro').addClass("positiveP"); $('.valueDifEuro').removeClass("negativeP"); } if((response.differencePricePercent).toString().includes("-")){ $('.valueDifPer').addClass("negativeP"); $('.valueDifPer').removeClass("positiveP"); } else { $('.valueDifPer').addClass("positiveP"); $('.valueDifPer').removeClass("negativeP"); } if((response.annualPercent).toString().includes("-")){ $('.valueDifAn').removeClass("positivePlus"); } else { $('.valueDifAn').addClass("positivePlus"); } } }); } function compareDate ( startDate, endDate ) { if ( startDate > endDate ) { alert ("Start date must be smaller than End date!"); return false; } return true; } getData(startDate,endDate,amount,type); });