<div class='container'>
<div class="stm_auto_loan_calculator">
	<div class="title">
		<!-- <i class="stm-icon-calculator"></i> -->
		<h5>Financing calculator</h5>
	</div>
	<div class="row">
		<div class="col-md-12">

			<!--Amount-->
			<div class="form-group">
				<div class="labeled">Vehicle price <span class="orange">(CHF )</span></div>
				<input type="text" class="numbersOnly vehicle_price" value=""/>
			</div>

			<div class="row">
				<div class="col-md-6 col-sm-6">
					<!--Interest rate-->
					<div class="form-group md-mg-rt">
						<div class="labeled">Interest rate <span class="orange">(%)</span></div>
						<input type="text" class="numbersOnly interest_rate" value="4.95" />
					</div>
				</div>
				<div class="col-md-6 col-sm-6">
					<!--Period-->
					<div class="form-group md-mg-lt">
						<div class="labeled">Period <span class="orange">(month)</span></div>
						<input type="text" class="numbersOnly period_month" value="24"/>
					</div>
				</div>
			</div>

			<!--Down Payment-->
			<div class="form-group">
				<div class="labeled">Down Payment <span class="orange">(CHF )</span></div>
				<input type="text" class="numbersOnly down_payment" value=""/>
			</div>


			<a href="#" class="button button-sm calculate_loan_payment dp-in">Calculate</a>


			<div class="calculator-alert alert alert-danger">

			</div>

		</div>

		<!--Results-->
		<div class="col-md-12">
			<div class="stm_calculator_results">
				<div class="stm-calc-results-inner">
					<div class="stm-calc-label">Monthly Payment</div>
					<div class="monthly_payment h5"></div>

					<div class="stm-calc-label">Total Interest Payment</div>
					<div class="total_interest_payment h5"></div>

					<div class="stm-calc-label">Total Amount to Pay</div>
					<div class="total_amount_to_pay h5"></div>
				</div>
			</div>
		</div>
	</div>
</div>

<script>
	(function($) {
		"use strict";

		$(document).ready(function () {
			var vehicle_price;
			var interest_rate;
			var down_payment;
			var period_month;

			var stmCurrency = "CHF ";
			var stmPriceDel = ",";
			var stmCurrencyPos = "left";

			$('.calculate_loan_payment').on('click', function(e){
				e.preventDefault();

				//Useful vars
				var current_calculator = $(this).closest('.stm_auto_loan_calculator');

				var calculator_alert = current_calculator.find('.calculator-alert');
				//First of all hide alert
				calculator_alert.removeClass('visible-alert');

				//4 values for calculating
				vehicle_price = parseFloat(current_calculator.find('input.vehicle_price').val());

				interest_rate = parseFloat(current_calculator.find('input.interest_rate').val());
				interest_rate = interest_rate/1200;

				down_payment = parseFloat(current_calculator.find('input.down_payment').val());

				period_month = parseFloat(current_calculator.find('input.period_month').val());

				//Help vars

				var validation_errors = true;

				var monthly_payment = 0;
				var total_interest_payment = 0;
				var total_amount_to_pay = 0;

				//Check if not nan
				if(isNaN(vehicle_price)){
					calculator_alert.text("Please fill Vehicle Price field");
					calculator_alert.addClass('visible-alert');
					current_calculator.find('input.vehicle_price').closest('.form-group').addClass('has-error');
					validation_errors = true;
				} else if (isNaN(interest_rate)) {
					calculator_alert.text("Please fill Interest Rate field");
					calculator_alert.addClass('visible-alert');
					current_calculator.find('input.interest_rate').closest('.form-group').addClass('has-error');
					validation_errors = true;
				} else if (isNaN(period_month)) {
					calculator_alert.text("Please fill Period field");
					calculator_alert.addClass('visible-alert');
					current_calculator.find('input.period_month').closest('.form-group').addClass('has-error');
					validation_errors = true;
				} else if (isNaN(down_payment)) {
					calculator_alert.text("Please fill Down Payment field");
					calculator_alert.addClass('visible-alert');
					current_calculator.find('input.down_payment').closest('.form-group').addClass('has-error');
					validation_errors = true;
				} else if (down_payment > vehicle_price) {
					//Check if down payment is not bigger than vehicle price
					calculator_alert.text("Down payment can not be more than vehicle price");
					calculator_alert.addClass('visible-alert');
					current_calculator.find('input.down_payment').closest('.form-group').addClass('has-error');
					validation_errors = true;
				} else {
					validation_errors = false;
				}

				if(!validation_errors) {
					var interest_rate_unused = interest_rate;
					var mathPow = Math.pow(1 + interest_rate, period_month);

					if(interest_rate == 0) {
						interest_rate_unused = 1;
					}

					monthly_payment = (interest_rate_unused != 1) ? (vehicle_price - down_payment) * interest_rate_unused * mathPow : (vehicle_price - down_payment) / period_month;
					var monthly_payment_div = (mathPow - 1);
					if(monthly_payment_div == 0) {
						monthly_payment_div = 1;
					}

					monthly_payment = monthly_payment/monthly_payment_div;
                    monthly_payment = monthly_payment.toFixed(2);

                    total_amount_to_pay = down_payment + (monthly_payment*period_month);
                    total_amount_to_pay = (interest_rate == 0) ? vehicle_price : total_amount_to_pay.toFixed(2);

                    total_interest_payment = total_amount_to_pay - vehicle_price;
                    total_interest_payment = (interest_rate == 0) ? 0 : total_interest_payment.toFixed(2);

					current_calculator.find('.stm_calculator_results').slideDown();
					current_calculator.find('.monthly_payment').text(stm_get_price_view(monthly_payment, stmCurrency, stmCurrencyPos, stmPriceDel ));
					current_calculator.find('.total_interest_payment').text(stm_get_price_view(total_interest_payment, stmCurrency, stmCurrencyPos, stmPriceDel ));
					current_calculator.find('.total_amount_to_pay').text(stm_get_price_view(total_amount_to_pay, stmCurrency, stmCurrencyPos, stmPriceDel ));
				} else {
					current_calculator.find('.stm_calculator_results').slideUp();
					current_calculator.find('.monthly_payment').text('');
					current_calculator.find('.total_interest_payment').text('');
					current_calculator.find('.total_amount_to_pay').text('');
				}
			})

			$(".numbersOnly").on("keypress keyup blur",function (event) {
				//this.value = this.value.replace(/[^0-9.]/g,'');
				$(this).val($(this).val().replace(/[^0-9.]/g,''));
				if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
					event.preventDefault();
				}

				if ( $(this).val() != '' ){
					$(this).closest('.form-group').removeClass('has-error');
				}
			});

					});

	})(jQuery);
</script>

</div>{"id":2408,"date":"2021-09-24T12:30:38","date_gmt":"2021-09-24T12:30:38","guid":{"rendered":"https:\/\/x208xanbba.preview.infomaniak.website\/services-2\/"},"modified":"2023-03-13T09:41:02","modified_gmt":"2023-03-13T09:41:02","slug":"prestations","status":"publish","type":"page","link":"https:\/\/www.bahmancars.ch\/en\/garantie\/prestations\/","title":{"rendered":"Financing"},"content":{"rendered":"<section class=\"wpb-content-wrapper\"><p>[vc_row][vc_column width=&#8221;1\/2&#8243;][vc_empty_space height=&#8221;65px&#8221;][vc_single_image image=&#8221;18599&#8243; img_size=&#8221;large&#8221; alignment=&#8221;center&#8221;][\/vc_column][vc_column width=&#8221;1\/2&#8243;][vc_empty_space][vc_column_text]<\/p>\n<div class=\"csc-header csc-header-n1\">\n<div class=\"csc-header csc-header-n1\">\n<h3 class=\"csc-firstHeader\">Do you want to get financing ?<\/h3>\n<p class=\"csc-firstHeader\">We can offer you a solution adapted to your needs :<\/p>\n<ul>\n<li class=\"csc-firstHeader\">Credit or Leasing<\/li>\n<li class=\"csc-firstHeader\">With or without a down payment<\/li>\n<li class=\"csc-firstHeader\">Choice of duration<\/li>\n<\/ul>\n<p>To make a financing request, please contact us by phone or e-mail, we also offer <strong>live financing simulations<\/strong> <strong>without obligation<\/strong>.<\/p>\n<p>In partnership with <i>Alphera<\/i><\/p>\n<\/div>\n<\/div>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;12px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<div>\n<div><\/div>\n<\/div>\n<p>[\/vc_column_text][vc_empty_space][\/vc_column][\/vc_row]<\/p>\n<\/section>","protected":false},"excerpt":{"rendered":"<p>[vc_row][vc_column width=&#8221;1\/2&#8243;][vc_empty_space height=&#8221;65px&#8221;][vc_single_image image=&#8221;18599&#8243; img_size=&#8221;large&#8221; alignment=&#8221;center&#8221;][\/vc_column][vc_column width=&#8221;1\/2&#8243;][vc_empty_space][vc_column_text] Do you want to get financing ? We can offer you a solution adapted to your needs : Credit or Leasing With or without a down payment Choice of duration To make a financing request, please contact us by phone or e-mail, we also offer live financing simulations&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2401,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2408","page","type-page","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bahmancars.ch\/en\/wp-json\/wp\/v2\/pages\/2408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bahmancars.ch\/en\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bahmancars.ch\/en\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bahmancars.ch\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bahmancars.ch\/en\/wp-json\/wp\/v2\/comments?post=2408"}],"version-history":[{"count":16,"href":"https:\/\/www.bahmancars.ch\/en\/wp-json\/wp\/v2\/pages\/2408\/revisions"}],"predecessor-version":[{"id":18601,"href":"https:\/\/www.bahmancars.ch\/en\/wp-json\/wp\/v2\/pages\/2408\/revisions\/18601"}],"up":[{"embeddable":true,"href":"https:\/\/www.bahmancars.ch\/en\/wp-json\/wp\/v2\/pages\/2401"}],"wp:attachment":[{"href":"https:\/\/www.bahmancars.ch\/en\/wp-json\/wp\/v2\/media?parent=2408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}