//  Calendar Popup
//  Written:  11-19-2001 by James Alarie <jalarie@umich.edu>
//
//  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, and an end date.  Format code 
//  may be uppercase or lowercase.  Valid codes are:
//
//    yyyymmdd,                               for year, month, day; no separators (default)
//    mm-dd-yyyy, mm/dd/yyyy                  for month, day, year with specified separator
//    dd-mm-yyyy, dd/mm/yyyy                  for day, month, year with specified separator
//    mm-dd-yy, mm/dd/yy, dd-mm-yy, dd/mm/yy  for a two-digit year
//    dd-mon-yy                               for SQL
//
//  The two dates must be in the mm/yyyy format and both default to the
//  current month.
//
//  Example:
//
//    <input type="text" name="Date1" />
//    <a href="javascript:void(0);" 
//      onclick="DatePop2('0','Date1','MM-DD-YYYY','10/2001','02/2002');">Get Date</a>
//
//  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 DatePop(FNum,IName,Format,Beg,End) {
          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;
            GD_End=End;
            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 < 1900) { Now_Y=Now_Y*1+1900; }
            if (GD_Beg == '') { GD_Beg=Now_M+'/'+Now_Y; }
            if (GD_End == '') { GD_End=Now_M+'/'+Now_Y; }
            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;
                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_Y2=GD_Y.substring(2,4);          // last two digits of year
                Out=''+GD_Y+GD_M+GD_D;              // default format is YYYYMMDD
                if (GD_Format == 'mm-dd-yyyy') { Out=GD_M+'-'+GD_D+'-'+GD_Y;  }
                if (GD_Format == 'mm/dd/yyyy') { Out=GD_M+'/'+GD_D+'/'+GD_Y;  }
                if (GD_Format == 'mm-dd-yy')   { Out=GD_M+'-'+GD_D+'-'+GD_Y2; }
                if (GD_Format == 'mm/dd/yy')   { Out=GD_M+'/'+GD_D+'/'+GD_Y2; }
                if (GD_Format == 'dd-mm-yyyy') { Out=GD_D+'-'+GD_M+'-'+GD_Y;  }
                if (GD_Format == 'dd/mm/yyyy') { Out=GD_M+'/'+GD_D+'/'+GD_Y;  }
                if (GD_Format == 'dd-mm-yy')   { Out=GD_D+'-'+GD_M+'-'+GD_Y2; }
                if (GD_Format == 'dd/mm/yy')   { Out=GD_M+'/'+GD_D+'/'+GD_Y2; }
                if (GD_Format == 'dd-mon-yy')  { Out=GD_D+'-'+Mabr[GD_M-1]+'-'+GD_Y2; }
                if (Rtn == '') { Rtn=Out; }
                else           { Rtn=Rtn+', '+Out; }
              }
            }
            Xeq='document.forms["'+GD_FNum+'"].'+GD_IName+'.value="'+Rtn+'";';
            eval(Xeq);
            PopUp1.window.close();
            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_page2(Beg, End) {
          WB=''+Beg.substring(3,7)+Beg.substring(0,2);
          WE=''+End.substring(3,7)+End.substring(0,2);
          Opts='width=350,height=400,scrollbars';
          PopUp1=window.open('','X',Opts);
          Out ='<html>\n  <head>\n    <title>Calendar Popup<\/title>\n';
	  Out+='<META HTTP-EQUIV="Pragma" CONTENT="no-cache">\n';
          Out+='  <\/head>\n';
          Out+='  <body background="jaa_bgnd.jpg" bgcolor="#ffffee" text="black" link="blue" vlink="#800088" alink="red">\n';
          Out+='    <center>\n';
          Out+='      <form>\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 (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'>"+mnn+" "+yr4+"<\/th>\n";
          out+="    <\/tr>\n";
          out+="    <tr>\n";
          out+="      <th bgcolor='#fdf8ce' width='30'>Sun<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30'>Mon<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30'>Tue<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30'>Wed<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30'>Thu<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30'>Fri<\/th>\n";
          out+="      <th bgcolor='#fdf8ce' width='30'>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 colspan='" + skp + "' bgcolor='silver'>&nbsp<\/td>\n");
          }
          var ix2 = skp;
          out = '';
          for (ix1 = 1; ix1 <= dys; ix1++) {
            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;
            }
            out =out+"      <td bgcolor='#e9ecf2' align='right'>";
              SDM=mon;
              SDD=ix1;
              if (SDM < 10) { SDM='0'+SDM; }
              if (SDD < 10) { SDD='0'+SDD; }
              out =out+ix1+'<input type="checkbox" name="'+yr4+SDM+SDD+'">';
            out =out+"<\/td>\n";
            ix9 += 1;
            ix2 += 1;
          }
          PopUp1.document.write(out);
          if (ix2 < 7) {
            ix2 = 7 - ix2;
            PopUp1.document.write("      <td 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 \/><input type=button value=' Finish ' onclick='self.opener.DatePop(-1);' \/><br/\>\n");
         }

          Out='      <\/form>\n';
          Out+='    <\/center>\n';
          Out+='  <\/body>\n<\/html>\n';
          PopUp1.document.write(Out);
          PopUp1.document.close();
          PopUp1.document.focus;
        }
