//  Calendar Popup
//  Written:  2001-11-19 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 DatePop2 with the form number or name, the name of the input 
//  field, a format code, a beginning date, an end date, a separator string
//  or 'null', and dates to exclude.
//
//  Dates:
//    The two dates must be in the yyyy/mm[/dd] format and both default to 
//    the current month.  Either or both may be an integer with a plus (+) or
//    minus (-) sign ahead of it to show offset from the current month.  If 
//    the day is specified, days outside the range will be excluded.
//
//  Format code may be uppercase or lowercase.  Valid codes are:
//
//    yyyymmdd                                year, month, date; no separators (default)
//    yyyy-mm-dd                              year, month, date with dashes
//    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-yyyy, dd-mon-yy                  SQL
//    dd mon yyyy, dd mon yy                  SQL without dashes//    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="DatePop2('0','Date1','MM-DD-YYYY','2001/10','2002/02');">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 Firefox 1.0, Netscape 4.78 and 6.2, Internet Explorer 5.5, Opera 5.12.
//  Does not work in WebTV 2.6 nor Opera 5.11.
//
//  Used in:
//    http://lrc.umflint.edu/lrcorder.asp
    
        function DatePop2(FNum,IName,Format,Beg,End,Separator,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_Beg=Beg;                             // beginning date
            GD_End=End;                             // ending date
            GD_Sep=Separator;                       // separator
            GD_Exc=Exclude;                         // dates to exclude
            if (!GD_Sep)          { GD_Sep=','; }   // default separator if not specified
            if (GD_Sep == '')     { GD_Sep=','; }   // ...if set to nothing
            if (GD_Sep == 'null') { GD_Sep='';  }   // ...forced to null if 'null'
            var Now=new Date();
            var Now_M=Now.getMonth()*1+1;           // Jan-Dec = 0-11
            var Now_D=Now.getDate();                // 1-31
            var Now_Y=Now.getYear();
            if (Now_M < 10) { Now_M='0'+Now_M; }
            if (Now_Y < 70)   { Now_Y=Now_Y*1+2000; }
            if (Now_Y < 1000) { Now_Y=Now_Y*1+1900; }
            if (GD_Beg == '') { GD_Beg=Now_Y+'/'+Now_M; }
            if (GD_End == '') { GD_End=Now_Y+'/'+Now_M; }
            if (GD_Beg.substring(0,1) == '+') {
              var M=Now_M*1+GD_Beg.substring(1,3)*1;
              var Y=Now_Y;
              if (M > 11) { Y=Now_Y*1+1; M=Now_M-12; }
              if (M < 10) { M='0'+M; }
              GD_Beg=Y+'/'+M;
            }
            if (GD_Beg.substring(0,1) == '-') {     // offset from current month
              var M=Now_M*1+GD_Beg.substring(1,3)*1;
              var Y=Now_Y;
              if (M < 0) { Y=Now_Y-1; M=Now_M*1+12; }
              if (M < 10) { M='0'+M; }
              GD_Beg=Y+'/'+M;
            }
            if (GD_End.substring(0,1) == '+') {
              var M=Now_M*1+GD_End.substring(1,3)*1;
              var Y=Now_Y;
              if (M > 11) { Y=Now_Y*1+1; M=Now_M-12; }
              if (M < 10) { M='0'+M; }
              GD_End=Y+'/'+M;
            }
            if (GD_End.substring(0,1) == '-') {
              var M=Now_M*1+GD_End.substring(1,3)*1;
              var Y=Now_Y;
              if (M < 0) { Y=Now_Y-1; M=Now_M*1+12; }
              if (M < 10) { M='0'+M; }
              GD_End=Y+'/'+M;
            }
            calendar_page2(GD_Beg, GD_End);         // display the calendar
            return true;
          } else {                                  // callback from popup
            var Rtn='';                             // nothing to return yet
            for (var ix1=0; ix1<PopUp1.document.forms[0].length; ix1++) {
              var I1=PopUp1.document.forms[0][ix1];
              if ((I1.type == 'checkbox') && (I1.checked)) {
                var Out=I1.name;
                Out=Out.substring(1);
                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
                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 == 'yyyy-mm-dd')  { 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-yyyy') { Out=GD_D+'-'+Mabr[GD_M-1]+'-'+GD_Y; }
            if (GD_Format == 'dd-mon-yy')   { Out=GD_D+'-'+Mabr[GD_M-1]+'-'+GD_Y2; }
            if (GD_Format == 'dd mon yyyy') { Out=GD_D+' '+Mabr[GD_M-1]+' '+GD_Y; }
            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; }
                if (Rtn == '') { Rtn=Out; }
                else           { Rtn=Rtn+GD_Sep+Out; }
              }
            }
            document.forms[GD_FNum][GD_IName].value=Rtn;
            PopUp1.window.close();
            return true;
          }
          return true;
        } // DatePop2


        function calendar_page2(Beg, End) {
          WB=''+Beg.substring(0,4)+Beg.substring(5,7);
          WBD=''+Beg.substring(8,10);
          WE=''+End.substring(0,4)+End.substring(5,7);
          WED=''+End.substring(8,10);
          if (!GD_Exc) { GD_Exc=''; }
          if ((WBD != '') && (WBD > 1)) {           // exclude some starting date(s)
            WBM=WB.substring(4,6);                  // the month
            Exc='';
            for (Exc1=1; Exc1<WBD; Exc1++) {
              if (Exc == '') { Exc=WBM+'/'+Exc1; }
              else { Exc=Exc+','+WBM+'/'+Exc1; }
            }
            if (GD_Exc == '') { GD_Exc=Exc; }
            else { GD_Exc=GD_Exc+','+Exc; }
          }
          if ((WED != '') && (WED < 31)) {          // exclude some ending date(s)
            WEM=WE.substring(4,6);
            Exc='';
            for (Exc1=WED*1+1; Exc1<=31; Exc1++) {
              if (Exc == '') { Exc=WEM+'/'+Exc1; }
              else { Exc=Exc+','+WEM+'/'+Exc1; }
            }
            if (GD_Exc == '') { GD_Exc=Exc; }
            else { GD_Exc=GD_Exc+','+Exc; }
          }
          var Excludes=GD_Exc.split(',');
          Opts='width=400,height=300,resizable,scrollbars';
          PopUp1=window.open('','DatePop',Opts);
          Out ='';
          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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">\n';
          Out+='  <head>\n    <title>Calendar Popup 2<\/title>\n';
          Out+='    <meta http-equiv="Content-Script-Type" content="text\/javascript" \/>\n';
          Out+='    <meta http-equiv="Content-Style-Type" content="text\/css" \/>\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/pics/vocabularyv03/" l gen true for "http://spruce.flint.umich.edu/~jalarie" r (n 0 s 0 v 0 l 0 oa 0 ob 0 oc 0 od 0 oe 0 of 0 og 0 oh 0 c 0)  gen true for "http://www.spruce.flint.umich.edu/~jalarie" r (n 0 s 0 v 0 l 0 oa 0 ob 0 oc 0 od 0 oe 0 of 0 og 0 oh 0 c 0))\' />\n';
          Out+='    <link rel="meta" href="http://spruce.flint.umich.edu/~jalarie/labels.rdf" type="application/rdf+xml" title="ICRA labels" />\n';
          Out+='    <link href="style1.css" rel="stylesheet" type="text\/css" \/>\n';
          Out+='  <\/head>\n';
          Out+='  <body class="body1">\n';
          Out+='   <div id="body">\n';
          Out+='    <div class="center">\n';
          Out+='      <form action="javascript:void(0);">\n';
          PopUp1.document.write(Out);

         while(WB <= WE) {
           WBY=WB.substring(0,4)*1;
           WBM=WB.substring(4,6)*1;
           yr4=WBY;
           mon=WBM;
           WBM=WBM*1+1;
           if (WBM < 10) { WBM='0'+WBM; }
           if (WBM > 12) {
             WBM=1;
             if (WBM < 10) { WBM='0'+WBM; }
             WBY=WBY*1+1;
           }
           WB=''+WBY+WBM;
          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 ((yr4%400 == 0)
          ||  ((yr4%100 != 0) && (yr4%4 == 0))) {   // is a leap year
            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='#ffff99' colspan='7'>"+mnn+" "+yr4+" <\/th>\n";
          out+="    <\/tr>\n";
          out+="    <tr>\n";
          out+="      <th bgcolor='#ffff99' width='40' abbr='Sunday'   >Sun <\/th>\n";
          out+="      <th bgcolor='#ffff99' width='40' abbr='Monday'   >Mon <\/th>\n";
          out+="      <th bgcolor='#ffff99' width='40' abbr='Tuesday'  >Tue <\/th>\n";
          out+="      <th bgcolor='#ffff99' width='40' abbr='Wednesday'>Wed <\/th>\n";
          out+="      <th bgcolor='#ffff99' width='40' abbr='Thursday' >Thu <\/th>\n";
          out+="      <th bgcolor='#ffff99' width='40' abbr='Friday'   >Fri <\/th>\n";
          out+="      <th bgcolor='#ffff99' width='40' 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='silver'>&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;
            SDJ=ix9;
            if (SDM < 10) { SDM='0'+SDM; }
            if (SDD < 10) { SDD='0'+SDD; }
            if (SDW < 10) { SDW='0'+SDW; }
            if (SDJ < 10) { SDJ='0'+SDJ; }
            if (SDJ < 100) { SDJ='0'+SDJ; }
//  The wierd string of numbers in the link on the next line gives
//  desired date as 4-digit year, month, day, ISO week number, day-of-week 
//  number, and Julean day-of-the-year number:
            O1 =out+"      <td bgcolor='aqua' align='right'>"+ix1+' <input type="checkbox" name="B'+yr4+SDM+SDD+SDW+ix2+SDJ+'" id="B'+yr4+SDM+SDD+SDW+ix2+SDJ+'" alt="'+yr4+SDM+SDD+SDW+ix2+SDJ+'" \/>';
            for (var ix3=0; ix3<Excludes.length; ix3++) {
              var Exclude=Excludes[ix3];
              if (Exclude) {
                var Exc1=Exclude.indexOf('/');
                var Exclude_M=Exclude.substring(0,Exc1);
                var Exclude_D=Exclude.substring(Exc1+1);
                if (Exclude_M*1 < 10) { Exclude_M='0'+Exclude_M*1; }
                if (Exclude_D*1 < 10) { Exclude_D='0'+Exclude_D*1; }
                if ((SDM == Exclude_M) && (SDD == Exclude_D)) {
                  O1=out+"      <td bgcolor='silver' align='right'>"+ix1+' &nbsp;&#150;&nbsp;&nbsp;';
                }
              }
            }
            out=O1;
            out =out+" <\/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='silver'>&nbsp;<\/td>\n");
          }
          PopUp1.document.write("    <\/tr>\n");
          PopUp1.document.write("  <\/tbody>\n");
          PopUp1.document.write("<\/table>\n");
          PopUp1.document.write("<br \/>\n");
         }

          Out ='        <input type="button" value="Ready" alt="Ready" title="Ready" onclick="self.opener.DatePop2(-1);" \/>\n';
          Out+='      <\/form>\n';
          Out+='    <\/div><!\-\- center \-\->\n';
          Out+='   <\/div>\n';
          Out+='  <\/body>\n<\/html>\n';
          PopUp1.document.write(Out);
          PopUp1.document.close();
          PopUp1.focus();
          return true;
        } // calendar_page2
