//
// support for pop-up calendar window
//
// 1998-11-23   Brian Ellis, FastLane Communications, Inc.
//
// $Id: calendar.js,v 1.1.1.1 2001/09/09 20:44:41 bill Exp $
//

//
// calendar() - called when user presses the calendar icon next to a date
//
function calendar(which) {
        cal = open("", "calendar", "height=220,width=280,scrollbars=yes,resizable=yes");
        objname = which;

        // get current values
        eval("curday = document.forms[0]." + objname + "_day.value");
        eval("curmonth = document.forms[0]." + objname + "_month.value");
        eval("curyear = document.forms[0]." + objname + "_year.value");

        // trim leading zeros so the numbers aren't seen as octal
        while (curday.charAt(0) == "0") curday = curday.substr(1);
        while (curmonth.charAt(0) == "0") curmonth = curmonth.substr(1);
        while (curyear.charAt(0) == "0") curyear = curyear.substr(1);

        // turn strings into ints
        curday = parseInt(curday);
        curmonth = parseInt(curmonth);
        curyear = parseInt(curyear);

        // handle blank/non-integer inputs
        if (isNaN(curday) || isNaN(curmonth) || isNaN(curyear)) {
                curday = 0;
                curmonth = 0;
                curyear = 0;
        }

        // if any part is zero, use today
        if ((curday == 0) || (curmonth == 0) || (curyear == 0)) {
                var today = new Date();
                curday = today.getDate();
                curmonth = today.getMonth() + 1;
                curyear = today.getYear();
                if (curyear < 1900)
                        curyear += 1900;
        }
        initialday = curday;
        initialmonth = curmonth;
        initialyear = curyear;
        cal_make();
}

//
// cal_back() - called when user goes back in time
//
function cal_back(months) {
        curmonth -= months;
        while (curmonth < 1) {
                --curyear;
                curmonth += 12;
        }
        cal_make();
}

//
// call_forw() - called when user goes forward in time
//
function cal_forw(months) {
        curmonth -= -months;    // not "+= months" because of int/string bs.
        while (curmonth > 12) {
                ++curyear;
                curmonth -= 12;
        }
        cal_make();
}

//
// cal_choose() - called when user presses on a date in the calendar
//
function cal_choose(day) {
        var daystr, monthstr;

        if (day < 10) {
                daystr = "0" + day;
        } else {
                daystr = "" + day;
        }
        if (curmonth < 10) { 
                monthstr = "0" + curmonth;
        } else {
                monthstr = "" + curmonth;
        }
        eval("document.forms[0]." + objname + "_day.value=\"" + daystr + "\"");
        eval("document.forms[0]." + objname + "_month.value=\"" + monthstr + "\"");
        eval("document.forms[0]." + objname + "_year.value=\"" + curyear + "\"");
        cal.close();
}

//
// mkcal() - fill in the calendar window
//
function cal_make() {
        var target, startdow, dow, d;

        //
        // what day of the week (dow) does this month start on?
        // Sunday = 1, Saturday = 7
        //
        target = new Date(curyear, curmonth-1, 1);
        startdow = target.getDay() + 1;

        //
        // header
        //
        cal.document.writeln("<html><head><title>Calendar</title></head><body>");
        cal.document.writeln("<center><h3>");
        cal.document.writeln("<a href='javascript:window.opener.cal_back(6)'>&lt;&lt;</a>&nbsp;&nbsp;");
        cal.document.writeln("<a href='javascript:window.opener.cal_back(1)'>&lt;</a>");
        cal.document.writeln(monthnames[curmonth-1] + " - " + curyear);
        cal.document.writeln("<a href='javascript:window.opener.cal_forw(1)'>&gt;</a>");
        cal.document.writeln("&nbsp;&nbsp;<a href='javascript:window.opener.cal_forw(6)'>&gt;&gt;</a>");
        cal.document.writeln("</h3>");
        cal.document.writeln("<table border=1>");
        cal.document.writeln("<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>");

        //
        // space over to the correct column for day 1 in this month
        //
        dow = 1;
        cal.document.writeln("<tr align=right>");
        while (startdow > dow) {
                cal.document.writeln("<td></td>");
                ++dow;
        }

        //
        // adjust daysinmonths[] for leap year
        //
        if (((curyear % 4) == 0) && (((curyear % 100) != 0) || ((curyear % 400) == 0))) {
                daysinmonths[1] = 29;
        } else {
                daysinmonths[1] = 28;
        }

        //
        // output all of the days in the month
        //
        for (d=1; d<=daysinmonths[curmonth-1]; ++d) {
                if ((d == initialday) && (curmonth == initialmonth) && (curyear == initialyear)) {
                        cal.document.writeln("<td><a href='javascript:window.opener.cal_choose(" + d + ")'><font color=#ff0000><b>" + d + "</b></font></a></td>");
                } else {
                        cal.document.writeln("<td><a href='javascript:window.opener.cal_choose(" + d + ")'>" + d + "</a></td>");
                }
                ++dow;
                if (dow > 7) {
                        cal.document.writeln("</tr><tr align=right>");
                        dow = 1;
                }
        }

        //
        // consume the remainder of the last row
        //
        while (dow <= 7) {
                cal.document.writeln("<td></td>");
                ++dow;
        }
        cal.document.writeln("</tr>");

        //
        // footer
        //
        cal.document.writeln("</table></center></body></html>");
        cal.document.close();
}

//
// perform one-time initialization now
//
daysinmonths = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
monthnames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
