


function date(name, year, month, day, hour, minYear, maxYear, pastAllowed)
{
    // Properties
    this.name = name;
    this.hour = hour;
    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.getHourNum = getHourNum;
    this.setDayMonthChoices = setDayMonthChoices;
    this.handler = null;
    var now = new Date();
    this.today = new Date();
    setUTCDate(today, now.getFullYear(), now.getMonth() + 1, now.getDate(), 0);
    this.months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
                   
    this.hours = new Array( "1 a.m.", "2 a.m.", "3 a.m.", "4 a.m.", "5 a.m.", "6 a.m.", "7 a.m.", "8 a.m.", "9 a.m.","10 a.m.","11 a.m.", "12 a.m.", "1 p.m.","2 p.m.","3 p.m.","4 p.m.","5 p.m.","6 p.m.","7 p.m.","8 p.m.","9 p.m.","10 p.m.","11 p.m.");
}


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, this.hour);
    return d;
}


function setUTCDate(d, year, month, day, hour)
{
    d.setFullYear(year);
    d.setMonth(month - 1);
    d.setDate(day);
    d.setHours(hour);
    d.setMinutes(0);
    d.setSeconds(0);
    d.setMilliseconds(0);
}


function setDate(millisecs)
{
    var newDate = new Date(millisecs);
    this.hour = newDate.getHours();
    this.day = newDate.getDate();
    this.month = newDate.getMonth() + 1;
    this.year = newDate.getFullYear();
    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);

    stringChoice(doc, this.name + "Hour", this.hours, "", -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.getMonth() + 1 &&
            this.year == this.today.getFullYear()) {
            startDay = this.today.getDate();
        }
         
        if (this.year == this.today.getFullYear()) {
            dispMonths = new Array();
            var j = 0;
            for (i = this.today.getMonth(); 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"));

    changeStringChoice(this.getField("Hour"), this.hours,  this.hours[this.hour-1]);


    this.getMonthNum();
    //this.getHourNum();

    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.getHourNum();

    this.year = getChoice(this.getField("Year"));

    if (!this.pastAllowed && this.getDate().getTime() < this.today.getTime()) {
        this.day = today.getDate();
        this.month = today.getMonth() + 1;
        this.year = today.getFullYear();
    }

    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 getHourNum()
{
    var hourName = getChoice(this.getField("Hour"));
    for (i = 0; i < this.hours.length; i++) {
        if (hourName == this.hours[i]) {
            this.hour = 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;
}

function stringChoice(doc, name, arr, def, size, onchange)
{
    var i;

    doc.write('<select name ="', name, '" id="', name, '"' );
    if (size != -1) {
        doc.write(' size="', size, '"');
    }
    if (onchange != "") {
        doc.write(" onchange='", onchange, "'");
    }
    doc.writeln('>');
    for (i = 0; i < arr.length; i++) {
        doc.write('<option ');
        if (arr[i] == def) {
            doc.write('selected ');
        }
        doc.writeln('value="', toHTML(arr[i]), '">', arr[i]);
    }
    doc.writeln('</select>');
}

function changeStringChoice(c, arr, def)
{

    var found = false;
    c.length = 0;
    c.selectedIndex = 0;
    for (i = 0; i < arr.length; i++) {
        var no = new Option();
        no.value = arr[i];
        no.text = no.value;
        c.options[c.options.length] = no;
        if (arr[i] == def) {
            found = true;
            c.selectedIndex = i;
        }
    }
    if (!found) {
        // Default not in list---select first element
        c.selectedIndex = 0;
    }
}


function getChoice(c)
{
    if ( c == null || c.options == null ) return 0;
    return c.options[c.selectedIndex].value
}


function getDefChoice(frm, field, def)
{
    var elem = findField(frm, field);
    if (elem == 0) {
        return def;
    }
    return elem.options[elem.selectedIndex].value
}


function numericChoice(doc, name, min, max, def, size, onchange)
{
    numericChoice(doc, name, min, max, def, size, onchange, false)
}

function numericChoice(doc, name, min, max, def, size, onchange, twoDigitMin)
{
    var arr = new Array(max - min + 1);
    var i;
    var val;

    for (i = 0; i < arr.length; i++) {
        val = min + i;   
        if (twoDigitMin && val >= 0 && val <= 9) {
            arr[i] = "0" + val;
        } else {
            arr[i] = val;
        }
    }
    if (twoDigitMin && def >= 0 && def <= 9) {
        def = "0" + def;
    }
    stringChoice(doc, name, arr, def, size, onchange);
}

function changeIntChoice(c, min, max, def)
{
    changeIntChoice(c, min, max, def, false)
}

function changeIntChoice(c, min, max, def, twoDigitMin)
{
    var arr = new Array(max - min + 1);
    var i;

    if (def < min) {
        def = min;
    }
    if (def > max) {
        def = max;
    }
    for (i = 0; i < arr.length; i++) {
        val = min + i;   
        if (twoDigitMin && val >= 0 && val <= 9) {
            arr[i] = "0" + val;
        } else {
            arr[i] = val;
        }
    }
    if (twoDigitMin && def >= 0 && def <= 9) {
        def = "0" + def;
    }
    changeStringChoice(c, arr, def);
}


function getIntChoice(c)
{
    var num = new Number(c.options[c.selectedIndex].value);
    return num.valueOf();
}


function getDefIntChoice(frm, field, def)
{
    var elem = findField(frm, field);
    if (elem == 0) {
        return def;
    }
    return getIntChoice(elem);
}


function getMaxIntChoice(c)
{
    var num = new Number(c.options[c.length - 1].value);
    return num.valueOf();
}


// Converts a string so that it can be included in HTML.
// This amounts to replacing the four characters with significance
// to HTML with their special symbols.
function toHTML(str)
{
    s = new String(str);
    s = s.replace(/&/g, '&amp;');   // Must be first!
    s = s.replace(/</g, '&lt;');
    s = s.replace(/>/g, '&gt;');
    s = s.replace(/"/g, '&quot;');
    return s;
}


