(function(code){
code(window.jQuery,window,document);
}(function($,window,document){
validateForm();
$('.as-custom-select.as-type').change();
$(document).on('click', '.mailMeResults', function(){
$('.as-modal-wrapper').addClass('open');
return false;
});
$(document).on('click', '.as-reset-filters', function(){
window.location.reload();
});
$(document).on('click', '.as-modal-wrapper .close-modal', function(){
$('.as-modal-wrapper').removeClass('open');
});
$(document).on('click', '.close-as-filters', function(){
$('.as-sidebar').removeClass('is-open');
$('body').removeClass('as-filters-open');
});
$(document).on('click', '.show-hide-filters', function(){
$('.as-sidebar').toggleClass('is-open');
$('body').toggleClass('as-filters-open');
});
$(document).on('change', '.as-custom-select.as-type', function(){
if($('select[name="as-type"]').val()=='Autolening'){
$('.as-type-voertuig').removeClass('d-none');
$('.as-type-leeftijd').removeClass('d-none');
$('.as-type-voertuig .option')[0].click();
$('.as-type-leeftijd input').each(function(){
var _this=$(this);
var parent=_this.parent();
if(_this.attr('value')!='0 tot 1 jaar'){
}});
$('.as-custom-select[name="as-leeftijd"]').val('0 tot 1 jaar').change();
}else{
$('.as-type-voertuig').addClass('d-none');
$('.as-type-voertuig input[type="hidden"]').val('');
$('.as-custom-select[name="as-leeftijd"] option').removeAttr('disabled');
$('.as-type-leeftijd').addClass('d-none');
$('[name="as-leeftijd"]').val('');
}});
$(document).on('change', '.as-custom-select, .as-form-control, .as-custom-checkbox input, input[name="type_voertuig"]', function(){
validateForm();
});
$(document).on('submit', '.as-sidebar', function(){
return false;
});
$(document).on('submit', '.as-form', function(){
var _this=$(this);
var data=_this.serialize();
var current_url=window.location.href;
var local_data=localStorage.getItem('lo_ls_as_'+current_url);
if(local_data){
data_l=local_data;
data_u=data_l.split('&');
$.each(data_u, function(k, v){
data_u[k]=decodeURIComponent(v).split('=');
});
var aanbieder=[];
$.each(data_u, function(k, v){
if(v[0]=='aanbieder[]'){
aanbieder.push(v[1]);
}});
if(aanbieder.length){
data_u.push([
'aanbieder',
aanbieder
]);
}
var clean_data={};
$.each(data_u, function(k, v){
if(v[1]!=''&&v[0]!='action'&&v[0]!='service'&&v[0]!='aanbieder[]'){
clean_data[v[0]]=v[1];
}});
$.each(clean_data, function(k, v){
if(Array.isArray(v)){
v=v.join(',');
}
data+='&'+k+'='+v
});
data+='&back_url='+current_url;
}
$.ajax({
url:_this.attr('action'),
data:data,
beforeSend: function(){
},
success:function(response){
_this.append('<div class="as-message">'+response+'</div>');
setTimeout(function(){
_this.find('.as-message').remove();
}, 10000);
}});
return false;
});
$('#as-qty').val(number_format($('#as-qty-range').val()));
$('#as-qty-range').on('change input', function (event){
var _this=$(this);
$('#as-qty').val(number_format(_this.val())).change();
});
$('#as-qty').on('change', function (event){
var _this=$(this);
var value=_this.val().replace('.','').replace('.','').replace('.','').replace('.','');
if(parseFloat(value) < parseFloat(_this.attr('min'))){
_this.val(_this.attr('min'));
}
if(parseFloat(value) > parseFloat(_this.attr('max'))){
_this.val(_this.attr('max'));
}
$('#as-qty-range').val(parseFloat(value));
$('#as-qty').val(number_format(value));
});
$(document).on('click','.switch .option', function(){
var _this=$(this);
var val=_this.text();
$('.switch .option').removeClass('active');
_this.addClass('active');
_this.parent().find('input[type="hidden"]').val(val).change();
});
function validateForm(){
console.log('validate');
if($('.as-sidebar').length){
var data=$('.as-sidebar').serialize();
var current_url=window.location.href;
var local_data=localStorage.getItem('lo_ls_as_'+current_url);
$('#ls_data_url').val(data).change();
console.log(data);
if($('.firstLoad').length){
console.log('first');
if(local_data=='echo'){
data=local_data;
data_u=data.split('&');
$.each(data_u, function(k, v){
data_u[k]=decodeURIComponent(v).split('=');
});
var aanbieder=[];
$.each(data_u, function(k, v){
if(v[0]=='aanbieder[]'){
aanbieder.push(v[1]);
}});
if(aanbieder.length){
data_u.push([
'aanbieder',
aanbieder
]);
}
$.each(data_u, function(k, v){
if(v[1]!=''&&v[0]!='action'&&v[0]!='service'){
if(v[0]=='as-qty'){
value=v[1].replace('.','').replace('.','').replace('.','').replace('.','');
$('#as-qty').val(value).change();
$('#as-qty-range').val(value).change();
}else{
if(v[0]=='aanbieder'){
$('.as-banks input').each(function(){
var __this=$(this);
if(v[1].includes(__this.val())){
__this.prop('checked', true);
}})
}else{
if(v[0]!='aanbieder[]'){
$('[name="'+v[0]+'"]').val(v[1]).change();
}}
}}
});
}else{
console.log('first');
local_data=data;
localStorage.setItem('lo_ls_as_'+current_url, data);
}}else{
local_data=data;
console.log('set')
localStorage.setItem('lo_ls_as_'+current_url, data);
}
$('#ls_months').val($('[name="as-looptijd"]').val());
$('#ls_amount').val($('#as-qty-range').val());
$.ajax({
url:$('.as-sidebar').attr('action'),
method: 'POST',
data:data,
beforeSend: function(){
$('.as-loader').removeClass('hidden');
},
success:function(response){
var _loan_ids=[];
$('.as-loader').addClass('hidden');
$('#mainAsContent').html(response);
$('.loan.d-none').remove();
if(!$('#mainAsContent .loan').length){
$('#mainAsContent').html('<p>De looptijd van je lening is te hoog voor dit bedrag. Geef een andere looptijd of een hoger bedrag in om een lening te kunnen simuleren. Of je kunt ook meerdere financiële instellingen selecteren.</p>');
$('#loan_ids').val('');
var current_looptijd=$('[name="as-looptijd"]').val();
var prev=false;
$('[name="as-looptijd"] option').each(function(){
var __this=$(this);
if(current_looptijd==__this.attr('value')){
if(prev){
$('[name="as-looptijd"]').val($(prev).text()).change();
}
return false;
}else{
prev=__this;
}});
}else{
$('#mainAsContent .loan').each(function(){
var __this=$(this);
_loan_ids.push(__this.attr('data-id'));
});
_loan_ids=_loan_ids.join(',')
$('#loan_ids').val(_loan_ids);
$('.as-banks .as-custom-checkbox input').each(function(){
});
}
if($('#mainAsContent .minAmount').length){
var currentVal=parseFloat($('#as-qty-range'). val());
var min=parseFloat($('#mainAsContent .minAmount').val());
var max=parseFloat($('#mainAsContent .maxAmount').val());
$('#as-qty-range').attr('min', min).attr('max', max);
$('.min-max span:first-child').html(number_format(min)+',-');
$('.min-max span:last-child').html(number_format(max)+',-');
if(currentVal < min){
$('#as-qty-range'). val(min);
$('#as-qty').val(min);
validateForm();
}
if(currentVal > max){
$('#as-qty-range'). val(max);
$('#as-qty').val(max);
validateForm();
}}
$('.firstLoad').remove();
}});
}}
function number_format(number, decimals, dec_point, thousands_point){
if(number==null||!isFinite(number)){
throw new TypeError("number is not valid");
}
if(!decimals){
var len=number.toString().split(',').length;
decimals=len > 1 ? len:0;
}
if(!dec_point){
dec_point='.';
}
if(!thousands_point){
thousands_point='.';
}
number=parseFloat(number).toFixed(decimals);
number=number.replace(",", dec_point);
var splitNum=number.split(dec_point);
splitNum[0]=splitNum[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousands_point);
number=splitNum.join(dec_point);
number=number.replace(',00', ',-');
return number;
}})
);