function sendRequest(request, url) {
  request.onreadystatechange = serveDrink;
  request.open("GET", url, true);
  request.send(null);
}

function getSize() {
  var sizeGroup = document.forms[0].wielkosc;
  for (i=0; i<sizeGroup.length; i++) {
    if (sizeGroup[i].checked == true) {
      return sizeGroup[i].value;
    }
  }
}

function getBeverage() {
  var beverageGroup = document.forms[0].rodzaj;
  for (i=0; i<beverageGroup.length; i++) {
    if (beverageGroup[i].checked == true) {
      return beverageGroup[i].value;
    }
  }  
}


function orderCoffee() {
  var name = document.getElementById("imie").value;
  var beverage = getBeverage();
  var size = getSize();

  var coffeemakerStatusDiv1 = 
   document.getElementById("ekspresdokawy1-status");
  var status = getText(coffeemakerStatusDiv1);
  
  if (status == "Bezczynny") {
    replaceText(coffeemakerStatusDiv1, "Aktualnie parzę: " +
                size + " " + beverage + " dla " + name);
    document.forms[0].reset();
    var url = "ekspresdokawy.php?name=" + escape(name) +
                               "&size=" + escape(size) +
                               "&beverage=" + escape(beverage) +
                               "&coffeemaker=1";
    sendRequest(request1, url);
  } else {
    var coffeemakerStatusDiv2 =
      document.getElementById("ekspresdokawy2-status");
    status = getText(coffeemakerStatusDiv2);
    
    if (status == "Bezczynny") {
      replaceText(coffeemakerStatusDiv2, "Aktualnie parzę " +
                  size + " " + beverage + " dla " + name);
      document.forms[0].reset();
      var url = "ekspresdokawy.php?name=" + escape(name) +
                                 "&size=" + escape(size) +
                                 "&beverage=" + escape(beverage) +
                                 "&coffeemaker=2";
      sendRequest(request2, url);
    } else {
      alert("Przykro mi! Oba ekspresy do kawy są aktualnie używane. " +
            "Spróbuj ponownie nieco później.");
    }
  }
}

function serveDrink() {
  if (request1.readyState == 4) {
    if (request1.status == 200) {
      var response = request1.responseText;
      var whichCoffeemaker = response.substring(0, 1);
      var name = response.substring(1, response.length);
      if (whichCoffeemaker == "1") {
        var coffeemakerStatusDiv1 = 
          document.getElementById("ekspresdokawy1-status");
        replaceText(coffeemakerStatusDiv1, "Bezczynny");
      } else {
        var coffeemakerStatusDiv2 = 
          document.getElementById("ekspresdokawy2-status");
        replaceText(coffeemakerStatusDiv2, "Bezczynny");
      }
      alert(name + ", Twoja kawa jest gotowa!");
      request1 = createRequest();
    } else 
      alert("Błąd! Status żądania ma wartość " + request1.status);
  } else if (request2.readyState == 4) {
    if (request2.status == 200) {
      var response = request2.responseText;
      var whichCoffeemaker = response.substring(0, 1);
      var name = response.substring(1, response.length);
      if (whichCoffeemaker == "1") {
        var coffeemakerStatusDiv1 = 
          document.getElementById("ekspresdokawy1-status");
        replaceText(coffeemakerStatusDiv1, "Bezczynny");
      } else {
        var coffeemakerStatusDiv2 = 
          document.getElementById("ekspresdokawy2-status");
        replaceText(coffeemakerStatusDiv2, "Bezczynny");
      }
      alert(name + ", Twoja kawa jest gotowa!");
      request2 = createRequest();
    } else 
      alert("Błąd! Status żądania ma wartość " + request2.status);
  }
}


