  
  var price_area;
  var DELIEVERY_TYPE;
  var total_price;
  var ClientType;
  var ThisElement;
  var DATA_FIELD = {
    0: ['name', 'address', 'city', 'code', 'mail', 'tel'],
    1: ['company', 'nip', 'name', 'address', 'city', 'code', 'mail', 'tel']
  };
  
  
  $(document).ready(function() {
    
    $('table#module_basket tbody td.cell_b input.basket_inp').keyup(function() {
      
      var id = $(this).attr('name');
      var num = $(this).val();
      var ThisElement = $(this);
      
      $.ajax({
        url: './ajax/basket_recount.php',
        data: {
          'id': id,
          'num': num
        },
        success: function(response) {
          $('table#module_basket tfoot td span.price').text(response).css('background-color', '#F86211');
          ThisElement.attr('disabled', true).css('opacity', 0.4);
          
          setTimeout(
            function() {
              $('table#module_basket tfoot td span.price').css('background-color', '#FFFFFF')
              ThisElement.removeAttr('disabled').css('opacity', 1).blur();
            }
            , 1500);
        },
        error: function(object, response) {
          window.alert(response);
        }
      });
      
    });
    
    
    
    $('table#module_options tfoot td span.price').text(total_price);
    
    $('select.module_select').change(function() {
      price = total_price + DELIEVERY_TYPE[$("select[name='type_delievery']").val()];
      $('table#module_options tfoot td span.price').text(price).css('background-color', '#F86211');
      setTimeout("$('table#module_options tfoot td span.price').css('background-color', '#FFFFFF')", 1000);
      
      // Obsługa checkboxa "PREZENT:"
      switch ($(this).val()) {
        case '1':
          $("input[name='sent_as_gift']").attr('disabled', 'disabled').css('opacity', 0.5);
          break;
          
        case '2':
          $("input[name='sent_as_gift']").removeAttr('disabled').css('opacity', 1);
          break;
          
        case '3':
          $("input[name='sent_as_gift']").attr('disabled', 'disabled').css('opacity', 0.5);
          break;
          
        case 'null':
          $("input[name='sent_as_gift']").attr('disabled', 'disabled').css('opacity', 0.5);
          break;
      }
    });
    
    
    
    $('a#module_submit').click(function() {
      
      var OK = true;
      $('select.module_select').each(function() {
        if ($(this).val() == 'null') {
          OK = false;
        }
      });
      
      if (OK) {
        $('form#module_confirm_form').submit();
      }
      else {
        window.alert('Wybierz wszystkie wymagane opcje wysyłki!');
      }
      
    });
    
    
    
    $("form#module_orderdata_form div.client_type").eq(ClientType).css('display', 'block');
    $("form#module_orderdata_form input[name='client_type']").eq(ClientType).attr('checked', 'checked');
    $("form#module_orderdata_form input[name='client_type']").change(function() {
      $("form#module_orderdata_form div.client_type").css('display', 'none');
      ClientType = $(this).val();
      $("form#module_orderdata_form div.client_type").eq($(this).val()).css('display', 'block');
    });
    
    
    
    $("form#module_orderdata_form div.row_footer a").click(function() {
      
      var OK = true;
      
      for (i in DATA_FIELD[ClientType]) {
        if ($("form#module_orderdata_form input[name='"+DATA_FIELD[ClientType][i]+"']").val() == '') {
          OK = false;
        }
      }
      
      if (OK) {
        $("form#module_orderdata_form").submit();
      }
      else {
        window.alert('Nie wypałniono wszystkich wymaganych pól!');
      }
      
    });
    
    // Początkowy stan checkboxa na Disabled!
    $("input[name='sent_as_gift']").attr('disabled', 'disabled').css('opacity', 0.5);
    
  });
