// Debit Dates
function DebDate()
{
  if (document.f.SY.selectedIndex == 0 | document.f.SM.selectedIndex == 0 |
	document.f.SD.selectedIndex == 0)
  {
    // document.f.LD.value = '';
    document.f.L45.value = '';
    document.f.ID.value = '';
    document.f.PD.value = '';
    return;
  }
  var CN = parent.document.f.CN.value;
  var FP = document.f.FP.value;
  var MS = new String("JanFebMarAprMayJunJulAugSepOctNovDec");
  var DAYim = new Number(24 * 60 * 60 * 1000); // Day in ms
  var Wim = new Number(7 * DAYim); // One week in ms
  var T45im = new Number(FP * DAYim); // 45 days in ms 
  var SelYr = document.f.SY.selectedIndex + 2007;
  var SelMo = document.f.SM.selectedIndex - 1;
  var SelDy = document.f.SD.selectedIndex;
  var PDim = Date.UTC(SelYr, SelMo, SelDy, 12, 0, 0, 0);
  var ODim = Date.UTC(2005, 0, 12, 13, 0, 0, 0); // First Wed 1/12/05
  var PDate = new Date(PDim);
  var ODat = new Date(ODim);
  var PDM = MS.substr(PDate.getMonth()*3,3);
  var PDate45 = new Date(PDim + T45im);
  var PDM45 = MS.substr(PDate45.getMonth()*3,3);
  var PDS = new String(PDM + " " + PDate.getDate() + " " + PDate.getFullYear());
  var PDS45 = new String(PDM45 + " " + PDate45.getDate() + " " + 
	PDate45.getFullYear());
  // document.f.LD.value = PDS;
  document.f.L45.value = PDS45;
  // while (PDate45 > ODat)
  // {
  //    var ODat = new Date(ODat.valueOf() + Wim);
  // }
  var PDim = PDate45.valueOf() - 7 * DAYim;
  var PDat = new Date(PDim);
  var PDM = MS.substr(PDat.getMonth()*3,3);
  var PDS = new String(PDM + " " + PDat.getDate() + " " + 
	PDat.getFullYear());
  document.f.PD.value = PDS;
  var IDim = PDate45.valueOf() - 21 * DAYim;
  var IDat = new Date(IDim);
  var IDM = MS.substr(IDat.getMonth()*3,3);
  var IDS = new String(IDM + " " + IDat.getDate() + " " + 
	IDat.getFullYear());
  document.f.ID.value = IDS;
  var weekday = IDat.getDay();
  var IDMonth = IDat.getDate();
  var WDS = new String("SunMonTueWedThuFriSat");
  var wday = WDS.substr(weekday*3,3);
  wm = window.open("","debitdates","width=650,height=550");
  wm.document.writeln("<head><title>Equity 4 U Debit Calendar for " + 
	CN + " </title>");
  wm.document.writeln("<style type='text/css'>");
  wm.document.writeln("td { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px } </style></head>");
  wm.document.writeln("<body><center><H4>Equity 4 U Debit Calendar for " +
	CN);
  wm.document.writeln("<br>First Debit: " + wday + " " + IDS + "</H4>\n");
  wm.document.writeln("<table border=2>\n");
  var mi = 0; var c14 = 0;
  var bgc = new String("");
  for (r=0; r<3; r++)
  {
    wm.document.writeln("<tr>\n");
    for (c=0; c<4; c++)
    {
      // For each month
      wm.document.writeln("<td align=center>\n");
      CY = IDat.getFullYear();
      IDV = IDat.getMonth()+mi;
      if (IDV > 11) { IDV = IDV - 12; CY = IDat.getFullYear() + 1; }
      IDM = MS.substr(IDV*3,3);
      mi++;
      wm.document.writeln("<B>" + IDM + " " + CY + "</B><BR>");

      var Day1 = new Date(CY, IDV, 1); // First day of Month
      // Day1.setYear(CY);
      // Day1.setMonth(IDV);
      // Day1.setDate(1);
      // Day1.setHours(1);
      // Day1.setMinutes(1);
      // Day1.setSeconds(1);
      // Day1.setMilliseconds(1);
      var wd1 = Day1.getDay(); // weekday of 1st of month
      // wm.document.writeln("<B>1st day " + IDV + " " + CY + ": " 
	// + WDS.substr(wd1*3,2) + "</B><BR>");

      wm.document.writeln("<table border=0>");
      wm.document.writeln("<tr>\n");
      for (d=0; d<7; d++)
      {
        wday = WDS.substr(d*3,1);
        wm.document.writeln("<td align=center>" + wday + "</td>");
      }
      wm.document.writeln("</tr>\n");

      mday = 0;
      for (w=0; w<6; w++)
      {
        wm.document.writeln("<tr>\n");
        for (d=0; d<7; d++)
        {
          if (d >= wd1)
          {
            mday++; wd1 = -1;
          }
          else
          {
            mday = 0;
          }
          if (IDV == 0 && mday > 31) { mday = 0; wd1= 9; }
          if (IDV == 1 && mday > 29 && (CY == 2008 | CY == 2012 | CY == 2016)) { mday = 0; wd1= 9; }
          if (IDV == 1 && mday > 28 && CY != 2008 && CY != 2012 && CY != 2016) { mday = 0; wd1= 9; }
          if (IDV == 2 && mday > 31) { mday = 0; wd1= 9; }
          if (IDV == 3 && mday > 30) { mday = 0; wd1= 9; }
          if (IDV == 4 && mday > 31) { mday = 0; wd1= 9; }
          if (IDV == 5 && mday > 30) { mday = 0; wd1= 9; }
          if (IDV == 6 && mday > 31) { mday = 0; wd1= 9; }
          if (IDV == 7 && mday > 31) { mday = 0; wd1= 9; }
          if (IDV == 8 && mday > 30) { mday = 0; wd1= 9; }
          if (IDV == 9 && mday > 31) { mday = 0; wd1= 9; }
          if (IDV == 10 && mday > 30) { mday = 0; wd1= 9; }
          if (IDV == 11 && mday > 31) { mday = 0; wd1= 9; }

          // Good date to display
          if (mday > 0)
            {
              bgc = "";
              if (IDMonth == 99)
              {
                c14++;
                if (c14 == 14)
                {
                   bgc = "bgcolor=yellow><B><U><font color=222222";
                   c14 = 0;
                }
              }
              if (mday == IDMonth)
              {
                bgc = "bgcolor=yellow><B><U><font color=222222"; IDMonth = 99;
              }
              wm.document.writeln("<td align=center " +  bgc + ">" 
		+ mday + "</td>"); 
            }
          else
            { wm.document.writeln("<td align=center>&nbsp;</td>"); }
        }
        wm.document.writeln("</tr>\n");
      }
      wm.document.writeln("</table>");
      wm.document.writeln("</td>\n");
    }
    wm.document.writeln("</tr>\n");
  }
  wm.document.writeln("</table>\n");
  
  wm.document.writeln("<form><input type=button value='Print Calendar' " +
	 " onClick='window.print()'><br>" +
	"Note: Transactions do not occur on weekends or holidays </form></body>\n");
  wm.document.close();
}
