var type; var code;

var webroot = '/';

function showErrorMessage() {

$('.loading').fadeOut('slow', function() {
  $('.error-modal').fadeIn();
});

}

function showLoading() {

$('.loading').fadeIn();

}

function hideLoading() {

$('.loading').fadeOut('slow');

}

function isPublicLink(links, path) {

var pathWithoutEndBar = path.replace(/\/?$/, '');
return links.includes(pathWithoutEndBar);

}

function redirectPublicLink() {

var currentPath = window.location.pathname;
var publicLinks = {{ site.public_links | jsonify }};
var isNotPublicLink = ! isPublicLink(publicLinks, currentPath);

// redirect to webroot page
if (isNotPublicLink) window.location.href = webroot;

showLoading();

try {
  $('.plan__box').each(function() {
    var promoCode = $(this).data('promocode');
    getDataForm($(this).find('.free-value'), promoCode, webroot);
  });
} catch (error) {
  window.location.href = webroot;
}

}

function getDataForm(freeValueElement, promoCode, webroot) {

$.ajax({
  url: CHECK_CODE + '/' + promoCode,
  method: 'GET',
  contentType: 'application/json',
  success: function(response) {
    var bonificacao = response[0].numeroMesesBonificacao;

    if (bonificacao) {
      freeValueElement.html(bonificacao);
      hideLoading();
    } else {
      window.location.href = webroot;
    }
  },
  error: function() {
    window.location.href = webroot;
  },
});

}

$(document).ready(function() {

redirectPublicLink();

$('.plan__checkbox').prop('checked', false);

var handleFormSubmit = function(actionUrl) {
  return function() {
    var promocode = $(event.target).data('promocode');
    event.preventDefault();
    window.location.href = actionUrl + '?origem=lp&codigo=' + promocode;
  };
};

$('.error-modal').on('click', function(event) {
  event.preventDefault();
  $('.error-modal').fadeOut();
});

});

var handleCheckbox = function(buttonId) {

return function(e) {
  var checked = $(e.target).prop('checked');
  $('#' + buttonId).prop('disabled', !checked);
};

};

$('.terms-checkbox').each(function() {

var buttonId = $(this).data('submitButtonId');
$(this).change(handleCheckbox(buttonId));

});