$(document).ready(function() {

/*
	var todayYear 	= $('#todayYear').val();
	var todayMonth 	= $('#todayMonth').val();
	var todayDay 	= $('#todayDay').val();
	

	$("#linkedDates").datepicker({     
		minDate: new Date(todayYear, todayMonth, todayDay),     
		maxDate: new Date(todayYear + 1, todayMonth, todayDay),
		changeMonth: false,
		changeYear: false,
		mandatory: true,
		changeFirstDay: false, 
		beforeShow: readLinked,     
		onSelect: updateLinked,     
		showOn: "both",     
		buttonImage: "/images/global/calendar.gif",     
		buttonImageOnly: true 
	});

	$("#emonth, #eyear").change(adjustDaySelect);
*/	
	calculateEndDate();
	toggleNumberOfDays();
});

// Prepare to show a date picker linked to three select controls 
function readLinked() { 
    $('#linkedDates').val($('#emonth').val() + '/' + 
        $('#eday').val() + '/' + $('#eyear').val()); 
    return {}; 
} 
 
// Update three select controls to match a date picker selection 
function updateLinked(date) { 
    $('#emonth').val(date.substring(0, 2)); 
    $('#eday').val(date.substring(3, 5)); 
    $('#eyear').val(date.substring(6, 10)); 
} 

// Figure out when the policy end date is based on start date, time, and term.
// We are using JavaScript's Date object here.
// See http://www.w3schools.com/jsref/jsref_obj_date.asp
function calculateEndDate() {

	var months = new Array(12);
		months[0]	= "January";
		months[1]	= "February";
		months[2]	= "March";
		months[3]	= "April";
		months[4]	= "May";
		months[5]	= "June";
		months[6]	= "July";
		months[7]	= "August";
		months[8]	= "September";
		months[9]	= "October";
		months[10]	= "November";
		months[11]	= "December";
		
	var daysofweek = new Array(7);
		daysofweek[0]	= "Sunday";
		daysofweek[1]	= "Monday";
		daysofweek[2]	= "Tuesday";
		daysofweek[3]	= "Wednesday";
		daysofweek[4]	= "Thursday";
		daysofweek[5]	= "Friday";
		daysofweek[6]	= "Saturday";


	var selected_day	= document.getElementById('eday').value;	// Policy Start Day
	var selected_month	= document.getElementById('emonth').value - 1; // Policy Start Month, start counting at 0, so decriment 1.
	var selected_year 	= document.getElementById('eyear').value;	// Policy Start Year
	var selected_hour 	= document.getElementById('ehour').value;	// Policy Start Hour
	var policy_term		= document.getElementById('tm').value;		// Policy Term
	
	// Determine policy days based on term
	if (policy_term == 2) {
		var policy_days = 182;	// 6-month term
	} else if (policy_term == 3) {
		var policy_days = 365;	// Annual term
	} else {
		var policy_days = document.getElementById('dn').value; // Use user supplied value for policy days
	}
	policy_days = parseInt(policy_days); // We must cast to Int or else the date addition won't work.
	
	var currentDate = new Date(selected_year, selected_month, selected_day, selected_hour, 0); // Set date object to currently selected date.
	
	currentDate.setDate(currentDate.getDate() + policy_days); // Increment date by how many days the policy lasts.
	
	// Convert hour to 12-hour format
	var hourFormatted 		= (currentDate.getHours() > 12) ? currentDate.getHours() - 12 : currentDate.getHours(); 
	
	// If we're at the 0 hour, we want it to actually diplay as 12, so adjust that here.
	if (hourFormatted == 0) {
		hourFormatted = 12;
	}

	// Add leading 0 to minutes less than 10	
	var minutesFormatted	= (currentDate.getMinutes() < 10) ? '0' +  currentDate.getMinutes() : currentDate.getMinutes(); 
	
	// Determine if we're in AM or PM
	var ampm				= (currentDate.getHours() >= 12) ? 'PM' : 'AM'; 

	// Format output
	var dateOutput 			= daysofweek[currentDate.getDay()] + ', ' + months[currentDate.getMonth()] + ' ' + currentDate.getDate() + ', ' + currentDate.getFullYear() + ' ' + hourFormatted + ':' + minutesFormatted + ' ' + ampm;
	
	// Update the div with the new expiration date
	document.getElementById("coverage_end_date").innerHTML = dateOutput;
}





