//  03-10-2003
//  07-30-2003
function CreateCalendar(month, year, hour, minute, am_pm)
{
    var today, startMonth, curDate, calendar, i;
    var dayptr = 0;
    var selected = new Array(2);
    
    today = new Date;
    
    //  Current stats
    if (month == null) month = today.getMonth();
    if (year == null) year = today.getYear();
    //  Netscape fix on creation of first calendar
    if (year < 1000) year += 1900;
    
    curDate = 1;
    calendar = '';
    calendar += STYLE_SHEET + '\n\n\n';

    //  Gets full info regarding first of the month
    startMonth = new Date(year, month, 1);
    
    //  getDay() returns day of the week that the date object has as its
    //  day param. ie (startMonth's day is 1 so startMonth.getDay() returns
    //  the day of the week that the 1 lands on in that month.
    //  This method is what's used in the loop to print out the dates.
    //  (0=Sunday to 6=Saturday) 
    
    var onLoadEvent = DATE_TIME_FIELD ? "self.opener.ChangeTime(\'am_pm\')" : "";
    //  Heading

    if (PAGE_TITLE)
    {
      calendar += '<title>' + PAGE_TITLE + '</title>\n';
    }
    else
    {
      calendar += '<title>' + FIELD_NAME.unfix() + '</title>\n';
    }

    calendar += '<body bgcolor = ' + BACKGROUND_COLOR + ' onLoad = "' + onLoadEvent + '">\n\n';
    //  Starts table
    if (DATE_TIME_FIELD) calendar += TABLE_TOP + '\n\n<form name = "cal"><table border = "0"><tr><td valign= "top">\n';
    calendar += '<center><table border = "1" cellpadding = "3" cellspacing = "0" bgcolor = ' + TABLE_COLOR + '>\n';
    calendar += '<tr><th colspan = "7">\n';

    //  BEGIN Calendar Header - BEGIN TABLE
    calendar += '<table border = "0" cellpadding = "0" cellspacing = "0" width = "100%"><tr>\n';
    //  Left arrows
    calendar += '<th width = "12%" align = "left"><a href = \'javascript:self.opener.ChangeDate("<<", ' + month + ', ' + year + ')\'><<</a></th>\n';
    calendar += '<th width = "12%">&nbsp;&nbsp;&nbsp;<a href = \'javascript:self.opener.ChangeDate("<", ' + month + ', ' + year + ')\'><</a></th>\n';
    // Month Name       
    calendar += '<th width = "52%" align = "center">' + monthNames[month] + ' ' + year + '</th>\n';
    //  Right arrows
    calendar += '<th width = "12%"><a href = \'javascript:self.opener.ChangeDate(">", ' + month + ', ' + year + ')\'>></a></th>\n';
    calendar += '<th width = "12%" align = "right">&nbsp;&nbsp;&nbsp;<a href = \'javascript:self.opener.ChangeDate(">>", ' + month + ', ' + year + ')\'>>></a></th>\n';
    calendar += '</tr></table>\n';
    //  End Calendar Header - END TABLE
    
    calendar += '</th></tr><tr>\n';
    //  Days of the week
    for (i = 0; i < dayNames.length; i++)
    {
        calendar += '<td style = "day">' + dayNames[i] + '</td>\n';
    }
    calendar += '</tr><tr>\n';
    //  Pads for the beginning
    for (i = 1; i <= startMonth.getDay(); i++)
    {
        calendar += '<td>&nbsp;</td>\n';
        dayptr++;
    }
    
    //  Print date
    // '"' + today + '"<BR>' + today.getDay() + ' ' + curDate + ' -' + today.getYear() + ' ' + year +  ' -' + today.getMonth() + ' ' +  month + '-' + 
    while (startMonth.getMonth() == month)
    {
        var fontColor = ((today.getDate() == curDate) && (today.getMonth() == month) && (today.getYear() == year)) ? HIGHLIGHTED_COLOR : REGULAR_COLOR;
        calendar += '<td align = "center"><a href = \'javascript:self.opener.SelectDate(' + month + ', ' + curDate + ', ' + year + ')\'><font color = "' + fontColor + '">' + curDate + '</font></td>\n';
        if (dayptr == 6)
        {
            calendar += '</tr><tr>\n';
            dayptr = -1;
        }
        startMonth.setDate(++curDate);
        dayptr++;
    }
    //  Pads for the end (only if needed)
    if (startMonth.getDay() > 0)
    {
        for (i = startMonth.getDay(); i <= 6; i++)
        {
            calendar += '<td>&nbsp;</td>\n';
        }
    }
    calendar += '</tr></table>\n';


    if (DATE_TIME_FIELD == 1)
    {
        //calendar += '</td><td valign = \"top\" bgcolor = \"$tablecolor\">
        calendar += '<br>' + DATE_WORDS + ': <input name = "date" type = "text" size = "14" onFocus = "document.cal.minute.focus()" onClick = "document.cal.minute.focus()" value = "' + DATE + '">\n';
        //  Time Section starts here
        calendar += '</td><td valign = "top"><table border = "0"><tr><td align = "right" valign = "top"><table border = "0"><tr><th>\n';
        //  Time
        calendar += TIME_WORDS + ':</th></tr></table><td valign = "top"><input name = "time" type = "text" size = "7" onFocus = "document.cal.minute.focus()" onClick = "document.cal.minute.focus()" valign = "top"> &nbsp; </td>\n';

        //  Hour selection
        calendar += '<td rowspan = "3" valign = "top"><select name = "hour" size = "12" width = "3" onChange = "self.opener.ChangeTime(\'hour\')">\n';
        for (i = 1; i <= 12; i++)
        {
            selected[0] = i == HOUR ? "selected" : "";
            calendar += '<option value = "' + i + '" ' + selected[0] + '>' + i + '\n';
        }
        calendar += '</select>\n';

        //  Minute selection
        calendar += '<select name = "minute" size = "12" width = "3" onChange = "self.opener.ChangeTime(\'minute\')">\n';
        for (i = 0; i <= 55; i = i + 5)
        {
            selected[0] = ((i == MINUTE) && (HOUR > 0)) ? "selected" : "";
            calendar += '<option value = "' + AddZero(i) + '" ' + selected[0] + '>' + AddZero(i) + '\n';
        }
        calendar += '</select> &nbsp;</td>\n';
        calendar += '';
            
        //  AM/PM selection
        if (SUFFIX == "AM")
            selected[0] = "selected";
        else if (SUFFIX == "PM")
            selected[1] = "selected";
        calendar += '<td rowspan = "3" valign = "top"><select name = "am_pm" size = "2" onChange = "self.opener.ChangeTime(\'am_pm\')">\n';
        calendar += '<option value = "AM" ' + selected[0] + '>AM\n';
        calendar += '<option value = "PM" ' + selected[1] + '>PM\n';
        calendar += '</select>\n';
        
        //  No Time check box
        if (!TIME_IS_MAND)
        {
            calendar += '<tr><th>' + NO_TIME_WORDS + ': </th><td><input name = "noTime" type = "checkbox" onClick= "self.opener.ToggleTime()" CHECKED> </tr>\n';
        }

        if (!(HIDE_LINK_OPTIONS))
        {
            var checkedLink = new Array();
            if (SEND_TO_CAL_VALUE == 0) checkedLink[0] = "CHECKED";
            if (SEND_TO_CAL_VALUE == 1) checkedLink[1] = "CHECKED";
            if (SEND_TO_CAL_VALUE == 2) checkedLink[2] = "CHECKED";
            //  Send to Calendar
            calendar += '<tr><th valign = "top">' + CALENDAR_WORDS + ': </th><td valign = "top">\n';
    
            calendar += '<input name = "SendToCalendar" type = "radio" value = "0" ' + checkedLink[0] + ' onClick = "self.opener.ChangeCalendarOption(0)">' + DONT_SEND_WORDS + '<br>\n';
            calendar += '<input name = "SendToCalendar" type = "radio" value = "1" ' + checkedLink[1] + ' onClick = "self.opener.ChangeCalendarOption(1)">' + PERSONAL_WORDS + '<br>\n';
            if (CREATE_APTS_IN_PROJ == 1) calendar += '<input name = "SendToCalendar" type = "radio" value = "2" ' + checkedLink[2] + ' onClick = "self.opener.ChangeCalendarOption(2)">' + PERSONAL_AND_PROJECT_WORDS;
            calendar += '</td></tr>\n';
        }
        //calendar += '<tr><td>&nbsp;</td><td>&nbsp;</td></tr>\n';
        calendar += '<td colspan = "2" align = "center">' + CLEAR_BUTTON + '</td>\n';
        calendar += '</td></tr></table><BR><a href = "javascript:void(0)"></table>\n';
        calendar += '\n\n' + TABLE_BOTTOM + '\n';
        calendar += '<p>' + GO_BUTTON + '</form>';
    }

    calendar += '</center>';
    var width = DATE_TIME_FIELD == 1 ? 625 : 300;
    var height = DATE_TIME_FIELD == 1 ? 295 : 200;
    popup = window.open('', 'popup', 'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes,screenX=150,screenY=150,top=150,left=150,status=no');                    
    popup.document.clear();
    popup.document.writeln(calendar);
    if (TIME_IS_MAND)
    {
	    if (popup.document.cal.hour.selectedIndex == -1)
            popup.document.cal.hour.selectedIndex = 0;
        ChangeTime('hour');
    }
    popup.document.close();
}

//  03-10-2003
//  03-11-2003
function ChangeDate (symbol, mon, year)
{
    if (symbol == "<")
    {
        if (mon == 0)
        {
            mon = 11;
            year--;
        }
        else
        {
            mon--;
        }
    }
    if (symbol == "<<")
    {
        year--;
    }

    if (symbol == ">")
    {
        if (mon == 11)
        {
            mon = 0;
            year++;
        }
        else
        {
            mon++;
        }
    }
    if (symbol == ">>")
    {
        year++;
    }
    CreateCalendar(mon, year)
}

//  03-11-2003
//  12-11-2003
function SelectDate (mon, day, year)
{
    mon++;
    if (DATE_TIME_FIELD) 
    {
        if (DATE_STYLE == 0) // American
            finalDate = mon + '/' + day + '/' + year;
        else if (DATE_STYLE == 1) // European
            finalDate = day + '/' + mon + '/' + year;
        else // ISO
            finalDate = year + '-' + mon + '-' + day;
        popup.document.cal.date.value = finalDate;
        DATE = finalDate;
    }
    else
    {
        eval('document.' + FORM_NAME + '.MonthInput_' + FIELD_NAME + '.value = mon;'); 
        eval('document.' + FORM_NAME + '.DayInput_' + FIELD_NAME + '.value = day;'); 
        eval('document.' + FORM_NAME + '.YearInput_' + FIELD_NAME + '.value = year;'); 
        if (REFRESH_AVAILABILITY_SCREEN)
        {
            RefreshPage();
        }
        popup.close();
    }
}

function AddZero (num)
{
    return (num < 10) ? '0' + num : num ;
}
                   
function ChangeTime(whichUnit)
{
    //  Minute can't be selected first
    if (popup.document.cal.minute.selectedIndex == -1)
    {
        popup.document.cal.minute.selectedIndex = 0;
    }

    //  If minute selected and hour is not selected, set others to blank
    if (popup.document.cal.hour.selectedIndex == -1 && whichUnit == 'minute')
    {
        popup.document.cal.minute.selectedIndex = -1;
    }

    //  If am/pm selected and hour is not selected, set others to blank
    if (popup.document.cal.hour.selectedIndex == -1 && whichUnit == 'am_pm')
    {
        popup.document.cal.minute.selectedIndex = -1;
        popup.document.cal.am_pm.selectedIndex = -1;
    }              
    
    //  Both hour and minute have been selected
    if (popup.document.cal.hour.selectedIndex >= 0 && popup.document.cal.minute.selectedIndex >= 0)
    {
        //  Set am/pm to am if not selected yet
        if (popup.document.cal.am_pm.selectedIndex == -1)
        {
            popup.document.cal.am_pm.selectedIndex = 0;
        }
        //  Display the time in the time box
        popup.document.cal.time.value = popup.document.cal.hour.selectedIndex + 1 + ':' + popup.document.cal.minute.options[popup.document.cal.minute.selectedIndex].value + ' ' + popup.document.cal.am_pm.options[popup.document.cal.am_pm.selectedIndex].value;
        HOUR = popup.document.cal.hour.selectedIndex + 1;
        MINUTE = popup.document.cal.minute.options[popup.document.cal.minute.selectedIndex].value;
        SUFFIX = popup.document.cal.am_pm.options[popup.document.cal.am_pm.selectedIndex].value
    }
  
    //  If no time was checked but an hour was selected, uncheck the no time box
    if (!TIME_IS_MAND)
    {
        if (popup.document.cal.noTime.checked == true && popup.document.cal.hour.selectedIndex >= 0)
        {
            popup.document.cal.noTime.checked = false;
        }
    }
}

function ToggleTime()
{
    if (popup.document.cal.noTime.checked == true)
    {
        popup.document.cal.hour.selectedIndex = -1;
        popup.document.cal.minute.selectedIndex = -1;
        popup.document.cal.am_pm.selectedIndex = -1;
        popup.document.cal.time.value = '';
        HOUR = '';
        MINUTE = '';
        SUFFIX = '';
    }
    else
    {
        popup.document.cal.hour.selectedIndex = 0;
        ChangeTime('hour');
    }
}

//  Last Edited: 05-13-2003
function ChangeCalendarOption(calOption)
{
   eval('document.' + FORM_NAME + '.SendToCalendar_' + FIELD_NAME + '.value = ' + calOption + ';');
   SEND_TO_CAL_VALUE = calOption;
}


//  Last Edited: 05-13-2003
function CloseWindow(changeDate)
{
    eval('var inputFromOtherWindow = document.' + FORM_NAME + '.' + FIELD_NAME + '_datetime;');
    if (popup.document.cal.date.value == '' && popup.document.cal.time.value != '')
    {
        popup.alert(NEED_DATE_WORDS);
    }
    else
    {
        inputFromOtherWindow.value = popup.document.cal.date.value;
        if (popup.document.cal.time.value != '')
        { 
            inputFromOtherWindow.value = inputFromOtherWindow.value + ' ' + popup.document.cal.time.value;
        } 
        FORM_NAME = null;
        FIELD_NAME = null;
        DATE_TIME_FIELD = null;
        HIDE_LINK_OPTIONS = null;
        DATE = null;
        HOUR = null;
        MINUTE = null;
        SUFFIX = null;
        SEND_TO_CAL_VALUE = null;
        popup.close();
    }
}

//  Instead of passing these values around to every function, they 
//  are made global for the duration of date processing
//  Created: 04-14-2003 Sharone
//  Last Edited: 12-11-2003
function InitializeVariables(formName, fieldName, dateTimeField, hideLinkOptions, timeIsMand, pageTitle, refreshAvailabilityScreen)
{
    var month, year;
    
    FORM_NAME = formName;
    FIELD_NAME = fieldName;
    DATE_TIME_FIELD = dateTimeField;
    HIDE_LINK_OPTIONS = hideLinkOptions;
    TIME_IS_MAND = timeIsMand;
    PAGE_TITLE = pageTitle;
    REFRESH_AVAILABILITY_SCREEN = refreshAvailabilityScreen;

    if (DATE_TIME_FIELD)
    {
        var dateObject = ParseDateTime();
        if (dateObject.month > 0) month = --dateObject.month
        if (dateObject.year > 0) year = dateObject.year;
    }
    CreateCalendar(month, year);
}

//  05-01-2003
function ParseDateTime()
{
    var data, month, year, day;
    DATE = "";
    eval('data = document.' + FORM_NAME + '.' + FIELD_NAME + '_datetime.value;');
    if (data)
    {
        dateTimeParts = data.split(' ');
        DATE = dateTimeParts[0];
        //  Gets the month and year values
        if (DATE_STYLE == 0)
        {
            dateParts = DATE.split('/');
            month = dateParts[0];
            year = dateParts[2];
        }
        else if (DATE_STYLE == 1)
        {   
            dateParts = DATE.split('/');
            month = dateParts[1];
            year = dateParts[2];
        }
        else
        {
            dateParts = DATE.split('-');
            month = dateParts[1];
            year = dateParts[0];
        }
        //  Sets the time values
        if (dateTimeParts.length > 1)
        {
            timeParts = dateTimeParts[1].split(':');
            HOUR = timeParts[0];
            MINUTE = timeParts[1];
            SUFFIX = dateTimeParts[2];
        }
        if (!HIDE_LINK_OPTIONS)
        {
            eval('SEND_TO_CAL_VALUE = document.' + FORM_NAME + '.SendToCalendar_' + FIELD_NAME + '.value');
        }
    
    }
    if (!SEND_TO_CAL_VALUE) SEND_TO_CAL_VALUE = 0;
        
    return {month:month, year:year};
}

//  Erases the date and time that's in the window
//  06-04-2004
function ClearData()
{
    popup.document.cal.date.value = "";
    popup.document.cal.noTime.checked = true;
    ToggleTime();
}