//the ajax xml object
var xmlhttp=null;
//the xml file that will be loaded into memory
//
//var toLoad = "/includes/js/aeCalendar.xml";
//var toLoad = "/entertainment/calendar/2011_2.xml";

var ldate = new Date();
var toLoad =  "/entertainment/calendar/"+ldate.getFullYear()+"_"+(ldate.getMonth()+1)+".xml";



var xmlDoc;
//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 lookHere = "/entertainment/calendar/default.asp";

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]

//this method sets up the xml object to work with the javascript depending on the browser
function sendRequest()
{  
   //setting up the firefox 1.5 ,safari 1.3, and opera 8.5 way
   if (window.XMLHttpRequest)
   {
      xmlhttp=new XMLHttpRequest();
   }
   //if that fails try set up for IE
   else if (window.ActiveXObject)
   {
      try
      {
         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(e)
      {
         try
         {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch(e)
         {
            xmlhttp = false;
         }
      }
   }
   //if the ajax object was set up properly then download and open the xml file in the 'toLoad' variable
   if (xmlhttp)
   {
      //setting up the eventhandler 
      xmlhttp.onreadystatechange=onReadyState;
      //opening the xml file
      xmlhttp.open("GET", toLoad, true);
      //no need to send anything back to the server
      xmlhttp.send(null);
   }
}

function onReadyState()
{
   if (xmlhttp.readyState==4)
   {
      if (xmlhttp.status==200)
      {

         xmlDoc = xmlhttp.responseXML;
         
         try
         {
            var query = window.location.search.substring(1);
            var pair = query.split("=date");
         
            var rawDate = pair[1].split("/");
            //dayView(rawDate[0],rawDate[1],rawDate[2]);
            drawCal(rawDate[1],rawDate[0]);
         }
         catch(e)
         {
            var today = new Date();
            var year = today.getFullYear();
            var month = today.getMonth()+1;
            var day = today.getDate();
            
            //dayView(year,month,day);
            drawCal(month,year,0);
         }
      }
   }
}



function drawMonth(month,year,drawFirst)
{
   document.getElementById('calendar').innerHTML = "Loading. . .";
   reloadXML("/entertainment/calendar/"+year+"_"+month+".xml");
   drawCal(month,year,drawFirst);
}

//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)
      {
         if(checkForEvent(year,month,currDate.getDate()))
         {
            calCode += '<td><a href="'+lookHere+'?date='+year+'/'+month+'/'+i+'">'+i+'</a></td>';
         }
         else
         {
            //calCode += '<td><a href="?date='+year+'/'+month+'/'+i+'">'+i+'</a></td>';
            calCode += '<td>'+i+'</td>';
         }
      }
      else
      {
         var dDay = weekDay;
         dDay++;
         var lastMonthDay = lastDay-(day-dDay);
         //if(checkForEvent(pYear,pMonth,lastMonthDay))
         //{
         //   calCode += '<td class="nextMonth"><a href="javascript:dayView('+pYear+','+pMonth+','+lastMonthDay+')">'+lastMonthDay+'</a></td>';
         //}
         //else
         //{
            calCode += '<td class="nextMonth">'+lastMonthDay+'</td>';
         //}
         i--;
      }
      
      //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)
         {
            //if(checkForEvent(nYear,nMonth,j))
            //{
            //   calCode += '<td class="nextMonth"><a href="javascript:dayView('+nYear+','+nMonth+','+j+')">'+j+'</a></td>';
            //}
            //else
            //{
               calCode += '<td class="nextMonth">'+j+'</a></td>';
            //}
            weekDay++;
            j++;
         }
      }
      weekDay++;
   }
   calCode += '</table>';
   //calCode += '<br />';
   //calCode += '<br /><br />Sponsored by: <br /><img src="/images/graves_164px.gif">';

   document.getElementById('calendar').innerHTML = calCode;
   
}

function checkForEvent(year,month,day)
{

    if(xmlDoc == null)
       return false;
         
    var startDates  = xmlDoc.getElementsByTagName("startDate");
    var endDates    = xmlDoc.getElementsByTagName("endDate");
     
    for(var i = 0; i < startDates.length; i++)
    {
       var syear  = startDates[i].attributes.getNamedItem("year").value;
       var smonth = startDates[i].attributes.getNamedItem("month").value;
       var sday   = startDates[i].attributes.getNamedItem("day").value;
       
       var eyear  = endDates[i].attributes.getNamedItem("year").value;
       var emonth = endDates[i].attributes.getNamedItem("month").value;
       var eday   = endDates[i].attributes.getNamedItem("day").value;
       
       var startDate = new Date();
       var endDate = new Date();
       var checkDate = new Date();
       
       startDate.setFullYear(syear,smonth-1,sday);
       endDate.setFullYear(eyear,emonth-1,eday);
       checkDate.setFullYear(year,month-1,day);
       
       if(syear == year && smonth == month && sday == day)
          return true;
       else if(eyear == year && emonth == month && eday == day)
          return true;
       else if(startDate < checkDate && checkDate < endDate)
          return true;
    }
    return false;
}

//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];
   }
}

function reloadXML(url)
{ 
   if (xmlhttp!=null)
   {
      xmlhttp.onreadystatechange=state_Change;
      xmlhttp.open("GET",url,false);
      xmlhttp.send(null);
   }
   else
   {
      alert("Your browser does not support XMLHTTP.");
   }
   
   if (xmlhttp.status==200)
   {
      xmlDoc = null;
      xmlDoc = xmlhttp.responseXML;
      
      return true;
   }
   else
   {
      //alert("Problem retrieving XML data");
      return false;
   }
}

function state_Change()
{
   if (xmlhttp.readyState==4)
   {
   }
}
