//  Calendar Popup
//  Written:  2001-11-14 by James Alarie <jalarie@umich.edu>
//    http://spruce.flint.umich.edu/~jalarie/
//
//  Within a form, create an input field and name it.  Create a link which
//  calls DatePop1 with the form number or name, the name of the input 
//  field, a format code, and dates to exclude.
//
//  Format code may be uppercase or lowercase.  Valid codes are:
//
//    yyyymmdd                                year, month, date; no separators (default)
//    yyyy-mm-dd, yyyy/mm/dd                  year, month, date (note 1)
//    mmddyyyy, mm-dd-yyyy, mm/dd/yyyy        month, date, year (note 1)
//    ddmmyyyy, dd-mm-yyyy, dd/mm/yyyy        date, month, year (note 1)
//    mmddyy, mm-dd-yy, mm/dd/yy              month, date, two-digit year (note 1)
//    ddmmyy, dd-mm-yy, dd/mm/yy              date, month, two-digit year (note 1)
//    yymmdd, yy-mm-dd, yy/mm/dd              two-digit year, month, date (note 1)
//    dd-mon-yy                               SQL
//    yyyy-Www, yyyyWww, yyWww                year and ISO-8601 week number (note 2)
//    yyyy-Www-d, yyyyWwwd, yyWwwd            year, week number, day number (notes 2 & 3)
//    yyyy-jjj, yyyyjjj                       year and day number (Julian)
//
//  Exclude:
//    Enter dates to exclude as MM/DD (current year is assumed) and separate
//    the dates with commas.
//
//      '01/01,12/25' excludes New Year's day and Christmas.
//
//  Example:
//
//    <input type="text" name="Date1" id="Date1" />
//    <a href="javascript:void(0);" onclick="DatePop1('0','Date1','MM-DD-YYYY');">Get Date</a>
//
//  Notes:
//    1.  The separators (dash or slash) may be whatever you wish.
//    2.  The ISO-8601 week number should be 52 or 53 for days in a previous 
//        year, but I return a zero (0) instead.
//    3.  Day numbers run from 1 to 7 to represent Monday through Sunday.
//
//  Works in frames.
//  Works in Netscape 4.78 and 6.2, Internet Explorer 5.5, Opera 5.12.
//  Does not work in WebTV 2.6 nor Opera 5.11.
    
        function DatePop1(FNum,IName,Format,Exclude) {
          if (FNum != -1) {                         // called from form
            GD_FNum=FNum;                           // the form number
            GD_IName=IName;                         // field name
            GD_Format=Format;                       // format specifier
            GD_Format=GD_Format.toLowerCase();
            GD_Exc=Exclude;                         // dates to exclude
            Now=new Date();
            Now_M=Now.getMonth()*1+1;               // Jan-Dec = 1-12
            Now_D=Now.getDate();                    // 1-31
            Now_Y=Now.getYear();
            if (Now_Y < 70)   { Now_Y=Now_Y*1+2000; }
            if (Now_Y < 1900) { Now_Y=Now_Y*1+1900; }
            calendar_page1(Now_Y, Now_M);           // display this month's calendar
            return true;
          } else {                                  // callback from popup
            //  IName   a date as YYYYMMDDWWD       year, month, date, week, day
            //          -1 to go backward a month
            //          -2 to go forward a month
            //          -3 to go backward a year
            //          -4 to go forward a year
            // output:
            //  document.forms[GD_FNum].GD_IName.value
            if (IName == -1) {                      // go backward a month
              Now_M=Now_M-1;
              if (Now_M == 0) {                     // before January
                Now_M=12;                           // December
                Now_Y=Now_Y-1;                      // previous year
              }
              PopUp1.document.open();               // clear popup window
              calendar_page1(Now_Y, Now_M);         // display a new month
              return false;
            }
            if (IName == -2) {                      // go forward a month
              Now_M=Now_M*1+1;
              if (Now_M == 13) {                    // after December
                Now_M=1;                            // January
                Now_Y=Now_Y*1+1;                    // following year
              }
              PopUp1.document.open();
              calendar_page1(Now_Y, Now_M);
              return false;
            }
            if (IName == -3) {                      // go backward a year
              Now_Y=Now_Y-1;
              PopUp1.document.open();
              calendar_page1(Now_Y, Now_M);
              return false;
            }
            if (IName == -4) {                      // go forward a year
              Now_Y=Now_Y*1+1;
              PopUp1.document.open();
              calendar_page1(Now_Y, Now_M);
              return false;
            }
            PopUp1.window.close();                  // no longer needed
            Out=new String(IName);
            var Mabr=new Array('JAN','FEB','MAR','APR','MAY',
              'JUN','JUL','AUG','SEP','OCT','NOV','DEC');
            GD_Y=Out.substring(0,4);                // year
            GD_M=Out.substring(4,6);                // month (1-12)
            GD_D=Out.substring(6,8);                // date
            GD_W=Out.substring(8,10);               // week
            GD_X=Out.substring(10,11);              // day of the week
            GD_J=Out.substring(11,14);              // Julean day
            GD_Y2=GD_Y.substring(2,4);              // last two digits of year
            GD_X1=(GD_X*1+6)%7+1;                   // day 1-7 for Monday-Sunday
            Out=''+GD_Y+GD_M+GD_D;                  // default format is YYYYMMDD
            if (GD_Format.match(/^mm.dd.yyyy$/)) {  // mm?dd?yyyy
              Sep=GD_Format.substring(2,3);         // pick up the separator
              Out=GD_M+Sep+GD_D+Sep+GD_Y;
            }
            if (GD_Format.match(/^mm.dd.yy$/)) {    // mm?dd?yy
              Sep=GD_Format.substring(2,3);         // pick up the separator
              Out=GD_M+Sep+GD_D+Sep+GD_Y2;
            }
            if (GD_Format.match(/^dd.mm.yyyy$/)) {  // dd?mm?yyyy
              Sep=GD_Format.substring(2,3);         // pick up the separator
              Out=GD_D+Sep+GD_M+Sep+GD_Y;
            }
            if (GD_Format.match(/^dd.mm.yy$/)) {    // dd?mm?yy
              Sep=GD_Format.substring(2,3);         // pick up the separator
              Out=GD_D+Sep+GD_M+Sep+GD_Y2;
            }
            if (GD_Format.match(/^yy.mm.dd$/)) {    // yy?mm?dd
              Sep=GD_Format.substring(2,3);         // pick up the separator
              Out=GD_Y2+Sep+GD_M+Sep+GD_D;
            }
            if (GD_Format.match(/^yyyy.mm.dd$/)) { 
              Sep=GD_Format.substring(4,5);         // pick up the separator
              Out=GD_Y+'-'+GD_M+'-'+GD_D;
            }
            if (GD_Format == 'mmddyyyy')    { Out=GD_M+GD_D+GD_Y; }
            if (GD_Format == 'mmddyy')      { Out=GD_M+GD_D+GD_Y2; }
            if (GD_Format == 'ddmmyyyy')    { Out=GD_D+GD_M+GD_Y; }
            if (GD_Format == 'ddmmyy')      { Out=GD_D+GD_M+GD_Y2; }
            if (GD_Format == 'yymmdd')      { Out=GD_Y2+GD_M+GD_D; }
            if (GD_Format == 'dd-mon-yy')   { Out=GD_D+'-'+Mabr[GD_M-1]+'-'+GD_Y2; }
            if (GD_Format == 'yyyy-www')    { Out=GD_Y+'-W'+GD_W; }
            if (GD_Format == 'yyyywww')     { Out=GD_Y+'W'+GD_W; }
            if (GD_Format == 'yyyy-www-d')  { Out=GD_Y+'-W'+GD_W+'-'+GD_X1; }
            if (GD_Format == 'yyyywwwd')    { Out=GD_Y+'W'+GD_W+GD_X1; }            
            if (GD_Format == 'yywww')       { Out=GD_Y2+'W'+GD_W; }
            if (GD_Format == 'yywwwd')      { Out=GD_Y2+'W'+GD_W+GD_X1; }
            if (GD_Format == 'yyyy-jjj')    { Out=GD_Y+'-'+GD_J; }
            if (GD_Format == 'yyyyjjj')     { Out=GD_Y+GD_J; }
            document.forms[GD_FNum][GD_IName].value=Out;
            return true;
          }
          return true;
        }


        function isLeapYear(y4) {
          if (y4 % 400 == 0) return true;
          if (y4 % 100 == 0) return false;
          if (y4 % 4   == 0) return true;
          return false;
        }


        function calendar_page1(yr4, mon) {
          Today=new Date();
          Today_Y=Today.getYear();
          Today_M=Today.getMonth();
          Today_D=Today.getDate();
          if (Today_Y < 70)   { Today_Y=Today_Y*1+2000; }
          if (Today_Y < 1900) { Today_Y=Today_Y*1+1900; }
          Today_M=Today_M*1+1;
          GD_Exc=GD_Exc+' ';
          var Excludes=GD_Exc.split(',');
          Opts='width=300,height=250,resizable,scrollbars';
          PopUp1=window.open('','DatePop',Opts);
          Out ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n';
          Out+='  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n';
          Out+='\n';
          Out+='<html lang="en-US">\n  <head>\n    <title>Calendar Popup<\/title>\n';
          Out+='    <meta http-equiv="PICS-Label" content=\'(PICS-1.1 "http:\/\/www.classify.org\/safesurf\/" l gen true for "http:\/\/spruce.flint.umich.edu\/~jalarie\/" r (SS~~000 1))\' \/>\n';
          Out+='    <meta http-equiv="pics-label" content=\'(pics-1.1 "http:\/\/www.icra.org\/ratingsv02.html" comment "basic online form" l gen true for "http:\/\/spruce.flint.umich.edu\/~jalarie\/" r (nz 1 ns 1 vz 1 vs 1 lz 1 oz 1 cz 1) "http:\/\/www.rsac.org\/ratingsv01.html" l gen true for "http:\/\/spruce.flint.umich.edu\/~jalarie\/" r (n 0 s 0 v 0 l 0))\' \/>\n';
          Out+='  <\/head>\n';
          Out+='  <body background="jaa_bgnd.jpg" bgcolor="#ffffee" text="black" link="blue" vlink="#800088" alink="red">\n';
          Out+='    <center>\n';
          PopUp1.document.write(Out);

          var mns = new Array("31,000,January", "28,031,February",
            "31,059,March", "30,090,April", "31,120,May", "30,151,June",
            "31,181,July", "31,212,August", "30,243,September",
            "31,273,October", "30,304,November", "31,334,December");
          var dys = mns[mon-1].substring(0,2);      // days in the month
          var ofs = mns[mon-1].substring(3,6);      // calendar offset
          var mnn = mns[mon-1].substring(7);        // month name
          if (isLeapYear(yr4)) {
            if (mon == 2) { dys = 29; }             // Feb. has 29 days
            if (mon > 2) { ofs = 1 * ofs + 1; }     // extra offset
          }
          var skp = Math.floor((1*yr4 - 1581) * 5 / 4);
          for (ix1 = 1600; ix1 < yr4; ix1 = ix1 + 100) {
            if (ix1 % 400 != 0) { skp = skp + 6; }
          }
          var skp0 = (skp + 4) % 7 + 1*ofs - 1;
          skp = (skp + 4 + 1*ofs) % 7;
          ix9=ofs*1+1;

          out ="<table border='1' cellspacing='2' cellpadding='2'>\n";
          out+="  <thead>\n";
          out+="    <tr>\n";
          out+="      <th bgcolor='#fdf8ce' colspan='7'>\n"
          out+="<a href='javascript:void(0);' onclick='self.opener.DatePop1(-1,-3);return false;' title=' year-1 '>y-<\/a>&nbsp;&nbsp;\n";
          out+="<a href='javascript:void(0);' onclick='self.opener.DatePop1(-1,-1);return false;' title=' month-1 '>m-<\/a>\n";
          out+="&nbsp;&nbsp;&nbsp;"+mnn+" "+yr4+"&nbsp;&nbsp;&nbsp;\n";
          out+="<a href='javascript:void(0);' onclick='self.opener.DatePop1(-1,-2);return false;' title=' month+1 '>m+<\/a>&nbsp;&nbsp;\n";
          out+="<a href='javascript:void(0);' onclick='self.opener.DatePop1(-1,-4);return false;' title=' year+1 '>y+<\/a>\n";
          out+="      <\/th>\n";
          out+="    <\/tr>\n";
          out+="    <tr>\n";
          out+="      <th bgcolor='#fdf8ce' width='30' abbr='Sunday'   >Sun<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30' abbr='Monday'   >Mon<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30' abbr='Tuesday'  >Tue<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30' abbr='Wednesday'>Wed<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30' abbr='Thursday' >Thu<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30' abbr='Friday'   >Fri<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30' abbr='Saturday' >Sat<\/th>\n";
          out+="    <\/tr>\n";
          out+="  <\/thead>\n";
          out+="  <tbody>\n";
          out =out+"    <tr>\n";
            PopUp1.document.write(out);
          var wk = Math.floor((skp0 + 1) / 7) + 1;
          if (skp > 0) {
            PopUp1.document.write("      <td align='left' colspan='" + skp + "' bgcolor='#264780'>&nbsp<\/td>\n");
          }
          var ix2 = skp;
          out = '';
          for (ix1 = 1; ix1 <= dys; ix1++) {
            ix8 = Math.floor((10+ix9-ix2)/7);       // ISO-8601 week number (Jan 4=1)
            ix7 = Math.floor((12+ix9-ix2)/7);       // week number when Jan 1=1
            if (ix2 >= 7) {
              PopUp1.document.write(out);
              out = '';
              PopUp1.document.write("    <\/tr>\n  <tr>\n");
              var wk = Math.floor((skp0 + ix1) / 7) + 1;
              ix2 = 0;
            }
            SDM=mon;
            SDD=ix1;
            SDW=ix8;
            if (SDM < 10) { SDM='0'+SDM; }
            if (SDD < 10) { SDD='0'+SDD; }
            if (SDW < 10) { SDW='0'+SDW; }
//  The wierd string of numbers in the two links on the next five lines give 
//  desired date as 4-digit year, month, day, ISO week number, day-of-week 
//  number, and Julean day-of-the-year number:
            if ((yr4 == Today_Y) && (SDM == Today_M) && (SDD == Today_D)) {
              O1 ='      <td bgcolor="#FFDE00" align="right"><a href="javascript:void(0);" onclick="self.opener.DatePop1(-1,'+yr4+SDM+SDD+SDW+ix2+ix9+');return false;" title=" select ">' + ix1 + '</a>';
            } else {
              O1 ='      <td bgcolor="e9ecf2" align="right"><a href="javascript:void(0);" onclick="self.opener.DatePop1(-1,'+yr4+SDM+SDD+SDW+ix2+ix9+');return false;" title=" select ">' + ix1 + '</a>';
            }
            for (var ix3=0; ix3<Excludes.length; ix3++) {
              var Exclude=Excludes[ix3];
              if (Exclude) {
                var Exclude_M=Exclude.substring(0,2);
                var Exclude_D=Exclude.substring(3,5);
                if ((SDM == Exclude_M) && (SDD == Exclude_D)) {
                  O1='      <td bgcolor="#264780" align="right">'+ix1;
                }
              }
            }
            out=out+O1+'<\/td>\n';
            ix9 += 1;
            ix2 += 1;
          }
          PopUp1.document.write(out);
          if (ix2 < 7) {
            ix2 = 7 - ix2;
            PopUp1.document.write("      <td align='left' colspan='" + ix2 +"' bgcolor='#264780'>&nbsp<\/td>\n");
          }
          PopUp1.document.write("    <\/tr>\n");
          PopUp1.document.write("  <\/tbody>\n");
          PopUp1.document.write("<\/table>\n");

          Out ='    <\/center>\n';
          Out+='  <\/body>\n<\/html>\n';
          PopUp1.document.write(Out);
          PopUp1.document.close();
          PopUp1.focus();
          return true;
        }
