Chaining datepickers

Chaining datepickers

I've got a number of datepickers (dynamically added by php), with ids such as from_1, to_1, from_2, to_2, from_3, to_3, etc.
I've managed to use the event search example to work with 2. As per ui example:

  1.                var dates = $('#form_1,#to_1').datepicker({
  2.                         (....other options...)
  3. onSelect: function( selectedDate ) {
  4. var option = this.id == "from_1" ? "minDate" : "maxDate",
  5. instance = $( this ).data( "datepicker" );
  6. date = $.datepicker.parseDate(
  7. instance.settings.dateFormat ||
  8. $.datepicker._defaults.dateFormat,
  9. selectedDate, instance.settings );
  10. dates.not( this ).datepicker( "option", option, date );
  11. }
  12. });
I'm looking to chain #from_2 to restrict dates prior to #to_1, #to_2 to restrict dates prior to #from_2, etc. I've been struggling with this for a few days. I can create sets of similar scripts, but that seems v. wasteful. php will allow their creation via a loop, but as I say, this seems wasteful. Anybody got a clue?