//---------Calendar Functions
function buildCalendar() {
	//document.write("<h1>Calendar</h1>");
	curDate = new Date();
	
	//curDate.setDate(curDate.getDate()-(curDate.getDate()));
	//makeCalendar(curDate, curDate.getFullYear(), curDate.getMonth(), curDate.getDate(), curDate.getDay(), false);
	curDate = new Date();
	makeCalendar(curDate, curDate.getFullYear(), curDate.getMonth(), curDate.getDate(), curDate.getDay(), true);
	//curDate.setDate(curDate.getDate()+((getDaysInMonth(curDate.getMonth(), curDate.getFullYear())-curDate.getDate())+1));
	//makeCalendar(curDate, curDate.getFullYear(), curDate.getMonth(), curDate.getDate(), curDate.getDay(), false);
}
function makeCalendar(fullDate, year, month, date, day, currentMonth) {
	//find where the first day of the month landed
		firstDay = getFirstDay(date, day);
	
	//we need to know how many days are in this month
		daysNum = getDaysInMonth(month, year);
	
	//we need to know how many weeks are in this month, to write the correct amount of rows
		weekNum = getWeekNum(daysNum, firstDay);
		
	//write the top of the calendar - this part is static
		var calTop = "<table cols='7' cellspacing='0'>\n";
		calTop += ("<caption>"+findMonthName(month)+" "+year+"</caption>\n");
		calTop += "<thead>\n";
		calTop += "<tr>\n";
		calTop += "<th>Sun</th>\n";
		calTop += "<th>Mon</th>\n";
		calTop += "<th>Tue</th>\n";
		calTop += "<th>Wed</th>\n";
		calTop += "<th>Thu</th>\n";
		calTop += "<th>Fri</th>\n";
		calTop += "<th>Sat</th>\n";
		calTop += "</tr>\n";
		calTop += "</thead>\n";
		calTop += "<tbody>\n";
	
	document.write(calTop);
	myDay = 1;
	
	for(i=0;i<weekNum;i++) {
		document.write("<tr>");
		
		for(j=0;j<7;j++) {
			if(myDay==1 && j<firstDay || myDay>daysNum){
				document.write("<td ");
				decideID(i, j, weekNum);
				document.write("&nbsp;</td>");
				//alert("Date " + myDay + " used option 1");
			}
			else if(j==firstDay && myDay==date) {				//checks for writing current date and if that date is the first day of the month
				if(currentMonth==true) {
					document.write("<td id='today' ");
					decideID(i, j, weekNum);
					document.write(myDay+"</td>");
				}					
				else {
					document.write("<td ");
					decideID(i, j, weekNum);
					document.write(myDay+"</td>");
				}
				//alert("Date " + myDay + " used option 2");
				myDay++;
			}
			else if(myDay==1 && j==firstDay && date!=1) {
				document.write("<td ");
				decideID(i, j, weekNum);
				document.write(myDay+"</td>");
				//alert("Date " + myDay + " used option 3");
				myDay++;
			}
			else if(date!=1 && myDay==date) {
				if(currentMonth==true) {
					document.write("<td id='today' ");
					decideID(i, j, weekNum);
					document.write(myDay+"</td>");
				}					
				else {
					document.write("<td ");
					decideID(i, j, weekNum);
					document.write(myDay+"</td>");
				}
				//alert("Date " + myDay + " used option 4");
				myDay++;
			}
			else if(myDay>1 && myDay<=daysNum) {
				document.write("<td ");
				decideID(i, j, weekNum);
				document.write(myDay+"</td>");
				//alert("Date " + myDay + " used option 5");
				myDay++;
			}
		}
		document.write("</tr>");
	}
	document.write("</tbody>\n");
	document.write("</table>");
}
function getWeekNum(daysNum, firstDay) {
	if( Math.round((daysNum+firstDay)/7)*(7-firstDay) < daysNum )
		return( Math.ceil((daysNum+firstDay)/7) );
	else
		return( Math.round((daysNum+firstDay)/7) );
}
function getDaysInMonth(m, y) {	
	if(m==1 && y%4==0)
		return(29);
	else if(m==1)
		return(28);
	else if(m==3 || m==5 || m==8 || m==10)
		return(30);
	else
		return(31);
}
function getFirstDay(date, day) {	//Find the first day of the week in any given month
	thatFriday = (6-day)+date;
	firstDay = ((thatFriday%7)-1)+(-1*(6));
	//firstDay = ((date%7)-1)+(-1*(day));
	
	if(firstDay<0)
		firstDay = firstDay*-1;
	
	if(firstDay>=7)
		firstDay = firstDay-7;
	
	return(firstDay);
}
function getCurrentDay(date, firstDay) {
	
}
function decideID(i, j, weekNum) {
	if(j==6 && i==weekNum-1) {				//bottom right corner
		document.write("id='rbside'>");
	}
	else if(j==6 && i!=weekNum-1)			//right side only
		document.write("id='rside'>");
	else if(i==weekNum-1)					//bottom side only
		document.write("id='bside'>");
	else { 									//no ID
		document.write(">");
		//alert("No Id");
	}
}
function findMonthName(m) {
	if(m==0)
		return("January");
	else if(m==1)
		return("February");
	else if(m==2)
		return("March");
	else if(m==3)
		return("April");
	else if(m==4)
		return("May");
	else if(m==5)
		return("June");
	else if(m==6)
		return("July");
	else if(m==7)
		return("August");
	else if(m==8)
		return("September");
	else if(m==9)
		return("October");
	else if(m==10)
		return("November");
	else if(m==11)
		return("December");
}
buildCalendar();