// CONSTRUCTOR for the CalendarPopup Object
function CalendarPopup() {
        var c;
        if (arguments.length>0) {
                c = new PopupWindow(arguments[0]);
                }
        else {
                c = new PopupWindow();
                c.setSize(150,175);
                }
        c.offsetX = -152;
        c.offsetY = 25;
        c.autoHide();
        // Calendar-specific properties
        c.monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
        c.monthAbbreviations = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
        c.dayHeaders = new Array("S","M","T","W","T","F","S");
        c.returnFunction = "CP_tmpReturnFunction";
        c.returnMonthFunction = "CP_tmpReturnMonthFunction";
        c.returnQuarterFunction = "CP_tmpReturnQuarterFunction";
        c.returnYearFunction = "CP_tmpReturnYearFunction";
        c.weekStartDay = 0;
        c.isShowYearNavigation = false;
        c.displayType = "date";
        c.disabledWeekDays = new Object();
        c.disabledDatesExpression = "";
        c.yearSelectStartOffset = 2;
        c.currentDate = null;
        c.todayText="Today";
        c.cssPrefix="";
        c.isShowNavigationDropdowns=false;
        c.isShowYearNavigationInput=false;
        window.CP_targetInput = null;
        window.CP_dateFormat = "MM/dd/yyyy";
        // Method mappings
        c.setReturnFunction = CP_setReturnFunction;
        c.setReturnMonthFunction = CP_setReturnMonthFunction;
        c.setReturnQuarterFunction = CP_setReturnQuarterFunction;
        c.setReturnYearFunction = CP_setReturnYearFunction;
        c.setMonthNames = CP_setMonthNames;
        c.setMonthAbbreviations = CP_setMonthAbbreviations;
        c.setDayHeaders = CP_setDayHeaders;
        c.setWeekStartDay = CP_setWeekStartDay;
        c.setDisplayType = CP_setDisplayType;
        c.setDisabledWeekDays = CP_setDisabledWeekDays;
        c.addDisabledDates = CP_addDisabledDates;
        c.setYearSelectStartOffset = CP_setYearSelectStartOffset;
        c.setTodayText = CP_setTodayText;
        c.showYearNavigation = CP_showYearNavigation;
        c.showCalendar = CP_showCalendar;
        c.hideCalendar = CP_hideCalendar;
        c.getStyles = getCalendarStyles;
        c.refreshCalendar = CP_refreshCalendar;
        c.getCalendar = CP_getCalendar;
        c.select = CP_select;
        c.setCssPrefix = CP_setCssPrefix;
        c.showNavigationDropdowns = CP_showNavigationDropdowns;
        c.showYearNavigationInput = CP_showYearNavigationInput;
        // Return the object
        return c;
        }

// Temporary default functions to be called when items clicked, so no error is thrown
function CP_tmpReturnFunction(y,m,d) {
        if (window.CP_targetInput!=null) {
                var dt = new Date(y,m-1,d,0,0,0);
                window.CP_targetInput.value = formatDate(dt,window.CP_dateFormat);
                }
        else {
                alert('Use setReturnFunction() to define which function will get the clicked results!');
                }
        }
function CP_tmpReturnMonthFunction(y,m) {
        alert('Use setReturnMonthFunction() to define which function will get the clicked results!\nYou clicked: year='+y+' , month='+m);
        }
function CP_tmpReturnQuarterFunction(y,q) {
        alert('Use setReturnQuarterFunction() to define which function will get the clicked results!\nYou clicked: year='+y+' , quarter='+q);
        }
function CP_tmpReturnYearFunction(y) {
        alert('Use setReturnYearFunction() to define which function will get the clicked results!\nYou clicked: year='+y);
        }

// Set the name of the functions to call to get the clicked item
function CP_setReturnFunction(name) { this.returnFunction = name; }
function CP_setReturnMonthFunction(name) { this.returnMonthFunction = name; }
function CP_setReturnQuarterFunction(name) { this.returnQuarterFunction = name; }
function CP_setReturnYearFunction(name) { this.returnYearFunction = name; }

// Over-ride the built-in month names
function CP_setMonthNames() {
        for (var i=0; i<arguments.length; i++) { this.monthNames[i] = arguments[i]; }
        }

// Over-ride the built-in month abbreviations
function CP_setMonthAbbreviations() {
        for (var i=0; i<arguments.length; i++) { this.monthAbbreviations[i] = arguments[i]; }
        }

// Over-ride the built-in column headers for each day
function CP_setDayHeaders() {
        for (var i=0; i<arguments.length; i++) { this.dayHeaders[i] = arguments[i]; }
        }

// Set the day of the week (0-7) that the calendar display starts on
// This is for countries other than the US whose calendar displays start on Monday(1), for example
function CP_setWeekStartDay(day) { this.weekStartDay = day; }

// Show next/last year navigation links
function CP_showYearNavigation() { this.isShowYearNavigation = (arguments.length>0)?arguments[0]:true; }

// Which type of calendar to display
function CP_setDisplayType(type) {
        if (type!="date"&&type!="week-end"&&type!="month"&&type!="quarter"&&type!="year") { alert("Invalid display type! Must be one of: date,week-end,month,quarter,year"); return false; }
        this.displayType=type;
        }

// How many years back to start by default for year display
function CP_setYearSelectStartOffset(num) { this.yearSelectStartOffset=num; }

// Set which weekdays should not be clickable
function CP_setDisabledWeekDays() {
        this.disabledWeekDays = new Object();
        for (var i=0; i<arguments.length; i++) { this.disabledWeekDays[arguments[i]] = true; }
        }

// Disable individual dates or ranges
// Builds an internal logical test which is run via eval() for efficiency
function CP_addDisabledDates(start, end) {
        if (arguments.length==1) { end=start; }
        if (start==null && end==null) { return; }
        if (this.disabledDatesExpression!="") { this.disabledDatesExpression+= "||"; }
        if (start!=null) { start = parseDate(start); start=""+start.getFullYear()+LZ(start.getMonth()+1)+LZ(start.getDate());}
        if (end!=null) { end=parseDate(end); end=""+end.getFullYear()+LZ(end.getMonth()+1)+LZ(end.getDate());}
        if (start==null) { this.disabledDatesExpression+="(ds<="+end+")"; }
        else if (end  ==null) { this.disabledDatesExpression+="(ds>="+start+")"; }
        else { this.disabledDatesExpression+="(ds>="+start+"&&ds<="+end+")"; }
        }

// Set the text to use for the "Today" link
function CP_setTodayText(text) {
        this.todayText = text;
        }

// Set the prefix to be added to all CSS classes when writing output
function CP_setCssPrefix(val) {
        this.cssPrefix = val;
        }

// Show the navigation as an dropdowns that can be manually changed
function CP_showNavigationDropdowns() { this.isShowNavigationDropdowns = (arguments.length>0)?arguments[0]:true; }

// Show the year navigation as an input box that can be manually changed
function CP_showYearNavigationInput() { this.isShowYearNavigationInput = (arguments.length>0)?arguments[0]:true; }

// Hide a calendar object
function CP_hideCalendar() {
        if (arguments.length > 0) { window.popupWindowObjects[arguments[0]].hidePopup(); }
        else { this.hidePopup(); }
        }

// Refresh the contents of the calendar display
function CP_refreshCalendar(index) {
        var calObject = window.popupWindowObjects[index];
        if (arguments.length>1) {
                calObject.populate(calObject.getCalendar(arguments[1],arguments[2],arguments[3],arguments[4],arguments[5]));
                }
        else {
                calObject.populate(calObject.getCalendar());
                }
        calObject.refresh();
        }

// Populate the calendar and display it
function CP_showCalendar(anchorname) {
        if (arguments.length>1) {
                if (arguments[1]==null||arguments[1]=="") {
                        this.currentDate=new Date();
                        }
                else {
                        this.currentDate=new Date(parseDate(arguments[1]));
                        }
                }
        this.populate(this.getCalendar());
        this.showPopup(anchorname);
        }

// Simple method to interface popup calendar with a text-entry box
function CP_select(inputobj, linkname, format) {
        var selectedDate=(arguments.length>3)?arguments[3]:null;
        if (!window.getDateFromFormat) {
                alert("calendar.select: To use this method you must also include 'date.js' for date formatting");
                return;
                }
        if (this.displayType!="date"&&this.displayType!="week-end") {
                alert("calendar.select: This function can only be used with displayType 'date' or 'week-end'");
                return;
                }
        if (inputobj.type!="text" && inputobj.type!="hidden" && inputobj.type!="textarea") {
                alert("calendar.select: Input object passed is not a valid form input object");
                window.CP_targetInput=null;
                return;
                }
        window.CP_targetInput = inputobj;
        this.currentDate=null;
        var time=0;
        if (selectedDate!=null) {
                time = getDateFromFormat(selectedDate,format)
                }
        else if (inputobj.value!="") {
                time = getDateFromFormat(inputobj.value,format);
                }
        if (selectedDate!=null || inputobj.value!="") {
                if (time==0) { this.currentDate=null; }
                else { this.currentDate=new Date(time); }
                }
        window.CP_dateFormat = format;
        this.showCalendar(linkname);
        }

// Get style block needed to display the calendar correctly
function getCalendarStyles() {
        var result = "";
        var p = "";
        if (this!=null && typeof(this.cssPrefix)!="undefined" && this.cssPrefix!=null && this.cssPrefix!="") { p=this.cssPrefix; }
        result += "<STYLE>\n";
        result += "."+p+"cpYearNavigation,."+p+"cpMonthNavigation { font-size: 9px; font-family:verdana, helvetica; }\n";
        result += "."+p+"cpDayColumnHeader, ."+p+"cpYearNavigation,."+p+"cpMonthNavigation,."+p+"cpCurrentMonthDate,."+p+"cpCurrentMonthDateDisabled,."+p+"cpOtherMonthDate,."+p+"cpOtherMonthDateDisabled,."+p+"cpCurrentDate,."+p+"cpCurrentDateDisabled,."+p+"cpTodayText,."+p+"cpTodayTextDisabled,."+p+"cpText { font-size: 9px; font-family:verdana, helvetica; }\n";
        result += "TD."+p+"cpDayColumnHeader { font-size: 9px; font-family:verdana, helvetica; }\n";
        result += "."+p+"cpCurrentMonthDate, ."+p+"cpOtherMonthDate, ."+p+"cpCurrentDate  { font-size: 9px; font-family:verdana, helvetica; }\n";
        result += "."+p+"cpCurrentMonthDateDisabled, ."+p+"cpOtherMonthDateDisabled, ."+p+"cpCurrentDateDisabled { font-size: 9px; font-family:verdana, helvetica; }\n";
        result += "."+p+"cpCurrentMonthDate, .cpCurrentDate { font-size: 9px; font-family:verdana, helvetica; }\n";
        result += "."+p+"cpOtherMonthDate { font-size: 9px; font-family:verdana, helvetica; }\n";
        result += "TD."+p+"cpCurrentDate { font-size: 9px; font-family:verdana, helvetica; }\n";
        result += "TD."+p+"cpCurrentDateDisabled { font-size: 9px; font-family:verdana, helvetica; }\n";
        result += "TD."+p+"cpTodayText, TD."+p+"cpTodayTextDisabled { font-size: 9px; font-family:verdana, helvetica;}\n";
        result += "A."+p+"cpTodayText, SPAN."+p+"cpTodayTextDisabled { height:15px; }\n";
        result += "A."+p+"cpTodayText { color:black; }\n";
        result += "."+p+"cpTodayTextDisabled { color:#D0D0D0; }\n";
        result += "."+p+"cpBorder { border:solid thin #808080; }\n";
        result += "</STYLE>\n";
        return result;
        }

// Return a string containing all the calendar code to be displayed
function CP_getCalendar() {
        var now = new Date();
        // Reference to window
        if (this.type == "WINDOW") { var windowref = "window.opener."; }
        else { var windowref = ""; }
        var result = "";
        // If POPUP, write entire HTML document
        if (this.type == "WINDOW") {
                result += "<HTML><HEAD><TITLE>Calendar</TITLE>"+this.getStyles()+"</HEAD><BODY MARGINWIDTH=0 MARGINHEIGHT=0 TOPMARGIN=0 RIGHTMARGIN=0 LEFTMARGIN=0>\n";
                result += '<CENTER><TABLE WIDTH=100% BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>\n';
                }
        else {
                result += '<TABLE CLASS="'+this.cssPrefix+'cpBorder" WIDTH=144 BORDER=1 BORDERWIDTH=1 CELLSPACING=0 CELLPADDING=1>\n';
                result += '<TR><TD ALIGN=CENTER>\n';
                result += '<CENTER>\n';
                }
        // Code for DATE display (default)
        // -------------------------------
 
