var globalBaseLink;
// Get the date
var bnetCal_curdate = new Date();
bnetCal_curMonth = bnetCal_curdate.getMonth() + 1;
bnetCal_curYear = bnetCal_curdate.getFullYear();

var pastTestDate;
var weekdaysOnly;

var todaysDate = new Date();
todaysDateNum = todaysDate.getDate();
// array to hold final source
var bnetCal_source = [];

var bnetCal_monthNames = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];

var bnetCal_dayNames = [
'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'
];


function bnetCal_create(m, y) {
	// we need to clear the source before we make a new calendar
	bnetCal_source = [];
	
	// make top of calendar table
	bnetCal_source.push("<table width='100%' cellpadding='0' cellspacing='0' class='bNetCal'><tr><th><a href='#' onclick='bnetCal_prevMonth(); return false;'>&laquo;</a></th><th colspan='4'>" + bnetCal_monthNames[m - 1] +" " + y + "</th><th><a href='#' onclick='bnetCal_nextMonth(); return false;'>&raquo;</a></th><th><a href='#' id='bNetCal_close' onclick='bnetCal_Hide(); return false;'>X</a></th></tr><tr>");
	
	// add day column headers
	for (i = 0; i < 7; i ++) {
		bnetCal_source.push("<td class='calDayHeaders'>"+bnetCal_dayNames[i]+"</td>");
	}
	//get starting point for calendar cells
	bnetCal_curdate.setMonth(m - 1);
	bnetCal_curdate.setFullYear(y);
	bnetCal_curdate.setDate(1);
	
	//set correct number of days depending on month
	if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
		days = 31;
	} else if (m == 4 || m == 6 || m == 9 || m == 11) {
		days = 30;
	} else {
		days = (y % 4 == 0) ? 29 : 28;
	}
	// get first day
	var firstDay = bnetCal_curdate.getDay();

	if(m < 10)
		m = "0"+m;	
	
	// start weeks row
	bnetCal_source.push("</tr><tr>");
	// if the first day isnt sunday we make a spacer cell to fill in the week up until the first day
	if (firstDay != 0) 
		bnetCal_source.push("<td class='spacerBlock' colspan='" + firstDay + "'></td>");
	for (i = 0; i < days; i ++) {
		// check for end of week, if true start new row
		if (firstDay == 0) {
			bnetCal_source.push("</tr><tr>");
		}
		// next we make a test date to see if each outputted day is in the future or not, we also check to see if it is today
		var futureTestDate = new Date(y,(m-1),(i+1));
		
		if(!pastTestDate)
			pastTestDate = new Date(2000,0,1);
		
		/*pastTestDate = */
		
		if((i+1) < 10)
			preI = "0";
		else
			preI = "";
		
		if(y == todaysDate.getFullYear() && (m-1) == todaysDate.getMonth() && (i+1) == todaysDate.getDate()) {
			if(weekdaysOnly == true) {
				if(firstDay == 0 || firstDay == 6)
					bnetCal_source.push("<td class='todaysDate'>" + (i + 1) + "</td>");
				else
					bnetCal_source.push("<td class='todaysDate'><a href='"+globalBaseLink + y + "" + m + "" + preI + (i + 1) + "'>" + (i + 1) + "</a></td>");
		}
		else
			bnetCal_source.push("<td class='todaysDate'><a href='"+globalBaseLink + y + "" + m + "" + preI + (i + 1) + "'>" + (i + 1) + "</a></td>");
		}
		else if(futureTestDate > todaysDate || pastTestDate >= futureTestDate)
			bnetCal_source.push("<td class='futureDate'>" + (i + 1) + "</td>");
		else {
			if(weekdaysOnly == true) {
				if(firstDay == 0 || firstDay == 6)
					bnetCal_source.push("<td>" + (i + 1) + "</td>");
				else
					bnetCal_source.push("<td><a href='"+globalBaseLink + y + "" + m + "" + preI + (i + 1) + "'>" + (i + 1) + "</a></td>");
			}
			else
				bnetCal_source.push("<td><a href='"+globalBaseLink + y + "" + m + "" + preI + (i + 1) + "'>" + (i + 1) + "</a></td>");
		}
		if((i + 1) == days) {
			var colSpan = 7 - (firstDay + 1);
			if(colSpan>0)
				bnetCal_source.push("<td class='spacerBlock' colspan='"+colSpan+"'>&nbsp;</td>")
		}
		// add the day cell
		
		//increment and continue
		firstDay ++;
		firstDay %= 7;
	}
	// Do the footer
	bnetCal_source.push("</tr></table>");
	//output source and clean
	$('#bNetCal_src').html(bnetCal_source.join(""));
	bnetCal_source = [];
}

// function to show the calendar, this is the function attached to DOM element event
function bnetCal_show(requestedYear, requestedMonth, requestedDay,baseLink, pastDate, ifWeekdaysOnly) {
	weekdaysOnly = ifWeekdaysOnly
	if(pastDate) {
		pastDateVals = pastDate.split(",");
		pastTestDate = new Date(pastDateVals[0], pastDateVals[1], pastDateVals[2]);
	}
	globalBaseLink = baseLink;
	if(!requestedMonth && !requestedYear && !requestedDay) {
		// Make a new date, and set the current month and year.
		var bnetCal_date = new Date();
		bnetCal_curMonth = bnetCal_date.getMonth() + 1;
		bnetCal_curYear = bnetCal_date.getFullYear();
	} else {
		bnetCal_curMonth = requestedMonth;
		bnetCal_curYear = requestedYear;
		
	}
	// build calendar
	bnetCal_create(bnetCal_curMonth, bnetCal_curYear);
	// show calendar
	$('#bNetCal_box').show();
}

// Hide the calendar. separate function because it can be called from within the page.
function bnetCal_Hide() {
	$('#bNetCal_box').hide();
}

// Moves to the next month...
function bnetCal_nextMonth() {
	// Increase the current month.
	bnetCal_curMonth ++;
	// We have passed December, let's go to the next year.
	// Increase the current year, and set the current month to January.
	if (bnetCal_curMonth > 12) {
		bnetCal_curMonth = 1; 
		bnetCal_curYear++;
	}
	// build new calendar.
	bnetCal_create(bnetCal_curMonth, bnetCal_curYear);
}

// Moves to the previous month...
function bnetCal_prevMonth() {
	bnetCal_curMonth = bnetCal_curMonth - 1;
	// We have passed January, let's go back to the previous year.
	// Decrease the current year, and set the current month to December.
	if (bnetCal_curMonth < 1) {
		bnetCal_curMonth = 12; 
		bnetCal_curYear = bnetCal_curYear - 1;
	}
	// build new calendar.
	bnetCal_create(bnetCal_curMonth, bnetCal_curYear);
}

// Moves to the next year...
function bnetCal_nextYear() {
	// Increase the current year.
	bnetCal_curYear++;
	// build new calendar.
	bnetCal_create(bnetCal_curMonth, bnetCal_curYear);
}

// Moves to the previous year...
function bnetCal_prevYear() {
	// Decrease the current year.
	bnetCal_curYear = bnetCal_curYear - 1;
	// build new calendar.
	bnetCal_create(bnetCal_curMonth, bnetCal_curYear);
}