text: ﻿//JS DateTime picker init method
  var flatPickrServerTimeFormat = "Z";//"Y-m-dTH:i:S";
  //From the server dates will be received yyyy-MM-ddTHH:mm:ss.fff
  var flatPickrUiTimeFormat = "H:i";
  function InitFlatPickrTimeInlineEdit(editWrapperSelector) {
  	var defaultDate = $(editWrapperSelector).attr("data-default-date");
  	flatpickr(editWrapperSelector + " .form-control", { time_24hr: true, defaultDate: defaultDate, dateFormat: flatPickrServerTimeFormat,noCalendar: true, enableTime: true, minuteIncrement: 1, altInput: true, altFormat: flatPickrUiTimeFormat });
  }
  
  function TimeInlineEditGenerateSelectors(fieldId, fieldName, config) {
  	//Method for generating selector strings of some of the presentation elements
  	var selectors = {};
  	selectors.viewWrapper = "#view-" + fieldId;
  	selectors.editWrapper = "#edit-" + fieldId;
  	return selectors;
  }
  
  function TimeInlineEditPreEnableCallback(fieldId, fieldName, config) {
  	var selectors = TimeInlineEditGenerateSelectors(fieldId, fieldName, config);
  	//init pickr only when needed
  	InitFlatPickrTimeInlineEdit(selectors.editWrapper , "datetime");
  	$(selectors.viewWrapper).hide();
  	$(selectors.editWrapper).show();
  	$(selectors.editWrapper + " .form-control").focus();
  }
  
  function TimeInlineEditPreDisableCallback(fieldId, fieldName, config) {
  	var selectors = TimeInlineEditGenerateSelectors(fieldId, fieldName, config);
  	$(selectors.editWrapper + " .invalid-feedback").remove();
  	$(selectors.editWrapper + " .form-control").removeClass("is-invalid");
  	$(selectors.editWrapper + " .save .fa").addClass("fa-check").removeClass("fa-spin fa-spinner");
  	$(selectors.editWrapper + " .save").attr("disabled", false);
  	$(selectors.viewWrapper).show();
  	$(selectors.editWrapper).hide();
  	var calendarInstance = document.querySelector(selectors.editWrapper + " .form-control")._flatpickr;
  	calendarInstance.destroy();
  }
  
  function TimeInlineEditInit(fieldId, fieldName, config) {
  	config = WebVellaTagHelpers.ProcessConfig(config);
  	var selectors = TimeInlineEditGenerateSelectors(fieldId, fieldName, config);
  	//Init enable action click
  	$(selectors.viewWrapper + " .action .btn").on("click", function (event) {
  		event.stopPropagation();
  		event.preventDefault();
  		TimeInlineEditPreEnableCallback(fieldId, fieldName, config);
  	});
  	//Init enable action dblclick
  	$(selectors.viewWrapper + " .form-control").on("dblclick", function (event) {
  		event.stopPropagation();
  		event.preventDefault();
  		TimeInlineEditPreEnableCallback(fieldId, fieldName, config);
  		//WebVellaTagHelpers.clearSelection();//double click causes text to be selected.
  		setTimeout(function () {
  			$(selectors.editWrapper + " .form-control").get(0).focus();
  		}, 200);
  	});
  	//Disable inline edit action
  	$(selectors.editWrapper + " .cancel").on("click", function (event) {
  		event.stopPropagation();
  		event.preventDefault();
  		TimeInlineEditPreDisableCallback(fieldId, fieldName, config);
  	});
  	//Save inline changes
  	$(selectors.editWrapper + " .save").on("click", function (event) {
  		event.stopPropagation();
  		event.preventDefault();
  		var inputValue = $(selectors.editWrapper + " .form-control").val();
  		if (!moment(inputValue).isValid()) {
  			toastr.error("invalid date", 'Error!', { closeButton: true, tapToDismiss: true });
  			return;
  		}
  
  		var submitObj = {};
  		submitObj[fieldName] = inputValue;
  		$(selectors.editWrapper + " .save .fa").removeClass("fa-check").addClass("fa-spin fa-spinner");
  		$(selectors.editWrapper + " .save").attr("disabled", true);
  		$(selectors.editWrapper + " .invalid-feedback").remove();
  		$(selectors.editWrapper + " .form-control").removeClass("is-invalid");
  		var apiUrl = config.api_url;
  
  		$.ajax({
  			headers: {
  				'Accept': 'application/json',
  				'Content-Type': 'application/json'
  			},
  			url: apiUrl,
  			type: 'PATCH',
  			data: JSON.stringify(submitObj),
  			success: function (response) {
  				if (response.success) {
  					TimeInlineEditInitSuccessCallback(response, fieldId, fieldName, config);
  				}
  				else {
  					TimeInlineEditInitErrorCallback(response, fieldId, fieldName, config);
  				}
  			},
  			error: function (jqXHR, textStatus, errorThrown) {
  				var response = {};
  				response.message = "";
  				if (jqXHR && jqXHR.responseJSON) {
  					response = jqXHR.responseJSON;
  				}
  				TimeInlineEditInitErrorCallback(response, fieldId, fieldName, config);
  			}
  		});
  	});
  }
  
  function TimeInlineEditInitSuccessCallback(response, fieldId, fieldName, config) {
  	var selectors = TimeInlineEditGenerateSelectors(fieldId, fieldName, config);
  	var newValue = WebVellaTagHelpers.ProcessNewValue(response, fieldName);
  	if (newValue !== null) {
  		var formatedDate = moment(newValue).format("DD MMM YYYY HH:mm");
  		$(selectors.viewWrapper + " .form-control").html(formatedDate);
  	}
  	else {
  		$(selectors.viewWrapper + " .form-control").html(newValue);
  	}
  	$(selectors.viewWrapper + " .form-control").attr("title", newValue);
  	$(selectors.editWrapper + " .form-control").val(newValue);
  	$(selectors.editWrapper).attr("data-default-date",newValue);
  	TimeInlineEditPreDisableCallback(fieldId, fieldName, config);
  	toastr.success("The new value is successfully saved", 'Success!', { closeButton: true, tapToDismiss: true });
  }
  
  function TimeInlineEditInitErrorCallback(response, fieldId, fieldName, config) {
  	var selectors = TimeInlineEditGenerateSelectors(fieldId, fieldName, config);
  	$(selectors.editWrapper + " .form-control").addClass("is-invalid");
  	var errorMessage = response.message;
  	if (!errorMessage && response.errors && response.errors.length > 0) {
  		errorMessage = response.errors[0].message;
  	}
  		
  	$(selectors.editWrapper + " .input-group").after("<div class='invalid-feedback'>" + errorMessage + "</div>");
  	$(selectors.editWrapper + " .invalid-feedback").show();
  	$(selectors.editWrapper + " .save .fa").addClass("fa-check").removeClass("fa-spin fa-spinner");
  	$(selectors.editWrapper + " .save").attr("disabled", false);
  	toastr.error("An error occurred", 'Error!', { closeButton: true, tapToDismiss: true });
  	console.log("error", response);
  }