//
// Add a method to get a timezone adjusted date for an input field that is a date picker.
// Use $('input[name="date"]').val(new Date().toDateInputValue()) to set the date of the input field.
//
Date.prototype.toDateInputValue = (function() {
	let local = new Date(this);
	local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
	return local.toJSON().slice(0,10);
});

Date.prototype.getWeek = function() {
	let dowOffset = 1;  // I am fixing this to indicate that the first day of the week is always Monday (weeks end on Sunday), for this application.  This was a parameter in the original code.
	
	//dowOffset = typeof(dowOffset) == 'number' ? dowOffset : 0; //default dowOffset to zero  - This should check to ensure that dowOffset is between 0..6
	let newYear = new Date(this.getFullYear(),0,1);
	let day = newYear.getDay() - dowOffset; //the day of week the year begins on
	day = (day >= 0 ? day : day + 7);
	// The number of days from the beginning of the year to this.day
	let daynum = Math.floor((this.getTime() - newYear.getTime() - (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
	let weeknum;
	
	// I have removed the mid-week starting cutoff detection because in this app we always want to start with week #1 (never have a week zero).
	//if(day < 4) {  //if the year starts before the middle of a week
		weeknum = Math.floor((daynum + day - 1) / 7) + 1;
		
		// I have turned off the detection of whether the last days of the year belong to this year's last week or next year's first week.  This gets too confusing and does not result in any additional usefulness.
		//if(weeknum > 52) {
		//	nYear = new Date(this.getFullYear() + 1, 0, 1);
		//	nday = nYear.getDay() - dowOffset;
		//	nday = nday >= 0 ? nday : nday + 7;
		//	// if the next year starts before the middle of the week, it is week #1 of that year
		//	weeknum = nday < 4 ? 1 : 53;
		//}
	//}
	//else {
	//    weeknum = Math.floor((daynum+day-1)/7);
	//}
	
	return weeknum;
};

Date.prototype.getUTCWeek = function() {
	let dowOffset = 1;  // I am fixing this to indicate that the first day of the week is always Monday (weeks end on Sunday), for this application.  This was a parameter in the original code.
	
	//dowOffset = typeof(dowOffset) == 'number' ? dowOffset : 0; //default dowOffset to zero  - This should check to ensure that dowOffset is between 0..6
	let newYear = new Date(this.getUTCFullYear(),0,1);
	let day = newYear.getDay() - dowOffset; //the day of week the year begins on
	day = (day >= 0 ? day : day + 7);
	// The number of days from the beginning of the year to this.day
	let daynum = Math.floor((this.getTime() - newYear.getTime() - (this.getTimezoneOffset() - newYear.getTimezoneOffset())*60000)/86400000) + 1;
	let weeknum;
	
	// I have removed the mid-week starting cutoff detection because in this app we always want to start with week #1 (never have a week zero).
	//if(day < 4) {  //if the year starts before the middle of a week
	weeknum = Math.floor((daynum + day - 1) / 7) + 1;
	
	// I have turned off the detection of whether the last days of the year belong to this year's last week or next year's first week.  This gets too confusing and does not result in any additional usefulness.
	//if(weeknum > 52) {
	//	nYear = new Date(this.getFullYear() + 1, 0, 1);
	//	nday = nYear.getDay() - dowOffset;
	//	nday = nday >= 0 ? nday : nday + 7;
	//	// if the next year starts before the middle of the week, it is week #1 of that year
	//	weeknum = nday < 4 ? 1 : 53;
	//}
	//}
	//else {
	//    weeknum = Math.floor((daynum+day-1)/7);
	//}
	
	return weeknum;
};