// Dynamic Date Chooser
// Version 0, April 2000
//
// Copyright (C) 2000,
// handerso@inficad.com
//
// This program is free software; you can redistribute it and/or
// modify it as necessary to meet your needs.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// Written: Harry K Anderson, III
// Date:    April 12, 2000
//

var min_year = 2000; // defines lowest year in year menu
var max_year = 2020; // defines highest year in the year menu

// make this false to prevent the weekday element from being displayed
var weekday_showing = false;

// make this true to make dayofweek return a number (0-6)
var dayofweek_returned_as_number = false;

// make this true to make month return a number (0-11)
var month_returned_as_number = true;

if (min_year <= 400)
 alert("Minimum year must be higher than 400 for this algorithm to work.");

// The following code adds three methods to the built-in Date object
function _strMonth() {
 var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
 return months[this.getMonth()];
}

function _strDay() {
 var days= new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
 return days[this.getDay()];
}

function _weekOf() {
 sunday = ((arguments[0]==null) || (!arguments[0])); // Check for optional argument
 return (new Date(this - ((this.getDay() - ((sunday) ? 0 : 1))*24*60*60*1000)));
}

// Add these methods to all dates
Date.prototype.weekOf = _weekOf;
Date.prototype.strMonth = _strMonth;
Date.prototype.strDay = _strDay;

// This function is called through the onChange event on your web page.
// It will determine the appropriate number of days in the Month and
// year combination, and repopulate the days drop down list on your
// page.
function changeDays(numb,date_form,aname) {
 MonthCtrl = eval("date_form." + aname +"month");
 DayCtrl = eval("date_form." + aname +"day");
 YearCtrl = eval("date_form." + aname +"year");

 mth = eval(MonthCtrl.selectedIndex);
 sel = eval(YearCtrl.selectedIndex);
 yr = eval(YearCtrl.options[sel].text);

 if (numb != 1) {
  numDays = numDaysIn(mth,yr);
  eval("date_form." + aname +"day.options.length="+numDays) ;
  for (i=27;i<numDays;i++) {
   j=i+1;
   eval("date_form." + aname +"day.options["+i+"].text = "+j);
  }
 }
}

// Calculates the number of days for a month and year combination.
function numDaysIn(mth,yr) {
 if (mth==3 || mth==5 || mth==8 || mth==10) return 30;
 else if ((mth==1) && leapYear(yr)) return 29;
 else if (mth==1) return 28;
 else return 31;
}

// Determines Leap year
function leapYear(yr) {
 if (((yr % 4 == 0) && yr % 100 != 0) || yr % 400 == 0)
  return true;
 else
  return false;
}

function arr() {
 this.length=arr.arguments.length;
 for (n=0;n<arr.arguments.length;n++) {
  this[n] = arr.arguments[n];
 }
}

weekdays = new arr("Sun.","Mon.","Tues.","Wed.", "Thurs.","Fri.","Sat.");

months = new arr("Jan.","Feb.","Mar.","Apr.","May","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec.");

var cur = new Date();

function getWeekDay(mth,day,yr) {
 first_day = firstDayOfYear(yr);
 for (num=0;num<mth;num++) {
  first_day += numDaysIn(num,yr);
 }
 first_day += day-1;
return first_day%7;
}

function firstDayOfYear(yr) {
 diff = yr - 401;
 return parseInt((1 + diff + (diff / 4) - (diff / 100) + (diff / 400)) % 7);
}

// fixes a Netscape 2 and 3 bug
function getFullYear(d) { // d is a date object
 yr = d.getYear();
 if (yr < 1000)
  yr+=1900;
 return yr;
}

function datedrop(myname,feeddate){
 if(!feeddate)
 {
  d=new Date();
  edmonth=d.getMonth();
  edday=d.getDate();
  edyear=d.getFullYear();
 }
 else
 {
  d=new Date(feeddate)
  edmonth=d.getMonth();
  edday=d.getDate();
  edyear=d.getFullYear();
 }

 // write month element

 document.write("<select name="+myname+"month size=1 onChange='changeDays(0,this.form,"+'"'+myname+'"'+")' class=tiny>");
 for (i=1;i<13;i++)
  document.write("<option"+(month_returned_as_number?" value="+i:"")+(edmonth==i-1?" selected":"")+">"+months[i-1]+"\n");

 // write day element
 document.write("</select><select name="+myname+"day size=1 onChange='changeDays(1,this.form,"+'"'+myname+'"'+")' class=tiny>\n");
 for (i=1;i<=numDaysIn(d.getMonth(),getFullYear(d));i++)
  document.write("<option"+(edday==i?" selected":"")+">"+i+"\n");

 // write year element
 document.write("</select><select name="+myname+"year size=1 onChange='changeDays(0,this.form,"+'"'+myname+'"'+")' class=tiny>\n");
 for (i=min_year;i<max_year;i++)
  document.write("<option"+(edyear==i?" selected":"")+">"+i+"\n");
 document.write("</select>");
}
