// init delivery terms change
$(document).ready(function (){
  $('.delivery-item input').bind('click', function () {initPayment();})
  initPayment();
  $('#montage').bind('click', montageButtonChanged);
  montageButtonChanged();
  initLogin();
});


function initLogin()
{
  $('#basket-login-head a').bind('click', function () {
    $('#basket-login-head').hide();
    $('#basket-login').show();
    $('#basket-login').find('input[name="auth_login"]').focus();
    return false;
  });
}


function deliveryChanged(delivery_id)
{  
  deliveryArray = aPaymentDeliveryTree[delivery_id];

  $('input[name="payment_id"]').each(function () {
    payment_id = $(this).val();
    find = false;
    if (deliveryArray)
    {
      for (i in deliveryArray.paymentIdList)
      {
        if (deliveryArray.paymentIdList[i] == payment_id)
        {
          $('#not_delivery_sentense').hide();
          $('#payment_'+payment_id).show();
          find = true;
        }
      }
    }
    if (!find)
    {            
      $(this).attr('checked', false);
      $('#payment_'+payment_id).hide();
    }
  });

}


function initPayment()
{
  selectedDelivery = $('.delivery-item').find('input:checked');
  if (selectedDelivery.length > 0)
  {
    deliveryChanged(selectedDelivery.val());
  }
}


function montageButtonChanged()
{
  if ($('#montage').attr('checked') == true)
  {
    $('.is_not_for_montage').hide();

    // ted musim vsechny  neskryte projit, a pokud neni zadny z nich zatrhnuty, tak jeden zatrhnu
    var is_checked = false;
    var firstInput = '';
    $('.delivery-item').each(function () {
      if ($(this).hasClass('is_not_for_montage') == false)
      {
        inp = $(this).find('input:checked');
        if (inp.length > 0) is_checked = true;
        if (firstInput == '') firstInput = $(this).find('input');
      }
    });
    if (is_checked == false) // neni zadny checknuty ze zobrazenych, tak checknu prvni z nich
    {
      $(firstInput).attr('checked', true);
      initPayment();
    }
  }
  else
  {
    $('.is_not_for_montage').show();
  }
}


/*
 * Pro DIC a IC
 */
$(document).ready(function () {
  $('input[name="ic"]').live('change', function () {
    removeWhiteSpaces(this);
  });
  $('input[name="dic"]').live('change', function () {
    removeWhiteSpaces(this);
  });
})

function removeWhiteSpaces(elm)
{
  var content;
  content = $(elm).val();
  if (content)
  {
    var re;
    re = /( )/g;
    content = content.replace(re, '');
    $(elm).val(content);
  }
}
