
(function($){

   $.fn.linkedSelect = function(url,destination,params) {

      var params = $.extend({
         firstOption : 'Please Select',
         loadingText : 'Loading...'
      },params);

      return this.each(function(){

//         $(this).change(function(){
         $(this).bind('change', function() {

            var $$ = $(this);

            $(destination).attr('disabled','false');

            $(destination).append('<option value="">' +params.loadingText+ '</option>').ajaxStart(function(){
               $$.show();
            });

//            $.getJSON(url,{ search: this.id, str: $$.val() }, function(j){
            $.getJSON(url,{str: $$.val() }, function(j){
               var options = '';
               if (j.length > 0) {
                  options = '<option value="">' +params.firstOption+ '</option>';
                  for (var i = 0; i < j.length; i++) {
                     options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
                  }
               }
               $(destination).removeAttr('disabled');
               $(destination).html(options);
               $(destination + ' option:first').attr('selected', 'selected');
            }); // end getJSON
         });  // end change
      }); // end return each
   };  // end function

})(jQuery);