$(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),
		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) { 

    var selected_date = $.datepicker.parseDate('mm/dd/yy',date);

    $('#emonth').val(selected_date.getMonth()+1); 
    $('#eday').val(selected_date.getDate()); 
    $('#eyear').val(selected_date.getDate()); 
    
    calculateEndDate();

} 

// 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() {
	
  if ($('#is_watercraft').val() == 'true') {
    var is_watercraft = true;
  } else {
    var is_watercraft = false;
  }
  
  if (lang == 'es') {

	var months = new Array(12);
		months[0]	= "enero";
		months[1]	= "febrero";
		months[2]	= "marzo";
		months[3]	= "abril";
		months[4]	= "mayo";
		months[5]	= "junio";
		months[6]	= "julio";
		months[7]	= "agosto";
		months[8]	= "septiembre";
		months[9]	= "octubre";
		months[10]	= "noviembre";
		months[11]	= "diciembre";
		
	var daysofweek = new Array(7);
		daysofweek[0]	= "domingo";
		daysofweek[1]	= "lunes";
		daysofweek[2]	= "martes";
		daysofweek[3]	= "mi&eacute;rcoles";
		daysofweek[4]	= "jueves";
		daysofweek[5]	= "viernes";
		daysofweek[6]	= "s&aacute;bado";
		
  	} else {
  		
  		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 policy_term		= document.getElementById('term_id').value;		// Policy Term
	
  if (is_watercraft) {
    var selected_hour 	= 0;	// Policy Start Hour
	} else {
    var selected_hour 	= document.getElementById('ehour').value;	
  }
  
  // Determine policy days based on term
	if (policy_term == 2) {
		var policy_days = 182;	// 6-month term
	} else if (policy_term == 3 || policy_term == 4) {
		var policy_days = 365;	// Annual term
	} else {
		var policy_days = document.getElementById('day_num').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(); 
	if (is_watercraft) {
		minutesFormatted = '01'; 
	}
	// 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;
}






