function checkForm(form)
{
  if (form.name.value.length==0) {
    alert('Введите имя и фамилию');
    form.name.focus();
    return false;
  }

  if (form.phone.value.length==0) {
    alert('Введите телефон');
    form.phone.focus();
    return false;
  }
  /*
  if (form.email.value.length==0) {
    alert('Введите e-mail');
    form.mail.focus();
    return false;
  }
  var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  if(!re.test(form.email.value)) {
    alert('Введите корректный e-mail');
    form.mail.focus();
    return false;
  }

  if (!document.getElementById('boxoffice').checked&&!document.getElementById('delivery').checked) {
    alert('Выберите способ получения заказа');
    form.boxoffice.focus();
    return false;
  }

  if (form.address.value.length==0) {
    alert('Введите адрес доставки');
    form.address.focus();
    return false;
  }

  if (!document.getElementById('morning').checked&&!document.getElementById('evening').checked) {
    alert('Выберите время доставки');
    form.morning.focus();
    return false;
  }
  */
  if (document.getElementById('date').value=='') {
    alert('Введите дату спектакля');
    form.date.focus();
    return false;
  }

  if (document.getElementById('category').value=='') {
    alert('Выберите категорию билетов');
    form.category.focus();
    return false;
  }

  if (form.num.value.length==0) {
    alert('Введите кол-во билетов');
    form.num.focus();
    return false;
  }

  return true;
}

function digit_case(x, s1, s2, s3)
{
  if (typeof s3 == 'undefined' || !s3) s3 = s2;

  if (x % 10 == 1 && x != 11)
    return x+' '+s1;
  else if (x % 10 >= 2 && x % 10 <= 4 && x != 12 && x != 13 && x != 14)
    return x+' '+s2;
  else
    return x+' '+s3;
}

function print_tickets_info()
{
  var e = {
    amount: document.forms.book_form.num.value,
    category: document.forms.book_form.category.value,
    date: document.forms.book_form.date.value,

    delivery: document.getElementById('delivery'),

    //today_div: document.getElementById('today_div'),

    map_link: document.getElementById('map_link'),

    count: document.getElementById('tickets_count_span'),
    cost: document.getElementById('tickets_cost_span')
  };

  var price = di_get_price(e.date, e.category);
  var shipment = e.delivery.checked ? 300 : 0;
  var cost = e.amount * price + shipment;

  e.count.innerHTML = digit_case(e.amount*1, 'билет', 'билета', 'билетов');
  e.cost.innerHTML = cost+' руб';

  var dis = e.delivery.checked ? false : true;

  //didis(document.forms.book_form.comment, dis);
  didis(document.getElementById('morning'), dis);
  didis(document.getElementById('evening'), dis);
  didis(document.forms.book_form.address, dis);

  var date_ar = e.date.split(/[.\x20:]+/);
  map_link.href = date_ar.length >= 3
    ? '/i/maps_operetta.html?y='+date_ar[2]+'&m='+date_ar[1]+'&d='+date_ar[0]
    : '/i/maps_operetta.html?y=2011&m=3&d=1';

  /*
  if (e.today_div)
  {
    e.today_div.style.display = e.date.substr(0, today_str.length) == today_str
      ? 'block'
      : 'none';
  }
  */
}

function di_get_price(date_str, zone)
{
  if (!date_str) return 0;

  var dw = wd_ar[date_str.substr(0,10)];
  var h = date_str.substr(11,2);

  /* old 8-9 zones
  if (h < 18)
  {
    var cat = 3;
  }
  else
  {
    if (dw == 5 || dw == 6 || is_day_holiday(date_str.substr(0, 10)))
      var cat = 2;
    else
      var cat = 1;
  }
  */

  // new 5 zones
  if (h < 18)
  {
    var cat = 3;
  }
  else
  {
    if (dw == 6 || is_day_holiday(date_str.substr(0, 10)))
      var cat = 2;
    else if (dw == 5 || dw == 7)
      var cat = 3;
    else
      var cat = 1;
  }

  //alert(date_str.substr(0,10)+' '+date_str.substr(3,7)+' z'+zone+' c'+cat);

  var idx;

  if (typeof diprice_ar[date_str.substr(0,10)] != 'undefined')
    idx = date_str.substr(0,10);
  else if (typeof diprice_ar['00.'+date_str.substr(3,7)] != 'undefined')
    idx = '00.'+date_str.substr(3,7);
  else if (typeof diprice_ar['00.00.'+date_str.substr(6,4)] != 'undefined')
    idx = '00.00.'+date_str.substr(6,4);
  else
    idx = '00.00.0';

  var mode = dimodes_ar[idx];
  var ar = diprice_ar[idx];

  var mode_wo_vip = mode == 5 || mode == 6;
  var mode_wo_last = mode == 5 || mode == 6;

  // mode and zones
  var category_sel = _ge('category');

  //alert(category_sel.options.length+' '+mode);

  if (
      /*
      (category_sel.options.length != mode + 1 && mode != 5) ||
      (category_sel.options.length != mode && mode == 5)
      */
      /*
      (category_sel.options.length != mode && mode != 5) ||
      (category_sel.options.length != mode - 1 && mode == 5)
      */
      category_sel.options.length != mode // - (mode_wo_last ? 1 : 0)
     )
  {
    while (category_sel.options.length > 1)
    {
      category_sel.options.remove(category_sel.options.length - 1);
    }

    var vv;
    var max_mode = mode_wo_last ? mode - 1 : mode;

    //alert(max_mode);

    for (var x = 0; x < max_mode; x++)
    {
      if (mode_wo_vip)
        //vv = x ? x + 1 : 'VIP 1';
        vv = x + 1;
      else
        vv = x ? x : 'VIP';

      var nn = document.createElement('option');
      nn.value = x+'';
      category_sel.options.add(nn);
      nn.innerText = vv;
    }
      /*
      while (category_sel.options.length < mode + 1)
      {
        var x = category_sel.options.length - 1;

        var nn = document.createElement('option');
        nn.value = x;
        category_sel.options.add(nn);
        nn.innerText = x+'    ';
      }

      while (category_sel.options.length > mode + 1)
      {
        category_sel.options.remove(category_sel.options.length - 1);
      }
      */
  }
  //

  if (!zone) return 0;

  return ar ? ar['z'+zone+'c'+cat] : 0;
}

function lead0(x)
{
  return x.toString().length == 1 ? "0"+x : x;
}

function is_day_holiday(str)
{
  var ar = typeof holidays_ar[str.substr(6)*1] != 'undefined' ? holidays_ar[str.substr(6)*1] : [];

  for (var i = 0; i < ar.length; i++)
    if (ar[i] == str) return true;

  return false;
}

function didis(e, state)
{
  e.disabled = state;

  if (e.type != 'radio' && e.type != 'checkbox')
    e.style.background = state ? "#aaa" : "#fff";
}

