/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


$(document).on('focus', '.has-error input', function () {
	$(this).parents('.has-error').find('.help-block-error').html('');
	$(this).parents('.has-error').removeClass('has-error');
});
$(document).on('change', '.has-error input', function () {
	$(this).parents('.has-error').find('.help-block-error').html('');
	$(this).parents('.has-error').removeClass('has-error');
});
$(document).on('blur', '.has-error input', function () {
	$(this).parents('.has-error').find('.help-block-error').html('');
	$(this).parents('.has-error').removeClass('has-error');
});


$(document).on('focus', '.has-error textarea', function () {
	$(this).parents('.has-error').find('.help-block-error').html('');
	$(this).parents('.has-error').removeClass('has-error');
});
$(document).on('change', '.has-error textarea', function () {
	$(this).parents('.has-error').find('.help-block-error').html('');
	$(this).parents('.has-error').removeClass('has-error');
});
$(document).on('blur', '.has-error textarea', function () {
	$(this).parents('.has-error').find('.help-block-error').html('');
	$(this).parents('.has-error').removeClass('has-error');
});
function formErrorMessagesShow(fieldPrefix, errorList) {
	if (errorList.isArray == null) {
		for (const key in errorList) {
			var errorMessage = errorList[key][0];
			if (errorMessage) {
				console.log(key);
				var fieldCont = $('.' + fieldPrefix + '-' + key);
				fieldCont.addClass('has-error');
				fieldCont.find('.help-block').html(errorMessage);
			}
		}
	}
}

$(document).on('click', '.alert-close-button', function () {
	$(this).parent().remove();
	if ($('.alert-item').length == 0) {
		$('.alerts-container').remove();
	}
});



$(document).on('change', '.filter_brand_select', function(){
	var thisObj = $(this);
	var url = thisObj.data('url');
	var brand_url = thisObj.val();
	$('.filter_brand_select').val(thisObj.val());
	var empty_ption =1;
	if(thisObj.data('childempty')==1) {
		empty_ption =0;
	}
	$.ajax({
		url: url,
		dataType: 'json',
		data: {brand: brand_url,  empty_ption:empty_ption},
		type: 'post',
		beforeSend:function(){
			thisObj.parents('.ajax_back_anim').addClass('ajax_requesting');
		},
		success: function (result)
		{
			$('.filter_model_select').empty();
			
			$('.filter_model_select').html(result.list);
			$('.filter_from_select').html(result.yearFrom);
			$('.filter_to_select').html(result.yearTo);
			thisObj.parents('.ajax_back_anim').removeClass('ajax_requesting');
		},
		error : function (xhr, textStatus, errorThrown) {
			thisObj.parents('.ajax_back_anim').removeClass('ajax_requesting');
		},
		complete : function (){
			thisObj.parents('.ajax_back_anim').removeClass('ajax_requesting');
		}
	});
});



$(document).on('change', '.filter_model_select', function(){
	var thisObj = $(this);
	var url = thisObj.data('url');
	var vehicle_model = thisObj.val();
	var brand = $('.filter_brand_select').val();
	$('.filter_model_select').val(thisObj.val());
	var empty_ption =1;
	if(thisObj.data('childempty')==1) {
		empty_ption =0;
	}
	$.ajax({
		url: url,
		dataType: 'json',
		data: {vehicle_model:vehicle_model, brand: brand, empty_ption:empty_ption},
		type: 'get',
		beforeSend:function(){
			thisObj.parents('.ajax_back_anim').addClass('ajax_requesting');
		},
		success: function (result)
		{
			
			$('.filter_from_select').html(result.yearFrom);
			$('.filter_to_select').html(result.yearTo);
			thisObj.parents('.ajax_back_anim').removeClass('ajax_requesting');
		},
		error : function (xhr, textStatus, errorThrown) {
			thisObj.parents('.ajax_back_anim').removeClass('ajax_requesting');
		},
		complete : function (){
			thisObj.parents('.ajax_back_anim').removeClass('ajax_requesting');
		}
	});
});


$(document).ready(function() {
    $('.select2_dr').select2();
});

// Filter slider
    if ($('.form-widget-filter__range').length) {
        const filterRange = document.querySelector('.form-widget-filter__range')
        const input0 = document.querySelector('.form-widget-filter__value_1')
        const input1 = document.querySelector('.form-widget-filter__value_2')
        const inputs = [input0, input1];
    
        noUiSlider.create(filterRange, {
            start: [$('.form-widget-filter__value_1').val(), $('.form-widget-filter__value_2').val()],
            connect: true,
            step: 100,
            // tooltips: true,
            format: wNumb({
                decimals: 0,
                prefix: '$ ',
                thousand: ','
            }),
            // tooltips: [true, wNumb({decimals: 1})],
            range: {
                'min': $('.form-widget-filter__slider').data('min'),
                'max': $('.form-widget-filter__slider').data('max')
            }
        });
    
        filterRange.noUiSlider.on('update', function (values, index) {
            inputs[index].value = values[index];
        });
    }
	
	$(document).on('submit', '#header-search-form', function(){
		if($('#header-search-input').val().trim().length==0) {
			$('#header-search-form').addClass('has-error');
			$('.sch-err').html('Type something to search');
			return false;
		}
	});
	$(document).on('submit', '.req_form_check', function(){
		var thisForm = $(this);
		var ret = true;
		thisForm.find('.required input').each(function(){
			if($(this).val().trim().length==0) {
				ret = false;
				$(this).parents('.required').addClass('has-error');
				$(this).parents('.required').find('.help-block-error').text('Can not be blank');
			}
		});
		thisForm.find('.email_val input').each(function(){
			var regexp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
			if(!this.value.match(regexp)) {
				ret = false;
				$(this).parents('.email_val').addClass('has-error');
				$(this).parents('.email_val').find('.help-block-error').text('Invalid email');
			}
		});
		thisForm.find('.required textarea').each(function(){
			if($(this).val().trim().length==0) {
				ret = false;
				$(this).parents('.required').addClass('has-error');
				$(this).parents('.required').find('.help-block-error').text('Can not be blank');
			}
		});
		
		return ret;
	});