///////////////////////////////////////////////////////////////////////
// Draw Month (Zero based)
///////////////////////////////////////////////////////////////////////
function LeapYear(year) 
{
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    
    return true;
}

function DrawMonthTable(n_InMonth, n_InYear) 
{
	var o_Now = new Date();

    var n_Day = 1;
	var n_Row = 1;
	var n_Buffer = "";
	var s_Class = "";
	
	var arr_Months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	
	var arr_MonthDaysNormal = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var arr_MonthDaysLeap = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	var d_Year = new Date();
	
	d_Year.setYear(n_InYear);
	d_Year.setDate(1);
	d_Year.setMonth(n_InMonth);
	
	var n_MonthDays = (LeapYear(d_Year.getFullYear())) ? parseInt(arr_MonthDaysLeap[d_Year.getMonth()]) : parseInt(arr_MonthDaysNormal[d_Year.getMonth()]);
	var n_Month = parseInt(d_Year.getMonth());
	var n_Year = parseInt(d_Year.getFullYear());
	
	// Get the first Day. If it's a sunday, change it to day 7 rather than day 0
	var n_StartDay = parseInt(d_Year.getDay());
	n_StartDay = (n_StartDay == 0) ? 7 : n_StartDay; 
	 
	var prevLink = (n_Month == 0) ? "11," + (n_Year-1) : (n_Month-1) + "," + n_Year;
	var nextLink = (n_Month == 11) ? "0," + (n_Year+1) : (n_Month+1) + "," + n_Year;
	
	var rows = 0;
	
	// Write month table header
    n_Buffer += "<table id=\"month\">";
	n_Buffer += "<tr><th colspan=\"7\" class=\"month\"><a title=\"Previous Month\" href=\"#\" onclick=\"DrawMonthTable(" + prevLink + "); return false;\">&laquo;</a> " + arr_Months[n_Month] + " " + n_Year + " <a title=\"Next Month\" href=\"#\" onclick=\"DrawMonthTable(" + nextLink + "); return false;\">&raquo;</a></th></tr>";
	n_Buffer += "<tr>";
    n_Buffer += "<th>M</th>";
    n_Buffer += "<th>T</th>";
    n_Buffer += "<th>W</th>";
    n_Buffer += "<th>T</th>";;
    n_Buffer += "<th>F</th>";
    n_Buffer += "<th>S</th>";
    n_Buffer += "<th>S</th>";
    n_Buffer += "</tr>";
    n_Buffer += "<tr>";
	
	// If it's not a Monday, create blanks until we get to the right column
	if(n_StartDay > 1)
	{
        for(var x = 0; x < (n_StartDay - 1); x++) 
       		n_Buffer += "<td>&nbsp;</td>";
    }	
	 
	// Create the rest of the first row (if any)
    for(var x = n_StartDay; x < 8; x++) 
	{	
        n_Buffer += "<td id=\"" + n_Day + "/" + (n_Month + 1) + "/" + n_Year + "\">" + n_Day + "</td>";
        n_Day++;
	}
	
	n_Buffer += "</tr>";
	
	rows++;
	 
    while(n_Day < (n_MonthDays + 1)) 
	{
		n_Buffer += "<tr>";
		 
        for(var x = 0; x < 7; x++) 
		{
			if(n_Day < (n_MonthDays + 1))
			{
				n_Buffer += "<td id=\"" + n_Day + "/" + (n_Month + 1)+ "/" + n_Year + "\">" + n_Day + "</td>";
	        
	        	n_Day++;
        	}
        	else
        	{
        		n_Buffer += "<td>&nbsp;</td>";
        	}
        }
		 
	    n_Buffer += "</tr>";
	    rows++;
    }

	if(rows < 6)
		n_Buffer += "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
	
	 n_Buffer += "</table>";
	 
	 
	 
	 document.getElementById('calendar').innerHTML = '';
	 document.getElementById('calendar').innerHTML = n_Buffer;
	 
	 var table = document.getElementById('month');
	 
	 var cells = table.getElementsByTagName('td');
	 
	 for(var x=0; x<cells.length; x++)
	 {
	 	 cells[x].style.backgroundColor = '#CAC48A';
	 	 
	 	 if(cells[x].id) cells[x].onclick = function() { ResetAllCells(); this.style.backgroundColor = '#FD843C'; document.forms['search_form'].calendardate.value = this.id; return false; }
	 }
	 	 	
	 	 	
	 //alert(cells[x].innerHTML);
}

function ResetAllCells()
{
	 var table = document.getElementById('month');
	 var cells = table.getElementsByTagName('td');
	 
	 for(var x=0; x<cells.length; x++)
	 	 cells[x].style.backgroundColor = '#CAC48A';
}

///////////////////////////////////////////////////////////////////////