// $RCSfile: popupcalendarfunctions.js,v $ $Revision: 1.2 $
         //************************************************************************
         // Global variables
         //************************************************************************

         var calendarWindow;

         //************************************************************************
      // Function:  getCalendarCtrlString
      // Purpose:   Return the appropriate localized calendar control string.
      //            (eg: prev, cancel, next).
      // Input:     stringID - Control string to return.
      //            language - language used by the WEB page.
      //            locale - locale used by the WEB page.
      // Output:    String containing the Calendar Control string.
         //************************************************************************

         var CALCTRLSTR_NEXT = 0;
         var CALCTRLSTR_PREV = 1;
         var CALCTRLSTR_CNCL = 2;

         function getCalendarCtrlString (stringID, language, locale)
         {
//          var enCalendarCtrlStrings = new Array ("Next", "Prev", "Close");
//          var esCalendarCtrlStrings = new Array ("Despu&#233s", "Anterior", "Cancelan");

            return (calendarCtrlStrings [stringID]);
         }

      //************************************************************************
      // Function:  parseYear
      // Purpose:   Find the index of the year in a HTML SELECT control.
      // Input:     year - year to locate in the control.
      //            inY - the control to search.
      // Output:    Index into the SELECT control.
         //************************************************************************

         function parseYear(year, yearCtrlStr)
         {
            var retval=0;
            var i=0;
            var formyear=0;
            for (i=0; i<=5; i++)
            {
               eval ("formyear = document.getElementById('" + yearCtrlStr + "').options[" + i + "].text;");
               if (year == formyear)
               {
                  retval=i;
                  break;
               }
         }
            return retval;
         }

         //************************************************************************
      // Function:  nextMonth
      // Purpose:   Increment the month.
      // Input:     month
      // Output:    month + 1
         //************************************************************************

         function nextMonth (month)
         {
            if (month==12)
            {
               return 1;
            }
            else
            {
               return (month+1);
            }
         }

         //************************************************************************
      // Function:  prevMonth
      // Purpose:   Previous month.
      // Input:     month
      // Output:    month - 1
         //************************************************************************

         function prevMonth (month)
         {
            var prevMonth = (month - 1)
            if (month==1)
            {
               prevMonth = 12;
            }
            return prevMonth
         }

         //************************************************************************
      // Function:  changeYear
      // Purpose:   Updates the year if incrementing or decrementing into the
      //            previous or following year.
      // Input:     direction - incrementing or decrementing
      //            month - month that is being updated.
      //            year - current year value.
      // Output:    Updated year.
         //************************************************************************

         function changeYear (direction, month, year)
         {
            // increments or decrements month when it goes past Jan or Dec
            var theYear = year
            if (direction=='next')
            {
               if (month == 12)
               {
                  theYear = (year + 1)
               }
            }
            if (direction=='prev')
            {
               if (month == 1)
               {
                  theYear = (year - 1)
               }
            }
            return theYear
         }

         //************************************************************************
      // Function:  createCalendar
      // Purpose:   Create the calendar popup.
      // Input:     formStr - HTML name of the form that contains the date controls.
      //            dayCtrlStr - HTML name of the day drop down.
      //            monthCtrlStr - HTML name of the month drop down.
      //            yearCtrlStr - HTML name of the year drop down.
      //            dowCtrlStr - HTML name of the day of week control.
      //            language - language used by the WEB page.
      //            locale - locale used by the WEB page.
      //            callBackFn - JavaScript function to call when closing the calendar.
      // Output:    none
         //************************************************************************

         function createCalendar (formStr, dayCtrlStr, monthCtrlStr, yearCtrlStr, dowCtrlStr, language, locale, callBackFn)
         {

            calendarWindow = window.open ('', 'Calendar', 'width=220,height=255,resizable=yes,scrollbars=no');

            var mthVal = eval ("parseInt(document.getElementById('" + monthCtrlStr + "').options [document.getElementById('" + monthCtrlStr + "').selectedIndex].value, 10)");
            var yearVal = eval ("document.getElementById('" + yearCtrlStr + "').options [document.getElementById('" + yearCtrlStr + "').selectedIndex].value");
            generateCalendar (calendarWindow, mthVal, yearVal, formStr, dayCtrlStr, monthCtrlStr, yearCtrlStr, dowCtrlStr, language, locale, callBackFn)
         }


         //************************************************************************
      // Function:  generateCalendar
      // Purpose:   Emit the HTML into the calendar popup window.
      // Input:     target - Target browser window for the calendar.
      //            month - Month of the calendar top create.
      //            year - Year of the calendar to create.
      //            formStr - HTML name of the form that contains the date controls.
      //            dayCtrlStr - HTML name of the day drop down.
      //            monthCtrlStr - HTML name of the month drop down.
      //            yearCtrlStr - HTML name of the year drop down.
      //            dowCtrlStr - HTML name of the day of week control.
      //            language - language used by the WEB page.
      //            locale - locale used by the WEB page.
      //            callBackFn - JavaScript function to call when closing the calendar.
      // Output:    none
         //************************************************************************

         function generateCalendar(target, month, year, formStr, dayCtrlStr, monthCtrlStr, yearCtrlStr, dowCtrlStr, language, locale, callBackFn)
         {
            target.document.open()
            calendar = "<html><head><title>Calendar II</title>"
            calendar +="<STYLE><!-- \n"
            calendar +="body { font-family: Verdana, Arial, Helvetica, sans; font-size: 10px; color: #828282; font-weight: normal; }\n"
            calendar +=".monthtitle { font-family: Verdana, Arial, Helvetica; font-size: 14px; color: #828282; LETTER-SPACING: 0px; font-weight: bold; BACKGROUND-COLOR: #dcd7cb } \n"
            calendar +=".wedaytitle { font-family: Verdana,Arial,Helvetica; font-size: 12px; color: black; } \n"
            calendar +=".daytitle { font-family: Verdana,Arial,Helvetica; font-size: 12px; color: #828282; } \n"
            calendar +=".day  { font-family: Verdana,Arial,Helvetica; font-size: 10px; color: #828282; } \n"
            calendar +=".nxtprev { font-family: Verdana,Arial,Helvetica; font-size: 10px; color: #828282; } \n"
            calendar +="A:link {color:#8C8C8C} A:visited {color:#8C8C8C} A:hover {color:#003300} \n"
            calendar +="--></STYLE>\n"
            calendar +="</head><body>\n"
            calendar +=" <table border=0 cellspacing=0 cellpadding=2 width=200>"
            calendar +="<tr valign=top>"
            var mthIdx = month;
            var endday = getDaysInMonth(mthIdx, year)
            var index = (mthIdx-1)
            var goPrevMonth = prevMonth(mthIdx)
            var goNextMonth = nextMonth(mthIdx)
            var nextYear = changeYear ('next', parseInt (month, 10), parseInt (year, 10))
            var prevYear = changeYear ('prev', parseInt (month, 10), parseInt (year, 10))
        // Create the calendar header
              calendar +=" <td colspan=1 align=left class=\"monthtitle\">"
              calendar +=" <a href='javascript:opener.generateCalendar(self," + goPrevMonth + "," + prevYear + ",\"" + formStr + "\",\"" + dayCtrlStr + "\",\"" + monthCtrlStr + "\",\"" + yearCtrlStr + "\",\""
             calendar +=  dowCtrlStr + "\", \"" + language + "\",\" " + locale + "\",\" " + callBackFn + "\")'><u>&lt;</u></a>"
            calendar +=" </td>"
            calendar +=" <td colspan=5 align=center class=\"monthtitle\">"
            calendar +=  getMonth (mthIdx) + " " + year
            calendar +=" </td>"
            calendar +=" <td colspan=1 align=right class=\"monthtitle\">"
            calendar +="<a href='javascript:opener.generateCalendar(self," + goNextMonth + "," + nextYear + ",\"" + formStr + "\",\"" + dayCtrlStr + "\",\"" + monthCtrlStr + "\",\"" + yearCtrlStr + "\",\""
            calendar +=  dowCtrlStr  + "\", \"" + language + "\",\" " + locale + "\",\" " + callBackFn + "\")'><u>&gt;</u></a></td></tr>"
            calendar +=" </tr><tr align=center>"
            calendar +=" <td width=10 class=\"wedaytitle\">&nbsp;S</td>"
            calendar +=" <td width=10 class=\"daytitle\">&nbsp;M</td>"
            calendar +=" <td width=10 class=\"daytitle\">&nbsp;T</td>"
            calendar +=" <td width=10 class=\"daytitle\">&nbsp;W</td>"
            calendar +=" <td width=10 class=\"daytitle\">&nbsp;T</td>"
            calendar +=" <td width=10 class=\"daytitle\">&nbsp;F</td>"
            calendar +=" <td width=10 class=\"wedaytitle\">&nbsp;S</td>"
            calendar +=" </tr>"

        // Create the days.
            wholeDate = month + "/01/" + year
            thedate = new Date(wholeDate)
            firstDay = thedate.getDay()
            selectedmonth = mthIdx;
            var today = new Date();
            var thisyear = today.getYear() + 1900;
            selectedyear = year
            var lastDay = (endday + firstDay+1)
            var iRows =0
            calendar +=" <tr>"
            for (var i = 1; i < lastDay; i++)
            {
               if (i <= firstDay)
               {
                  calendar +=" <td>&nbsp;</td>"
               }
               else
               {
                  calendar +=" <td align=center><a href='JavaScript:opener.closeCalendar(" + (i-firstDay) + ", \"" + formStr + "\", \"" + dayCtrlStr + "\", \"" + monthCtrlStr + "\",\"" + yearCtrlStr + "\",\"" + dowCtrlStr
                  calendar += "\", \"" +  language + "\", \"" + locale + "\",\"" + callBackFn + "\");self.close();'>"+(i-firstDay)+"</a></td>"
               }
               if (i % 7 == 0 )
               {
                  if (i != lastDay-1)
                  {
                     calendar +=" </tr><tr>"
                     iRows = iRows + 1
                  }
                  else
                  {
                     iRows = iRows
                  }
               }
            }
            calendar +=" </tr>"

        // Create the calendar footer
            iRows = iRows + 1
            calendar +=" <tr><td colspan=7 align=center width=200><hr noshade></td></tr>"
            for (var icounter = 1; icounter <= 6-iRows; icounter++)
            {
               calendar +=" <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>"
            }

            calendar +=" <td align=center colspan=7 class=\"monthtitle\"><a href='javascript:self.close()'>" + getCalendarCtrlString (CALCTRLSTR_CNCL, language, locale) + "</a></td>"
            calendar +=" </tr></table></body></html>"

            target.document.write(calendar);
            target.document.close();
         }

         //************************************************************************
      // Function:  closeCalendar
      // Purpose:   Close the calendar and update the parent forms date controls.
      // Input:     day - selected day.
      //            formStr - HTML name of the form that contains the date controls.
      //            dayCtrlStr - HTML name of the day drop down.
      //            monthCtrlStr - HTML name of the month drop down.
      //            yearCtrlStr - HTML name of the year drop down.
      //            dowCtrlStr - HTML name of the day of week control.
      //            language - language used by the WEB page.
      //            locale - locale used by the WEB page.
      //            callBackFn - JavaScript function to call when closing the calendar.
      // Output:    none
         //************************************************************************

         function closeCalendar (day, formStr, dayCtrlStr, monthCtrlStr, yearCtrlStr, dowCtrlStr, language, locale, callBackFn)
         {
        // Update the controls.
            var yrIdx = parseYear (selectedyear, yearCtrlStr);
            eval ("document.getElementById('" + dayCtrlStr + "').selectedIndex = " + parseInt(day-1, 10));
            eval ("document.getElementById('" + monthCtrlStr + "').selectedIndex = "  + selectedmonth + "-1");
            eval ("document.getElementById('" + yearCtrlStr + "').selectedIndex = " + yrIdx);

            eval ("document.getElementById('" + dowCtrlStr + "').value = '"
                           + getDayOfWeek (buildCRSDate (selectedyear,
                                                         selectedmonth,
                                                         day)) + "'");
        // Call the callback function.
        eval (callBackFn);
         }
