(function($){

   $.fn.linkedSelect = function(url,destination,params) {

      var params = $.extend({

         firstOption : 'Please Select',
         loadingText : 'Loading...',
		 defaultValue: '',
		 forValue 	 : '',
		 trigger	 : 'no'
      },params);

      var $dest = $(destination);

      return this.each(function(){

         $(this).bind('change', function() {

            var $$ = $(this);

            $dest.attr('disabled','false')
                 .append('<option value="' + params.loadingText + '">' + params.loadingText + '</option>')
                 .ajaxStart(function(){

                    $$.show();

            });

            $.getJSON(url,{str: $$.val() }, function(j){
               if (j.length > 0) {
                  var options = '<option value="">' +params.firstOption+ '</option>';
                  for (var i = 0; i < j.length; i++) {
                     options += '<option id = "' + j[i].optionDisplay.replace(" ", "-").replace(".", "") + '" value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
                  }
               }
//			   alert (params.defaultValue.replace(" ", "-"));
			   if ($$.val() == params.forValue) {
	               $dest.removeAttr('disabled')
	                    .html(options)
   	                    .find("#" + params.defaultValue.replace(" ", "-").replace(".", ""))
	                    .attr('selected', 'selected');

//	    .find("option:contains(" + params.defaultValue + ")")						

						$dest.trigger('change');
						
			   } else {
	               $dest.removeAttr('disabled')
	                    .html(options)
	                    .find("option:first")
	                    .attr('selected', 'selected');
				}
				
				if (gFIRST_TIME == true && params.trigger == "true") {
					$("#go").trigger("click");
					gFIRST_TIME = false;
				}
            }); // end getJSON

         });  // end change

      }); // end return each

   };  // end function

})(jQuery);