window.eligibility_loaded=true;
window.rsf1_loaded=true;
window.challenges_loaded=true;
window.event_list_ran=false;
window.disaster_feed_submitted=false;

jQuery(document).ready( function () {

	disaster_feed_submitted=false;

	//manipulate event times with js

	setTimeout(function() {

		if (!jQuery('.fema-event-date-line').length) {

			var start_date = jQuery(".event-start-date-number").text();
			var start_time = jQuery(".event-start-time").text();
			var start_date_text = jQuery(".event-start-date").text();

			if (start_date) {

				var start_date_array = start_date_text.split(" ");
				var start_date_number_array = start_date.split("-");

				var start_day = parseInt(start_date_array[1].slice(0, -1));
				var start_month = parseInt(start_date_number_array[1]);
				var start_year = parseInt(start_date_array[2]);

				var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; //'Asia/Kolhata' for Indian Time.
				//console.log(timezone);

				const eventDate = new Date(start_date + '.000Z');
				const formatter5 = Intl.DateTimeFormat("en-US", {
				  timeZone: timezone,
				  hour: "numeric",
				  minute: "numeric",
				  timeZoneName: "short"
				});
				var start_date_output = formatter5.format(eventDate);
				const formatterDay = Intl.DateTimeFormat("en-US", {
				  timeZone: timezone,
				  day: "numeric",
				});
				var start_day_output = formatterDay.format(eventDate);
				const formatterMonth = Intl.DateTimeFormat("en-US", {
					timeZone: timezone,
					month: "numeric",
				});
				var start_month_output = formatterMonth.format(eventDate);
				const formatterYear = Intl.DateTimeFormat("en-US", {
					timeZone: timezone,
					year: "numeric",
				});
				var start_year_output = formatterYear.format(eventDate);

				var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];

				if ((start_day_output < start_day) || (start_month_output < start_month) || (start_year_output < start_year)) {
					if (start_month_output < start_month) {
						start_date_array[0] = months[start_month_output - 1];
					}
					jQuery(".event-start-date").text(start_date_array[0] + " " + start_day_output + ", " + start_date_array[2]);
				}

				start_date_output = start_date_output.replace("AM", "a.m.");
				start_date_output = start_date_output.replace("PM", "p.m.");

				jQuery(".event-start-time").text(start_date_output);

			}

			var end_date = jQuery(".event-end-date-number").text();
			var end_date_text = jQuery(".event-end-date-hidden").text();
			var end_time = jQuery(".event-end-time").text();

			if (end_date) {

				var end_date_array = end_date_text.split(" ");
				var end_day = parseInt(end_date_array[1].slice(0, -1));
				var end_year = parseInt(end_date_array[2]);

				var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; //'Asia/Kolhata' for Indian Time.

				const eventDate = new Date(end_date + '.000Z');
				const formatter5 = Intl.DateTimeFormat("en-US", {
					timeZone: timezone,
					hour: "numeric",
					minute: "numeric",
					timeZoneName: "short"
				});
				var end_date_output = formatter5.format(eventDate);
				const formatterDay = Intl.DateTimeFormat("en-US", {
					timeZone: timezone,
					day: "numeric",
				});
				var end_day_output = formatterDay.format(eventDate);
				const formatterMonth = Intl.DateTimeFormat("en-US", {
					timeZone: timezone,
					month: "numeric",
				});
				var end_month_output = formatterMonth.format(eventDate);
				const formatterYear = Intl.DateTimeFormat("en-US", {
					timeZone: timezone,
					year: "numeric",
				});
				var end_year_output = formatterYear.format(eventDate);

				if (end_day_output<end_day) {
					if ((start_day < end_day_output) || (start_month < end_month_output) || (start_year < end_year_output)) {
						var event_end_date = jQuery(".event-end-date").text();
						const updated_date = event_end_date.split(" ");
						var updated_date_string = updated_date[0] + " " + end_day_output + ", " + updated_date[2];
						jQuery(".event-end-date").text(updated_date_string);
						jQuery(".event-range-symbol").show();
						jQuery(".event-end-date").show();
					} else {
						jQuery(".event-range-symbol").hide();
						jQuery(".event-end-date").hide();
					}
				}

				if ((start_month < end_month_output) || (start_year < end_year_output)) {
					jQuery(".event-range-symbol").show();
					jQuery(".event-end-date").show();
				}

				end_date_output = end_date_output.replace("AM", "a.m.");
				end_date_output = end_date_output.replace("PM", "p.m.");
				jQuery(".event-end-time").text(end_date_output);

		}
	}

}, 300);

	// sticky heaser for assistance banner on disaster pages
	jQuery(window).scroll(function(){
			var sticky = jQuery('.sticky-header'),
			scroll = jQuery(window).scrollTop();

		if (scroll >= 200) sticky.addClass('fixed');
		else sticky.removeClass('fixed');
	});

	var langCode = drupalSettings.path.currentLanguage;

	// adds datatable functionality to .datatable class
	jQuery('.datatable').DataTable();

	// adds an hr after every h2 in the content region
	jQuery('.region-content h2').append('<hr />');

	// hiding dropdown menu until page is loaded
	jQuery('ul.hide-on-load').removeClass('hide-on-load');
	jQuery('div.usa-banner__content.usa-accordion__content').removeClass('hide-on-load');

	jQuery( "#edit-dhlocation" ).focus(function() {
		if (jQuery('#edit-dhlocation').val() == 'Enter your city and state, or ZIP code') {
			jQuery( "#edit-dhlocation" ).val("");
		}
		if (jQuery('#edit-dhlocation').val() == 'Ingrese su ciudad y estado, o código postal') {
			jQuery( "#edit-dhlocation" ).val("");
		}
	});

jQuery(document).on('ajaxComplete', function(event, xhr, settings) {

	if (jQuery('.fema-event-date-line').length) {
		window.event_list_ran = false;
		//adjust event times to user's timezone in event view, using luxon library
		if (!window.event_list_ran) {
				setTimeout(function() {

					jQuery('.fema-start-time').each(function() {
						var start_date=jQuery(this).text();
						var str = start_date,
						    delimiter = ' ',
						    start = 3,
						    tokens = str.split(delimiter).slice(start),
						    result = tokens.join(delimiter);
						start_time = result;
						//console.log(start_time);

						// To get the substring BEFORE the nth occurence
						var tokens2 = str.split(delimiter).slice(0, start),
				    start_date = tokens2.join(delimiter); // this

						var start_day = parseInt(tokens2[1].slice(0, -1));
						var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; //'Asia/Kolhata' for Indian Time.

						var start_date_number = jQuery(this).closest('.fema-event-date').find(".event-start-date-number").text();
						var start_date_number_array = start_date_number.split("-");

						var start_month = parseInt(start_date_number_array[1]);
						var start_year = parseInt(start_date_number_array[0]);

						const eventDate = new Date(start_date_number + '.000Z');
						const formatter5 = Intl.DateTimeFormat("en-US", {
						  timeZone: timezone,
						  hour: "numeric",
						  minute: "numeric",
						  timeZoneName: "short"
						});
						const formatterDay2 = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							day: "numeric"
						});
						const formatterMonth = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							month: "numeric",
						});
						var start_month_output = formatterMonth.format(eventDate);
						const formatterYear = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							year: "numeric",
						});
						var start_year_output = formatterYear.format(eventDate);

						var start_date_output = formatter5.format(eventDate);
						var test1_output = formatterDay2.format(eventDate);

						if ((test1_output<start_day) || (start_month_output < start_month) || (start_year_output < start_year)) {
							var months = [ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" ];
							if (start_month_output < start_month) {
								tokens2[0] = months[start_month_output - 1];
							}
	 						tokens2[1] = test1_output + ",";
	 						start_date = tokens2.join(' ');
						}

						start_date_output = start_date_output.replace("AM", "a.m.");
						start_date_output = start_date_output.replace("PM", "p.m.");

						var new_start_date = jQuery(this).closest('.fema-event-date').find(".event-start-date").text();

						jQuery(this).text(start_date + " " + start_date_output);
					});

					jQuery('.fema-end-time').each(function() {
						var end_date=jQuery(this).text();
						//console.log(end_date);
						var str = end_date,
								delimiter = ' ',
								start = 0,
								tokens = str.split(delimiter).slice(start),
								result = tokens.join(delimiter);
						end_time = result;

						// To get the substring BEFORE the nth occurence
						var tokens2 = str.split(delimiter).slice(0, start),
						end_date = tokens2.join(delimiter); // this

						var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; //'Asia/Kolhata' for Indian Time.

						var start_date_number = jQuery(this).closest('.fema-event-date').find(".event-start-date-number").text();
						var end_date_number = jQuery(this).closest('.fema-event-date').find(".event-start-end-number").text();

						const eventDate = new Date(end_date_number + '.000Z');
						const formatter5 = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							hour: "numeric",
							minute: "numeric",
							timeZoneName: "short"
						});
						var end_date_output = formatter5.format(eventDate);
						end_date_output = end_date_output.replace("AM", "a.m.");
						end_date_output = end_date_output.replace("PM", "p.m.");

						//run a date test
						const test1 = new Date(end_date_number + '.000Z');
						const test2 = new Date(start_date_number + '.000Z');
						//end day
						const formatter6 = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							day: "numeric"
						});
						var test1_output_day = formatter6.format(test1);
						//start month
						const formatter7 = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							month: "numeric",
						});
						var test1_output_month = formatter7.format(test2);
						//start year
						const formatter8 = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							year: "numeric",
						});
						var test1_output_year = formatter8.format(test2);

						var compare = parseInt(tokens[1].replace(",",""));
						var compare_month = new Date(Date.parse(tokens[0] +" 1, 2012")).getMonth()+1;
						var compare_year = tokens[2].trim();

						if (test1_output_day < compare) {
							if (test1_output_month == compare_month) {
								if (test1_output_year == compare_year) {
									var month_print = eventDate.toLocaleString('default', { month: 'short' });
									//pad the day if necessary
									if (test1_output_day < 10) {
										test1_output_day = '0' + test1_output_day;
									}
									jQuery(this).closest('.grid-item').find(".content-icon.fema-event").html(
										'<div class="fema-event-date-block"><time datetime="00Z" class="datetime">' + month_print + '</time><br><time datetime="00Z" class="datetime">' + test1_output_day + '</time></div>'
									);
								}
							}
						}

						//if event type is 'Important Date', don't change this
						var event_type = jQuery(this).closest('.fema-event-date').find(".event-type").text();
						var new_end_date = jQuery(this).closest('.fema-event-date').find(".event-end-date").text();
						var new_end_date_adjusted = jQuery(this).closest('.fema-event-date').find(".event-end-date-adjusted").text();

						const enddate1 = new Date(new_end_date);
						const enddate2 = new Date(new_end_date_adjusted);

						if (event_type != 'Important Date') {
							//here is where the grid icon logic should go
							//apply same start date logic down here
							const eventDate = new Date(start_date_number + '.000Z');
							const formatter5 = Intl.DateTimeFormat("en-US", {
								timeZone: timezone,
								hour: "numeric",
								minute: "numeric",
								timeZoneName: "short"
							});
							const formatterDay2 = Intl.DateTimeFormat("en-US", {
								timeZone: timezone,
								day: "numeric"
							});
							var month_print = eventDate.toLocaleString('default', { month: 'short' });
							const formatter6 = Intl.DateTimeFormat("en-US", {
								timeZone: timezone,
								day: "numeric"
							});
							var test1_start_day = formatter6.format(eventDate);
							//pad the day if necessary
							if (test1_start_day < 10) {
								test1_start_day = '0' + test1_start_day;
							}
							if (start_date_number.slice(0,10) == end_date_number.slice(0,10)) {
								jQuery(this).text(end_date + " " + end_date_output);
								jQuery(this).closest('.grid-item').find(".content-icon.fema-event").html(
									'<div class="fema-event-date-block"><time datetime="00Z" class="datetime">' + month_print + '</time><br><time datetime="00Z" class="datetime">' + test1_start_day + '</time></div>'
								);

							} else {
								if (enddate2 <  enddate1) {
									const formatter7 = Intl.DateTimeFormat("en-US", {
										timeZone: timezone,
										day: "numeric"
									});
									var test1_end_day = formatter7.format(enddate2);
									if (test1_end_day < 10) {
										test1_end_day = '0' + test1_end_day;
									}
									var month_print_end = enddate2.toLocaleString('default', { month: 'short' });
									jQuery(this).text(new_end_date_adjusted + " " + end_date_output);
									//jQuery(this).closest('.grid-item').find(".fema-event-date-block.range.end").html(
									//	'<time datetime="00Z" class="datetime">' + month_print_end + '</time><br><time datetime="00Z" class="datetime">' + test1_end_day + '</time>'
									//);
								} else {
									jQuery(this).text(new_end_date + " " + end_date_output);
								}
								jQuery(this).closest('.grid-item').find(".fema-event-date-block.range.start").html(
									'<time datetime="00Z" class="datetime">' + month_print + '</time><br><time datetime="00Z" class="datetime">' + test1_start_day + '</time>'
								);
							}
						} else {
							if (enddate2 <  enddate1) {
								jQuery(this).text(new_end_date_adjusted);
							} else {
								jQuery(this).text(new_end_date);
							}
						}

					});

			}, 400);

			window.event_list_ran=true;
		}
	}

		if (jQuery('#edit-field-roadmap-rsf-value').length) {
			if (!window.eligibility_loaded) {
			jQuery("#edit-field-roadmap-rsf-value").empty();
			var eligibility_value = jQuery("#edit-field-recovery-eligibility-value").val();

			jQuery.ajax({
						type: "POST",
						url: '/dynamic_home/resources_1',
						data: { eligibility_value1 : eligibility_value },
						success: function(response)
						{
								for (var key of Object.keys(response)) {
										var option = jQuery('<option></option>').attr("value", key).text(response[key]);
										jQuery("#edit-field-roadmap-rsf-value").append(option);
								}
								window.eligibility_loaded=true;
					 }
			 });
			}
		}

		if (jQuery('#edit-field-roadmap-challenges-value').length) {
			if (!window.rsf1_loaded) {
			var selected_val = jQuery("#edit-field-roadmap-challenges-value").val();
			jQuery("#edit-field-roadmap-challenges-value").empty();
			var eligibility_value = jQuery("#edit-field-recovery-eligibility-value").val();
			var rsf_value = jQuery("#edit-field-roadmap-rsf-value").val();

			jQuery.ajax({
						type: "POST",
						url: '/dynamic_home/resources_2',
						data: { eligibility_value1 : eligibility_value, rsf_value1: rsf_value },
						success: function(response)
						{
								for (var key of Object.keys(response)) {

										if (selected_val==key) {
											var option = jQuery('<option></option>').attr("value", key).attr("selected", true).text(response[key]);
										} else {
											var option = jQuery('<option></option>').attr("value", key).text(response[key]);
										}
										jQuery("#edit-field-roadmap-challenges-value").append(option);
								}
								window.rsf1_loaded=true;
					 }
			 });
			}
		}
	});

	jQuery( "#edit-dhlocation" ).focusout(function() {
		if (jQuery('#edit-dhlocation').val() == '') {
			if (langCode == 'es') {
				jQuery( "#edit-dhlocation" ).val("Ingrese su ciudad y estado, o código postal");
			} else {
				jQuery( "#edit-dhlocation" ).val("Enter your city and state, or ZIP code");
			}
		}
	});

	Date.prototype.stdTimezoneOffset = function() {
		var jan = new Date(this.getFullYear(), 0, 1);
		var jul = new Date(this.getFullYear(), 6, 1);
		return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
	}

	Date.prototype.dst = function() {
		return this.getTimezoneOffset() < this.stdTimezoneOffset();
	}

	if (jQuery('.field--name-field-feature-event-date').length) {
		var date_value=jQuery(".field--name-field-feature-event-date").text().trim();
		tokens = date_value.split("-").slice(0);
		var date2 = new Date(tokens[1].trim());

		var date1 = new Date(jQuery('time:first').attr('datetime'));
		var date2 = new Date(jQuery('time:nth-child(2)').attr('datetime'));

		var newDate = new Date(date1.getTime());
		var newDate2 = new Date(date2.getTime());

		let options = { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone, timeZoneName: 'short' };

		var t = 0;
		newDate = newDate.toLocaleString('en-US', options).replace(/,/g, match => ++t === 2 ? '' : match);
		t = 0;
		newDate2 = newDate2.toLocaleString('en-US', options).replace(/,/g, match => ++t === 2 ? '' : match);

		var modified = [newDate,newDate2].map((item, i) => {
			return item.replace('PM','pm').replace('AM','am').replace('am','a.m.').replace('pm','p.m.');
		});

		jQuery(".field--name-field-feature-event-date").text(modified[0] + ' - ' + modified[1]);

	}

  if (jQuery('.event-start-time').length) {
		//adjust event times to user's timezone in event view, using luxon library
		var start_date=jQuery(".event-start-date-number").text();
		var start_time=jQuery(".event-start-time").text();
		var end_time=jQuery(".event-end-time").text();

		var d = new Date();
		var n = d.getTimezoneOffset();
		var msec_offset='';
		var start_val='';

		if(d.dst()) {
			msec_offset=14400000;
			start_val=4;
		} else {
			msec_offset=18000000;
			start_val=5;
		}

		if (n==600) {
			n=660;
		}

		var timezone_print="";

		//timezone print
		switch (n/60) {
		  case start_val:
			  if (d.dst()) {
		    	timezone_print = "EDT";
				} else {
					timezone_print = "EST";
				}
		    break;
			case (start_val+1):
				if(d.dst()) {
					timezone_print = "CDT";
				} else {
					timezone_print = "CST";
				}
				break;
			case (start_val+2):
				if(d.dst()) {
					timezone_print = "MDT";
				} else {
					timezone_print = "MST";
				}
				break;
			case (start_val+3):
				if(d.dst()) {
					timezone_print = "PDT";
				} else {
					timezone_print = "PST";
				}
				break;
			case (start_val+4):
				if(d.dst()) {
					timezone_print = "AKDT";
				} else {
					timezone_print = "AKST";
				}
				break;
			case (start_val+5):
				if(d.dst()) {
					timezone_print = "HDT";
				} else {
					timezone_print = "HST";
				}
				break;
			case (start_val+6):
				if(d.dst()) {
					timezone_print = "HDT";
				} else {
					timezone_print = "HST";
				}
				break;
		}

		//var milli_offset = (3600000 * n/60);
		var msec_start = Date.parse(start_date+' '+start_time);
		var msec_end = Date.parse(start_date+' '+end_time);
		/* msec_start = (msec_start-milli_offset+msec_offset);
		msec_end = (msec_end-milli_offset+msec_offset); */

		var DateTime = luxon.DateTime;
		var dt_start = DateTime.fromMillis(msec_start);
		var dt_end = DateTime.fromMillis(msec_end);

		var newStartTime = dt_start.toLocaleString(DateTime.TIME_SIMPLE) + " " + timezone_print;
		var newEndTime = dt_end.toLocaleString(DateTime.TIME_SIMPLE)  + " " + timezone_print;
		var modified = [start_time,end_time].map((item, i) => {
			return item.replace('PM','pm').replace('AM','am').replace('am','a.m.').replace('pm','p.m.');
		});
		jQuery(".event-start-time").text(modified[0] + " " + timezone_print);
		jQuery(".event-end-time").text(modified[1] + " " + timezone_print);
		jQuery(".event-time").show();
	}

	//disaster feed handler functions
	jQuery("#edit-submit-feed-on-disaster-pages").click(function(){
		disaster_feed_submitted=true;
	});

	if (jQuery('.disaster-feed-total-rows').length) {
		var total_rows = jQuery('.disaster-feed-total-rows').text();
		if (total_rows > 5) {
			jQuery("#views-exposed-form-feed-on-disaster-pages-block-2").show();
		}
		if (disaster_feed_submitted) {
			jQuery("#views-exposed-form-feed-on-disaster-pages-block-2").show();
		}
	}

if (jQuery('.active-text-replace').length) {
	var active_text = jQuery('.active-text-replace').text();
	const text_array = active_text.split(" - ");
	const count = parseInt(text_array[0], 10);
	if (count==1) {
		var text_replace = Drupal.t('There is currently @count active disaster declared in @location.', {'@count': count, '@location': text_array[1]});
		jQuery('.active-text-replace').text(text_replace);
	} else {
		var text_replace = Drupal.t('There are currently @count active disasters declared in @location.', {'@count': count, '@location': text_array[1]});
		jQuery('.active-text-replace').text(text_replace);
	}
}

} );

(function ($, drupalSettings) {

Drupal.behaviors.eventDateFix = {
	attach: function (context) {
		//event list view

		Date.prototype.stdTimezoneOffset = function() {
			var jan = new Date(this.getFullYear(), 0, 1);
			var jul = new Date(this.getFullYear(), 6, 1);
			return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
		}

		Date.prototype.dst = function() {
			return this.getTimezoneOffset() < this.stdTimezoneOffset();
		}

		if (jQuery('.fema-event-date-line').length) {
			//adjust event times to user's timezone in event view, using luxon library
			//todo - apply to load and ajaxcomplete
			if (!window.event_list_ran) {
					setTimeout(function() {

			    jQuery('.fema-start-time').each(function() {
						var start_date=jQuery(this).text();
						var str = start_date,
						    delimiter = ' ',
						    start = 3,
						    tokens = str.split(delimiter).slice(start),
						    result = tokens.join(delimiter);
						start_time = result;

						// To get the substring BEFORE the nth occurence
						var tokens2 = str.split(delimiter).slice(0, start),
				    start_date = tokens2.join(delimiter); // this

						var start_day = parseInt(tokens2[1].slice(0, -1));

						var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; //'Asia/Kolhata' for Indian Time.

						var start_date_number = jQuery(this).closest('.fema-event-date').find(".event-start-date-number").text();
						var start_date_number_array = start_date_number.split("-");

						var start_month = parseInt(start_date_number_array[1]);
						var start_year = parseInt(start_date_number_array[0]);

						const eventDate = new Date(start_date_number + '.000Z');
						const formatter5 = Intl.DateTimeFormat("en-US", {
						  timeZone: timezone,
						  hour: "numeric",
						  minute: "numeric",
						  timeZoneName: "short"
						});
						const formatterDay2 = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							day: "numeric"
						});
						const formatterMonth = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							month: "numeric",
						});
						var start_month_output = formatterMonth.format(eventDate);
						const formatterYear = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							year: "numeric",
						});
						var start_year_output = formatterYear.format(eventDate);

						var start_date_output = formatter5.format(eventDate);
						var test1_output = formatterDay2.format(eventDate);

						if ((test1_output<start_day) || (start_month_output < start_month) || (start_year_output < start_year)) {
							var months = [ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" ];
							if (start_month_output < start_month) {
								tokens2[0] = months[start_month_output - 1];
							}
	 						tokens2[1] = test1_output + ",";
	 						start_date = tokens2.join(' ');
						}

						start_date_output = start_date_output.replace("AM", "a.m.");
						start_date_output = start_date_output.replace("PM", "p.m.");
						//console.log(start_date_output);

						var new_start_date = jQuery(this).closest('.fema-event-date').find(".event-start-date").text();

						jQuery(this).text(start_date + " " + start_date_output);

					});

					jQuery('.fema-end-time').each(function() {
						var end_date=jQuery(this).text();
						//console.log(end_date);
						var str = end_date,
								delimiter = ' ',
								start = 0,
								tokens = str.split(delimiter).slice(start),
								result = tokens.join(delimiter);
						end_time = result;

						// To get the substring BEFORE the nth occurence
						var tokens2 = str.split(delimiter).slice(0, start),
						end_date = tokens2.join(delimiter); // this

						var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; //'Asia/Kolhata' for Indian Time.

						var start_date_number = jQuery(this).closest('.fema-event-date').find(".event-start-date-number").text();
						var end_date_number = jQuery(this).closest('.fema-event-date').find(".event-start-end-number").text();

						const eventDate = new Date(end_date_number + '.000Z');
						const formatter5 = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							hour: "numeric",
							minute: "numeric",
							timeZoneName: "short"
						});
						var end_date_output = formatter5.format(eventDate);
						end_date_output = end_date_output.replace("AM", "a.m.");
						end_date_output = end_date_output.replace("PM", "p.m.");

						//run a date test
						const test1 = new Date(end_date_number + '.000Z');
						const test2 = new Date(start_date_number + '.000Z');
						//end day
						const formatter6 = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							day: "numeric"
						});
						var test1_output_day = formatter6.format(test1);
						//start month
						const formatter7 = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							month: "numeric",
						});
						var test1_output_month = formatter7.format(test2);
						//start year
						const formatter8 = Intl.DateTimeFormat("en-US", {
							timeZone: timezone,
							year: "numeric",
						});
						var test1_output_year = formatter8.format(test2);

						var compare = parseInt(tokens[1].replace(",",""));
						var compare_month = new Date(Date.parse(tokens[0] +" 1, 2012")).getMonth()+1;
						var compare_year = tokens[2].trim();

						if (test1_output_day < compare) {
							if (test1_output_month == compare_month) {
								if (test1_output_year == compare_year) {
									var month_print = eventDate.toLocaleString('default', { month: 'short' });
									//pad the day if necessary
									if (test1_output_day < 10) {
										test1_output_day = '0' + test1_output_day;
									}
									jQuery(this).closest('.grid-item').find(".content-icon.fema-event").html(
										'<div class="fema-event-date-block"><time datetime="00Z" class="datetime">' + month_print + '</time><br><time datetime="00Z" class="datetime">' + test1_output_day + '</time></div>'
									);
								}
							}
						}

						//if event type is 'Important Date', don't change this
						var event_type = jQuery(this).closest('.fema-event-date').find(".event-type").text();
						var new_end_date = jQuery(this).closest('.fema-event-date').find(".event-end-date").text();
						var new_end_date_adjusted = jQuery(this).closest('.fema-event-date').find(".event-end-date-adjusted").text();

						const enddate1 = new Date(new_end_date);
						const enddate2 = new Date(new_end_date_adjusted);

						if (event_type != 'Important Date') {
							//here is where the grid icon logic should go
							//apply same start date logic down here
							const eventDate = new Date(start_date_number + '.000Z');
							const formatter5 = Intl.DateTimeFormat("en-US", {
								timeZone: timezone,
								hour: "numeric",
								minute: "numeric",
								timeZoneName: "short"
							});
							const formatterDay2 = Intl.DateTimeFormat("en-US", {
								timeZone: timezone,
								day: "numeric"
							});
							var month_print = eventDate.toLocaleString('default', { month: 'short' });
							const formatter6 = Intl.DateTimeFormat("en-US", {
								timeZone: timezone,
								day: "numeric"
							});
							var test1_start_day = formatter6.format(eventDate);
							//pad the day if necessary
							if (test1_start_day < 10) {
								test1_start_day = '0' + test1_start_day;
							}
							if (start_date_number.slice(0,10) == end_date_number.slice(0,10)) {
								jQuery(this).text(end_date + " " + end_date_output);
								jQuery(this).closest('.grid-item').find(".content-icon.fema-event").html(
									'<div class="fema-event-date-block"><time datetime="00Z" class="datetime">' + month_print + '</time><br><time datetime="00Z" class="datetime">' + test1_start_day + '</time></div>'
								);

							} else {
								if (enddate2 <  enddate1) {
									const formatter7 = Intl.DateTimeFormat("en-US", {
										timeZone: timezone,
										day: "numeric"
									});
									var test1_end_day = formatter7.format(enddate2);
									if (test1_end_day < 10) {
										test1_end_day = '0' + test1_end_day;
									}
									var month_print_end = enddate2.toLocaleString('default', { month: 'short' });
									jQuery(this).text(new_end_date_adjusted + " " + end_date_output);
									//jQuery(this).closest('.grid-item').find(".fema-event-date-block.range.end").html(
									//	'<time datetime="00Z" class="datetime">' + month_print_end + '</time><br><time datetime="00Z" class="datetime">' + test1_end_day + '</time>'
									//);
								} else {
									jQuery(this).text(new_end_date + " " + end_date_output);
								}
								jQuery(this).closest('.grid-item').find(".fema-event-date-block.range.start").html(
									'<time datetime="00Z" class="datetime">' + month_print + '</time><br><time datetime="00Z" class="datetime">' + test1_start_day + '</time>'
								);
							}
						} else {
							if (enddate2 <  enddate1) {
								jQuery(this).text(new_end_date_adjusted);
							} else {
								jQuery(this).text(new_end_date);
							}
						}
					});

				}, 300);

				window.event_list_ran=true;
			}
		}
 }
};

Drupal.behaviors.totalRows = {
	attach: function (context) {
		jQuery("#edit-submit-feed-on-disaster-pages").click(function(){
			window.disaster_feed_submitted=true;
		});
		if (jQuery('.disaster-feed-total-rows').length) {
			var total_rows = jQuery('.disaster-feed-total-rows').text();
			if (total_rows > 5) {
				jQuery("#views-exposed-form-feed-on-disaster-pages-block-2").show();
			}
			if (disaster_feed_submitted) {
				jQuery("#views-exposed-form-feed-on-disaster-pages-block-2").show();
			}
		}
	}
};

Drupal.behaviors.challengesChange = {
	attach: function (context) {
		if (jQuery('#edit-field-recovery-eligibility-value').length) {
			jQuery('#edit-field-recovery-eligibility-value').change(function() {
				window.eligibility_loaded=false;
				if(Drupal.AjaxCommands){
					Drupal.AjaxCommands.prototype.viewsScrollTop = null;
				}
				jQuery('#edit-submit-recovery-resource-library')[0].click();
			});
		}
	}
};

Drupal.behaviors.challengesChange2 = {
	attach: function (context) {
		if (jQuery('#edit-field-roadmap-rsf-value').length) {
			jQuery('#edit-field-roadmap-rsf-value').change(function() {
				window.rsf1_loaded=false;
				if(Drupal.AjaxCommands){
					Drupal.AjaxCommands.prototype.viewsScrollTop = null;
				}
				jQuery('#edit-submit-recovery-resource-library')[0].click();
			});
		}
	}
};

Drupal.behaviors.challengesChange3 = {
	attach: function (context) {
		if (jQuery('#edit-field-roadmap-challenges-value').length) {
			jQuery('#edit-field-roadmap-challenges-value').change(function() {
				window.rsf1_loaded=false;
				if(Drupal.AjaxCommands){
					Drupal.AjaxCommands.prototype.viewsScrollTop = null;
				}
				jQuery('#edit-submit-recovery-resource-library')[0].click();
			});
		}
	}
};

Drupal.behaviors.roadmapReset = {
	attach: function (context) {
		if (jQuery('#edit-field-roadmap-challenges-value').length) {
			jQuery(document).on("click","#edit-reset", function() {
				window.location.replace(window.location.protocol + '//' + window.location.hostname + window.location.pathname);
				return false;
			});
		}
	}
};

Drupal.behaviors.alterDatepicker = {
	attach: function (context) {
		if (jQuery('#edit-field-release-date-value-min').length) {
			if (jQuery.datepicker._defaults) {
				jQuery.datepicker._defaults.changeMonth = true;
				jQuery.datepicker._defaults.changeYear = true;
				jQuery.datepicker._defaults.yearRange = '2018:c';
			}
		}
	}
};


}(jQuery, drupalSettings));