function validate(what, text) {
  if (what == 'quantity') {
    var re = /^[0-9]{1,2}$/;
  } else if (what == 'email') {
    var re = /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/;
  } else if (what == 'delivery_address') {
    var re = /^(?:.*(?:\r\n|\n|\r)){2,}/;
  } else if (what == 'billing_address') {
    if (text == '') {
      return true;
    }
    var re = /^(?:.*(?:\r\n|\n|\r)){2,}/;
  } else {
    var re = /^notpermitted$/;
  }
  return re.test(text);
}

$(document).ready(function () {
  var username = $("#username").html();
  $("#quantity_exclamation").hide();
  $("#email_exclamation").hide();

  $("#modal-save").click(function() {
    // Validation Code
    var is_valid = true;
    $("#success_message").addClass('is-hidden');
    $("#error_message").addClass('is-hidden');
    if (!validate('quantity',$('#quantity').val())) {
       $('#quantity_errormsg').html('Ung&uuml;ltige Anzahl');
       $("#quantity").addClass("is-danger");
       $("#quantity_exclamation").show();
       is_valid = false;
    } else {
       $('#quantity_errormsg').html('');
       $("#quantity").removeClass("is-danger");
       $("#quantity_exclamation").hide();
    }
  
    if (!validate('email',$('#email').val())) {
       $('#email_errormsg').html('Ung&uuml;ltige E-Mail Adresse');
       $("#email").addClass("is-danger");
       $("#email_exclamation").show();
       is_valid = false;
    } else {
       $('#email_errormsg').html('');
       $("#email").removeClass("is-danger");
       $("#email_exclamation").hide();
    }
  
    if (!validate('delivery_address',$('#delivery_address').val())) {
       $('#delivery_address_errormsg').html('Ung&uuml;ltige Adresse');
       $("#delivery_address").addClass("is-danger");
       is_valid = false;
    } else {
       $('#delivery_address_errormsg').html('');
       $("#delivery_address").removeClass("is-danger");
    }

    if (!validate('billing_address',$('#billing_address').val())) {
       $('#billing_address_errormsg').html('Ung&uuml;ltige Adresse');
       $("#billing_address").addClass("is-danger");
       is_valid = false;
    } else {
       $('#billing_address_errormsg').html('');
       $("#billing_address").removeClass("is-danger");
    }
  
  
    if (!($('#accept_terms').is(':checked'))) {
       $('#accept_terms_errormsg').html('Bitte zustimmen');
       $("#accept_terms_label").addClass("has-text-danger");
       is_valid = false;
    } else {
       $('#accept_terms_errormsg').html('');
       $("#accept_terms_label").removeClass("has-text-danger");
    }
  
    if (!(is_valid)) {
      return;
    }
  
    $.ajax({
      url: "order",
      type: "POST", //send it through post method
      dataType: "json",
      data: JSON.stringify({ "quantity": $('#quantity').val(), 
                             "email": $('#email').val(),
                             delivery_address: $("#delivery_address").val(),
                             billing_address: $("#billing_address").val() 
                           }
                          ),
      success: function(response) {
        $("#modal").removeClass("is-active");
        $("#success_message").removeClass("is-hidden");
      },
      error: function(xhr) {
        $("#modal").removeClass("is-active");
        $("#error_message").removeClass("is-hidden");
      }
    });

  });

  $("#modal-close").click(function () {
    $("#modal").removeClass("is-active");
    window.location.replace("/");
  });

  function callback() {
     $('#quantity').focus().select();
     if (username != "") {
       $("#email").val(username);
     }
  }

  setTimeout(callback, 10);
});
