// This is code for a pop-up month calendar. The user can scroll through months. When the user clicks
// a date, that date is passed to the form fields specified.
calPopAdder=new Array(0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5);					// Used to determine the first day of the month
calPopNumDays=new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);	// # days/month

/* popUpCalInit(m, d, y)
	m=ID of the form element containing the month value
	y=ID of the year
	d=ID of the date
*/
function popUpCalInit(m, d, y) {	// Call this to make the calendar appear
	calPopMonthID=m;				// Start with the date passed from the form
	calPopDateID=d;
	calPopYearID=y;
	// These next 5 values are based on the graphics provided to the code
	calPopStartYear=2008;	// The first year in the graphic: so I can't try to scroll beyond
	calPopEndYear=2012;		// The last year in the graphic
	calPopHeight=14;		// The height offset for the year and month bars
	calPopWidth=20;			// The width offset for the date bar
	calPopDateHeight=84;	// The height of each date bar (31 days, 30 days, 29, 28)
	calPopYear=Number(document.getElementById(y).value);		// Get the current year per what's set in the form
	calPopMonth=Number(document.getElementById(m).value)-1;	// Get the current month
	calPopMoveMonth();									// Set up the calendar to point at this month
	if(popUpCalInit.arguments.length==4) {
		document.getElementById("calPopUp").style.top=(document.getElementById(popUpCalInit.arguments[3]).offsetTop)+"px";
	} else {
		document.getElementById("calPopUp").style.top="400px";
		document.getElementById("calPopUp").style.left="300px";
	}
	document.getElementById("calPopUp").style.display="block";	// display the calendar
}

function calPopUpChangeMonth(i) {		// I get here if I've clicked on Prev (-1) or Next (1)
	calPopMonth+=i;						// Add the month index
	if (calPopMonth==-1) {				// This means I've gone Jan -> Dec	
		if(calPopYear>calPopStartYear) {	// If I have the graphics to change the year
			calPopYear--;					// Change the year
			calPopMonth=11;					// 11=December (months are counted from 0 by javascript)
		} else calPopMonth=0;				// Else stay stuck at January of the first year
	}
	if (calPopMonth==12) {					// Attempt to go Dec -> Jan
		if(calPopYear<calPopEndYear) {		// If I have the graphics
			calPopYear++;					// Increment year
			calPopMonth=0;					// 0=January
		} else calPopMonth=11;				// Otherwise I'm stuck in December of the last year

	}
	calPopMoveMonth();						// Set up the calendar to display the new month
}

function calPopMoveMonth() {
	// The month and year are in graphic sliders that must be offset by -calPopHeight*index
	document.getElementById("calPopUpMonthBar").style.top="-"+(calPopHeight*calPopMonth)+"px";
	document.getElementById("calPopUpYearBar").style.top="-"+(calPopHeight*(calPopYear-calPopStartYear))+"px";
	
	// The date bar slides left to right depending on what day is the first of the month
	// The date bar slides up and down depending on if the month has 31, 30, 29 or 28 days
	document.getElementById("calPopUpDateBar").style.left="-"+(120-calPopWidth*calPopWhatDate(calPopMonth, calPopYear))+"px";
	document.getElementById("calPopUpDateBar").style.top="-"+((31-(calPopNumDays[calPopMonth])-(((calPopYear%4)==0 && (calPopMonth==1))?1:0))*84)+"px";
	calPopFind1=calPopWhatDate(calPopMonth, calPopYear);
	calPopFind31=(calPopNumDays[calPopMonth])+(((calPopYear%4)==0 && (calPopMonth==1))?1:0);
}

function calPopWhatDate(month, year) {	// Given month+year, what day is the 1st of the month?
	// Remember that month is indexed from zero, so Jan=0, Dec=11; date is fixed at 1
	// This algorithm won't work in the year 2100. I probably won't be around to see this
	return (((year+1-2007)+Math.floor((year-2005)/4)+calPopAdder[month]+(((year%4)==0 && (month>1))?1:0))%7);
}

function calPopGoDate(click) {
	var popUpClickSpot=0;
	popUpClickSpot=Number(click.substring(9, 10))+7*Number(click.substring(8, 9))-calPopFind1+1;
	if (popUpClickSpot>0 && popUpClickSpot<=calPopFind31) {
		document.getElementById(calPopMonthID).value=calPopMonth+1;
		document.getElementById(calPopDateID).value=popUpClickSpot;
		document.getElementById(calPopYearID).value=calPopYear;
		document.getElementById("calPopUp").style.display="none";	// display the calendar
	}
}

function calPopClose() {
		document.getElementById("calPopUp").style.display="none";
}