//the string that gets printed to the dynamic div
var strOutput = "";

var lBound = 0;
var uBound = 9;

var epm = 10;
var pages = 0;
var activePage = 1;
var paginate = "";

var prevDay = 0;
var prevMonth = 0;
var prevYear;

var months = new Array("January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November","December");
var days = new Array("Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31]


function drawMonth(month,year,drawFirst)
{
   drawCal(month,year,drawFirst);
}

function renderCal()
{
   var date = new Date();
   drawCal(date.getMonth()+1,date.getFullYear(),0);
}


//This function draws the calender for the given month and year
function drawCal(month,year,drawFirst)
{
   //these 4 values hold the next and previous month and year with respect
   //to the provided month and year.
   var pMonth;
   var nMonth;
   var pYear = year;
   var nYear = year;
   
   //checking for calendar wrap around one year behind
   if(month != 1)
      pMonth = month-1;
   else
   {
      pMonth = 12;
      pYear--;
      
   }
      
   //checking for calendar wrap around one year before
   if(month != 12)
   {
      nMonth = month;
      nMonth++;
   }
   else
   {
      nMonth=1
      nYear++;
   }    
   
   //generating the calendar header
   var calCode = "";
   calCode += '<table class="calendar" cellpadding="4" cellspacing="0">';
   calCode += '<tr><td class="noline"><a href="javascript:drawMonth('+pMonth+','+pYear+',0)">&#171;</a></td>';
   calCode += '<td class="noline" colspan="5"><strong>'+getMonth(month-1)+' '+year+'</strong></td>';
   calCode += '<td><a href="javascript:drawMonth('+nMonth+','+nYear+',0)">&#187;</a></td></tr>';
   calCode += '<tr><td class="days">Su</td><td class="days">M</td><td class="days">Tu</td><td class="days">W</td><td class="days">Th</td><td class="days">F</td><td class="days">Sa</td></tr><tr>';
   
   var daysThisMonth = getDaysInMonth(month,year);
   var weekDay = 0;
   var lastDay = getDaysInMonth(pMonth,pYear);
   
   //this loop is used to create the calendar
   for(var i = 1; i<=daysThisMonth; i++)
   {
      var currDate = new Date();
      currDate.setFullYear(year,month-1,i);
      day = currDate.getDay();


//starting a new week
      if(weekDay == 7)
      {
         weekDay=0;
calCode += '</tr><tr>';
      }
      
      //checking if the current weekDay in the loop matches with the current weekday of the month
      //if so print that day, else we print the previous month's date
      if(day==weekDay)
      {
         calCode += '<td><a href="http://www.mspmag.com/entertainment/calendar/default.asp?date='+year +'/'+month+'/'+i+'">'+i+'</a></td>';
      }
      else
      {
         var dDay = weekDay;
         dDay++;
         var lastMonthDay = lastDay-(day-dDay);

         calCode += '<td class="nextMonth"><a href="http://www.mspmag.com/entertainment/calendar/default.asp?date='+pYear+'/'+pMonth+'/'+lastMonthDay+'">'+lastMonthDay+'</a></td>';

         i--;
      }
      //aeCalendar.js Script Written by Justin Jeffress
      //justin.jeffress@crownpeak.com
      //checking if it's the last day of the month, and if there is an incomplete
      //week that needs to be filled with the first j days of the next month
      if(i==daysThisMonth && weekDay!=6)
      {  
         var j = 1;
         while(weekDay!=6)
         {

            calCode += '<td class="nextMonth"><a href="http://www.mspmag.com/entertainment/calendar/default.asp?date='+nYear+'/'+nMonth+'/'+j+'">'+j+'</a></td>';

            weekDay++;
            j++;
         }
      }
      weekDay++;
   }
   calCode += '</table>';

   document.getElementById('calendar').innerHTML = calCode;
   
   //if(drawFirst == 1)
   //{
   //   dayView(year,month,1);
   //}
}


//this function returns the name of the month given a number
function getMonth(month)
{   
   return months[month];
}

//this function returns the name of the weekday given a number
function getDay(day)
{
   return days[day];
}

//this function retrieves the number of days in a month given an month and a year
//it also takes into account a leap year.
function getDaysInMonth(month,year)
{
   if ((month-1==1)&&(year%4==0)&&((year%100!=0)||(year%400==0)))
   {
      return 29;
   }
   else
   {
      return daysInMonth[month-1];
   }
}
