



(function($){



   $.fn.linkedSelect = function(url,destination,params,val_callback) {



      var params = $.extend({



         firstOption : 'Selecione...',



         loadingText : 'Carregando...'



      },params);



      var $dest = $(destination);



      return this.each(function(){



         $(this).bind('change', function() {



            var $$ = $(this);



            $dest.attr('disabled','false')

                 .append('<option value="">' +params.loadingText+ '</option>')

                 .ajaxStart(function(){



                    $$.show();



            });

						var user_params = {id: $$.attr('id'), val: $$.val() };

						if (typeof(val_callback)=='function') {

							$.merge(user_params, val_callback());

						}

						

            $.getJSON(url,user_params, function(j){



							if (j.exc) {

								alert(j.exc);

								return;

							}

               

							 if (j.length > 0) {



                  var options = '<option value="">' +params.firstOption+ '</option>';



                  for (var i = 0; i < j.length; i++) {



                     options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';



                  }



               }



               $dest.removeAttr('disabled')

                    .html(options)

                    .find('option:first')

                    .attr('selected', 'selected');



            }); // end getJSON



         });  // end change



      }); // end return each



   };  // end function



})(jQuery);





(function($){



   $.fn.linkedSelect2 = function(url,destination,params,val_callback) {



      var params = $.extend({



         firstOption : 'Selecione...',



         loadingText : 'Carregando...'



      },params);



      var $dest = $(destination);



      return this.each(function(){



         $(this).bind('ready', function() {



            var $$ = $(this);



            $dest.attr('disabled','false')

                 .append('<option value="">' +params.loadingText+ '</option>')

                 .ajaxStart(function(){



                    $$.show();



            });

						var user_params = {id: $$.attr('id'), val: $$.val() };

						if (typeof(val_callback)=='function') {

							$.merge(user_params, val_callback());

						}

						

            $.getJSON(url,user_params, function(j){



							if (j.exc) {

								alert(j.exc);

								return;

							}

               

							 if (j.length > 0) {



                  var options = '<option value="">' +params.firstOption+ '</option>';



                  for (var i = 0; i < j.length; i++) {



                     options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';



                  }



               }



               $dest.removeAttr('disabled')

                    .html(options)

                    .find('option:first')

                    .attr('selected', 'selected');



            }); // end getJSON



         });  // end change



      }); // end return each



   };  // end function



})(jQuery);