function updateCount(element) {
  if(element.previous('input') || element.next('input')) {
    var inputTag = element.innerHTML == '+' ? element.previous('input') : element.next('input');
    var inputValue = $F(inputTag);
    var inputValueNumber = parseInt(inputValue.split(' ')[0]) + (element.innerHTML == '+' ? 1 : -1);
    inputValueNumber = inputValueNumber < 1 ? 1 : inputValueNumber;
    var terminal = (inputValue.split(' ')[1] == 'x' ? inputValue.split(' ')[1] : 'x');
    inputTag.value = inputValueNumber + ' ' + terminal;
  }
}

function toggleProcedures(element) {
  var elNextUl = element.next('ul');
  if(elNextUl.visible()) {
    Effect.BlindUp(elNextUl,{ duration: .1 });
    title = element.firstDescendant().readAttribute('title').split('-')[0];
    element.firstDescendant().update(title);
  } else {
    Effect.BlindDown(elNextUl,{ duration: .1 });
    title = element.firstDescendant().readAttribute('title').split('-')[1];
    element.firstDescendant().update(title);
  }
}

function focusAndCheckInput(element) {
  var Check = element.select('input[type="checkbox"]')[0];
  element.select('input[type="text"]')[0].focus();
  Check.checked = !Check.checked;
}

function companyInfo(event) {
  if(($('payment_method').value=="fakturou") || ($('payment_method').value=="invoice") || ($('payment_method').value=="durch Rechnung") || ($('payment_method').value=="fakturą") || ($('payment_method').value=="по счету-фактуре")) {
    $('company_info').show();
  } else { 
    $('company_info').hide();
  }
}

function voucherNameY() {
  var chequeY = $('cheque_yes');
  if(chequeY.checked==true) {
    $('voucher_name_show').show();
  }
}
function voucherNameN() {
  var chequeN = $('cheque_no');
  if(chequeN.checked==true) {
    $('voucher_name_show').hide();
  }
}

function handler(e) {
  var el = Event.element(e);
  var tag = el.tagName.toLowerCase();
  var data = $A(arguments);
  
  data.shift();
  if (tag == "a" && el.hasClassName('countlink')) {
    Event.stop(e);
    updateCount(el);
  }
  if (tag == "h4" && el.hasClassName('toggleProcedures')) {
    Event.stop(e);
    toggleProcedures(el);
  }
  if (tag == "li" && el.up('ul').hasClassName('procedures')) {
    Event.stop(e);
    focusAndCheckInput(el);
  }
}


document.observe('dom:loaded', function() {
  Event.observe(document.body, 'click', handler.bindAsEventListener(handler));
  if($('payment_method')) { Event.observe($('payment_method'), 'change', companyInfo.bindAsEventListener(companyInfo)); }
  if($('cheque_yes')) { Event.observe($('cheque_yes'), 'click', voucherNameY.bindAsEventListener(voucherNameY)); }
  if($('cheque_no')) { Event.observe($('cheque_no'), 'click', voucherNameN.bindAsEventListener(voucherNameN)); }
  if($('calendar_since') && $('calendar_until')) {
    Calendar.setup({ inputField : "date_since", ifFormat : "%d.%m.%Y", button : "calendar_since", align : "tR" });
    Calendar.setup({ inputField : "date_until", ifFormat : "%d.%m.%Y", button : "calendar_until", align : "tR" });

  }
  if($('birthdate')) { Calendar.setup({ inputField : "psw1", ifFormat : "%d.%m.%Y", button : "birthdate", align : "tR" }); }
});
