/*******************************************************************************************
* inline Calendar - By Nathan DeJong for AccessDubuqe.com
* The basic drawing of calendar based off of Basic Calendar
* 3 files required
* <filename>.css - CSS style Sheet for look of Calendar, named different for different looks
* calendar.js - all functions for calendar
* <your page> - page you want calendar on - must have <span class="calendar"></span> on it
*
* Basic Calendar-By Brian Gosselin at http://scriptasylum.com/bgaudiodr/
* Script featured on Dynamic Drive (http://www.dynamicdrive.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
*******************************************************************************************/

// Variable for current day
var todaydate = new Date();
var curmonth  = todaydate.getMonth()+1;  //get current month (1-12)
var curyear   = todaydate.getFullYear(); //get current year

// Draws calendar
function openCalendar(c,l){
	document.getElementById(c).innerHTML = buildCal(curmonth,curyear,c,l);
}

// Flips calendar to Previous Month
function prevMonth(c,l){
	if (curmonth == 1) {curmonth = 12; curyear -= 1;}
	else curmonth -= 1;
	//document.getElementById(c).innerHTML = buildCal(curmonth,curyear,c,l);
	changeCal(curmonth,curyear,c,l);
}

// Flips calendar to Next Month
function nextMonth(c,l){
	if (curmonth == 12) {curmonth = 1; curyear += 1;}
	else curmonth += 1;
	//document.getElementById(c).innerHTML = buildCal(curmonth,curyear,c,l);
	changeCal(curmonth,curyear,c,l);
}

// actual code for drawing calender
function buildCal(m,y,c,l){
	var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
	var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

	var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
	oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

	var todaydate=new Date() //DD added
	var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added
		dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
	
	var t='<table class="main" cols="3" cellpadding="0" border="0" cellspacing="0"><tr align="center">';
	t+='<td align="left" class="month"><a href="javascript:prevMonth(\''+c+'\',\''+l+'\');">&laquo;<\/a><\/td>';
	t+='<td colspan="1" align="center" class="month" id="monthchange">'+mn[m-1]+' - '+y+'<\/td>';
	t+='<td align="right" class="month"><a href="javascript:nextMonth(\''+c+'\',\''+l+'\');">&raquo;<\/a><\/td>';
	t+='<\/tr><tr><td colspan="3" style="padding:0px;"><div id="dayschange"><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr align="center">';
	for(s=0;s<7;s++)t+='<td class="daysofweek">'+"SMTWTFS".substr(s,1)+'<\/td>';
	t+='<\/tr><tr align="center">';
	for(var ic=1;ic<=42;ic++){
		var x=((ic-oD.od>=0)&&(ic-oD.od<dim[m-1]))? ic-oD.od+1 : '&nbsp;';
		if (x!='&nbsp;'){
			if(x==scanfortoday)
				x='<a href="'+l+'?date='+y+'-'+m+'-'+x+'" id="today">'+x+'<\/a>';
			else
				x='<a href="'+l+'?date='+y+'-'+m+'-'+x+'">'+x+'<\/a>';
		}
		if (x=='&nbsp;')
			t+='<td class="daysblank">'+x+'<\/td>';
		else 
			t+='<td class="days">'+x+'<\/td>';
		if(((ic)%7==0)&&(ic<36))t+='<\/tr><tr align="center">';
	}
	t+='<\/tr></table></div></td></tr><tr><td colspan="3" align="center" class="daysofweek">&nbsp;<\/td><\/tr><\/table>';
	return t;
}

function changeCal(m,y,c,l){
	var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
	var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

	var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
	oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

	var todaydate=new Date() //DD added
	var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added
		dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
	
	
	document.getElementById("monthchange").innerHTML = mn[m-1]+' - '+y;
	
	var t='<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr align="center">';
	for(s=0;s<7;s++)t+='<td class="daysofweek">'+"SMTWTFS".substr(s,1)+'<\/td>';
	t+='<\/tr><tr align="center">';
	for(var ic=1;ic<=42;ic++){
		var x=((ic-oD.od>=0)&&(ic-oD.od<dim[m-1]))? ic-oD.od+1 : '&nbsp;';
		if (x!='&nbsp;'){
			if(x==scanfortoday)
				x='<a href="'+l+'?date='+y+'-'+m+'-'+x+'" id="today">'+x+'<\/a>';
			else
				x='<a href="'+l+'?date='+y+'-'+m+'-'+x+'">'+x+'<\/a>';
		}
		if (x=='&nbsp;')
			t+='<td class="daysblank">'+x+'<\/td>';
		else 
			t+='<td class="days">'+x+'<\/td>';
		if(((ic)%7==0)&&(ic<36))t+='<\/tr><tr align="center">';
	}
	t+='<\/tr></table>';
	
	document.getElementById("dayschange").innerHTML = t;
}
