<!-- Custom Theme Scripts -->
{{ javascript_include("js/custom.js") }}

<script>
$( function() {
  $('.change-language').click(function(){
    var language = $(this).data('lang');
    $('#country').val(language);

    $.ajax({
      type: 'POST',
      url: "{{ url('session/changelanguage') }}",
      dataType: 'json',
      data: {
        language: language
      }
    }).done(function(data) {
      window.location.reload();
    }).error(function(x, t, m) {
  		printAjaxError(x, t, m);
    });
  });
});
$('.request-access').click(function(){
  $('#request-access-box').toggle();
  $('.pwm-field-icon').toggle();
});
$('#request-access-btn').click(function(){
  $(".msg-error").hide();
  $(".msg-success").hide();

  $('#businessname').css('background-color', 'white');
  $('#vatcode').css('background-color', 'white');
  $('#email').css('background-color', 'white');
  $('#address').css('background-color', 'white');
  $('#city').css('background-color', 'white');

  var descri = $('#businessname').val();
  var dsetic = $('#signboard').val();
  var ivacfi = $('#vatcode').val();
  var indema = $('#email').val();
  var numtel = $('#telephone').val();
  var indiri = $('#address').val();
  var ccitta = $('#city').val();
  var dsnazi = $('#country option:selected').html();
  var provin = $('#province').val();

  if (descri == "" || ivacfi == "" || indema == "" || indiri == "" || ccitta == "") {
    $('#businessname').css('background-color', descri != "" ? 'white' : 'red');
    $('#vatcode').css('background-color', ivacfi != "" ? 'white' : 'red');
    $('#email').css('background-color', indema != "" ? 'white' : 'red');
    $('#address').css('background-color', indiri != "" ? 'white' : 'red');
    $('#city').css('background-color', ccitta != "" ? 'white' : 'red');
  } else {
    $.ajax({
      type: 'POST',
      url: "{{ url('session/saverequest') }}",
      dataType: 'json',
      data: {
        descri: descri,
        dsetic: dsetic,
        ivacfi: ivacfi,
        indema: indema,
        numtel: numtel,
        indiri: indiri,
        ccitta: ccitta,
        dsnazi: dsnazi,
        provin: provin,
      }
    }).done(function(data) {
      if (data == "OK") {
        showTemporary(".msg-success", 5000);

        {% if reqaccess != "" %}
        var emailBody = "{{ mt._('account.customer.businessname') }}: " + descri + "\n";
        emailBody += "{{ mt._('account.customer.signboard') }}: " + dsetic + "\n";
        emailBody += "{{ mt._('account.customer.vatcode') }}: " + ivacfi + "\n";
        emailBody += "Email: " + indema + "\n";
        emailBody += "{{ mt._('account.customer.telephone') }}: " + numtel + "\n";
        emailBody += "{{ mt._('account.customer.address') }}: " + indiri + "\n";
        emailBody += "{{ mt._('account.customer.city') }}: " + ccitta + "\n";
        emailBody += "{{ mt._('account.customer.country') }}: " + dsnazi + "\n";
        emailBody += "{{ mt._('account.customer.province') }}: " + provin + "\n";
        emailBody += "***********************************************\n\n";
        emailBody += "{{ mt._('account.customer.autogenerated') }}";
        window.location = 'mailto:' + "{{ reqaccess }}" + '?subject=' + encodeURIComponent("{{ mt._('login.requestregistration') }}") + '&body=' + encodeURIComponent(emailBody);
        {% endif %}
      } else {
        showTemporary(".msg-error", 5000);
      }
    }).error(function(x, t, m) {
  		printAjaxError(x, t, m);
      showTemporary(".msg-error", 5000);
    });
  }
});
</script>
