function date(name, year, month, day, minYear, maxYear, pastAllowed)
{
    // Properties
    this.name = name;
    this.day = day;
    this.month = month;
    this.year = year;
    this.pastAllowed = pastAllowed;

    if (minYear > maxYear) {
        maxYear = minYear;
    }
    if (this.year < minYear) { 
        this.year = minYear;
    }
    if (this.year > maxYear) { 
        this.year = maxYear;
    }

    this.minYear = minYear;
    this.maxYear = maxYear;

    // Methods
    this.yyyyMMdd = yyyyMMdd;
    this.writeToForm = writeToForm;
    this.dateChanged = dateChanged;
    this.getField = getField;
    this.getDate = getDate;
    this.setDate = setDate;
    this.getMonthNum = getMonthNum;
    this.setDayMonthChoices = setDayMonthChoices;
    this.handler = null;
    var now = new Date();
    this.today = new Date();
    setUTCDate(today, now.getFullYear(), now.getMonth() + 1, 
        now.getDate());
    this.months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
}


function yyyyMMdd()
{
    return this.year + "-" + zeropad(this.month) + "-" + zeropad(this.day);
}


function getDate()
{
    var d = new Date();
    setUTCDate(d, this.year, this.month, this.day);
    return d;
}


function setUTCDate(d, year, month, day)
{
    d.setUTCFullYear(year);
    d.setUTCDate(1);    // Make sure date is valid for the month we are about to set
    d.setUTCMonth(month - 1);
    d.setUTCDate(day);
    d.setUTCHours(0);
    d.setUTCMinutes(0);
    d.setUTCSeconds(0);
    d.setUTCMilliseconds(0);
}


function setDate(millisecs)
{
    var newDate = new Date(millisecs);
    this.day = newDate.getUTCDate();
    this.month = newDate.getUTCMonth() + 1;
    this.year = newDate.getUTCFullYear();
    this.setDayMonthChoices();
    changeIntChoice(this.getField("Year"),  this.minYear, this.maxYear,
        this.year);
}


function writeToForm(doc, fName, objName, handler)
{
    var action = objName + ".dateChanged();";

    numericChoice(doc, this.name + "Day", 1, 31, 1, -1, action, true);

    stringChoice(doc, this.name + "Month", this.months, "", -1, action);

    numericChoice(doc, this.name + "Year", this.minYear, this.maxYear,
        this.year, -1, action);

    doc.write('<input type="hidden" name=' + this.name + ' value="0">');

    this.handler = handler;
    this.fName = fName;

    this.setDayMonthChoices();
}


function setDayMonthChoices()
{
    var dispMonths = this.months;

    var startDay = 1;
    if (!this.pastAllowed) {
        if (this.month == this.today.getUTCMonth() + 1 &&
            this.year == this.today.getUTCFullYear()) {
            startDay = this.today.getUTCDate();
        }
         
        if (this.year == this.today.getUTCFullYear()) {
            dispMonths = new Array();
            var j = 0;
            for (i = this.today.getUTCMonth(); i < this.months.length; i++) {
                dispMonths[j] = this.months[i];
                j++;
            }
        }
    }

    changeIntChoice(this.getField("Day"), startDay, 
        monthLen(this.month, this.year), this.day, true);

    changeStringChoice(this.getField("Month"), dispMonths,
        this.months[this.month - 1]);

    this.day = getChoice(this.getField("Day"));
    this.getMonthNum();

    this.getField("").value = this.getDate().getTime();
}


function dateChanged()
{
// Update the date, and redisplay if updateForm is set to true
    // Made this a number to get rid of the leading 0 for 01 to 09.
    // Before this change dates 02 to 09 couldn't be selected!
    this.day = new Number(getChoice(this.getField("Day")));

    this.getMonthNum();

    this.year = getChoice(this.getField("Year"));

    if (!this.pastAllowed && this.getDate().getTime() < this.today.getTime()) {
        this.day = today.getUTCDate();
        this.month = today.getUTCMonth() + 1;
        this.year = today.getUTCFullYear();
    }

    this.setDayMonthChoices();

    if (this.handler != null) {
        eval(this.handler);
    }
}

function getMonthNum()
{
    var monthName = getChoice(this.getField("Month"));
    for (i = 0; i < this.months.length; i++) {
        if (monthName == this.months[i]) {
            this.month = i + 1;
            break;
        }
    }
}


function getField(extn)
{
    return eval(this.fName + "." + this.name + extn);
}


function monthLen(month, year)
{
    if (month == 2) {
        if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
            return 29;
        } else {
            return 28;
        }
    }

    if (month == 4 || month == 6 || month == 9 || month == 11) {
        return 30;
    }
    return 31;
}

