﻿$(document).ready(function() {
	$("#rescdate").hide();
	var dateMin = new Date();
	var weekDays = AddWeekDays(-30);
	dateMin.setDate(dateMin.getDate()-1825);
	var months=new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	var dayname=new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	var natDays = [
		//NEW YEARS
		[1,1,2005],	
		[1,1,2006],
		[1,1,2007],		
		[1,1,2008],		
		[1,1,2009],		
		[1,1,2010],
		[1,1,2011],
		[1,1,2012],		
		[1,1,2013],	
		[1,1,2014],		
		[1,1,2015],	
		[1,1,2016],
		[1,1,2017],		
		[1,1,2018],		
		[1,1,2019],		
		[1,1,2020],
		
		//MLK
		[1,18,2005],
		[1,16,2006],
		[1,22,2007],
		[1,21,2008],
		[1,19,2009],
		[1,18,2010],
		[1,17,2011],
		[1,16,2012],
		[1,21,2013],		
		[1,20,2013],		
		[1,20,2014],
		[1,19,2015],
		[1,18,2016],
		[1,16,2017],
		[1,15,2018],
		[1,21,2019],
		[1,20,2020],
		
		//PRESIDENTS DAY
		[2,14,2005],
		[2,20,2006],
		[2,19,2007],
		[2,18,2008],
		[2,16,2009],
		[2,15,2010],
		[2,14,2011],
		[2,20,2012],
		[2,18,2013],
		[2,17,2014],
		[2,16,2015],
		[2,15,2016],
		[2,20,2017],
		[2,18,2018],
		[2,17,2019],
		[2,16,2020],

		//MEMORIAL DAY
		[5,30,2005],	
		[5,29,2006],
		[5,28,2007],
		[5,26,2008],
		[5,25,2009],
		[5,31,2010],		
		[5,30,2011],
		[5,28,2012],
		[5,27,2013],
		[5,26,2014],
		[5,25,2015],	
		[5,30,2016],
		[5,28,2017],
		[5,27,2018],
		[5,26,2019],
		[5,25,2020],
		
		//JULY 4
		[7,4,2005],		
		[7,4,2006],
		[7,4,2007],
		[7,4,2008],
		[7,4,2009],
		[7,4,2010],
		[7,4,2011],
		[7,4,2012],		
		[7,4,2013],
		[7,4,2014],
		[7,4,2015],
		[7,4,2016],
		[7,4,2017],
		[7,4,2018],
		[7,4,2019],		
		[7,4,2020],
		
		//LABOR DAY
		[9,5,2005],
		[9,4,2006],		
		[9,3,2007],		
		[9,1,2008],
		[9,7,2009],		
		[9,6,2010],
		[9,5,2011],
		[9,3,2012],		
		[9,2,2013],		
		[9,1,2014],
		[9,7,2015],		
		[9,6,2016],		
		[9,5,2017],
		[9,3,2018],		
		[9,2,2019],		
		[9,1,2020],
		
		//COLUMBUS DAY
		[10,10,2005],
		[10,9,2006],		
		[10,15,2007],
		[10,13,2008],
		[10,12,2009],
		[10,11,2010],
		[10,10,2011],
		[10,8,2012],		
		[10,14,2013],
		[10,13,2014],
		[10,12,2015],
		[10,11,2016],		
		[10,10,2017],
		[10,8,2018],		
		[10,14,2019],
		[10,13,2020],

		//VETERANS DAY
		[11,11,2005],
		[11,11,2006],
		[11,11,2007],
		[11,11,2008],		
		[11,11,2009],		
		[11,11,2010],
		[11,11,2011],
		[11,11,2012],
		[11,11,2013],		
		[11,11,2014],		
		[11,11,2015],
		[11,11,2016],		
		[11,11,2017],		
		[11,11,2018],		
		[11,11,2019],
		[11,11,2020],		
		
		//THANKSGIVING
		[11,24,2005],
		[11,23,2006],		
		[11,22,2007],		
		[11,27,2008],		
		[11,26,2009],
		[11,25,2010],
		[11,24,2011],
		[11,22,2012],		
		[11,28,2013],		
		[11,27,2014],		
		[11,26,2015],
		[11,25,2016],
		[11,24,2017],
		[11,22,2018],		
		[11,28,2019],		
		[11,27,2020],		
		
		//CHRISTMAS
		[12,25,2005],
		[12,25,2006],
		[12,25,2007],		
		[12,25,2008],		
		[12,25,2009],
		[12,25,2010],
		[12,25,2011],
		[12,25,2012],		
		[12,25,2013],		
		[12,25,2014],		
		[12,25,2015],		
		[12,25,2016],
		[12,25,2017],		
		[12,25,2018],		
		[12,25,2019],
		[12,25,2020]						
	];

	function noWeekendsOrHolidays(date) {
		var noWeekend = $.datepicker.noWeekends(date);
		if (noWeekend[0]) {
			return nationalDays(date);
		} else {
			return noWeekend;
		}
	}

	function nationalDays(date) {
		for (i = 0; i < natDays.length; i++) {
			if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1] && date.getFullYear() == natDays[i][2]) {
 				return [false, natDays[i][2] + '_day'];
 			}
 		}
 	
 		return [true, ''];
	}
 
	function highlightDays(date) {
		
		for (i = 0; i < natDays.length; i++) {
			if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1] && date.getFullYear() == natDays[i][2]) {
	      	return [true, 'holiday'];
	      }
	    }
	   return [true, ''];
	}

	function AddWeekDays(weekDaysToAdd) {
		var daysToAdd = 0;
 		var mydate = new Date();
 		var day = mydate.getDay();
 		weekDaysToAdd = weekDaysToAdd - (5 - day);
 
 		if ((5 - day) < weekDaysToAdd || weekDaysToAdd == 1) {
 			daysToAdd = (5 - day) + 2 + daysToAdd;
 		} else { // (5-day) >= weekDaysToAdd
 			daysToAdd = (5 - day) + daysToAdd
 		}
 
 		while (weekDaysToAdd != 0) {
 			var week = weekDaysToAdd - 5;
 			if (week > 0) {
 				daysToAdd = 7 + daysToAdd;
 				weekDaysToAdd = weekDaysToAdd - 5;
 			} else { // week < 0
 				daysToAdd = (5 + week) + daysToAdd;
 				weekDaysToAdd = weekDaysToAdd - (5 + week);
 			}
	}
 
	return daysToAdd;
	}
		
	function calcResc(d,m,y) {
		
		var rescDate=new Date(y,m,d);
		var found=0;
		d=d;
		
		for (i=0; i<=2; i++) {
			found=0;
			d=d+1;
			rescDate=new Date(y,m,d);
			
			if ((rescDate.getDay())==0 && found!=1) {
				i--;
				found=1;
			}
			
			for (z=0; z<natDays.length; z++) {
				if (rescDate.getMonth() == natDays[z][0] - 1 && rescDate.getDate() == natDays[z][1] && rescDate.getFullYear() == natDays[z][2] && found!=1) {
 					i--;
					found=1;
 				}
 			}
		}
		
		d = rescDate.getDate();
 		m = months[rescDate.getMonth()];
 		y = rescDate.getFullYear();
		$("#rescdate").html("Rescission ends <strong>MIDNIGHT</strong> of: <strong>" + dayname[rescDate.getDay()] + " " + m + " " + d + ", " + y + "</strong>");
		$("#rescdate").show();

	}
	
	$("#resCalc input[type=submit]").click(calcDate);
	$("#resCalc").submit(calcDate);
	
	function calcDate() {
		var maxDate=new Date("12/31/2020");
		var curDate=new Date();
		var seDate=$("#textDate").val();
		seDate = new Date(seDate);
	
		if (seDate.getFullYear()<2000){
			var tYear=seDate.getFullYear();
			tYear=tYear+100;
			seDate.setFullYear(tYear);
		}

		var isErrors=0;
		
		if (seDate<=dateMin){
			alert("Date must be later than " + dateMin.getMonth() + "/" + dateMin.getDate() + "/" + dateMin.getFullYear());
			isErrors=1;
		}

		if (seDate>=maxDate){
			alert("Date must be earlier than 12/31/2020");
			isErrors=1;
		}

		if (isNaN(seDate)) {
			alert("Please enter a valid date or select from the calendar below.");
			isErrors=1;
		}

		if (isErrors==0) {
			var d = seDate.getDate();
 			var m = seDate.getMonth();
 			var y = seDate.getFullYear();
 			$("#textDate").val(m+1+"/"+d+"/"+y);
			calcResc(d,m,y);
		}

		return false;
	}



	$('#datepicker').datepicker({
		numberOfMonths: 3,
		showCurrentAtPos: 0,
		dateFormat: 'mm/dd/yy',
		gotoCurrent: false,
		beforeShowDay: highlightDays,
 		maxDate: "12/31/2020",
 		altField: '#textDate',
 		showOn: "focus",
 		firstDay: 7,
 		changeFirstDay: false,
 		minDate: dateMin,
		defaultDate: -30,
 		onSelect: function() {
 			$('#resCalc').removeClass('firstload');
 			var chosendate=$('#datepicker').datepicker("getDate");
 			var start = chosendate;
 			var date = new Date(start);
 			var d = date.getDate();
 			var m = date.getMonth();
 			var y = date.getFullYear();
 			calcResc(d,m,y);
 			
			
		}
	});

	var dateToday = new Date();
	var d = dateToday.getDate();
 	var m = dateToday.getMonth();
 	var y = dateToday.getFullYear();
	$("#textDate").val(m+1+"/"+d+"/"+y);
	

});

