<?php echo $header; ?>

<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">
    <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>
        <?php } ?>
        <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_document', function() {

	$('.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=fidelity/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 } ?>


$('#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);
    }
  });
});

$('#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 {
          var shipping_address = $('#payment-address input[name=\'shipping_address\']:checked').attr('value');

          if (shipping_address) {
            $.ajax({
              url: 'index.php?route=fidelity/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=fidelity/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=fidelity/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);
              }
            });
          }

        $.ajax({
          url: 'index.php?route=fidelity/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);
    }
  });
});

$('#button-payment-address').live('click', function() {
	$.ajax({
		url: 'index.php?route=fidelity/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');
				}
			} else {
				$.ajax({
					url: 'index.php?route=fidelity/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);
					}
				});
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

    // Shipping Address
    $('#button-shipping-address').live('click', function() {
        $.ajax({
		url: 'index.php?route=fidelity/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);
		},
		complete: function() {
      $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
    },
    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']) {
          $('.btn-loading').removeClass('btn-loading').prop('disabled', false);
                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=fidelity/confirm',
                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();

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

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

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

                  $('#shipping-address .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) {
                  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=fidelity/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; ?>