
  /**
  * Links one set of data dropdowns with
  * another.
  */
  var linkDateDropdowns = function(sDayDD, sMonthDD, sYearDD, tDayDD, tMonthDD, tYearDD) {

    var that = this;

    // Attach event handlers
    $([sDayDD, sMonthDD, sYearDD]).each(function() {
      this.change(handleSourceChanged);
    });

    $([tDayDD, tMonthDD, tYearDD]).each(function() {
      this.change(handleTargetChanged);
    });

    this.settings = {
      advanceDays: 7
    }

    this.getSourceDate = function() {
      return getDateParts(sDayDD, sMonthDD, sYearDD);
    }

    this.getTargetDate = function() {
      return getDateParts(tDayDD, tMonthDD, tYearDD);
    }

    this.selectTargetDate = function(d, m, y) {
      select(d, m, y, tDayDD, tMonthDD, tYearDD);
    }

    this.selectSourceDate = function() {
      select(d, m, y, sDayDD, sMonthDD, sYearDD);
    }

    function select(d, m, y, dayDD, monthDD, yearDD) {

      $(dayDD[0].options).each(function() {
        if(this.value == d) {
          this.selected = 'selected';
          return;
        }
      });

      $(monthDD[0].options).each(function() {
        if(this.value == m) {
          this.selected = 'selected';
          return;
        }
      });

      $(yearDD[0].options).each(function() {
        if(this.value == y) {
          this.selected = 'selected';
          return;
        }
      });
    }

    function getDateParts(dayDD, monthDD, yearDD) {
      return  {
        date: parseInt(dayDD.val(), 10),
        month: parseInt(monthDD.val(), 10),
        year: parseInt(yearDD.val(), 10),
      }
    }

    function handleSourceChanged() {

      var source = that.getSourceDate();
      var target = that.getTargetDate();

      // Calculate new target date
      var dateObj = new Date(source.year, source.month-1, source.date+that.settings.advanceDays);
      y = dateObj.getFullYear();
      m = dateObj.getMonth()+1;
      d = dateObj.getDate();

      selectTargetDate(d, m, y);
    }

    function handleTargetChanged() {
    }
  }

  /**
  * Populates the dropdowns
  */
  var dateDropdowns = function(dayDD, monthDD, yearDD) {

    var shortMonths = ['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'];
    var longMonths  = ['Januar', 'Februar', 'Marz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'];

    var y = null;
    var m = null;
    var d = null;

    this.settings = {
      pastYears: 5,
      futureYears: 10,
      toDateAdvanceDays: 0
    }

    this.create = function() {

      // Get current date
      var dateObj = new Date();
      y = dateObj.getFullYear();
      m = dateObj.getMonth();
      d = dateObj.getDate();

      if(this.settings.toDateAdvanceDays > 0) {
        dateObj = new Date(y, m, d+this.settings.toDateAdvanceDays);
        y = dateObj.getFullYear();
        m = dateObj.getMonth();
        d = dateObj.getDate();
      }

      // Create years array
      var years = new Array();
      for(var i = y-this.settings.pastYears, k = y+this.settings.futureYears; i < k; ++i) {
        years[years.length] = i;
      }

      // Populate month and year dropdowns
      populateDropdown(monthDD, shortMonths, true, m);
      populateDropdown(yearDD, years, false, y);
      populateDropdown(dayDD, new Array(31), false, d);

      this.bind();
    }

    this.bind = function() {
      $(dayDD).bind('change', handleDayChange);
      $(monthDD).bind('change', handleMonthChange);
      $(yearDD).bind('change', handleMonthChange);
    }

    function handleDayChange() {
      d = $(dayDD).val();
    }

    function handleMonthChange() {
      var daysInMonth = 32 - new Date(yearDD.value, monthDD.value, 32).getDate();
      populateDropdown(dayDD, new Array(daysInMonth), false, d);
    }

    function populateDropdown(dropdown, items, indexValue, select) {
      dropdown.options.length = 0;

      for(var i = 0, k = items.length; i < k; i++) {
        var val = (items[i] == undefined? i+1 : items[i]);
        var opt = document.createElement('OPTION');
        opt.value = (indexValue? i : val);
        opt.innerHTML = val;
        if(select == opt.value) {
          opt.selected = true;
        }
        dropdown.appendChild(opt);
      }
    }

    /*
    dd/mm/yyyy
    or
    dd-mm-yyyy

    @public
    */
    this.setDate = function(date) {
      var re = /(\d?\d)[-/](\d?\d)[-/](\d\d\d\d)/;
      var match = null;
      if(match = date.match(re)) {
        dayDD.value   = match[1];
        monthDD.value = match[2];
        yearDD.value  = match[3];
      }
    }
  }
