var months = new Array();
months[0] = "January";
months[1] = "February";
months[2] = "March";
months[3] = "April";
months[4] = "May";
months[5] = "June";
months[6] = "July";
months[7] = "August";
months[8] = "September";
months[9] = "October";
months[10] = "November";
months[11] = "December";

var monthDayCount = new Array();
monthDayCount[0] = 31;
monthDayCount[1] = 28;
monthDayCount[2] = 31;
monthDayCount[3] = 30;
monthDayCount[4] = 30;
monthDayCount[5] = 31;
monthDayCount[6] = 31;
monthDayCount[7] = 31;
monthDayCount[8] = 30;
monthDayCount[9] = 31;
monthDayCount[10] = 30;
monthDayCount[11] = 31;

var now = new Date();
var nowYear = now.getFullYear();
	       
var globalMonth = 0;

function isLeapYear(year)
{
	year = parseInt(year);

	if(year%4 == 0)
	{
		if(year%100 != 0)
		{
			return true;
		}
		else
		{
			if(year%400 == 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
    
    return false;
}



//function loadCurrentMonth()
//{
//   //moveMonth(now.getMonth());
//}
//            
//function moveMonth(month)
//{
//    
//    
//    //if(month < 12 && month > -1)
//	//{
//	    if(month == -1)
//	    {
//	        nowYear = nowYear - 1;
//	        month = 11;
//	    }
//	    
//	    if(month == 12)
//	    {
//	        nowYear = nowYear + 1;
//	        month = 0;
//	    }
//	    
//	    if(month == 1)
//	    {
//	        if(isLeapYear(nowYear))
//	        {
//	            monthDayCount[1] = 29;
//	        }
//	        else
//	        {
//	            monthDayCount[1] = 28;
//	        }
//	    }
//	    
//	    globalMonth = month;
//	                                        
//	    var dt = new Date();
//	    dt.setFullYear(nowYear);
//	    dt.setMonth(month,1);
//	    
//	    var dayOffSet = dt.getDay();
//				
//	    for(var x=0; x<dayOffSet; x++)
//	    {
//		    var cell = document.getElementById("d" + x);
//		    cell.innerText = "";
//		    cell.bgColor = "#ffffff";
//            cell.innerText = " ";
//            cell.borderColor = "#ece9d8";
//        }
//                
//	    for(var x=0; x<monthDayCount[month]; x++)
//	    {
//		    var dayNum = dayOffSet + x;
//		    var cellId = "d" + dayNum;
//    	    var cell = document.getElementById(cellId);

//		    //if(cell != null)
//		    //{
//			    cell.innerText = x + 1;
//			    cell.bgColor = "#ffffff";
//			    cell.borderColor = "#b4bfd6";
//			    cell.setAttribute("className","date");
//		    //}
//	    }
//	
//	    for(var x=monthDayCount[month] + dayOffSet; x<42; x++)
//	    {
//		    var cell = document.getElementById("d" + x);
//		    cell.innerText = "";
//		    cell.bgColor = "#ffffff";
//		    cell.innerText = " ";
//		    cell.borderColor = "#ece9d8";
//	    }		
//			
//		//if(globalMonth == now.getMonth())
//		//{
//	    //    var str = "d" + (now.getDate() + dayOffSet - 1);
//	     //   var cell = document.getElementById(str);
//	     //   cell.bgColor = "#f8e1e6";
//	       
//	    //}
//                
//	    var headerCell = document.getElementById("calHeader");
//	    headerCell.innerHTML = "<a style=\"cursor:hand;\" onclick=\"moveMonth(globalMonth - 1);\"><<</a>&nbsp; &nbsp; &nbsp;" + months[month] + "&nbsp; &nbsp; &nbsp<a style=\"cursor:hand\" onclick=\"moveMonth(globalMonth + 1);\">>></a>";
//	   
//        var infoHeaderCell = document.getElementById("calInfoHeader");
//        infoHeaderCell.innerText = months[month] + " " + nowYear;
//        
//	    loadEntries(globalMonth, dayOffSet, nowYear);
//	    
//	//}
//	
//	
//}

function loadEntries(month, dayOffSet, year)
{
    var actMonth = month + 1;
    
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.load("xml/calendar.xml");
	    
    var rootNode = xmlDoc.documentElement;
    var years = rootNode.getElementsByTagName("YEAR");
	
	//clear existing entries
	var calInfoTable = document.getElementById("calendarInfoTable");
    
    var numRows = calInfoTable.rows.length;
    
    for(var rowCnt = 1; rowCnt < numRows; rowCnt++)
    {
        calInfoTable.deleteRow(1);
    }
	    
    for(var yearCnt=0; yearCnt<years.length; yearCnt ++)
    {
        if(parseInt(years[yearCnt].getAttribute("num")) == year)
        {
            var monthNodes = rootNode.getElementsByTagName("MONTH");
    
            for(var x=0; x<monthNodes.length; x++)
            {
                if(parseInt(monthNodes[x].getAttribute("num")) == (month + 1))
                {
                    var dates = monthNodes[x].getElementsByTagName("DATE");
                    
                    for(var y=0; y<dates.length; y++)
                    {
                        //Update cells on the calendar
                        var idNum = "d" + (parseInt(dates[y].getAttribute("num")) + dayOffSet - 1);
                        var cell = document.getElementById(idNum);
	                    cell.setAttribute("className","dateEntry");   
	                    
	                    //Add info
	                    var dateCell = calInfoTable.insertRow().insertCell(0);
                        dateCell.innerHTML = "<td>" + months[month] + " " + dates[y].getAttribute("num") + "</td>";
                        dateCell.setAttribute("className","calendarInfoDate");
                        
                        var entries = dates[y].getElementsByTagName("ENTRY");
                        
                        for(var z=0; z<entries.length; z++)
                        {
                           var titleCell = calInfoTable.insertRow().insertCell(0);
                           titleCell.innerHTML = "<td>" + entries[z].childNodes[0].text + "</td>";
                           titleCell.setAttribute("className","calendarInfoTitle");
                           
                           var descCell = calInfoTable.insertRow().insertCell(0);
                           descCell.innerHTML = "<td>" + entries[z].childNodes[1].text + "</td>";
                           descCell.setAttribute("className","calendarInfoDescription");
                           
                           calInfoTable.insertRow().insertCell(0).innerHTML = "<BR>";
                        }
                    } 
            
                    break;        
                }
            }
	            
            break;
       }    
   }
	    
}