(function($){
  // очищаем select
  $.fn.clearSelect = function() {
	  return this.each(function(){
		  if(this.tagName=='SELECT') {
		      this.options.length = 0;
		      $(this).attr('disabled','disabled');
		  }
	  });
  }
  // заполняем select
  $.fn.fillSelect = function(dataArray) {
 	  return this.clearSelect().each(function(){
		  if(this.tagName=='SELECT') {
			  var currentSelect = this;
			  $.each(dataArray,function(index,data){
				  var option = new Option(data.text,data.value);
				  if($.support.cssFloat) {
					  currentSelect.add(option,null);
				  } else {
					  currentSelect.add(option);
				  }
			  });
		  }
	  });
  }
})(jQuery);
 
$(document).ready(function(){
 
  // выбор метро
  function adjustMetro(){
  	var townValue = $('#id_town').val();
  	var tmpSelect = $('#id_metro');
  	if(townValue.length == 0) 
	{
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  	} 
	else 
	{
  		$.getJSON(
				  '/includes/cascadeselectmetro.php',
				  {id_town:townValue},
				  function(data) 
				  { 
				  	tmpSelect.fillSelect(data).attr('disabled',''); 
				  }
		);
  		
  	}
  };   
  
   $('#id_town').change(function(){
  	adjustMetro();
  });
 
});

