jQuery(document).ready(function( $ ) {
	$(document).on('click','[data-pafe-form-builder-nav="next"]',function(){
		var formID = $(this).data('pafe-form-builder-nav-form-id'),
			$wrapper = $(this).closest('.pafe-multi-step-form__content-item'),
    		$fields = $wrapper.find('[data-pafe-form-builder-form-id='+ formID +']'),
    		requiredText = $(this).data('pafe-form-builder-required-text'), 
    		error = 0;

		$fields.each(function(){
			if ( $(this).data('pafe-form-builder-stripe') == undefined && $(this).data('pafe-form-builder-html') == undefined ) {
				var $checkboxRequired = $(this).closest('.elementor-field-type-checkbox.elementor-field-required');
				var checked = 0;
				if ($checkboxRequired.length > 0) {
					checked = $checkboxRequired.find("input[type=checkbox]:checked").length;
				}

				if ( !$(this)[0].checkValidity() && $(this).closest('.elementor-widget').css('display') != 'none' && $(this).data('pafe-form-builder-honeypot') == undefined || checked == 0 && $checkboxRequired.length > 0) {
					if ($(this).css('display') == 'none' || $(this).data('pafe-form-builder-image-select') != undefined || $checkboxRequired.length > 0) {
						$(this).closest('.elementor-field-group').find('[data-pafe-form-builder-required]').html(requiredText);
					} else {
						if ($(this).data('pafe-form-builder-image-select') == undefined) {
							$(this)[0].reportValidity();
						}
					}
					error++;
				} else {
					$(this).closest('.elementor-field-group').find('[data-pafe-form-builder-required]').html('');
				}
			}
		});

		if (error == 0) {
			$wrapper.removeClass('active');
			$wrapper.next().addClass('active');
			var index = $wrapper.next().index(),
				$progressbarItem = $(this).closest('.pafe-multi-step-form').find('.pafe-multi-step-form__progressbar-item');
			$progressbarItem.eq(index).addClass('active');

			var $scrollToTop = $(this).closest('[data-pafe-multi-step-form-scroll-to-top]');

			if ($scrollToTop.length > 0) {
				var breakPointMd = $('[data-pafe-break-point-md]').data('pafe-break-point-md'),
					breakPointLg = $('[data-pafe-break-point-lg]').data('pafe-break-point-lg'),
					windowWidth = window.innerWidth;

				if( windowWidth >= breakPointLg ) {
					$('html, body').animate({
						scrollTop: $scrollToTop.offset().top - $scrollToTop.data('pafe-multi-step-form-scroll-to-top-offset-desktop') }
					, 300);
				}

				if( windowWidth >= breakPointMd && windowWidth < breakPointLg ) {
					$('html, body').animate({
						scrollTop: $scrollToTop.offset().top - $scrollToTop.data('pafe-multi-step-form-scroll-to-top-offset-tablet') }
					, 300);
				}

				if( windowWidth < breakPointMd ) {
					$('html, body').animate({
						scrollTop: $scrollToTop.offset().top - $scrollToTop.data('pafe-multi-step-form-scroll-to-top-offset-mobile') }
					, 300);
				}
			}
		}
	});

	function nextMultiStepForm($this) {
		var $wrapper = $this.closest('.pafe-multi-step-form__content-item'),
			formID = $wrapper.find('[data-pafe-form-builder-form-id]').data('pafe-form-builder-form-id');

		$wrapper.removeClass('active');
		$wrapper.next().addClass('active');
		var index = $wrapper.next().index(),
			$progressbarItem = $this.closest('.pafe-multi-step-form').find('.pafe-multi-step-form__progressbar-item');
		$progressbarItem.eq(index).addClass('active');

		var $scrollToTop = $this.closest('[data-pafe-multi-step-form-scroll-to-top]');

		if ($scrollToTop.length > 0) {
			var breakPointMd = $('[data-pafe-break-point-md]').data('pafe-break-point-md'),
				breakPointLg = $('[data-pafe-break-point-lg]').data('pafe-break-point-lg'),
				windowWidth = window.innerWidth;

			if( windowWidth >= breakPointLg ) {
				$('html, body').animate({
					scrollTop: $scrollToTop.offset().top - $scrollToTop.data('pafe-multi-step-form-scroll-to-top-offset-desktop') }
				, 300);
			}

			if( windowWidth >= breakPointMd && windowWidth < breakPointLg ) {
				$('html, body').animate({
					scrollTop: $scrollToTop.offset().top - $scrollToTop.data('pafe-multi-step-form-scroll-to-top-offset-tablet') }
				, 300);
			}

			if( windowWidth < breakPointMd ) {
				$('html, body').animate({
					scrollTop: $scrollToTop.offset().top - $scrollToTop.data('pafe-multi-step-form-scroll-to-top-offset-mobile') }
				, 300);
			}
		}
	}

	$(document).on('change','[data-pafe-form-builder-multi-step-form-autonext]',function(){
		nextMultiStepForm($(this));
	});

	$(document).on('click','.image_picker_selector>li>div',function(){
		if ($(this).closest('.elementor-field').find('[data-pafe-form-builder-multi-step-form-autonext]').length > 0) {
			nextMultiStepForm($(this));
		}
	});

	$(document).on('click','[data-pafe-form-builder-nav="prev"]',function(){
		var formID = $(this).data('pafe-form-builder-nav-form-id'),
			$wrapper = $(this).closest('.pafe-multi-step-form__content-item');

		$wrapper.removeClass('active');
		$wrapper.prev().addClass('active');
		var index = $wrapper.index(),
			$progressbarItem = $(this).closest('.pafe-multi-step-form').find('.pafe-multi-step-form__progressbar-item');
		$progressbarItem.eq(index).removeClass('active');

		var $scrollToTop = $(this).closest('[data-pafe-multi-step-form-scroll-to-top]');

		if ($scrollToTop.length > 0) {
			var breakPointMd = $('[data-pafe-break-point-md]').data('pafe-break-point-md'),
				breakPointLg = $('[data-pafe-break-point-lg]').data('pafe-break-point-lg'),
				windowWidth = window.innerWidth;

			if( windowWidth >= breakPointLg ) {
				$('html, body').animate({
					scrollTop: $scrollToTop.offset().top - $scrollToTop.data('pafe-multi-step-form-scroll-to-top-offset-desktop') }
				, 300);
			}

			if( windowWidth >= breakPointMd && windowWidth < breakPointLg ) {
				$('html, body').animate({
					scrollTop: $scrollToTop.offset().top - $scrollToTop.data('pafe-multi-step-form-scroll-to-top-offset-tablet') }
				, 300);
			}

			if( windowWidth < breakPointMd ) {
				$('html, body').animate({
					scrollTop: $scrollToTop.offset().top - $scrollToTop.data('pafe-multi-step-form-scroll-to-top-offset-mobile') }
				, 300);
			}
		}
	});
});