// Ian Cozens - lynx@nakedhippy.co.uk
// Generates drop down lists that change to allow only valid dates as the year or month is changed

var today = new Date();                              // Fixes todays date as a variable
var days_of_month = new Array("31","28","31","30","31","30","31","31","30","31","30","31"); // Total number of days in each month
var month_names = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec");

function isLeapYear(year) {                          // Performs a check on a given year to see if its a leap year
	return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? true : false;
}

function check_days() {                              // Re-populate the DAYS select list when month is changed

  var selected_month = document.getElementById('month_sel').value;
  var selected_year = document.getElementById('year_sel').value;
  
  if(today.getYear() < 1900) {                       //Check the year... some browsers return a three digit equivalent
    this_year = (today.getYear() + 1900);
  }
  else {
    this_year = today.getYear();
  }
  if((isLeapYear(selected_year) == true) && (selected_month == 2)) {
    var max_days = 29;
  }
  else {
    var max_days = days_of_month[selected_month -1];
  }
  if(((selected_month - 1) == today.getMonth()) && (selected_year == this_year)) {     // If selected month is this month and selected year is this year...
    start_day = today.getDate();                     // ... days start from today ...
  }
  else {
      start_day = 1;                                   // ... otherwise start from 1.
  }
  var days = '<SELECT ID="day_sel" NAME="day" STYLE=\"font-size: 9;\">';

  for(i=start_day ; i<=max_days ; i++) {
    days += '<OPTION VALUE="' + i +'">' + i + '</OPTION>';
  }
  days += '</SELECT>'
  document.getElementById("day").innerHTML = days;

}

function check_months() {                            // Re-populate the MONTHS select list when year is changed

  if(today.getYear() < 1900) {                       //Check the year... some browsers return a three digit equivalent
    this_year = (today.getYear() + 1900);
  }
  else {
    this_year = today.getYear();
  }
  if(document.getElementById('year_sel').value == this_year) { // If the new value matches current year
    start_month = today.getMonth() ;                  // ... start the list using current month as the first choice ...
  }
  else {
    start_month = 0;                                 // ... Otherwise start the months from January
  }
  var mnth = '<SELECT ID="month_sel" NAME="month" onChange="check_days();" STYLE=\"font-size: 9;\">';

  for(x=start_month ; x<12 ; x++) {
    mnth += '<OPTION VALUE="' + (x + 1) +'">' + month_names[x] + '</OPTION>';
  }
  mnth += '</SELECT>'
  document.getElementById("month").innerHTML = mnth;

  check_days();

}

function valid_date_lists() {                        // Builds the select lists when the page is loaded

  // Deals with the YEAR select list
  start_year = today.getYear()
  if(start_year < 1900) { start_year = start_year + 1900; }
  var yrs = '<SELECT ID="year_sel" NAME="year" onChange="check_months();" STYLE=\"font-size: 9;\">';
  for(z=start_year ; z<=(start_year + 5) ; z++) {
    yrs += '<OPTION VALUE="' + z +'">' + z + '</OPTION>';
  }
  yrs += '</SELECT>'
  document.getElementById("year").innerHTML = yrs;
  
  // Deals with the MONTH select list
  start_month = today.getMonth();
  var mnth = '<SELECT ID="month_sel" NAME="month" onChange="check_days();" STYLE=\"font-size: 9;\">';
  for(x=(start_month) ; x<12 ; x++) {
    mnth += '<OPTION VALUE="' + x +'">' + month_names[x] + '</OPTION>';
  }
  mnth += '</SELECT>'
  document.getElementById("month").innerHTML = mnth;
  
  // Deals with the DAY select list
  var max_days = days_of_month[today.getMonth()];
  start_day = today.getDate();
  var days = '<SELECT ID="day_sel" NAME="day" STYLE=\"font-size: 9;\">';
  for(i=start_day ; i<=max_days ; i++) {
    days += '<OPTION VALUE="' + i +'">' + i + '</OPTION>';
  }
  days += '</SELECT>'
  document.getElementById("day").innerHTML = days;

}
