<?php if (!$logged && !isset($quickconfirm) && !isset($this->request->get['old']) && !$this->config->get('register_old') && !$this->ftrgeralregistry->isApp()) { ?>
  <script>
    window.location.href = 'index.php?route=account/register&checkout=true';
  </script>
<?php } ?>

<?php echo $header; ?>
<?php
    if ($this->config->get('wg24_seo_fer_facebook_status')=='enable'){
        if (!$this->config->get('wg24_seo_fer_facebook_pixel')==''){
            echo html_entity_decode("<script>	window.waitEvent('scripts_document', function() {
 fbq('track', 'InitiateCheckout'); });</script>", ENT_QUOTES, 'UTF-8');
        }
    }
?>

<div class="container-fluid">
  <?php echo $breadcrumbs; ?>
  <div class="row">
    <div class="col-xs-12">
      <h2 class="heading-1"><?php echo $heading_title; ?></h2>
    </div>
  </div>
  <div class="row" data-products='<?php echo $products; ?>' data-google-analytics='<?php echo $google_analytics; ?>'>
    <div class="col-xs-12">
      <div class="content-box">
        <?php echo $content_top; ?>
        <?php if (!$logged) { ?>
        <div id="payment-address">
          <div class="checkout-heading"><span><?php echo $text_checkout_account; ?></span></div>
          <div class="checkout-content"></div>
        </div>
        <?php } else { ?>
        <div id="payment-address">
          <div class="checkout-heading"><span><?php echo $text_checkout_payment_address; ?></span></div>
          <div class="checkout-content"></div>
        </div>
        <?php } ?>
        <?php if ($shipping_required) { ?>
        <div id="shipping-address">
          <div class="checkout-heading"><?php echo $text_checkout_shipping_address; ?></div>
          <div class="checkout-content"></div>
        </div>
        <div id="shipping-method">
          <div class="checkout-heading"><?php echo $text_checkout_shipping_method; ?></div>
          <div class="checkout-content"></div>
        </div>
        <?php } ?>
        <div id="payment-method">
          <div class="checkout-heading"><?php echo $text_checkout_payment_method; ?></div>
          <div class="checkout-content"></div>
        </div>
        <div id="confirm">
          <div class="checkout-heading"><?php echo $text_checkout_confirm; ?></div>
          <div class="checkout-content"></div>
        </div>
      </div>
    </div>
	</div>
	<?php if ($content_bottom) { ?>
		<div class="row">
			<div class="col-xs-12">
				<?php echo $content_bottom; ?>
			</div>
		</div>
  	<?php } ?>
</div>



<script type="text/javascript">
window.waitEvent('scripts_window', function() {
  var DATA_PRODUCTS = JSON.parse(document.querySelector('[data-products]').getAttribute('data-products'));
  const DATA_GOOGLE_ANALYTICS = JSON.parse(document.querySelector('[data-google-analytics]').getAttribute('data-google-analytics'));

	$('#checkout .checkout-content input[name=\'account\']').live('change', function() {
		if ($(this).attr('value') == 'register') {
			$('#payment-address .checkout-heading span').html('<?php echo $text_checkout_account; ?>');
		} else {
			$('#payment-address .checkout-heading span').html('<?php echo $text_checkout_payment_address; ?>');
		}
	});

	$('.checkout-heading a').live('click', function() {
		$('.checkout-content').slideUp('slow');
	$('.checkout-content > .one-page-checkout > #opc-billing > #checkout-step-billing > .error-payment').remove();
		$(this).parent().parent().find('.checkout-content').slideDown('slow', function(){
			$('.btn-loading').removeClass('btn-loading').prop('disabled', false);
		});
	});
<?php if (!$logged) { ?>
	<?php if(isset($quickconfirm)) { ?>
		quickConfirm();
  <?php }else{ ?>

    $.ajax({
      url: 'index.php?route=account/register_content&checkout=true',
      dataType: 'html',
      success: function(html) {
        $('#checkout .checkout-content').html(html);
        <?php if($this->config->get('loginredesociais_google_status')=="enable") { ?>loadGoogleLogin();<?php } ?>
        $('#checkout .checkout-content').slideDown('slow', function(){
          $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
        });
      },
      error: function(xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });

	<?php } ?>
<?php } else { ?>

	<?php if(isset($quickconfirm)) { ?>
		quickConfirm();
	<?php }else{ ?>
		$.ajax({

			url: 'index.php?route=checkout/payment_address',
			dataType: 'html',
			success: function(html) {
			    $('#payment-address .checkout-content').html(html);
				$('#payment-address #button-address').click();
				$('#button-payment-address').click();

			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	<?php } ?>
<?php } ?>

// Checkout
// $('#form-register').live('submit', function(e) {
//   e.preventDefault();
// 	$.ajax({
// 		url: 'index.php?route=account/register_content&checkout=true',
// 		dataType: 'html',
// 		beforeSend: function() {
// 			$('#button-account').attr('disabled', true);
// 			$('#button-account').addClass('btn-loading');
// 		},
// 		complete: function() {
// 			$('#button-account').attr('disabled', false);
//       $('#button-account').removeClass('btn-loading');
// 		},
// 		success: function(html) {
// 			$('.warning, .error').remove();
//
// 			$('#payment-address .checkout-content').html(html);
//
// 			$('#checkout .checkout-content').slideUp('slow');
//
// 			$('#payment-address .checkout-content').slideDown('slow');
//
// 			$('.checkout-heading a').remove();
//
// 			$('#checkout .checkout-heading').append('<a><?php echo $text_modify; ?></a>');
// 		},
// 		error: function(xhr, ajaxOptions, thrownError) {
// 			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
// 		}
// 	});
// });

// Login
$('#form-login').live('submit', function(e) {
  e.preventDefault();
	$.ajax({
		url: 'index.php?route=account/login/validate&checkout=true',
		type: 'post',
		data: $(this).serializeObject(),
		dataType: 'json',
		beforeSend: function() {
			$('#button-login').addClass('btn-loading').prop('disabled', true);
		},
		complete: function() {
			$('#button-login').removeClass('btn-loading').prop('disabled', false);
		},
		success: function(json) {
			$('.warning, .error').remove();

			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
        notificationMsg(json['error']['warning'],'warning');
				$('input[name="password"]').val('');
				$('.warning').fadeIn('slow');
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

// Register
$('#form-register').live('submit', function(e) {
	e.preventDefault();

  var formData = $('#form-register').serializeObject();

  if ($('.g-recaptcha').length > 0) {
    formData['g-recaptcha-response'] = grecaptcha.getResponse();
  }

	$.ajax({
		url: 'index.php?route=account/register_content/validate&checkout=true',
		type: 'post',
		data: formData,
		dataType: 'json',
		beforeSend: function() {
			$('#button-register').addClass('btn-loading').prop('disabled', true);
		},
		complete: function() {
			$('#button-register').removeClass('btn-loading').prop('disabled', false);
		},
		success: function(json) {
      $('.warning, .text-error').remove();
      $('.border-error').removeClass('border-error');
			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
				if (json['error']['warning']) {
					notificationMsg(json['error']['warning'], 'error');
				}

        if (json['error']['firstname']) {
					$('[name=\'firstname\']').addClass('border-error').after('<span class="text-error">' + json['error']['firstname'] + '</span>');
				}

				if (json['error']['google_recaptcha']) {
					$('.g-recaptcha').addClass('border-error').after('<span class="text-error">' + json['error']['google_recaptcha'] + '</span>');
				}

				if (json['error']['data_nascimento']) {
					$('[name=\'data_nascimento\']').addClass('border-error').after('<span class="text-error">' + json['error']['data_nascimento'] + '</span>');
				}

				if (json['error']['cpf']) {
					$('[name=\'cpf\']').addClass('border-error').after('<span class="text-error">' + json['error']['cpf'] + '</span>');
				}

				if (json['error']['cnpj']) {
					$('[name=\'cnpj\']').addClass('border-error').after('<span class="text-error">' + json['error']['cnpj'] + '</span>');
				}

				if (json['error']['razao_social']) {
					$('[name=\'razao_social\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['razao_social'] + '</span>');
				}


				if (json['error']['lastname']) {
					$('[name=\'lastname\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['lastname'] + '</span>');
				}

				if (json['error']['email']) {
					$('[name=\'email\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['email'] + '</span>');
				}

				if (json['error']['telephone']) {
					$('[name=\'telephone\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['telephone'] + '</span>');
				}

        if (json['error']['celular']) {
					$('[name=\'celular\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['celular'] + '</span>');
				}

				if (json['error']['address_1']) {
					$('[name=\'address_1\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['address_1'] + '</span>');
				}

        if (json['error']['address_2']) {
					$('[name=\'address_2\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['address_2'] + '</span>');
				}

        if (json['error']['complemento']) {
					$('[name=\'complemento\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['complemento'] + '</span>');
				}

        if (json['error']['apelido']) {
					$('[name=\'apelido\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['apelido'] + '</span>');
				}

				if (json['error']['numero']) {
					$('[name=\'numero\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['numero'] + '</span>');
				}

				if (json['error']['city']) {
					$('[name=\'city\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['city'] + '</span>');
				}

				if (json['error']['postcode']) {
					$('[name=\'postcode\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['postcode'] + '</span>');
				}

				if (json['error']['country']) {
					$('[name=\'country_id\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['country'] + '</span>');
				}

				if (json['error']['zone']) {
					$('[name=\'zone_id\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['zone'] + '</span>');
				}

				if (json['error']['password']) {
					$('[name=\'password\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['password'] + '</span>');
				}

        if (json['error']['confirm']) {
					$('[name=\'confirm\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['confirm'] + '</span>');
				}

        if (json['error']['captcha']) {
					$('[name=\'captcha\']').addClass('border-error').after('<span class="text-error text-small">' + json['error']['captcha'] + '</span>');
				}

        if ($('.g-recaptcha').length > 0) {
          grecaptcha.reset();
        }

        $('#form-register .text-error').each(function() {
          if ($(this).prev('input').attr('name') == 'captcha') {
            $('#form-register .last-control + .js-accordion-content').slideDown().siblings('#form-register .js-accordion-content').slideUp();
            $('.conclude-register').removeClass('hidden');
          } else {
            $(this).closest('#form-register .js-accordion-content').slideDown().siblings('#form-register .js-accordion-content').slideUp();
          }
          return false;
        });
			} else {
				<?php if ($shipping_required) { ?>
				var shipping_address = $('#payment-address input[name=\'shipping_address\']:checked').attr('value');

				if (shipping_address) {
					$.ajax({
						url: 'index.php?route=checkout/shipping_method',
						dataType: 'html',
						success: function(html) {
							$('#shipping-method .checkout-content').html(html);

							$('.checkout-content').slideUp('slow');

							$('#shipping-method .checkout-content').slideDown('slow', function(){
                $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
              });

							$('#checkout .checkout-heading a').remove();
							$('#payment-address .checkout-heading a').remove();
							$('#shipping-address .checkout-heading a').remove();
							$('#shipping-method .checkout-heading a').remove();
							$('#payment-method .checkout-heading a').remove();

							$('#shipping-address .checkout-heading').append('<a><?php echo $text_modify; ?></a>');
							$('#payment-address .checkout-heading').append('<a><?php echo "" ?></a>');

							$.ajax({
								url: 'index.php?route=checkout/shipping_address',
								dataType: 'html',
								success: function(html) {
									$('#shipping-address .checkout-content').html(html);
								},
								error: function(xhr, ajaxOptions, thrownError) {
									alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
								}
							});
						},
						error: function(xhr, ajaxOptions, thrownError) {
							alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
						}
					});
				} else {
					$.ajax({
						url: 'index.php?route=checkout/shipping_address',
						dataType: 'html',
						success: function(html) {
							$('#shipping-address .checkout-content').html(html);

							$('.checkout-content').slideUp('slow');

							$('#shipping-address .checkout-content').slideDown('slow', function() {
                $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
              });

							$('#checkout .checkout-heading a').remove();
							$('#payment-address .checkout-heading a').remove();
							$('#shipping-address .checkout-heading a').remove();
							$('#shipping-method .checkout-heading a').remove();
							$('#payment-method .checkout-heading a').remove();

							$('#payment-address .checkout-heading').append('<a><?php echo "" ?></a>');
						},
						error: function(xhr, ajaxOptions, thrownError) {
							alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
						}
					});
				}
				<?php } else { ?>
				$.ajax({
					url: 'index.php?route=checkout/payment_method',
					dataType: 'html',
					success: function(html) {
						$('#payment-method .checkout-content').html(html);

						$('#payment-address .checkout-content').slideUp('slow');

						$('#payment-method .checkout-content').slideDown('slow', function(){
              $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
            });

						$('#checkout .checkout-heading a').remove();
						$('#payment-address .checkout-heading a').remove();
						$('#payment-method .checkout-heading a').remove();

						$('#payment-address .checkout-heading').append('<a><?php echo "" ?></a>');
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
				<?php } ?>

				$.ajax({
					url: 'index.php?route=checkout/payment_address',
					dataType: 'html',
					success: function(html) {
						$('#payment-address .checkout-content').html(html);

						$('#payment-address .checkout-heading span').html('<?php echo $text_checkout_payment_address; ?>');
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

// Payment Address
$('#button-payment-address').live('click', function() {
	$.ajax({
		url: 'index.php?route=checkout/payment_address/validate',
		type: 'post',
		data: $('#payment-address input[type=\'text\'], #payment-address input[type=\'password\'], #payment-address input[type=\'checkbox\']:checked, #payment-address input[type=\'radio\']:checked, #payment-address input[type=\'hidden\'], #payment-address select'),
		dataType: 'json',
		beforeSend: function() {
			$('#button-payment-address').addClass('btn-loading').prop('disabled', true);
		},
		complete: function() {},
    error: function() {
      $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
    },
		success: function(json) {
			$('.warning, .error').remove();

			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
				if (json['error']['warning']) {
          notificationMsg(json['error']['warning'],'warning');
				}

				if (json['error']['firstname']) {
					$('#payment-address input[name=\'firstname\']').after('<span class="error">' + json['error']['firstname'] + '</span>');
				}

				if (json['error']['lastname']) {
					$('#payment-address input[name=\'lastname\']').after('<span class="error">' + json['error']['lastname'] + '</span>');
				}

				if (json['error']['telephone']) {
					$('#payment-address input[name=\'telephone\']').after('<span class="error">' + json['error']['telephone'] + '</span>');
				}

				if (json['error']['address_1']) {
					$('#payment-address input[name=\'address_1\']').after('<span class="error">' + json['error']['address_1'] + '</span>');
				}

				if (json['error']['numero']) {
					$('#payment-address input[name=\'numero\']').after('<span class="error">' + json['error']['numero'] + '</span>');
				}


				if (json['error']['city']) {
					$('#payment-address input[name=\'city\']').after('<span class="error">' + json['error']['city'] + '</span>');
				}

				if (json['error']['postcode']) {
					$('#payment-address input[name=\'postcode\']').after('<span class="error">' + json['error']['postcode'] + '</span>');
				}

				if (json['error']['country']) {
					$('#payment-address select[name=\'country_id\']').after('<span class="error">' + json['error']['country'] + '</span>');
				}

				if (json['error']['zone']) {
					$('#payment-address select[name=\'zone_id\']').after('<span class="error">' + json['error']['zone'] + '</span>');
				}
			} else {
				<?php if ($shipping_required) { ?>
				$.ajax({
					url: 'index.php?route=checkout/shipping_address',
					dataType: 'html',
					success: function(html) {
						$('#shipping-address .checkout-content').html(html);

						$('#payment-address .checkout-content').slideUp('slow');

						$('#shipping-address .checkout-content').slideDown('slow', function(){
              $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
            });

						$('#payment-address .checkout-heading a').remove();
						$('#shipping-address .checkout-heading a').remove();
						$('#shipping-method .checkout-heading a').remove();
						$('#payment-method .checkout-heading a').remove();

						$('#payment-address .checkout-heading').append('<a><?php echo "" ?></a>');
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
				<?php } else { ?>
				$.ajax({
					url: 'index.php?route=checkout/payment_method',
					dataType: 'html',
					success: function(html) {
						$('#payment-method .checkout-content').html(html);

						$('#payment-address .checkout-content').slideUp('slow');

						$('#payment-method .checkout-content').slideDown('slow', function(){
              $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
            });

						$('#payment-address .checkout-heading a').remove();
						$('#payment-method .checkout-heading a').remove();

						$('#payment-address .checkout-heading').append('<a><?php echo "" ?></a>');
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
				<?php } ?>

				$.ajax({
					url: 'index.php?route=checkout/payment_address',
					dataType: 'html',
					success: function(html) {
						$('#payment-address .checkout-content').html(html);
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
			}

			if (DATA_GOOGLE_ANALYTICS) {
			  if (DATA_GOOGLE_ANALYTICS.find(ga => ga === 'gtag')) {
          window.googleAnalytics('begin_checkout', {
            currency: 'BRL',
            items: DATA_PRODUCTS,
          });
        }

        if (DATA_GOOGLE_ANALYTICS.find(ga => ga === 'gtm')) {
          window.googleTagManager('begin_checkout', {
            currency: 'BRL',
            items: DATA_PRODUCTS,
          });
        }
      }
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

// Shipping Address
$('#button-shipping-address').live('click', function() {
  if (!$('#form-address').valid() || $(this).hasClass('btn-loading')) return;

	$.ajax({
		url: 'index.php?route=checkout/shipping_address/validate',
		type: 'post',
		data: $('#shipping-address input[type=\'text\'], #shipping-address input[type=\'password\'], #shipping-address input[type=\'checkbox\']:checked, #shipping-address input[type=\'radio\']:checked, #shipping-address input[type=\'hidden\'], #shipping-address select'),
		dataType: 'json',
		beforeSend: function() {
      $('#button-shipping-address').addClass('btn-loading').prop('disabled', true);
		},
		success: function(json) {
			$('.warning, .error').remove();

			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
        $('.btn-loading').removeClass('btn-loading').prop('disabled', false);

				if (json['error']['warning']) {
          notificationMsg(json['error']['warning'],'warning');
				}

				if (json['error']['firstname']) {
          $('#shipping-address input[name=\'firstname\']').siblings('.form-error').remove();
					$('#shipping-address input[name=\'firstname\']').after('<span class="form-error">' + json['error']['firstname'] + '</span>');
				}

				if (json['error']['lastname']) {
          $('#shipping-address input[name=\'lastname\']').siblings('.form-error').remove();
					$('#shipping-address input[name=\'lastname\']').after('<span class="form-error">' + json['error']['lastname'] + '</span>');
				}

				if (json['error']['email']) {
          $('#shipping-address input[name=\'email\']').siblings('.form-error').remove();
					$('#shipping-address input[name=\'email\']').after('<span class="form-error">' + json['error']['email'] + '</span>');
				}

				if (json['error']['telephone']) {
          $('#shipping-address input[name=\'telephone\']').siblings('.form-error').remove();
					$('#shipping-address input[name=\'telephone\']').after('<span class="form-error">' + json['error']['telephone'] + '</span>');
				}

				if (json['error']['address_1']) {
          $('#shipping-address input[name=\'address_1\']').siblings('.form-error').remove();
					$('#shipping-address input[name=\'address_1\']').after('<span class="form-error">' + json['error']['address_1'] + '</span>');
				}

        if (json['error']['address_2']) {
          $('#shipping-address input[name=\'address_2\']').siblings('.form-error').remove();
					$('#shipping-address input[name=\'address_2\']').after('<span class="form-error">' + json['error']['address_2'] + '</span>');
				}

        if (json['error']['complemento']) {
          $('#shipping-address input[name=\'complemento\']').siblings('.form-error').remove();
					$('#shipping-address input[name=\'complemento\']').after('<span class="form-error">' + json['error']['complemento'] + '</span>');
				}

        if (json['error']['apelido']) {
          $('#shipping-address input[name=\'apelido\']').siblings('.form-error').remove();
					$('#shipping-address input[name=\'apelido\']').after('<span class="form-error">' + json['error']['apelido'] + '</span>');
				}

				if (json['error']['numero']) {
          $('#shipping-address input[name=\'numero\']').siblings('.form-error').remove();
					$('#shipping-address input[name=\'numero\']').after('<span class="form-error">' + json['error']['numero'] + '</span>');
				}

				if (json['error']['city']) {
          $('#shipping-address input[name=\'city\']').siblings('.form-error').remove();
					$('#shipping-address input[name=\'city\']').after('<span class="form-error">' + json['error']['city'] + '</span>');
				}

				if (json['error']['postcode']) {
          $('#shipping-address input[name=\'postcode\']').siblings('.form-error').remove();
					$('#shipping-address input[name=\'postcode\']').after('<span class="form-error">' + json['error']['postcode'] + '</span>');
				}

				if (json['error']['zone']) {
          $('#shipping-address select[name=\'zone_id\']').siblings('.form-error').remove();
					$('#shipping-address select[name=\'zone_id\']').after('<span class="form-error">' + json['error']['zone'] + '</span>');
				}

			} else {
				$.ajax({
					url: 'index.php?route=checkout/shipping_method',
					dataType: 'html',
					success: function(html) {
            var selectedAddress;
            if($('#shipping-address-existing')[0].checked){
              let selectedAddressIndex = $('#shipping-address select[name=address_id]')[0].options.selectedIndex;
              selectedAddress = $('#shipping-address select[name=address_id]')[0].options[selectedAddressIndex].innerText;
            } else {
              let zoneName = $('#shipping-address select[name=\'zone_id\']')[0].options[$('#shipping-address select[name=\'zone_id\']')[0].options.selectedIndex].label;
              let apelido = $('#shipping-address input[name=\'apelido\']').val() == '' ? '' : ' (' + $('#shipping-address input[name=\'apelido\']').val() + ')';
              let complemento = $('#shipping-address input[name=\'complemento\']').val() == '' ? '' : ', ' + $('#shipping-address input[name=\'complemento\']').val();
              selectedAddress = $('#shipping-address input[name=\'firstname\']').val() + ' '
                              + $('#shipping-address input[name=\'lastname\']').val();
              selectedAddress += apelido + ' - '
                              + $('#shipping-address input[name=\'address_1\']').val() + ', '
                              + $('#shipping-address input[name=\'numero\']').val() + ', '
                              + $('#shipping-address input[name=\'address_2\']').val()
                              + complemento
                              + ' - '
                              + $('#shipping-address input[name=\'city\']').val() + ', '
                              + zoneName + ' ' + '- Brasil'
              ;
            }
            $('#shipping-address .checkout-heading').empty();

            $('#shipping-method .checkout-content').html(html);

						$('#shipping-address .checkout-content').slideUp('slow');

						$('#shipping-method .checkout-content').slideDown('slow', function() {
                $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
            });

						$('#shipping-address .checkout-heading a').remove();
						$('#shipping-method .checkout-heading a').remove();
						$('#payment-method .checkout-heading a').remove();

						$('#shipping-address .checkout-heading').append(
						        '<a><?php echo $text_modify; ?></a> ' +
                    'Endereço de entrega:<br/> <div class="top-margin-medium"><b>'+selectedAddress+'</b></div>'
            );
					},
					error: function(xhr, ajaxOptions, thrownError) {
            $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});

			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
      $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

// Guest
$('#button-guest').live('click', function() {
	$.ajax({
		url: 'index.php?route=checkout/guest/validate',
		type: 'post',
		data: $('#payment-address input[type=\'text\'], #payment-address input[type=\'checkbox\']:checked, #payment-address input[type=\'radio\']:checked, #payment-address input[type=\'hidden\'], #payment-address select'),
		dataType: 'json',
		beforeSend: function() {
			$('#button-guest').addClass('btn-loading').prop('disabled', true);
		},
		complete: function() {
			$('#button-guest').removeClass('btn-loading').prop('disabled', false);
		},
		success: function(json) {
			$('.warning, .error').remove();

			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
				if (json['error']['warning']) {
          notificationMsg(json['error']['warning'],'warning');
				}

				if (json['error']['firstname']) {
					$('#payment-address input[name=\'firstname\'] + br').after('<span class="error">' + json['error']['firstname'] + '</span>');
				}

				if (json['error']['data_nascimento']) {
					$('#payment-address input[name=\'data_nascimento\'] + br').after('<span class="error">' + json['error']['data_nascimento'] + '</span>');
				}

				if (json['error']['cpf']) {
					$('#payment-address input[name=\'cpf\'] + br').after('<span class="error">' + json['error']['cpf'] + '</span>');
				}

				if (json['error']['cnpj']) {
					$('#payment-address input[name=\'cnpj\'] + br').after('<span class="error">' + json['error']['cnpj'] + '</span>');
				}

				if (json['error']['razao_social']) {
					$('#payment-address input[name=\'razao_social\']').after('<span class="error">' + json['error']['razao_social'] + '</span>');
				}


				if (json['error']['lastname']) {
					$('#payment-address input[name=\'lastname\'] + br').after('<span class="error">' + json['error']['lastname'] + '</span>');
				}

				if (json['error']['email']) {
					$('#payment-address input[name=\'email\'] + br').after('<span class="error">' + json['error']['email'] + '</span>');
				}

				if (json['error']['telephone']) {
					$('#payment-address input[name=\'telephone\'] + br').after('<span class="error">' + json['error']['telephone'] + '</span>');
				}

				if (json['error']['address_1']) {
					$('#payment-address input[name=\'address_1\'] + br').after('<span class="error">' + json['error']['address_1'] + '</span>');
				}

				if (json['error']['numero']) {
					$('#payment-address input[name=\'numero\'] + br').after('<span class="error">' + json['error']['numero'] + '</span>');
				}


				if (json['error']['city']) {
					$('#payment-address input[name=\'city\'] + br').after('<span class="error">' + json['error']['city'] + '</span>');
				}

				if (json['error']['postcode']) {
					$('#payment-address input[name=\'postcode\'] + br').after('<span class="error">' + json['error']['postcode'] + '</span>');
				}

				if (json['error']['country']) {
					$('#payment-address select[name=\'country_id\'] + br').after('<span class="error">' + json['error']['country'] + '</span>');
				}

				if (json['error']['zone']) {
					$('#payment-address select[name=\'zone_id\'] + br').after('<span class="error">' + json['error']['zone'] + '</span>');
				}
			} else {
				<?php if ($shipping_required) { ?>
				var shipping_address = $('#payment-address input[name=\'shipping_address\']:checked').attr('value');

				if (shipping_address) {
					$.ajax({
						url: 'index.php?route=checkout/shipping_method',
						dataType: 'html',
						success: function(html) {
							$('#shipping-method .checkout-content').html(html);

							$('#payment-address .checkout-content').slideUp('slow');

							$('#shipping-method .checkout-content').slideDown('slow', function() {
                $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
              });

							$('#payment-address .checkout-heading a').remove();
							$('#shipping-address .checkout-heading a').remove();
							$('#shipping-method .checkout-heading a').remove();
							$('#payment-method .checkout-heading a').remove();

							$('#payment-address .checkout-heading').append('<a><?php echo "" ?></a>');
							$('#shipping-address .checkout-heading').append('<a><?php echo $text_modify; ?></a>');

							$.ajax({
								url: 'index.php?route=checkout/guest_shipping',
								dataType: 'html',
								success: function(html) {
									$('#shipping-address .checkout-content').html(html);
								},
								error: function(xhr, ajaxOptions, thrownError) {
									alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
								}
							});
						},
						error: function(xhr, ajaxOptions, thrownError) {
							alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
						}
					});
				} else {
					$.ajax({
						url: 'index.php?route=checkout/guest_shipping',
						dataType: 'html',
						success: function(html) {
							$('#shipping-address .checkout-content').html(html);

							$('#payment-address .checkout-content').slideUp('slow');

							$('#shipping-address .checkout-content').slideDown('slow', function() {
                $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
              });

							$('#payment-address .checkout-heading a').remove();
							$('#shipping-address .checkout-heading a').remove();
							$('#shipping-method .checkout-heading a').remove();
							$('#payment-method .checkout-heading a').remove();

							$('#payment-address .checkout-heading').append('<a><?php echo "" ?></a>');
						},
						error: function(xhr, ajaxOptions, thrownError) {
							alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
						}
					});
				}
				<?php } else { ?>
				$.ajax({
					url: 'index.php?route=checkout/payment_method',
					dataType: 'html',
					success: function(html) {
						$('#payment-method .checkout-content').html(html);

						$('#payment-address .checkout-content').slideUp('slow');

						$('#payment-method .checkout-content').slideDown('slow', function(){
              $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
            });

						$('#payment-address .checkout-heading a').remove();
						$('#payment-method .checkout-heading a').remove();

						$('#payment-address .checkout-heading').append('<a><?php echo "" ?></a>');
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
				<?php } ?>
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

// Guest Shipping
$('#button-guest-shipping').live('click', function() {
	$.ajax({
		url: 'index.php?route=checkout/guest_shipping/validate',
		type: 'post',
		data: $('#shipping-address input[type=\'text\'], #shipping-address select'),
		dataType: 'json',
		beforeSend: function() {
			$('#button-guest-shipping').addClass('btn-loading').prop('disabled', true);
		},
		complete: function() {},
    error: function() {
      $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
    },
		success: function(json) {
			$('.warning, .error').remove();

			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
				if (json['error']['warning']) {
          notificationMsg(json['error']['warning'],'warning');
				}

				if (json['error']['firstname']) {
					$('#shipping-address input[name=\'firstname\']').after('<span class="error">' + json['error']['firstname'] + '</span>');
				}

				if (json['error']['lastname']) {
					$('#shipping-address input[name=\'lastname\']').after('<span class="error">' + json['error']['lastname'] + '</span>');
				}

				if (json['error']['address_1']) {
					$('#shipping-address input[name=\'address_1\']').after('<span class="error">' + json['error']['address_1'] + '</span>');
				}

				if (json['error']['numero']) {
					$('#shipping-address input[name=\'numero\']').after('<span class="error">' + json['error']['numero'] + '</span>');
				}

				if (json['error']['city']) {
					$('#shipping-address input[name=\'city\']').after('<span class="error">' + json['error']['city'] + '</span>');
				}

				if (json['error']['postcode']) {
					$('#shipping-address input[name=\'postcode\']').after('<span class="error">' + json['error']['postcode'] + '</span>');
				}

				if (json['error']['country']) {
					$('#shipping-address select[name=\'country_id\']').after('<span class="error">' + json['error']['country'] + '</span>');
				}

				if (json['error']['zone']) {
					$('#shipping-address select[name=\'zone_id\']').after('<span class="error">' + json['error']['zone'] + '</span>');
				}
			} else {
				$.ajax({
					url: 'index.php?route=checkout/shipping_method',
					dataType: 'html',
					success: function(html) {
						$('#shipping-method .checkout-content').html(html);

						$('#shipping-address .checkout-content').slideUp('slow');

						$('#shipping-method .checkout-content').slideDown('slow', function() {
              $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
            });

						$('#shipping-address .checkout-heading a').remove();
						$('#shipping-method .checkout-heading a').remove();
						$('#payment-method .checkout-heading a').remove();

						$('#shipping-address .checkout-heading').append('<a><?php echo $text_modify; ?></a>');
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

$('#button-shipping-method').live('click', function() {
  if ($(this).hasClass('btn-loading')) return;
  $('#payment-method .checkout-heading').empty().append('<?php echo $text_checkout_payment_method?>');

  $.ajax({
		url: 'index.php?route=checkout/shipping_method/validate',
		type: 'post',
		data: $('#shipping-method input[type=\'radio\']:checked, #shipping-method textarea, #shipping-method input[type=\'text\']'),
		dataType: 'json',
		beforeSend: function() {
			
      $('#button-shipping-method').addClass('btn-loading').prop('disabled', true);
		},
		success: function(json) {
			
      if (DATA_GOOGLE_ANALYTICS) {
        if (DATA_GOOGLE_ANALYTICS.find(ga => ga === 'gtag')) {
            window.googleAnalytics('add_shipping_info', {
                currency: 'BRL',
                items: DATA_PRODUCTS,
                shipping_tier: $.trim($('#shipping-method input[type=\'radio\']:checked').closest('.radio').find('.radio-text').text()),
            });
        }

        if (DATA_GOOGLE_ANALYTICS.find(ga => ga === 'gtm')) {
          window.googleTagManager('add_shipping_info', {
            currency: 'BRL',
            items: DATA_PRODUCTS,
            shipping_tier: $.trim($('#shipping-method input[type=\'radio\']:checked').closest('.radio').find('.radio-text').text()),
          });
        }
      }

			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
				
        $('.btn-loading').removeClass('btn-loading').prop('disabled', false);

				if (json['error']['warning']) {
          notificationMsg(json['error']['warning'],'warning');
				}
				
			} else {
        $('#shipping-method .checkout-heading').empty().append('Forma de entrega: <b>'+json['selectedShippingTitle']+'</b>');
				$.ajax({
					url: 'index.php?route=checkout/payment_method',
					dataType: 'html',
					success: function(html) {
						$('#payment-method .checkout-content').html(html);

						$('#shipping-method .checkout-content').slideUp('slow');

						$('#payment-method .checkout-content').slideDown('slow', function() {
              $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
            });

						$('#shipping-method .checkout-heading a').remove();
						$('#payment-method .checkout-heading a').remove();

						$('#shipping-method .checkout-heading').append('<a><?php echo $text_modify; ?></a>');
					},
					error: function(xhr, ajaxOptions, thrownError) {
            $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			
      $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

$('#button-payment-method').live('click', function() {
  $('.checkout-content > .one-page-checkout > #opc-billing > #checkout-step-billing > .error-payment').remove();

	$('#button_tentarnovamente').live('click', function() {

		$.ajax({
			url: 'index.php?route=checkout/confirm',
			dataType: 'html',
			beforeSend: function() {
				$('#button_tentarnovamente').addClass('btn-loading');
			},
			complete: function() {
				// $('.wait').remove();
        		$('#button_tentarnovamente').removeClass('btn-loading');
			},
			success: function(html) {
				$('#confirm .checkout-content').html(html);

				$('#payment-method .checkout-content').slideUp('slow');

				$('#confirm .checkout-content').slideDown('slow', function() {
					$('.btn-loading').removeClass('btn-loading').prop('disabled', false);
				});

				$('#payment-method .checkout-heading a').remove();
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});

	});

  var
    dataPayment = {};
    dataPayment['payment_method'] = $('#payment-method input[type=radio]:checked').val();
    dataPayment['agree'] = $('#payment-method input[name="agree"]:checked').val();
    dataPayment['comment'] = $('#payment-method textarea').val();

    if($('#payment-method input[name="feedback_order"]').is(':checked')){
      dataPayment['feedback_order'] = $('#payment-method input[name="feedback_order"]').val();
      dataPayment['feedback_order_whatsapp'] = $('#payment-method input[name="feedback_order_whatsapp"]').val();
    }

  if(typeof $('#payment-method input[type=\'radio\']:checked').data('code-type') != 'undefined'){
    dataPayment['payment_method_type'] = $('#payment-method input[type=\'radio\']:checked').data('code-type');
  }

	$.ajax({
		url: 'index.php?route=checkout/payment_method/validate',
		type: 'post',
		data: dataPayment,
		dataType: 'json',
		beforeSend: function() {
			$('#button-payment-method').addClass('btn-loading').prop('disabled', true);
		},
		complete: function() {},
    error: function() {
      $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
    },
		success: function(json) {
			$('.warning, .error').remove();

			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
				if (json['error']['warning']) {
					// $('#payment-method .checkout-content').prepend('<div class="warning"><span>' + json['error']['warning'] + '<i class="btn-close fas fa-times" aria-hidden="true"></i></span></div>');
					// $('.warning').fadeIn('slow');
				notificationMsg(json['error']['warning'],'warning');
					$('#button-payment-method').removeClass('btn-loading').prop('disabled', false);
					}

				if (json['error']['payment']) {
					$('.checkout-content > .one-page-checkout > #opc-billing > #checkout-step-billing').prepend('<div class="error-payment text-error border-error background-error padding-medium vertical-margin-medium text-medium">'+json['error']['payment']+'</div>');
					}
			} else {
				$.ajax({
					url: 'index.php?route=checkout/confirm',
					dataType: 'html',
					success: function(html) {
						if (DATA_GOOGLE_ANALYTICS) {
						if (DATA_GOOGLE_ANALYTICS.find(ga => ga === 'gtag')) {
							window.googleAnalytics('add_payment_info', {
							currency: 'BRL',
							items: DATA_PRODUCTS,
							payment_type: $.trim($('#payment-method input[type=radio]:checked').closest('.radio').find('.radio-text').text())
							});
						}

						if (DATA_GOOGLE_ANALYTICS.find(ga => ga === 'gtm')) {
							window.googleTagManager('add_payment_info', {
							currency: 'BRL',
							items: DATA_PRODUCTS,
							payment_type: $.trim($('#payment-method input[type=radio]:checked').closest('.radio').find('.radio-text').text())
							});
						}
						}

						let selectedPaymentTitle = $('#payment-method input[type=radio]:checked')[0].labels[0].innerText;

						$('#payment-method .checkout-heading').empty().append('<?php echo $text_checkout_payment_method ?>: <b>'+selectedPaymentTitle+'</b>');

						$('#confirm .checkout-content').html(html);

						$('#payment-method .checkout-content').slideUp('slow');

						$('#confirm .checkout-content').slideDown('slow', function() {
							$('.btn-loading').removeClass('btn-loading').prop('disabled', false);
						});

						$('#payment-method .checkout-heading a').remove();

						$('#payment-method .checkout-heading').append('<a><?php echo $text_modify; ?></a>');
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
						
			}		
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

function quickConfirm(module){
	$.ajax({
		url: 'index.php?route=checkout/confirm',
		dataType: 'html',
		success: function(html) {
			$('#confirm .checkout-content').html(html);
			$('#confirm .checkout-content').slideDown('slow', function() {
				$('.btn-loading').removeClass('btn-loading').prop('disabled', false);
			});
			$('.checkout-heading a').remove();

			$('#checkout .checkout-heading a').remove();
			$('#checkout .checkout-heading').append('<a><?php echo $text_modify; ?></a>');

			$('#shipping-address .checkout-heading a').remove();
			$('#shipping-address .checkout-heading').append('<a><?php echo $text_modify; ?></a>');

			$('#shipping-method .checkout-heading a').remove();
			$('#shipping-method .checkout-heading').append('<a><?php echo $text_modify; ?></a>');

			$('#payment-address .checkout-heading a').remove();
			$('#payment-address .checkout-heading').append('<a><?php echo "" ?></a>');

			$('#payment-method .checkout-heading a').remove();
			$('#payment-method .checkout-heading').append('<a><?php echo $text_modify; ?></a>');
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
}
});
</script>
<?php echo $footer; ?>
