// JScript source code
// Author: Gregery Benjamin Lund
// Dated: December 31 2005

//these arrays aid in producing the calendars
////////////////
//Housekeeping//
////////////////
var day_of_week = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var month_of_year = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var Calendar = new Date(); // not needed in calendar.js file
//Calendar.setDate(5);
var delay = 0; // not needed in calendar.js file
function setDelay(newDelay)
{
    delay = newDelay;
}
var twoDel = 2; // not needed in calendar.js file
var threeDel = 3; // not needed in calendar.js file
var standardDel = 7; // not needed in calendar.js file

//after the Calendar is set then these variables are set to there values
var year = Calendar.getYear();	    // Returns year
var month = Calendar.getMonth();    // Returns month (0-11)
var today = Calendar.getDate();    // Returns day (1-31)
var weekday = Calendar.getDay();    // Returns day (1-31)
//these variables take on the charactristics of the delivery days and delay
//values that will be printed
var twoDay, threeDay, standard; //standardDay is replaced with standard
///////////
// Fix 3 //
///////////
var sameDay = 0;
if(Calendar.getHours() < 12)
{
    sameDay = 1;
}
///////////
// Fix 2 //
///////////
/*
    //set the two day delivery date //
    twoDay = (Calendar.getDate() + twoDel + delay);
*/
//set the two day delivery date //
var today = Calendar.getDate(); // not needed in calendar.js file
var wkDayCnt = 0;
while(wkDayCnt < (twoDel + delay) - sameDay)
{
    Calendar.setDate(Calendar.getDate() + 1);
    if(Calendar.getDay() == 1 || Calendar.getDay() == 2 || Calendar.getDay() == 3 || Calendar.getDay() == 4 || Calendar.getDay() == 5)
    {
        wkDayCnt++;
    }
}
twoDay = Calendar.getDate();
//set the threeDay day delivery date //
//reset values
Calendar.setDate(today);
wkDayCnt = 0;
while(wkDayCnt < (threeDel + delay) - sameDay)
{
    Calendar.setDate(Calendar.getDate() + 1);
    if(Calendar.getDay() == 1 || Calendar.getDay() == 2 || Calendar.getDay() == 3 || Calendar.getDay() == 4 || Calendar.getDay() == 5)
    {
        wkDayCnt++;
    }
}
threeDay = Calendar.getDate();
//set the standard day delivery date //
//reset values
Calendar.setDate(today);
wkDayCnt = 0;
while(wkDayCnt < (standardDel + delay) - sameDay)
{
    Calendar.setDate(Calendar.getDate() + 1);
    if(Calendar.getDay() == 1 || Calendar.getDay() == 2 || Calendar.getDay() == 3 || Calendar.getDay() == 4 || Calendar.getDay() == 5)
    {
        wkDayCnt++;
    }
}
standard = Calendar.getDate();

//local control variables
//var retainTodayDay = 0;
var hasPrinted = 0;
var calendarCount = 1;

var DAYS_OF_WEEK = 7;    // "constant" for number of days in a week
var DAYS_OF_MONTH = 31;    // "constant" for number of days in a month
//this structure is constructed to take care the months that have 31 days and 30 days and then to 
//consider the 28 days in feburary and then the 29 days in the leap year for feburary
//it also fallows the three rules that govern the evaluations of leap years
if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11)
{
    DAYS_OF_MONTH = 31;
}
else if(month == 1)
{
    if(year % 100 == 0 && year % 400 == 0 || year % 4 == 0)
    {
        DAYS_OF_MONTH = 29;
    }
    else
    {
        DAYS_OF_MONTH = 28;
    }
}
else
{
    DAYS_OF_MONTH = 30;
}

if(standard < threeDay || standard < twoDay)
{
    standard += DAYS_OF_MONTH;
}
if(threeDay < twoDay)
{
    threeDay += DAYS_OF_MONTH;
}

var cal;    // Used for printing
//after setting the variables of the date that are local
//it is safe to set it to 1 that the calendar will start with
Calendar.setDate(1);    // Start the calendar day at '1'
//and the month is set to the element that is the current month
Calendar.setMonth(month);    // Start the calendar month at now
/*Functions*/

/* VARIABLES FOR FORMATTING
NOTE: You can format the 'BORDER', 'BGCOLOR', 'CELLPADDING', 'BORDERCOLOR'
      tags to customize your caledanr's look. */

var TR_start = '<TR>';
var TR_end = '</TR>';
var highlight_S_start = '<TD WIDTH="30"><TABLE CELLSPACING=0 BORDER=1 BGCOLOR=EACC72 BORDERCOLOR=008000><TR><TD WIDTH=20><B><CENTER>';
var highlight_S_end   = '</CENTER></TD></TR></TABLE></B>';
var highlight_3D_start = '<TD WIDTH="30"><TABLE CELLSPACING=0 BORDER=1 BGCOLOR=72CCEA BORDERCOLOR=008000><TR><TD WIDTH=20><B><CENTER>';
var highlight_3D_end   = '</CENTER></TD></TR></TABLE></B>';
var highlight_2D_start = '<TD WIDTH="30"><TABLE CELLSPACING=0 BORDER=1 BGCOLOR=D77215 BORDERCOLOR=008000><TR><TD WIDTH=20><B><CENTER>';
var highlight_2D_end   = '</CENTER></TD></TR></TABLE></B>';
var highlight_start = '<TD WIDTH="30"><TABLE CELLSPACING=0 BORDER=1 BGCOLOR=DEDEFF BORDERCOLOR=CCCCCC><TR><TD WIDTH=20><B><CENTER>';
var highlight_end   = '</CENTER></TD></TR></TABLE></B>';

var TD_start = '<TD WIDTH="30"><CENTER>';
var TD_end = '</CENTER></TD>';

/* BEGIN CODE FOR CALENDAR
NOTE: You can format the 'BORDER', 'BGCOLOR', 'CELLPADDING', 'BORDERCOLOR'
tags to customize your calendar's look.*/

cal =  '<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=0 BORDERCOLOR=BBBBBB><TR><TD>';
cal += '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2>' + TR_start;
cal += '<TD COLSPAN="' + DAYS_OF_WEEK + '" BGCOLOR="#EFEFEF"><CENTER><B>';
cal += month_of_year[month]  + '   ' + year + '</B>' + TD_end + TR_end;
cal += TR_start;

//   DO NOT EDIT BELOW THIS POINT  //

// LOOPS FOR EACH DAY OF WEEK
for(index=0; index < DAYS_OF_WEEK; index++)
{

// BOLD TODAY'S DAY OF WEEK
if(weekday == index)
cal += TD_start + '<B>' + day_of_week[index] + '</B>' + TD_end;

// PRINTS DAY
else
cal += TD_start + day_of_week[index] + TD_end;
}

cal += TD_end + TR_end;
cal += TR_start;

// FILL IN BLANK GAPS UNTIL TODAY'S DAY
for(index=0; index < Calendar.getDay(); index++)
cal += TD_start + '  ' + TD_end;

// LOOPS FOR EACH DAY IN CALENDAR
for(index=0; index < DAYS_OF_MONTH; index++)
{
  if( Calendar.getDate() > index )
  {
  // RETURNS THE NEXT DAY TO PRINT
  week_day =Calendar.getDay();
  // START NEW ROW FOR FIRST DAY OF WEEK
  if(week_day == 0)
  cal += TR_start;
  if(week_day != DAYS_OF_WEEK)
  {
  // SET VARIABLE INSIDE LOOP FOR INCREMENTING PURPOSES
  var day  = Calendar.getDate();
    
  // HIGHLIGHT TODAY'S DATE
  if(today==Calendar.getDate())// && hasPrinted == 0)
  { 
    //
    cal += highlight_start + day + highlight_end + TD_end;
  }
  // HIGHLIGHT TwoDay DATE
  else if(twoDay==Calendar.getDate())
  {
    //
    cal += highlight_2D_start + day + highlight_2D_end + TD_end;
  }
  // HIGHLIGHT ThreeDay DATE
  else if(threeDay==Calendar.getDate())
  {
    //
    cal += highlight_3D_start + day + highlight_3D_end + TD_end;
  }
  // HIGHLIGHT standard DATE
  else if(standard==Calendar.getDate())
  {
    //
    cal += highlight_S_start + day + highlight_S_end + TD_end;
  }
  // PRINTS DAY
  else
    cal += TD_start + day + TD_end;
  }
  
  
  // END ROW FOR LAST DAY OF WEEK
  if(week_day == DAYS_OF_WEEK)
  cal += TR_end;
  }
  // INCREMENTS UNTIL END OF THE MONTH
  Calendar.setDate(Calendar.getDate()+1);
}// end for loop

cal += '</TD></TR></TABLE></TABLE>';
//////cal += 'Standard = ' + standard;

//  PRINT CALENDAR
if(standard > DAYS_OF_MONTH || threeDay > DAYS_OF_MONTH || twoDay > DAYS_OF_MONTH)
{
    calendarCount = 2;
    cal += '</BR>';
    cal += '<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=0 BORDERCOLOR=BBBBBB>';
    cal += '    <TR>';
    cal += '        <TD>';
    cal += '            <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2>';
    cal += '                <TR>';
    cal += '                    <TD COLSPAN="' + DAYS_OF_WEEK + '" BGCOLOR="#EFEFEF">';
    if(month >= 11)
    {
        month = 0;
        year = year + 1;
    }
    else
    {
        month++;
    }
    Calendar.setDate(1);    // Start the calendar day at '1'
    Calendar.setMonth(month);    // Start the calendar month at now
    
    cal += '                        <CENTER><B>' + month_of_year[month]  + '   ' + year + '</B>';
    cal += '                    </TD>';
    cal += '                </TR>';
    cal += TR_start;
    // LOOPS FOR EACH DAY OF WEEK
    for(index=0; index < DAYS_OF_WEEK; index++)
    {
        cal += TD_start + day_of_week[index] + TD_end;
    }
    cal += TR_end;
    
    /*
    if(month >= 11)
    {
        month = 0;
        year = year + 1;
    }
    */
    var DAYS_OF_MONTH_2ND_CAL = 31;    // "constant" for number of days in a month
    //this structure is constructed to take care
    if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11)
    {
        DAYS_OF_MONTH_2ND_CAL = 31;
    }
    else if(month == 1)
    {
        if(year % 100 == 0 && year % 400 == 0 || year % 4 == 0)
        {
            DAYS_OF_MONTH_2ND_CAL = 29;
        }
        else
        {
            DAYS_OF_MONTH_2ND_CAL = 28;
        }
    }
    else
    {
        DAYS_OF_MONTH_2ND_CAL = 30;
    }   
    
///document.write('</br>DAYS_OF_MONTH_2ND_CAL = ' + DAYS_OF_MONTH_2ND_CAL);
///document.write('</br>month = ' + month);
//Calendar.setHours(13);
///document.write('</br>Calendar.getHours() = ' + Calendar.getHours());
  /*
  document.write('twoDay = ' + twoDay);
  document.write('threeDay = ' + threeDay);
  document.write('standard = ' + standard);
  document.wrtie('Calendar.getDate() = ' + Calendar.getDate());
  */
  if(standard > DAYS_OF_MONTH)
  {
    //glitch found on line 313
    standard = standard - DAYS_OF_MONTH;// + 1;
    //standard = standard - DAYS_OF_MONTH - 1; //prevously was coded on 314 which when you take 31 from 32 
    //and then one result is 0 no calendar date is every equal
  }
  else
  {
    standard = 32;
  }
  if(threeDay > DAYS_OF_MONTH)
  {
    threeDay = threeDay - DAYS_OF_MONTH;
  }
  else
  {
    threeDay = 32;
  }
  if(twoDay > DAYS_OF_MONTH)
  {
    twoDay = twoDay - DAYS_OF_MONTH;
  }
  else
  {
    twoDay = 32;
  }/*
  */
    cal += TR_start;

// FILL IN BLANK GAPS UNTIL TODAY'S DAY
for(index=0; index < Calendar.getDay(); index++)
cal += TD_start + '  ' + TD_end;

// LOOPS FOR EACH DAY IN CALENDAR
for(index=0; index < DAYS_OF_MONTH_2ND_CAL; index++)
{
  if( Calendar.getDate() > index )
  {
  // RETURNS THE NEXT DAY TO PRINT
  week_day =Calendar.getDay();
  // START NEW ROW FOR FIRST DAY OF WEEK
  if(week_day == 0)
  cal += TR_start;
  if(week_day != DAYS_OF_WEEK)
  {
  // SET VARIABLE INSIDE LOOP FOR INCREMENTING PURPOSES
  var day  = Calendar.getDate();
  var wkd = 0;
  // HIGHLIGHT TODAY'S DATE
  if(twoDay==Calendar.getDate()){
    ///document.write('</br>2 Calendar.getDay() = ' + Calendar.getDay());
    //day = twoDay;
    cal += highlight_2D_start + day + highlight_2D_end + TD_end;
    }
  // HIGHLIGHT ThreeDay DATE
  /**/else if(threeDay==Calendar.getDate()){
    ///document.write('</br>3 Calendar.getDay() = ' + Calendar.getDay());
    //day = threeDay;
    cal += highlight_3D_start + day + highlight_3D_end + TD_end;
    }
  // HIGHLIGHT standard DATE
  else 
  if(standard==Calendar.getDate()){
    ///document.write('</br>S Calendar.getDay() = ' + Calendar.getDay());
    //day = standard;
    cal += highlight_S_start + day + highlight_S_end + TD_end;
    }
  // PRINTS DAY
  else
  cal += TD_start + day + TD_end;
  }
  // END ROW FOR LAST DAY OF WEEK
  if(week_day == DAYS_OF_WEEK)
  cal += TR_end;
  }
  // INCREMENTS UNTIL END OF THE MONTH
  Calendar.setDate(Calendar.getDate()+1);
}// end for loop
    
    
    
    
    
    cal += '            </TABLE>';
    cal += '        </TD>';
    cal += '    </TR>';
    cal += '</TABLE>';
}


    cal += '</BR>';
    cal += '<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=0 BORDERCOLOR=BBBBBB WIDTH=242>';
    cal += '    <TR>';
    cal += '        <TD>';
    cal += '            <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2 WIDTH=100%>';
    cal += '                <TR>';
    cal += '                    <TD COLSPAN="2" BGCOLOR="#EFEFEF">';
    cal += '                        <CENTER><B>Legend</B>';
    cal += '                    </TD>';
    cal += '                </TR>';
    cal += '                <TR>';
    var days = "2";
    cal += highlight_2D_start + days + highlight_2D_end + TD_end;
    cal += '                    <TD COLSPAN="0"> Day Delivery';
    cal += '                    </TD>';
    cal += '                </TR>';
    cal += '                <TR>';
    days = "3";
    cal += highlight_3D_start + days + highlight_3D_end + TD_end;
    cal += '                    <TD COLSPAN="0"> Day Delivery';
    cal += '                    </TD>';
    cal += '                </TR>';
    cal += '                <TR>';
    days = "S";
    cal += highlight_S_start + days + highlight_S_end + TD_end;
    cal += '                    <TD COLSPAN="0"> Standard Delivery';
    cal += '                    </TD>';
    cal += '                </TR>';
    cal += '            </TABLE>';
    cal += '        </TD>';
    cal += '    </TR>';
    cal += '</TABLE>';
    
    var topping = "<html>";
        topping += "    <head>";
        topping += "    <title>Delivery times</title>";
        topping += "    </head>";
        topping += "    <body>";
    var bottom  = "    </body>";
        bottom  += "</html>";

function getCal()
{
    if(calendarCount == 1)
    {
        TheNewWin = window.open('','name','windowX=200,windowY=200,height=344,width=260,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
        TheNewWin.document.write(topping);
        TheNewWin.document.write(cal);
        TheNewWin.document.write(bottom);
    }
    else if(calendarCount == 2)
    {
        TheNewWin = window.open('','name','windowX=200,windowY=200,height=536,width=260,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
        TheNewWin.document.write(topping);
        TheNewWin.document.write(cal);
        TheNewWin.document.write(bottom);
    }
    //return false;
}
function recallCal()
{
    if(calendarCount == 1)
    {
        TheNewWin = window.open('','name','windowX=200,windowY=200,height=344,width=260,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
        TheNewWin.document.write(topping);
        TheNewWin.document.write(cal);
        TheNewWin.document.write(bottom);
    }
    else if(calendarCount == 2)
    {
        TheNewWin = window.open('','name','windowX=200,windowY=200,height=536,width=260,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
        TheNewWin.document.write(topping);
        TheNewWin.document.write(cal);
        TheNewWin.document.write(bottom);
    }
    //return TheNewWin;
}

//  End -->