$().ready(function() {
   function noMatchFunction() {
      $('#id-result').html( (document.getElementById('id-part-name').value != "") ? "Запчасти не найдено." : "");
      $('#id-part-id').val("0");
   }

   function formatResult(row) {
      return row[0].replace(/(<.+?>)/gi, '');
   }

   $("#id-part-name").autocomplete("_search.php", {
      matchContains: true,
      delay: 400,
      minChars: 2,
      matchSubset: true,
      cacheLength: 20,
      max: 15,
      autoFill: false,
      mustMatch: true,
      funcNoMatch: noMatchFunction,
      selectFirst: true,
      formatResult: formatResult
   });
   /*
   $("#id-part-name").change(function() {
      $(this).search();
   });
   */
   $("#id_search_form").submit(function() {
      //$("#id-search-part-name").blur();
      return SearchParamsOk();
   });

   $('#id-part-name').result(function(event, data, formatted) {
      $('#id-result').html( !data ? ( (document.getElementById('id-part-name').value != "") ? "Запчасти не найдено." : "") : "");
      $('#id-part-id').val( !data ? "0" : data[1].replace(/(<.+?>)/gi, ''));
      $('#id-parttype-id').val( !data ? "0" : data[2].replace(/(<.+?>)/gi, ''));
   });


   $("#id-load-parts-list-link").click(function(){
      if ($("#id-all-parts-list").css("display") == "none") {
        if ($("#id-all-parts-list").html() == "") {
          $("#id-all-parts-list").load("_parts_list.php", function (){
            $('#id-all-parts-list').slideToggle("slow");
          });
        }
        else {
          $('#id-all-parts-list').slideToggle("slow");
        }
      }
      else {
        $('#id-all-parts-list').slideToggle("slow");
      }
      return false;
   });
});

