function getCustomerInfo() {
  var phone = document.getElementById("phone").value;
  var url = "szukajKlientaNB.php?phone=" + phone;
  request.open("GET", url, true);
  request.onreadystatechange = updatePage;
  request.send(null);
}

function updatePage() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      var customerAddress = request.responseText;
      document.getElementById("address").value = customerAddress;
    } else {
      var message = request.getResponseHeader("Status");
      if ((message.length == null) || (message.length <= 0)) {
        alert("Błąd! Status żądania ma wartość " + request.status);
      } else {
        alert(message);
      }
    }
  }
}

function submitOrder() {
  var phone = document.getElementById("tel").value;
  var address = document.getElementById("adres").value;
  var order = document.getElementById("zamowienie").value;
  var url = "zlozZamowienie.php";
  request.open("POST", url, true);
  request.onreadystatechange = showConfirmation;
  request.setRequestHeader("Content-Type",
                           "application/x-www-form-urlencoded");
  request.send("phone=" + phone +
               "&address=" + escape(address) +
               "&order=" + escape(order));
}

function showConfirmation() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      var response = request.responseText;
      if (response.indexOf("Error") > -1) {
        alert(response);
      } else {
        var mainDiv = document.getElementById("main-page");
        var orderForm = document.getElementById("order-form");

        pElement = document.createElement("p");
        textNode = document.createTextNode("Twoje zamówienie powinno zostać dostarczone w ciągu" +
          response + " minut. Życzymy smacznego!");
        pElement.appendChild(textNode);

        mainDiv.replaceChild(pElement, orderForm);
      }
    } else {
      var message = request.getResponseHeader("Status");
      if ((message.length == null) || (message.length <= 0)) {
        alert("Błąd! Status żądania ma wartość " + request.status);
      } else {
        alert(message);
      }
    }
  }
}
