function check_count(curr, id)
{
  if ( parseInt(curr.value) + parseInt(getObj(id).value) > 10 )
  {
    alert("Maksymalna łączna ilość biletów to 10")
    curr.value = 10 - getObj(id).value
    return false
  }
}

function strtrim(s)
{
  return s.replace(/^\s*/, "").replace(/\s*$/, "");
}

function makeReservation()
{

  var name  = document.forms.reservation.name.value
  var email = document.forms.reservation.email.value
  var phone = document.forms.reservation.phone.value
  var mailme = document.forms.reservation.mailme.checked

  //var invoice = false;
  var invoice = document.forms.reservation.invoice.checked;
  if (invoice) {
    var company = strtrim(document.forms.reservation.company.value);
    var address = strtrim(document.forms.reservation.address.value);
    var nip     = strtrim(document.forms.reservation.nip.value);
    var payment = strtrim(document.forms.reservation.payment.value);
  }
  

  name  = strtrim(name)
  email = strtrim(email)
  phone = strtrim(phone)

  totalCount = parseInt(getObj('count_normal').value) + parseInt(getObj('count_reduced').value)

  if ( totalCount <= 0  || totalCount > 10 )
  {
    alert("Nieprawidłowa ilość rezerwowanych biletów")
    return;
  }
  if ( name == "" )
  {
    document.forms.reservation.name.style.backgroundColor = "#f0a0a0";
    alert("Proszę podać imię i nazwisko")
    return;
  }
  if ( email == "" )
  {
    document.forms.reservation.email.style.backgroundColor = "#f0a0a0";
    alert("Proszę podać adres e-mail")
    return;
  }
  if ( phone == "" )
  {
    document.forms.reservation.phone.style.backgroundColor = "#f0a0a0";
    alert("Proszę podać numer telefonu")
    return;
  }
  if ( invoice) 
  {
    if (company == "")
    {
      alert("Proszę podać nazwę firmy");
      return;
    }
    if (address == "")
    {
      alert("Proszę podać adres firmy");
      return;
    }
    if (nip == "")
    {
      alert("Proszę podać NIP");
      return;
    }
    if (payment != "gotówka" && payment != "przelew" && payment != "karta")
    {
      alert("Proszę wybrać formę płatności");
      return;
    }
  }
 
  var params = {
    action: 1,
    email: email
  }

  if ( mailme )
  {
    rpcQuery("rpc.php?function=mailing.subscription", params)
  }  

  document.forms.reservation.submit()
}

function handleTicketType () {
  var select  = $('ticketType');
  
  if (!select) 
    return;

  var value   = select.value;
  var option  = $$('#ticketType option[value="'+ value +'"]')[0];

  if (option.hasClass('reduced')) {
    // show reduced option
    $('reducedTicketsRow').removeClass('hidden');
  } else {
    // hide reduced
    $('count_reduced').value = 0;
    $('reducedTicketsRow').addClass('hidden');
  }
};

// initial handling
handleTicketType();

// events
if ($('ticketType'))
  $('ticketType').addEvent('change', handleTicketType);

$('invoice').addEvent('change', function() {
  if ($('invoice').checked) {
    $$('tr.invoice_form').each (function(row) {
      $(row).removeClass('hidden');
    });
  } else {
    $$('tr.invoice_form').each (function(row) {
      $(row).addClass('hidden');
    });
  }
});

