function TripleCalendar(xYear, xMonth, xDay, xHidden, maximum) {
  xYear.get(0).obj = this;
  xMonth.get(0).obj = this;
  xDay.get(0).obj = this;
  this.curDate = new Date();
  if (maximum) {
    this.maximum = maximum;
  }
  this.curDate.setFullYear(this.curDate.getFullYear() - 14);
  this.setToDate = false;
  try {
    var date = xHidden.val().split(".");
    if (date.length == 3) {
      if (date[0].indexOf('0') == 0) {
        date[0] = date[0].substr(1, 1);
      }
      if (date[1].indexOf('0') == 0) {
        date[1] = date[1].substr(1, 1);
      }
      this.curDate.setFullYear(parseInt(date[2]), parseInt(date[1]) - 1, parseInt(date[0]));
      this.setToDate = true;
    }
  } catch (e) {
    this.curDate = new Date();
  }

  this.year = xYear;
  this.month = xMonth;
  this.day = xDay;
  this.hidden = xHidden;

  this.year.change(this.leapYear);
  this.month.change(this.populateDays);
  this.day.change(this.setDate);

  this.populateYears();
  this.populateDays(false);

  this.month.val(this.curDate.getMonth() + 1);
  this.year.val(this.curDate.getFullYear());
  this.day.val(this.curDate.getDate());
  if (this.setToDate) this.setDate();

  if (!this.setToDate) {
    xYear.val(0);
    xMonth.val(0);
    xDay.val(0);
  }
}

TripleCalendar.prototype.populateDays = function(setDate) {
  //  if (this.year.val() == 0 || this.month.val() == 0 || this.day.val() == 0) return;
  if (setDate !== false) setDate = true;
  var obj = this;
  if (this.obj) {
    obj = this.obj;
  }
  var monthStr = obj.month.val();
  var selectedDay = obj.day.val();
  if (monthStr != "") {
    var theMonth = parseInt(monthStr) - 1;
    if (theMonth == -1) theMonth = 0;
    var options = obj.day.get(0).options;
    options.length = 1;

    //options[0] = new Option("Day");
    options[0].value = 0;
    for (var i = 1; i < monthDays[theMonth] + 1; i++) {
      options[i] = new Option(i);
      options[i].value = i;
    }
  }
  obj.day.val((selectedDay > options.length - 1) ? options.length - 1 : selectedDay);
  if (setDate) obj.setDate();
};
TripleCalendar.prototype.setDate = function() {
  var obj = this;
  if (this.obj) {
    obj = this.obj;
  }
  var day = obj.day.val();
  var month = obj.month.val();
  if (day == 0 || month == 0 || parseInt(obj.year.val()) == 0) return obj.hidden.val("");
  if (day.length == 1) {
    day = "0" + day;
  }
  if (month.length == 1) {
    month = "0" + month;
  }
  obj.hidden.val(day + "." + month + "." + obj.year.val());
};

TripleCalendar.prototype.leapYear = function() {
  // if (this.year.val() == 0 || this.month.val() == 0 || this.day.val() == 0) return;

  var obj = this;
  if (this.obj) {
    obj = this.obj;
  }
  var y = obj.year.val();
  if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
    monthDays[1] = 29;
  } else {
    monthDays[1] = 28;
  }
  obj.populateDays();
};

TripleCalendar.prototype.populateYears = function() {
  var obj = this;
  if (this.obj) {
    obj = this.obj;
  }

  var options = obj.year.get(0).options;
  var curYear = new Date().getFullYear();

  options.length = 1;
  var k = 0;
  //options[k] = new Option("Year");
  options[k++].value = 0;
  for (var i = curYear - 14; i >= curYear - 100; i--) {
    options[k] = new Option(i);
    options[k].value = i;
    k++;
  }
};
