PageSourceSearch

https://bakersfieldschoice.com/wp-content/plugins/supportcandy/framework/scripts.js?ver=3.5.3

js bakersfieldschoice.com collected 2026-09-24 12:39:32 UTC 94,045 bytes, 3,539 lines download raw bytes

1jQuery(document).ready(function () {
2  wpsc_reset_responsive_style();
3  jQuery(window).resize(function () {
4    wpsc_reset_responsive_style(true);
5  });
6  wpsc_document_ready();
7
8  // for check there is value in reply box.
9  jQuery(window).on("popstate", function () {
10    if (wpsc_is_description_text()) {
11      if (!confirm(supportcandy.translations.warning_message)) {
12        return;
13      }
14    }
15  });
16
17  // Change the element from h1 to h2
18  if (jQuery('#link-modal-title').length > 0) {
19    jQuery('#link-modal-title').replaceWith('<h2 id="link-modal-title">' + jQuery('#link-modal-title').html() + '</h2>');
20  }
21});
22
23/**
24 * Apply responsive styles based on container size
25 */
26function wpsc_reset_responsive_style(isWindowResize = false) {
27  var lastContainerWidth = supportcandy.lastContainerWidth
28    ? supportcandy.lastContainerWidth
29    : 0;
30  var containerWidth = jQuery("#wpsc-container").outerWidth();
31
32  if (isWindowResize && lastContainerWidth === containerWidth) {
33    return;
34  }
35
36  supportcandy.lastContainerWidth = containerWidth;
37
38  jQuery("#wpsc-container").hide();
39  var style = "xs";
40  if (containerWidth <= 768) {
41    style = "xs";
42  } else if (containerWidth > 768 && containerWidth <= 992) {
43    style = "sm";
44  } else if (containerWidth > 992 && containerWidth <= 1200) {
45    style = "md";
46  } else {
47    style = "lg";
48  }
49  supportcandy.responsiveStyle = style;
50  var styleURL =
51    supportcandy.plugin_url +
52    "framework/responsive/" +
53    style +
54    ".css?version=" +
55    supportcandy.version;
56  var styleEl =
57    '<link id="wpsc-responsive-css" rel="stylesheet" type="text/css" href="' +
58    styleURL +
59    '" />';
60  jQuery("#wpsc-responsive-css").remove();
61  jQuery("head").append(styleEl);
62  wpsc_apply_responsive_styles();
63  jQuery("#wpsc-container").show();
64}
65
66/**
67 * Apply visible elements on screen resize
68 */
69function wpsc_el_reset_visible() {
70  jQuery(
71    ".wpsc-visible-xs,.wpsc-visible-sm,.wpsc-visible-md,.wpsc-visible-lg"
72  ).hide();
73  jQuery(".wpsc-visible-" + supportcandy.responsiveStyle).show();
74}
75
76/**
77 * Apply hidden elements on screen resize
78 */
79function wpsc_el_reset_hidden() {
80  jQuery(
81    ".wpsc-hidden-xs,.wpsc-hidden-sm,.wpsc-hidden-md,.wpsc-hidden-lg"
82  ).show();
83  jQuery(".wpsc-hidden-" + supportcandy.responsiveStyle).hide();
84}
85
86/**
87 * Toggle humbargar menu
88 */
89function wpsc_toggle_humbargar() {
90  var flag = supportcandy.humbargar ? true : false;
91  if (flag) {
92    jQuery(".wpsc-humbargar-menu").hide(
93      "slide",
94      { direction: "right" },
95      200,
96      function () {
97        jQuery(".wpsc-humbargar-overlay").hide();
98      }
99    );
100    supportcandy.humbargar = false;
101  } else {
102    jQuery(".wpsc-humbargar-overlay").show();
103    jQuery(".wpsc-humbargar-menu").show("slide", { direction: "right" }, 200);
104    supportcandy.humbargar = true;
105  }
106}
107
108/**
109 * Close humbargar menu if open
110 */
111function wpsc_close_humbargar() {
112  var flag = supportcandy.humbargar ? true : false;
113  if (flag) {
114    jQuery(".wpsc-humbargar-menu").hide();
115    jQuery(".wpsc-humbargar-overlay").hide();
116    supportcandy.humbargar = false;
117  }
118}
119
120/**
121 * Bulk selector change
122 */
123function wpsc_bulk_select_change() {
124  var checked = jQuery(".wpsc-bulk-selector").is(":checked");
125  jQuery(".wpsc-bulk-select").prop("checked", checked);
126}
127
128/**
129 * Bulk item selector change
130 */
131function wpsc_bulk_item_select_change() {
132  var items = jQuery(".wpsc-bulk-select:checked");
133  var checked = items.length === 0 ? false : true;
134  jQuery(".wpsc-bulk-selector").prop("checked", checked);
135}
136
137/**
138 * Show modal with loading screen
139 */
140function wpsc_show_modal() {
141  jQuery(".wpsc-modal .inner-container").hide();
142  jQuery(".wpsc-modal .loader").show();
143  jQuery(".wpsc-modal").show();
144}
145
146/**
147 * Close modal
148 */
149function wpsc_show_modal_inner_container() {
150  jQuery(".wpsc-modal .loader").hide();
151  jQuery(".wpsc-modal .inner-container").show(
152    "slide",
153    { direction: "up" },
154    200
155  );
156}
157
158/**
159 * Close modal
160 */
161function wpsc_close_modal() {
162  jQuery(".wpsc-modal .inner-container").hide(
163    "slide",
164    { direction: "up" },
165    200,
166    function () {
167      jQuery(".wpsc-modal").hide();
168    }
169  );
170}
171
172/**
173 * OFF toggle button click
174 */
175function wpsc_toggle_off(el) {
176  var off = jQuery(el);
177  var on = jQuery(el).next();
178  var input = on.next();
179
180  if (input.val() === 0) {
181    return;
182  }
183
184  input.val(0);
185  on.removeClass("active");
186  off.addClass("active");
187}
188
189/**
190 * ON toggle button click
191 */
192function wpsc_toggle_on(el) {
193  var on = jQuery(el);
194  var off = jQuery(el).prev();
195  var input = on.next();
196
197  if (input.val() === 1) {
198    return;
199  }
200
201  input.val(1);
202  off.removeClass("active");
203  on.addClass("active");
204}
205
206/**
207 * Scroll to top of WPSC component
208 */
209function wpsc_scroll_top() {
210  jQuery("html, body").animate(
211    {
212      scrollTop: jQuery("#wpsc-container").offset().top - 42,
213    },
214    500
215  );
216}
217
218/**
219 * Toggle individual widgets
220 */
221function wpsc_toggle_mob_it_widgets() {
222  var status = parseInt(
223    jQuery(".wpsc-it-mob-widgets-inner-container").data("status")
224  );
225
226  if (!status) {
227    jQuery(".wpsc-it-mob-widgets-inner-container").slideDown();
228    jQuery(".wpsc-it-mob-widget-trigger-btn .down").hide();
229    jQuery(".wpsc-it-mob-widget-trigger-btn .up").show();
230    jQuery(".wpsc-it-mob-widgets-inner-container").data("status", 1);
231  } else {
232    jQuery(".wpsc-it-mob-widgets-inner-container").slideUp();
233    jQuery(".wpsc-it-mob-widget-trigger-btn .up").hide();
234    jQuery(".wpsc-it-mob-widget-trigger-btn .down").show();
235    jQuery(".wpsc-it-mob-widgets-inner-container").data("status", 0);
236  }
237}
238
239/**
240 *  Get refresh current ticket
241 */
242function wpsc_it_ab_refresh(ticket_id) {
243  if (wpsc_is_description_text()) {
244    if (confirm(supportcandy.translations.warning_message)) {
245      wpsc_clear_saved_draft_reply(ticket_id);
246    } else {
247      return;
248    }
249  }
250
251  wpsc_get_individual_ticket(ticket_id);
252}
253
254/**
255 * Get close current ticket modal UI
256 */
257function wpsc_it_close_ticket(ticket_id, nonce) {
258  if (wpsc_is_description_text()) {
259    if (!confirm(supportcandy.translations.warning_message)) return;
260  }
261
262  var flag = confirm(supportcandy.translations.confirm);
263  if (flag) {
264    if (wpsc_is_description_text()) {
265      wpsc_clear_saved_draft_reply(ticket_id);
266    }
267  } else {
268    return;
269  }
270
271  var data = { action: "wpsc_it_close_ticket", ticket_id, _ajax_nonce: nonce };
272  jQuery.post(supportcandy.ajax_url, data, function (response) {
273    wpsc_run_ajax_background_process();
274    wpsc_after_close_ticket(ticket_id);
275  });
276}
277
278/**
279 * Get duplicate ticket
280 */
281function wpsc_it_get_duplicate_ticket(ticket_id, nonce) {
282  wpsc_show_modal();
283  var data = {
284    action: "wpsc_it_get_duplicate_ticket",
285    ticket_id,
286    _ajax_nonce: nonce,
287  };
288  jQuery.post(supportcandy.ajax_url, data, function (response) {
289    jQuery(".wpsc-modal-header").text(response.title);
290    jQuery(".wpsc-modal-body").html(response.body);
291    jQuery(".wpsc-modal-footer").html(response.footer);
292    wpsc_show_modal_inner_container();
293  });
294}
295
296/**
297 * Set duplicate ticket
298 */
299function wpsc_it_set_duplicate_ticket(el, ticket_id) {
300  if (wpsc_is_description_text()) {
301    if (confirm(supportcandy.translations.warning_message)) {
302      wpsc_clear_saved_draft_reply(ticket_id);
303    } else {
304      return;
305    }
306  }
307
308  var form = jQuery("form.duplicate")[0];
309  var dataform = new FormData(form);
310  jQuery(".wpsc-modal-footer button").attr("disabled", true);
311  jQuery(el).text(supportcandy.translations.please_wait);
312
313  jQuery
314    .ajax({
315      url: supportcandy.ajax_url,
316      type: "POST",
317      data: dataform,
318      processData: false,
319      contentType: false,
320    })
321    .done(function (response) {
322      wpsc_close_modal();
323      wpsc_get_individual_ticket(response.ticket_id);
324    });
325}
326
327/**
328 * Get delete ticket
329 */
330function wpsc_it_delete_ticket(ticket_id, nonce) {
331  if (wpsc_is_description_text()) {
332    if (!confirm(supportcandy.translations.warning_message)) {
333      return;
334    } else {
335      var is_tinymce =
336        typeof tinyMCE != "undefined" &&
337        tinyMCE.activeEditor &&
338        !tinyMCE.activeEditor.isHidden();
339      if (is_tinymce && tinymce.get("description")) {
340        tinyMCE.get("description").setContent("");
341      } else {
342        jQuery("#description").val("");
343      }
344      wpsc_clear_saved_draft_reply(ticket_id);
345    }
346  }
347
348  var flag = confirm(supportcandy.translations.confirm);
349  if (!flag) {
350    return;
351  }
352
353  var data = { action: "wpsc_it_delete_ticket", ticket_id, _ajax_nonce: nonce };
354  jQuery.post(supportcandy.ajax_url, data, function (response) {
355    wpsc_get_ticket_list();
356    wpsc_run_ajax_background_process();
357  });
358}
359
360/**
361 * Restore ticket
362 */
363function wpsc_it_ticket_restore(ticket_id, nonce) {
364  var flag = confirm(supportcandy.translations.confirm);
365  if (!flag) {
366    return;
367  }
368
369  var data = {
370    action: "wpsc_it_ticket_restore",
371    ticket_id,
372    _ajax_nonce: nonce,
373  };
374  jQuery.post(supportcandy.ajax_url, data, function (response) {
375    wpsc_get_individual_ticket(ticket_id);
376  });
377}
378
379/**
380 * Get delete ticket
381 */
382function wpsc_it_archive_ticket(ticket_id, nonce) {
383  if (wpsc_is_description_text()) {
384    if (!confirm(supportcandy.translations.warning_message)) {
385      return;
386    } else {
387      var is_tinymce =
388        typeof tinyMCE != "undefined" &&
389        tinyMCE.activeEditor &&
390        !tinyMCE.activeEditor.isHidden();
391      if (is_tinymce && tinymce.get("description")) {
392        var description = tinyMCE.get("description").setContent("");
393      } else {
394        var description = jQuery("#description").val("");
395      }
396      wpsc_clear_saved_draft_reply(ticket_id);
397    }
398  }
399
400  var flag = confirm(supportcandy.translations.confirm);
401  if (!flag) {
402    return;
403  }
404
405  var data = { action: "wpsc_it_archive_ticket", ticket_id, _ajax_nonce: nonce };
406  jQuery.post(supportcandy.ajax_url, data, function (response) {
407    wpsc_get_ticket_list();
408    wpsc_run_ajax_background_process();
409  });
410}
411
412/**
413 * Delete permanently
414 */
415function wpsc_it_delete_permanently(ticket_id, nonce) {
416  var flag = confirm(supportcandy.translations.delete_permanently);
417  if (!flag) {
418    return;
419  }
420
421  var data = {
422    action: "wpsc_it_delete_permanently",
423    ticket_id,
424    _ajax_nonce: nonce,
425  };
426  jQuery.post(supportcandy.ajax_url, data, function (response) {
427    wpsc_get_ticket_list();
428  });
429}
430
431/**
432 * Block/unblock notifications
433 */
434function wpsc_it_block_unblock_notifications(ticketId, isBlock, nonce) {
435
436	if (!ticketId || typeof isBlock !== 'boolean' || !nonce) {
437		console.error('Invalid parameters passed to wpsc_it_block_unblock_notifications');
438		return;
439	}
440
441	const message = isBlock
442		? supportcandy.translations.block_notifications
443		: supportcandy.translations.unblock_notifications;
444
445	if (!window.confirm(message)) {
446		return;
447	}
448
449  var data = {
450			action: 'wpsc_it_block_unblock_notifications',
451			ticket_id: ticketId,
452			is_block: isBlock ? 1 : 0,
453			_ajax_nonce: nonce,
454  };
455  jQuery.post(supportcandy.ajax_url, data, function (response) {
456    wpsc_get_individual_ticket(ticketId);
457  });
458}
459
460/**
461 *  Get edit ticket subject modal UI
462 */
463function wpsc_it_get_edit_subject(ticket_id) {
464  wpsc_show_modal();
465  var data = {
466    action: "wpsc_it_get_edit_subject",
467    ticket_id,
468  };
469  jQuery.post(supportcandy.ajax_url, data, function (response) {
470    jQuery(".wpsc-modal-header").text(response.title);
471    jQuery(".wpsc-modal-body").html(response.body);
472    jQuery(".wpsc-modal-footer").html(response.footer);
473    wpsc_show_modal_inner_container();
474  });
475}
476
477/**
478 *  Set edit ticket subject
479 */
480function wpsc_it_set_edit_subject(el, ticket_id) {
481  if (wpsc_is_description_text()) {
482    if (!confirm(supportcandy.translations.warning_message)) {
483      wpsc_clear_saved_draft_reply(ticket_id);
484    } else {
485      return;
486    }
487  }
488
489  var form = jQuery("form.edit-subject")[0];
490  var dataform = new FormData(form);
491  jQuery(".wpsc-modal-footer button").attr("disabled", true);
492  jQuery(el).text(supportcandy.translations.please_wait);
493  jQuery
494    .ajax({
495      url: supportcandy.ajax_url,
496      type: "POST",
497      data: dataform,
498      processData: false,
499      contentType: false,
500    })
501    .done(function (res) {
502      wpsc_close_modal();
503      wpsc_get_individual_ticket(ticket_id);
504    });
505}
506
507/**
508 * Get change status modal UI
509 */
510function wpsc_it_get_edit_ticket_status(ticket_id, nonce) {
511  wpsc_show_modal();
512  var data = {
513    action: "wpsc_it_get_edit_ticket_status",
514    ticket_id,
515    _ajax_nonce: nonce,
516  };
517  jQuery.post(supportcandy.ajax_url, data, function (response) {
518    // Set to modal.
519    jQuery(".wpsc-modal-header").text(response.title);
520    jQuery(".wpsc-modal-body").html(response.body);
521    jQuery(".wpsc-modal-footer").html(response.footer);
522    // Display modal.
523    wpsc_show_modal_inner_container();
524  });
525}
526
527/**
528 * Update ticket status
529 *
530 * @param {*} el
531 */
532function wpsc_it_set_edit_ticket_status(el, ticket_id) {
533  if (wpsc_is_description_text()) {
534    if (confirm(supportcandy.translations.warning_message)) {
535      wpsc_clear_saved_draft_reply(ticket_id);
536    } else {
537      return;
538    }
539  }
540
541  var form = jQuery("form.frm-edit-ticket-status")[0];
542  var dataform = new FormData(form);
543  jQuery(".wpsc-modal-footer button").attr("disabled", true);
544  jQuery(el).text(supportcandy.translations.please_wait);
545  jQuery
546    .ajax({
547      url: supportcandy.ajax_url,
548      type: "POST",
549      data: dataform,
550      processData: false,
551      contentType: false,
552    })
553    .done(function (res) {
554      wpsc_close_modal();
555      wpsc_get_individual_ticket(ticket_id);
556      wpsc_run_ajax_background_process();
557    });
558}
559
560/**
561 * Get additional recipients modal UI
562 */
563function wpsc_it_get_add_ar(ticket_id, nonce) {
564  wpsc_show_modal();
565  var data = {
566    action: "wpsc_it_get_add_ar",
567    ticket_id,
568    _ajax_nonce: nonce,
569  };
570  jQuery.post(supportcandy.ajax_url, data, function (response) {
571    jQuery(".wpsc-modal-header").text(response.title);
572    jQuery(".wpsc-modal-body").html(response.body);
573    jQuery(".wpsc-modal-footer").html(response.footer);
574    wpsc_show_modal_inner_container();
575  });
576}
577
578/**
579 * Update additional recipients
580 *
581 * @param {*} el
582 */
583function wpsc_it_set_add_ar(el, ticket_id) {
584  if (wpsc_is_description_text()) {
585    if (confirm(supportcandy.translations.warning_message)) {
586      wpsc_clear_saved_draft_reply(ticket_id);
587    } else {
588      return;
589    }
590  }
591
592  var form = jQuery("form.additional-recipients")[0];
593  var dataform = new FormData(form);
594  jQuery(".wpsc-modal-footer button").attr("disabled", true);
595  jQuery(el).text(supportcandy.translations.please_wait);
596  jQuery
597    .ajax({
598      url: supportcandy.ajax_url,
599      type: "POST",
600      data: dataform,
601      processData: false,
602      contentType: false,
603    })
604    .done(function (res) {
605      wpsc_close_modal();
606      wpsc_get_individual_ticket(ticket_id);
607    });
608}
609
610/**
611 * Get assigned agents modal UI
612 */
613function wpsc_it_get_edit_assigned_agents(ticket_id, nonce) {
614  wpsc_show_modal();
615  var data = {
616    action: "wpsc_it_get_edit_assigned_agents",
617    ticket_id,
618    _ajax_nonce: nonce,
619  };
620  jQuery.post(supportcandy.ajax_url, data, function (response) {
621    jQuery(".wpsc-modal-header").text(response.title);
622    jQuery(".wpsc-modal-body").html(response.body);
623    jQuery(".wpsc-modal-footer").html(response.footer);
624    wpsc_show_modal_inner_container();
625  });
626}
627
628/**
629 * Update assigned agents
630 *
631 * @param {*} el
632 */
633function wpsc_it_set_edit_assigned_agents(el, ticket_id, uniqueId) {
634  if (wpsc_is_description_text()) {
635    if (confirm(supportcandy.translations.warning_message)) {
636      wpsc_clear_saved_draft_reply(ticket_id);
637    } else {
638      return;
639    }
640  }
641
642  var form = jQuery("form.change-assignee." + uniqueId)[0];
643  var dataform = new FormData(form);
644  jQuery(".wpsc-modal-footer button").attr("disabled", true);
645  jQuery(el).text(supportcandy.translations.please_wait);
646  jQuery
647    .ajax({
648      url: supportcandy.ajax_url,
649      type: "POST",
650      data: dataform,
651      processData: false,
652      contentType: false,
653    })
654    .done(function (res) {
655      wpsc_close_modal();
656      if (res.viewPermission) {
657        wpsc_get_individual_ticket(ticket_id);
658      } else {
659        wpsc_get_ticket_list();
660      }
661      wpsc_run_ajax_background_process();
662    });
663}
664
665/**
666 * Get raised by modal UI
667 */
668function wpsc_it_get_edit_raised_by(ticket_id, nonce) {
669  wpsc_show_modal();
670  var data = {
671    action: "wpsc_it_get_edit_raised_by",
672    ticket_id,
673    _ajax_nonce: nonce,
674  };
675  jQuery.post(supportcandy.ajax_url, data, function (response) {
676    // Set to modal.
677    jQuery(".wpsc-modal-header").text(response.title);
678    jQuery(".wpsc-modal-body").html(response.body);
679    jQuery(".wpsc-modal-footer").html(response.footer);
680    // Display modal.
681    wpsc_show_modal_inner_container();
682  });
683}
684
685/**
686 * Update raised by
687 *
688 * @param {*} el
689 */
690function wpsc_it_set_edit_raised_by(el, ticket_id, uniqueId) {
691  if (wpsc_is_description_text()) {
692    if (confirm(supportcandy.translations.warning_message)) {
693      wpsc_clear_saved_draft_reply(ticket_id);
694    } else {
695      return;
696    }
697  }
698
699  var type = jQuery(".type." + uniqueId).val();
700  if (type == "new") {
701    var name = jQuery(".name." + uniqueId)
702      .val()
703      .trim();
704    var email = jQuery(".email." + uniqueId)
705      .val()
706      .trim();
707    if (!name || !email) {
708      return;
709    }
710
711    if (!validateEmail(email)) {
712      alert(supportcandy.translations.raisedByEditWidget.invalidEmail);
713      return;
714    }
715  }
716
717  var form = jQuery("form.change-raised-by." + uniqueId)[0];
718  var dataform = new FormData(form);
719  jQuery(".wpsc-modal-footer button").attr("disabled", true);
720  jQuery(el).text(supportcandy.translations.please_wait);
721  jQuery
722    .ajax({
723      url: supportcandy.ajax_url,
724      type: "POST",
725      data: dataform,
726      processData: false,
727      contentType: false,
728    })
729    .done(function (res) {
730      wpsc_close_modal();
731      wpsc_get_individual_ticket(ticket_id);
732    });
733}
734
735/**
736 * Get ticket fields modal UI
737 */
738function wpsc_it_get_edit_ticket_fields(ticket_id, nonce) {
739  wpsc_show_modal();
740  var data = {
741    action: "wpsc_it_get_edit_ticket_fields",
742    ticket_id,
743    _ajax_nonce: nonce,
744  };
745  jQuery.post(supportcandy.ajax_url, data, function (response) {
746    jQuery(".wpsc-modal-header").text(response.title);
747    jQuery(".wpsc-modal-body").html(response.body);
748    jQuery(".wpsc-modal-footer").html(response.footer);
749    wpsc_show_modal_inner_container();
750  });
751}
752
753/**
754 * Update ticket fields
755 *
756 * @param {*} el
757 */
758function wpsc_it_set_edit_ticket_fields(el, ticket_id) {
759  if (wpsc_is_description_text()) {
760    if (confirm(supportcandy.translations.warning_message)) {
761      wpsc_clear_saved_draft_reply(ticket_id);
762    } else {
763      return;
764    }
765  }
766
767  var form = jQuery("form.change-ticket-fields")[0];
768  var dataform = new FormData(form);
769  jQuery(".wpsc-modal-footer button").attr("disabled", true);
770  jQuery(el).text(supportcandy.translations.please_wait);
771  jQuery
772    .ajax({
773      url: supportcandy.ajax_url,
774      type: "POST",
775      data: dataform,
776      processData: false,
777      contentType: false,
778    })
779    .done(function (res) {
780      wpsc_close_modal();
781      wpsc_get_individual_ticket(ticket_id);
782    });
783}
784
785/**
786 * Get agent only filds modal UI
787 */
788function wpsc_it_get_edit_agentonly_fields(ticket_id, nonce) {
789  wpsc_show_modal();
790  var data = {
791    action: "wpsc_it_get_edit_agentonly_fields",
792    ticket_id,
793    _ajax_nonce: nonce,
794  };
795  jQuery.post(supportcandy.ajax_url, data, function (response) {
796    jQuery(".wpsc-modal-header").text(response.title);
797    jQuery(".wpsc-modal-body").html(response.body);
798    jQuery(".wpsc-modal-footer").html(response.footer);
799    wpsc_show_modal_inner_container();
800  });
801}
802
803/**
804 * Update agent only fields
805 *
806 * @param {*} el
807 */
808function wpsc_it_set_edit_agentonly_fields(el, ticket_id) {
809  if (wpsc_is_description_text()) {
810    if (confirm(supportcandy.translations.warning_message)) {
811      wpsc_clear_saved_draft_reply(ticket_id);
812    } else {
813      return;
814    }
815  }
816
817  var form = jQuery("form.change-agentonly-fields")[0];
818  var dataform = new FormData(form);
819  jQuery(".wpsc-modal-footer button").attr("disabled", true);
820  jQuery(el).text(supportcandy.translations.please_wait);
821  jQuery
822    .ajax({
823      url: supportcandy.ajax_url,
824      type: "POST",
825      data: dataform,
826      processData: false,
827      contentType: false,
828    })
829    .done(function (res) {
830      wpsc_close_modal();
831      wpsc_get_individual_ticket(ticket_id);
832    });
833}
834
835/**
836 * Get edit thread
837 */
838function wpsc_it_get_edit_thread(el, ticket_id, thread_id, nonce) {
839  var thread = jQuery(el).closest(".wpsc-thread");
840  thread.html(supportcandy.loader_html);
841
842  var data = {
843    action: "wpsc_it_get_edit_thread",
844    ticket_id,
845    thread_id,
846    _ajax_nonce: nonce,
847  };
848  jQuery.post(supportcandy.ajax_url, data, function (res) {
849    thread.html(res);
850  });
851}
852
853/**
854 * Set edit thread
855 */
856function wpsc_it_set_edit_thread(el, uniqueId) {
857  var form = jQuery("form.edit-thread")[0];
858  var dataform = new FormData(form);
859
860  var is_tinymce =
861    typeof tinyMCE != "undefined" &&
862    tinyMCE.activeEditor &&
863    !tinyMCE.activeEditor.isHidden();
864  var description =
865    is_tinymce && tinymce.get(uniqueId)
866      ? tinyMCE.get(uniqueId).getContent()
867      : jQuery("#" + uniqueId)
868          .val()
869          .trim();
870  if (!description) {
871    return;
872  }
873
874  dataform.append("thread_content", description);
875
876  var thread = jQuery(el).closest(".wpsc-thread");
877  thread.html(supportcandy.loader_html);
878
879  jQuery
880    .ajax({
881      url: supportcandy.ajax_url,
882      type: "POST",
883      data: dataform,
884      processData: false,
885      contentType: false,
886    })
887    .done(function (res) {
888      if (thread.next().length > 0) {
889        thread.next().before(res);
890      } else {
891        thread.parent().append(res);
892      }
893      thread.remove();
894    });
895}
896
897/**
898 * Delete thread
899 */
900function wpsc_it_thread_delete(el, ticket_id, thread_id, nonce) {
901  var flag = confirm(supportcandy.translations.confirm);
902  if (!flag) {
903    return;
904  }
905
906  var thread = jQuery(el).closest(".wpsc-thread");
907  thread.html(supportcandy.loader_html);
908
909  var data = {
910    action: "wpsc_it_thread_delete",
911    ticket_id,
912    thread_id,
913    _ajax_nonce: nonce,
914  };
915  jQuery.post(supportcandy.ajax_url, data, function (res) {
916    if (res.trim()) {
917      if (thread.next().length > 0) {
918        thread.next().before(res);
919      } else {
920        thread.parent().append(res);
921      }
922    }
923    thread.remove();
924  });
925}
926
927/**
928 * Get thread html
929 */
930function wpsc_it_get_thread(el, ticket_id, thread_id, _ajax_nonce) {
931  var thread = jQuery(el).closest(".wpsc-thread");
932  thread.html(supportcandy.loader_html);
933
934  var data = {
935    action: "wpsc_it_get_thread",
936    ticket_id,
937    thread_id,
938    _ajax_nonce,
939  };
940  jQuery.post(supportcandy.ajax_url, data, function (res) {
941    if (thread.next().length > 0) {
942      thread.next().before(res);
943    } else {
944      thread.parent().append(res);
945    }
946    thread.remove();
947  });
948}
949
950/**
951 * View thread log
952 */
953function wpsc_it_view_thread_log(ticket_id, thread_id, log_id, _ajax_nonce) {
954  wpsc_show_modal();
955  var data = {
956    action: "wpsc_it_view_thread_log",
957    ticket_id,
958    thread_id,
959    log_id,
960    _ajax_nonce,
961  };
962  jQuery.post(supportcandy.ajax_url, data, function (response) {
963    jQuery(".wpsc-modal-header").text(response.title);
964    jQuery(".wpsc-modal-body").html(response.body);
965    jQuery(".wpsc-modal-footer").html(response.footer);
966    wpsc_show_modal_inner_container();
967  });
968}
969
970/**
971 * View thread
972 */
973function wpsc_it_view_deleted_thread(ticket_id, thread_id, _ajax_nonce) {
974  wpsc_show_modal();
975  var data = {
976    action: "wpsc_it_view_deleted_thread",
977    ticket_id,
978    thread_id,
979    _ajax_nonce,
980  };
981  jQuery.post(supportcandy.ajax_url, data, function (response) {
982    jQuery(".wpsc-modal-header").text(response.title);
983    jQuery(".wpsc-modal-body").html(response.body);
984    jQuery(".wpsc-modal-footer").html(response.footer);
985    wpsc_show_modal_inner_container();
986  });
987}
988
989/**
990 * Restore thread
991 */
992function wpsc_it_restore_thread(ticket_id, thread_id, _ajax_nonce) {
993  var flag = confirm(supportcandy.translations.confirm);
994  if (!flag) {
995    return;
996  }
997
998  wpsc_close_modal();
999  var thread = jQuery(".wpsc-thread." + thread_id);
1000  thread.html(supportcandy.loader_html);
1001
1002  var data = {
1003    action: "wpsc_it_restore_thread",
1004    ticket_id,
1005    thread_id,
1006    _ajax_nonce,
1007  };
1008  jQuery.post(supportcandy.ajax_url, data, function (res) {
1009    if (thread.next().length > 0) {
1010      thread.next().before(res);
1011    } else 
1011{
1012      thread.parent().append(res);
1013    }
1014    thread.remove();
1015  });
1016}
1017
1018/**
1019 * Restore thread
1020 */
1021function wpsc_it_thread_delete_permanently(ticket_id, thread_id, _ajax_nonce) {
1022  var flag = confirm(supportcandy.translations.confirm);
1023  if (!flag) {
1024    return;
1025  }
1026
1027  wpsc_close_modal();
1028  var thread = jQuery(".wpsc-thread." + thread_id);
1029  thread.html(supportcandy.loader_html);
1030
1031  var data = {
1032    action: "wpsc_it_thread_delete_permanently",
1033    ticket_id,
1034    thread_id,
1035    _ajax_nonce,
1036  };
1037  jQuery.post(supportcandy.ajax_url, data, function (res) {
1038    thread.remove();
1039  });
1040}
1041
1042/**
1043 * Get macros modal UI
1044 */
1045function wpsc_get_macros() {
1046  wpsc_show_modal();
1047  var data = {
1048    action: "wpsc_get_macros",
1049  };
1050  jQuery.post(supportcandy.ajax_url, data, function (response) {
1051    // Set to modal.
1052    jQuery(".wpsc-modal-header").text(response.title);
1053    jQuery(".wpsc-modal-body").html(response.body);
1054    jQuery(".wpsc-modal-footer").html(response.footer);
1055    // Display modal.
1056    wpsc_show_modal_inner_container();
1057    jQuery("input[type=text]").focus();
1058  });
1059}
1060
1061/**
1062 * Add and condition item
1063 * @param {*} el
1064 * @param {*} uniqueId
1065 */
1066function wpsc_add_and_condition(el, uniqueId) {
1067  var container = jQuery(el)
1068    .closest(".wpsc-form-filter-container")
1069    .find(".and-container");
1070  container.append(jQuery(".and-template." + uniqueId).html());
1071  container = container.find(".and-item:last-child .or-container");
1072  container.append(jQuery(".or-template." + uniqueId).html());
1073  container.find("select").selectWoo();
1074}
1075
1076/**
1077 * Add and condition item
1078 * @param {*} el
1079 * @param {*} uniqueId
1080 */
1081function wpsc_add_or_condition(el, uniqueId) {
1082  var container = jQuery(el).closest(".and-item").find(".or-container");
1083  container.append(jQuery(".or-template." + uniqueId).html());
1084  container.find(":last-child").find("select").selectWoo();
1085}
1086
1087/**
1088 * Remove condition item
1089 * @param {*} el
1090 */
1091function wpsc_remove_condition_item(el) {
1092  var andItem = jQuery(el).closest(".and-item");
1093  jQuery(el).closest(".wpsc-form-filter-item").remove();
1094  if (andItem.find(".or-container").children().length === 0) {
1095    jQuery(andItem).remove();
1096  }
1097}
1098
1099/**
1100 * Get JSON string for the condition input of given name
1101 * @param {*} name
1102 */
1103function wpsc_get_condition_json(name) {
1104  var conditions = [];
1105  jQuery(".wpsc-form-filter-container." + name + " .and-item").each(
1106    function () {
1107      var andCondition = [];
1108      jQuery(this)
1109        .find(".wpsc-form-filter-item")
1110        .each(function () {
1111          var slug = jQuery(this).find("select.filter").first().val();
1112          if (!slug) {
1113            return;
1114          }
1115          var operator = jQuery(this).find("select.operator").first().val();
1116          if (!operator) {
1117            return;
1118          }
1119          var operand_val_1 = jQuery(this).find(".operand_val_1").first().val();
1120          if (!operand_val_1) {
1121            return;
1122          }
1123          var filter = { slug, operator, operand_val_1 };
1124          if (operator === "BETWEEN") {
1125            var operand_val_2 = jQuery(this)
1126              .find(".operand_val_2")
1127              .first()
1128              .val();
1129            if (!operand_val_2) {
1130              return;
1131            }
1132            filter.operand_val_2 = operand_val_2;
1133          }
1134          andCondition.push(filter);
1135        });
1136      conditions.push(andCondition);
1137    }
1138  );
1139  return conditions;
1140}
1141
1142/**
1143 * Get form filter oprators
1144 */
1145function wpsc_get_ticket_filter_operators(el, nonce) {
1146  var content = jQuery(el).closest(".content");
1147  var slug = jQuery(el).val().trim();
1148
1149  content.find(".conditional, .wpsc-inline-loader").remove();
1150  if (!slug) {
1151    return;
1152  }
1153
1154  content.append(supportcandy.inline_loader);
1155  var data = {
1156    action: "wpsc_get_ticket_filter_operators",
1157    slug: slug,
1158    _ajax_nonce: nonce,
1159  };
1160  jQuery.post(supportcandy.ajax_url, data, function (response) {
1161    content.find(".wpsc-inline-loader").remove();
1162    content.append(response);
1163  });
1164}
1165
1166/**
1167 * Get ticket filter value input(s)
1168 */
1169function wpsc_get_ticket_filter_operands(el, id, nonce) {
1170  var content = jQuery(el).closest(".content");
1171  var operator = jQuery(el).val().trim();
1172
1173  content.find(".conditional.operand, .wpsc-inline-loader").remove();
1174  if (!operator) {
1175    return;
1176  }
1177
1178  content.append(supportcandy.inline_loader);
1179  var data = {
1180    action: "wpsc_get_ticket_filter_operands",
1181    operator,
1182    id,
1183    _ajax_nonce: nonce,
1184  };
1185  jQuery.post(supportcandy.ajax_url, data, function (response) {
1186    content.find(".wpsc-inline-loader").remove();
1187    content.append(response);
1188  });
1189}
1190
1191/**
1192 * Single file attachment upload handler
1193 */
1194function wpsc_set_attach_single(el, uniqueId, slug) {
1195  var fileAttachment = el.files[0];
1196  wpsc_file_upload(fileAttachment, uniqueId, slug, true);
1197  jQuery(el).val("");
1198}
1199
1200/**
1201 * Myltiple file attachment upload handler
1202 */
1203function wpsc_set_attach_multiple(el, uniqueId, slug) {
1204  jQuery.each(el.files, function (index, fileAttachment) {
1205    wpsc_file_upload(fileAttachment, uniqueId, slug);
1206  });
1207  jQuery(el).val("");
1208}
1209
1210/**
1211 * Trigger description attachments
1212 */
1213function wpsc_trigger_desc_attachments(uniqueId) {
1214  jQuery("input." + uniqueId).trigger("click");
1215}
1216
1217/**
1218 * Remove an attachment
1219 */
1220function wpsc_remove_attachment(el) {
1221  var single = jQuery(el).data("single");
1222  var uniqueId = jQuery(el).data("uniqueid");
1223  jQuery(el).closest(".wpsc-editor-attachment").remove();
1224  if (single) {
1225    jQuery("input." + uniqueId).show();
1226  }
1227}
1228
1229/**
1230 * Attachment upload progess update
1231 */
1232function wpscAttachmentUploadProgress(e) {
1233  if (e.lengthComputable) {
1234    var max = e.total;
1235    var current = e.loaded;
1236    var progrss = current / max;
1237    e.currentTarget.attachment
1238      .find(".attachment-waiting")
1239      .circleProgress("value", progrss);
1240  }
1241}
1242
1243/**
1244 * Change raised by or create ticket as
1245 */
1246function wpsc_get_change_create_as(nonce) {
1247  wpsc_show_modal();
1248  var data = { action: "wpsc_get_change_create_as", _ajax_nonce: nonce };
1249  jQuery.post(supportcandy.ajax_url, data, function (response) {
1250    // Set to modal.
1251    jQuery(".wpsc-modal-header").text(response.title);
1252    jQuery(".wpsc-modal-body").html(response.body);
1253    jQuery(".wpsc-modal-footer").html(response.footer);
1254    // Display modal.
1255    wpsc_show_modal_inner_container();
1256  });
1257}
1258
1259/**
1260 * Change create as
1261 */
1262function wpsc_set_change_create_as(uniqueId, _ajax_nonce) {
1263  var name = jQuery("input.name." + uniqueId)
1264    .val()
1265    .trim();
1266  var email = jQuery("input.email." + uniqueId)
1267    .val()
1268    .trim();
1269  if (!name || !email) {
1270    alert(supportcandy.translations.req_fields_missing);
1271    return;
1272  }
1273
1274  if (!validateEmail(email)) {
1275    alert(supportcandy.translations.invalidEmail);
1276    return;
1277  }
1278
1279  var data = { action: "wpsc_add_new_create_as", name, email, _ajax_nonce };
1280  jQuery.post(supportcandy.ajax_url, data, function (response) {
1281    jQuery("input.name").val(response.name);
1282    jQuery("input.email").val(response.email);
1283
1284    // add option and select.
1285    if (jQuery("select.create-as").val() != response.id) {
1286      var newOption = new Option(response.label, response.id, false, false);
1287      jQuery("select.create-as").append(newOption);
1288      jQuery("select.create-as").val(response.id).trigger("change");
1289    }
1290
1291    wpsc_close_modal();
1292    wpsc_after_change_create_as();
1293  });
1294}
1295
1296/**
1297 * Check visibility conditions
1298 */
1299function wpsc_check_tff_visibility() {
1300  var conditionalFields = jQuery(".wpsc-tff.conditional");
1301  if (conditionalFields.length === 0) {
1302    return;
1303  }
1304
1305  const callerId = Math.floor(Math.random() * 100000);
1306  supportcandy.checkTffVisibityCallers =
1307    supportcandy.checkTffVisibityCallers || [];
1308  supportcandy.checkTffVisibityCallers.push(callerId);
1309
1310  jQuery(".wpsc-ct-loader").html(supportcandy.inline_loader);
1311  jQuery("select.create-as").attr("disabled", true);
1312
1313  var form = jQuery("form.wpsc-create-ticket")[0];
1314  var dataform = new FormData(form);
1315
1316  var is_tinymce =
1317    typeof tinyMCE != "undefined" &&
1318    tinyMCE.activeEditor &&
1319    !tinyMCE.activeEditor.isHidden();
1320  if (is_tinymce && tinymce.get("description")) {
1321    var description = tinyMCE.get("description").getContent();
1322  } else {
1323    var description = jQuery("#description").val();
1324  }
1325
1326  dataform.append("description", description);
1327  dataform.append("action", "wpsc_check_tff_visibility");
1328  jQuery
1329    .ajax({
1330      url: supportcandy.ajax_url,
1331      type: "POST",
1332      data: dataform,
1333      processData: false,
1334      contentType: false,
1335    })
1336    .done(function (results) {
1337      jQuery.each(results, function (key, val) {
1338        if (parseInt(val) === 0) {
1339          jQuery(".wpsc-tff." + key).addClass("wpsc-hidden");
1340          jQuery(".wpsc-tff." + key).removeClass("wpsc-visible");
1341        } else {
1342          jQuery(".wpsc-tff." + key).removeClass("wpsc-hidden");
1343          jQuery(".wpsc-tff." + key).addClass("wpsc-visible");
1344        }
1345      });
1346      jQuery(".wpsc-ct-loader").html("");
1347      jQuery("select.create-as").removeAttr("disabled");
1348    })
1349    .fail(function (xhr, status, error) {
1350      console.error("AJAX request failed:", status, error);
1351      jQuery(".wpsc-ct-loader").html("");
1352      jQuery("select.create-as").removeAttr("disabled");
1353    })
1354    .always(function () {
1355      // remove callerId from supportcandy.checkTffVisibityCallers array
1356      supportcandy.checkTffVisibityCallers =
1357        supportcandy.checkTffVisibityCallers.filter(function (id) {
1358          return id !== callerId;
1359        });
1360    });
1361}
1362
1363/**
1364 * Validate input for email address
1365 */
1366function validateEmail(email) {
1367  var re =
1368    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
1369  return re.test(email);
1370}
1371
1372/**
1373 * Validate input for URL
1374 */
1375function validateURL(url) {
1376  var re =
1377    /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
1378  return re.test(url);
1379}
1380
1381/**
1382 * Validate input for number
1383 */
1384function validateNumber(number) {
1385  var re = /^[0-9]+$/;
1386  return re.test(number);
1387}
1388
1389/**
1390 * Change event for filter
1391 */
1392function wpsc_tl_filter_change(el, type) {
1393  const filterSlug = jQuery(el).val()?.trim();
1394  const isTicketList = type === 'ticket_list';
1395  supportcandy.prevFilter = supportcandy.ticketList.filters.filterSlug;
1396
1397  if (filterSlug === 'custom') {
1398    wpsc_tl_get_custom_filter( type );
1399    return;
1400  }
1401
1402  supportcandy.ticketList.filters = { filterSlug };
1403  isTicketList ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1404}
1405
1406/**
1407 * Change ticket list page
1408 */
1409function wpsc_tl_set_page(page, type) {
1410  switch (page) {
1411    case "first":
1412      if (supportcandy.ticketList.pagination.current_page == 1) {
1413        return;
1414      }
1415      supportcandy.ticketList.filters.page_no = 1;
1416      break;
1417
1418    case "prev":
1419      if (supportcandy.ticketList.pagination.current_page < 2) {
1420        return;
1421      }
1422      supportcandy.ticketList.filters.page_no =
1423        supportcandy.ticketList.pagination.current_page - 1;
1424      break;
1425
1426    case "next":
1427      if (!supportcandy.ticketList.pagination.has_next_page) {
1428        return;
1429      }
1430      supportcandy.ticketList.filters.page_no =
1431        supportcandy.ticketList.pagination.current_page + 1;
1432      break;
1433
1434    case "last":
1435      if (
1436        supportcandy.ticketList.pagination.current_page ==
1437        supportcandy.ticketList.pagination.total_pages
1438      ) {
1439        return;
1440      }
1441      supportcandy.ticketList.filters.page_no =
1442        supportcandy.ticketList.pagination.total_pages;
1443      break;
1444  }
1445  type === 'ticket_list' ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1446}
1447
1448/**
1449 * Reset ticket list filters
1450 */
1451function wpsc_tl_reset_filter( type ) {
1452  var filters = { filterSlug: "" };
1453  supportcandy.ticketList.filters = filters;
1454  type === 'ticket_list' ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1455}
1456
1457/**
1458 * Search input keyup
1459 */
1460function wpsc_tl_search_keyup(e, el, type) {
1461  if (e.keyCode !== 13) {
1462    return;
1463  }
1464  var search = jQuery(el).val().trim();
1465  supportcandy.ticketList.filters.search = search;
1466  supportcandy.ticketList.filters.page_no = 1;
1467  type === 'ticket_list' ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1468}
1469
1470/**
1471 * Apply filters
1472 */
1473function wpsc_tl_apply_filter_btn_click( type ) {
1474  supportcandy.ticketList.filters.search = jQuery("input.wpsc-search-input")
1475    .val()
1476    .trim();
1477  supportcandy.ticketList.filters.orderby = jQuery(
1478    "select.wpsc-input-sort-by"
1479  ).val();
1480  supportcandy.ticketList.filters.order = jQuery(
1481    "select.wpsc-input-sort-order"
1482  ).val();
1483  supportcandy.ticketList.filters.page_no = 1;
1484  type === 'ticket_list' ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1485}
1486
1487/**
1488 * Sort ticket list by clicking on a sortable column header.
1489 * Toggles ASC/DESC when the same column is clicked again, otherwise
1490 * defaults to ASC for the newly selected column.
1491 */
1492function wpsc_tl_sort_column( slug, type ) {
1493  var filters = supportcandy.ticketList.filters;
1494  if ( filters.orderby === slug ) {
1495    filters.order = filters.order === 'ASC' ? 'DESC' : 'ASC';
1496  } else {
1497    filters.orderby = slug;
1498    filters.order = 'ASC';
1499  }
1500  filters.page_no = 1;
1501  type === 'ticket_list' ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1502}
1503
1504/**
1505 * Get custom filter UI
1506 */
1507function wpsc_tl_get_custom_filter( type ) {
1508  wpsc_show_modal();
1509  
1510  const isTicketList = type === 'ticket_list';
1511  const action = isTicketList ? 'wpsc_get_tl_custom_filter' : 'wpsc_get_atl_custom_filter';
1512
1513  const data = {
1514    action,
1515    _ajax_nonce: supportcandy.nonce,
1516  };
1517  if (supportcandy.ticketList.filters.filterSlug === 'custom') {
1518    data.filters = supportcandy.ticketList.filters;
1519  }
1520  jQuery.post(supportcandy.ajax_url, data, function (response) {
1521    jQuery('.wpsc-modal-header').text(response.title);
1522    jQuery('.wpsc-modal-body').html(response.body);
1523    jQuery('.wpsc-modal-footer').html(response.footer);
1524    wpsc_show_modal_inner_container();
1525  });
1526}
1527
1528/**
1529 * Apply custom filter
1530 */
1531function wpsc_tl_apply_custom_filter(el, type) {
1532
1533  var filters = wpsc_get_condition_json("custom_filters");
1534  if (
1535    filters.length === 0 ||
1536    (filters.length === 1 && filters[0].length === 0)
1537  ) {
1538    alert(supportcandy.translations.req_fields_missing);
1539    return;
1540  }
1541  
1542  const cust_filter = type === "ticket_list" ? "wpsc-tl-custom-filter" : "wpsc-atl-custom-filter";
1543  filters = {
1544    filterSlug: "custom",
1545    "parent-filter": jQuery(
1546      "." + cust_filter + " select[name=parent-filter]"
1547    ).val(),
1548    filters: JSON.stringify(filters),
1549    orderby: jQuery("." + cust_filter + " select[name=sort-by]").val(),
1550    order: jQuery("." + cust_filter + " select[name=sort-order]").val(),
1551    page_no: 1,
1552  };
1553  supportcandy.ticketList.filters = filters;
1554
1555  wpsc_close_modal();
1556  type === "ticket_list" ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1557}
1558
1559/**
1560 * Edit ticket list filter
1561 */
1562function wpsc_tl_edit_filter( type ) {
1563  if (supportcandy.ticketList.filters.filterSlug == "custom") {
1564    wpsc_tl_get_custom_filter( type );
1565  } else {
1566    type === "ticket_list" ? wpsc_tl_get_edit_saved_filter() : wpsc_atl_get_edit_saved_filter();
1567  }
1568}
1569
1570/**
1571 * Add saved filter
1572 */
1573function wpsc_tl_add_saved_filter() {
1574  var filters = wpsc_get_condition_json("custom_filters");
1575  if (
1576    filters.length === 0 ||
1577    (filters.length === 1 && filters[0].length === 0)
1578  ) {
1579    alert(supportcandy.translations.req_fields_missing);
1580    return;
1581  }
1582
1583  filters = {
1584    "parent-filter": jQuery(
1585      ".wpsc-tl-custom-filter select[name=parent-filter]"
1586    ).val(),
1587    filters: JSON.stringify(filters),
1588    orderby: jQuery(".wpsc-tl-custom-filter select[name=sort-by]").val(),
1589    order: jQuery(".wpsc-tl-custom-filter select[name=sort-order]").val(),
1590  };
1591
1592  supportcandy.tempFilters = filters;
1593  wpsc_close_modal();
1594  setTimeout(function () {
1595    wpsc_tl_get_add_saved_filter();
1596  }, 500);
1597}
1598
1599/**
1600 * Get add saved filter ui
1601 */
1602function wpsc_tl_get_add_saved_filter() {
1603  wpsc_show_modal();
1604  var data = { action: "wpsc_tl_get_add_saved_filter" };
1605  jQuery.post(supportcandy.ajax_url, data, function (response) {
1606    // Set to modal.
1607    jQuery(".wpsc-modal-header").text(response.title);
1608    jQuery(".wpsc-modal-body").html(response.body);
1609    jQuery(".wpsc-modal-footer").html(response.footer);
1610    // Display modal.
1611    wpsc_show_modal_inner_container();
1612  });
1613}
1614
1615/**
1616 * Save and apply custom filter
1617 */
1618function wpsc_tl_set_add_saved_filter(el, nonce) {
1619  var label = jQuery("input.wpsc-cf-label").val().trim();
1620  if (!label) {
1621    alert(supportcandy.translations.req_fields_missing);
1622    return;
1623  }
1624
1625  jQuery(".wpsc-modal-footer button").attr("disabled", true);
1626  jQuery(el).text(supportcandy.translations.please_wait);
1627
1628  var data = {
1629    action: "wpsc_tl_set_add_saved_filter",
1630    filters: supportcandy.tempFilters,
1631    _ajax_nonce: nonce,
1632  };
1633  data.filters.label = label;
1634
1635  jQuery.post(supportcandy.ajax_url, data, function (response) {
1636    supportcandy.ticketList.filters = { filterSlug: response.slug };
1637    wpsc_close_modal();
1638    wpsc_get_ticket_list();
1639  });
1640}
1641
1642/**
1643 * Get edit saved filter modal
1644 */
1645function wpsc_tl_get_edit_saved_filter() {
1646  wpsc_show_modal();
1647  var data = {
1648    action: "wpsc_tl_get_edit_saved_filter",
1649    filterSlug: supportcandy.ticketList.filters.filterSlug,
1650    _ajax_nonce: supportcandy.nonce,
1651  };
1652  jQuery.post(supportcandy.ajax_url, data, function (response) {
1653    // Set to modal.
1654    jQuery(".wpsc-modal-header").text(response.title);
1655    jQuery(".wpsc-modal-body").html(response.body);
1656    jQuery(".wpsc-modal-footer").html(response.footer);
1657    // Display modal.
1658    wpsc_show_modal_inner_container();
1659  });
1660}
1661
1662/**
1663 * Set edit saved filter
1664 */
1665function wpsc_tl_set_edit_saved_filter(el) {
1666  var label = jQuery("input.wpsc-cf-label").val().trim();
1667  if (!label) {
1668    alert(supportcandy.translations.req_fields_missing);
1669    return;
1670  }
1671
1672  var filters = wpsc_get_condition_json("custom_filters");
1673  if (
1674    filters.length === 0 ||
1675    (filters.length === 1 && filters[0].length === 0)
1676  ) {
1677    alert(supportcandy.translations.req_fields_missing);
1678    return;
1679  }
1680
1681  jQuery(".wpsc-modal-footer button").attr("disabled", true);
1682  jQuery(el).text(supportcandy.translations.please_wait);
1683
1684  var form = jQuery(".wpsc-tl-custom-filter")[0];
1685  var dataform = new FormData(form);
1686  dataform.append("filters", JSON.stringify(filters));
1687
1688  jQuery
1689    .ajax({
1690      url: supportcandy.ajax_url,
1691      type: "POST",
1692      data: dataform,
1693      processData: false,
1694      contentType: false,
1695    })
1696    .done(function (res) {
1697      wpsc_close_modal();
1698      wpsc_get_ticket_list();
1699    });
1700}
1701
1702/**
1703 * Delete saved filter
1704 */
1705function wpsc_tl_delete_saved_filter( type ) {
1706  var flag = confirm(supportcandy.translations.confirm);
1707  if (!flag) {
1708    return;
1709  }
1710
1711  var data = {
1712    action: "wpsc_tl_delete_saved_filter",
1713    slug: supportcandy.ticketList.filters.filterSlug,
1714    _ajax_nonce: supportcandy.nonce,
1715  };
1716  jQuery.post(supportcandy.ajax_url, data, function (res) {
1717    supportcandy.ticketList.filters = { filterSlug: "" };
1718    wpsc_get_ticket_list();
1719  });
1720}
1721
1722/**
1723 * Close custom filter modal
1724 */
1725function wpsc_tl_close_custom_filter_modal() {
1726  jQuery("select.wpsc-input-filter").val(supportcandy.prevFilter);
1727  wpsc_close_modal();
1728}
1729
1730/**
1731 * Get form filter oprators
1732 */
1733function wpsc_tc_get_operators(el, nonce) {
1734  var content = jQuery(el).closest(".content");
1735  var slug = jQuery(el).val().trim();
1736
1737  content.find(".conditional, .wpsc-inline-loader").remove();
1738  if (!slug) {
1739    return;
1740  }
1741
1742  content.append(supportcandy.inline_loader);
1743  var data = {
1744    action: "wpsc_tc_get_operators",
1745    slug: slug,
1746    _ajax_nonce: nonce,
1747  };
1748  jQuery.post(supportcandy.ajax_url, data, function (response) {
1749    content.find(".wpsc-inline-loader").remove();
1750    content.append(response);
1751  });
1752}
1753
1754/**
1755 * Get email notification condition operands
1756 */
1757function wpsc_tc_get_operand(el, slug, nonce) {
1758  var content = jQuery(el).closest(".content");
1759  var operator = jQuery(el).val().trim();
1760
1761  content.find(".conditional.operand, .wpsc-inline-loader").remove();
1762  if (!operator) {
1763    return;
1764  }
1765
1766  content.append(supportcandy.inline_loader);
1767  var data = {
1768    action: "wpsc_tc_get_operand",
1769    operator,
1770    slug,
1771    _ajax_nonce: nonce,
1772  };
1773  jQuery.post(supportcandy.ajax_url, data, function (response) {
1774    content.find(".wpsc-inline-loader").remove();
1775    content.append(response);
1776  });
1777}
1778
1779/**
1780 * Add image to editor
1781 *
1782 * @param {*} editor
1783 */
1784function wpsc_add_custom_image_tinymce(editor, nonce) {
1785  wpsc_show_modal();
1786  var data = {
1787    action: "wpsc_add_custom_image_tinymce",
1788    editor_id: editor.id,
1789    _ajax_nonce: nonce,
1790  };
1791  jQuery.post(supportcandy.ajax_url, data, function (response) {
1792    // Set to modal.
1793    jQuery(".wpsc-modal-header").text(response.title);
1794    jQuery(".wpsc-modal-body").html(response.body);
1795    jQuery(".wpsc-modal-footer").html(response.footer);
1796    // Display modal.
1797    wpsc_show_modal_inner_container();
1798  });
1799}
1800
1801/**
1802 * Check whether given url has image or not
1803 *
1804 * @param {*} url
1805 */
1806function isValidImageURL(url) {
1807  if (typeof url !== "string") {
1808    return false;
1809  }
1810  return !!url.match(/\w+\.(jpg|jpeg|gif|png|tiff|bmp)$/gi);
1811}
1812
1813/**
1814 * Upload image to tinymce editor
1815 *
1816 * @param {*} el
1817 */
1818function wpsc_insert_editor_img(ed) {
1819  if (
1820    jQuery("#wpsc-tinymce-image-url").val().trim() === "" &&
1821    validateURL(jQuery("#wpsc-tinymce-image-url").val().trim()) &&
1822    isValidImageURL(jQuery("#wpsc-tinymce-image-url").val().trim())
1823  ) {
1824    alert(supportcandy.translations.req_fields_error);
1825    return;
1826  }
1827
1828  if (jQuery("#wpsc-tinymce-image-width").val().trim() === "") {
1829    alert(supportcandy.translations.req_fields_error);
1830    return;
1831  }
1832
1833  if (jQuery("#wpsc-tinymce-image-height").val().trim() === "") {
1834    alert(supportcandy.translations.req_fields_error);
1835    return;
1836  }
1837
1838  var is_tinymce =
1839    typeof tinyMCE != "undefined" &&
1840    tinyMCE.activeEditor &&
1841    !tinyMCE.activeEditor.isHidden();
1842  if (!is_tinymce) {
1843    return;
1844  }
1845
1846  wpsc_close_modal();
1847
1848  var source = jQuery("#wpsc-tinymce-image-url").val().trim();
1849  var height = jQuery("#wpsc-tinymce-image-height").val().trim();
1850  var width = jQuery("#wpsc-tinymce-image-width").val().trim();
1851  tinymce
1852    .get(ed)
1853    .execCommand(
1854      "mceInsertContent",
1855      false,
1856      '<img height="' +
1857        height +
1858        '" width="' +
1859        width +
1860        '" src="' +
1861        source +
1862        '"/>'
1863    );
1864}
1865
1866/**
1867 * Add image to editor
1868 *
1869 * @param {*} editor
1870 */
1871function wpsc_edit_custom_image_tinymce(editor, image_obj, nonce) {
1872  if (!(editor && image_obj)) {
1873    alert(supportcandy.translations.req_fields_error);
1874    return;
1875  }
1876
1877  wpsc_show_modal();
1878  var data = {
1879    action: "wpsc_edit_custom_image_tinymce",
1880    editor_id: editor.id,
1881    height: image_obj.height,
1882    width: image_obj.width,
1883    src: image_obj.src,
1884    _ajax_nonce: nonce,
1885  };
1886  jQuery.post(supportcandy.ajax_url, data, function (response) {
1887    // Set to modal.
1888    jQuery(".wpsc-modal-header").text(response.title);
1889    jQuery(".wpsc-modal-body").html(response.body);
1890    jQuery(".wpsc-modal-footer").html(response.footer);
1891    // Display modal.
1892    wpsc_show_modal_inner_container();
1893  });
1894}
1895
1896/**
1897 * Get create ticket from thread
1898 */
1899function wpsc_it_thread_new_ticket(el, ticket_id, thread_id, nonce) {
1900  wpsc_show_modal();
1901  var data = {
1902    action: "wpsc_it_thread_new_ticket",
1903    ticket_id,
1904    thread_id,
1905    _ajax_nonce: nonce,
1906  };
1907  jQuery.post(supportcandy.ajax_url, data, function (response) {
1908    jQuery(".wpsc-modal-header").text(response.title);
1909    jQuery(".wpsc-modal-body").html(response.body);
1910    jQuery(".wpsc-modal-footer").html(response.footer);
1911    wpsc_show_modal_inner_container();
1912  });
1913}
1914
1915/**
1916 * Get thread info
1917 *
1918 * @param {*} thread_id
1919 */
1920function wpsc_it_thread_info(el, ticket_id, thread_id, nonce, type = 'ticket_list') {
1921  wpsc_show_modal();
1922  var data = {
1923    action: type === 'ticket_list' ? "wpsc_it_thread_info" : "wpsc_it_archive_thread_info",
1924    thread_id,
1925    ticket_id,
1926    _ajax_nonce: nonce,
1927  };
1928  jQuery.post(supportcandy.ajax_url, data, function (response) {
1929    jQuery(".wpsc-modal-header").text(response.title);
1930    jQuery(".wpsc-modal-body").html(response.body);
1931    jQuery(".wpsc-modal-footer").html(response.footer);
1932    wpsc_show_modal_inner_container();
1933  });
1934}
1935
1936/**
1937 * Set thread new ticket
1938 */
1939function wpsc_it_set_thread_new_ticket(el, thread_id, nonce) {
1940  var form = jQuery("form.new_ticket_from_thread")[0];
1941  var dataform = new FormData(form);
1942  jQuery(".wpsc-modal-footer button").attr("disabled", true);
1943  jQuery(el).text(supportcandy.translations.please_wait);
1944  jQuery
1945    .ajax({
1946      url: supportcandy.ajax_url,
1947      type: "POST",
1948      data: dataform,
1949      processData: false,
1950      contentType: false,
1951    })
1952    .done(function (response) {
1953      wpsc_close_modal();
1954      wpsc_get_individual_ticket(response.ticket_id);
1955    });
1956}
1957
1958/**
1959 * Get customer other tickets
1960 */
1961function wpsc_get_rb_other_tickets(el, ticket_id, nonce) {
1962  wpsc_show_modal();
1963  var data = {
1964    action: "wpsc_get_rb_other_tickets",
1965    ticket_id,
1966    _ajax_nonce: nonce,
1967  };
1968  jQuery.post(supportcandy.ajax_url, data, function (response) {
1969    jQuery(".wpsc-modal-header").text(response.title);
1970    jQuery(".wpsc-modal-body").html(response.body);
1971    jQuery(".wpsc-modal-footer").html(response.footer);
1972    wpsc_show_modal_inner_container();
1973  });
1974}
1975
1976/**
1977 * Get raised by info
1978 */
1979function wpsc_get_rb_info(el, ticket_id, nonce) {
1980  wpsc_show_modal();
1981  var data = { action: "wpsc_get_rb_info", ticket_id, _ajax_nonce: nonce };
1982  jQuery.post(supportcandy.ajax_url, data, function (response) {
1983    jQuery(".wpsc-modal-header").text(response.title);
1984    jQuery(".wpsc-modal-body").html(response.body);
1985    jQuery(".wpsc-modal-footer").html(response.footer);
1986    wpsc_show_modal_inner_container();
1987  });
1988}
1989
1990/**
1991 * Thread expander.
1992 */
1993function wpsc_init_thread_expanders($threadTexts) {
1994  $threadTexts.filter(".wpsc-collapsed").each(function () {
1995    if (this.scrollHeight > this.clientHeight + 1) {
1996      jQuery(this)
1997        .parent()
1998        .find(".wpsc-ticket-thread-expander")
1999        .text(supportcandy.translations.view_more)
2000        .show();
2001    } else {
2002      // Content already fits within the collapsed max-height; no need
2003      // for an expander link.
2004      jQuery(this).removeClass("wpsc-collapsed");
2005    }
2006  });
2007}
2008
2009function wpsc_ticket_thread_expander_toggle(el) {
2010  var $threadText = jQuery(el).parent().find(".thread-text");
2011  if ($threadText.hasClass("wpsc-collapsed")) {
2012    $threadText.removeClass("wpsc-collapsed").addClass("wpsc-expanded");
2013    jQuery(el).text(supportcandy.translations.view_less);
2014  } else {
2015    $threadText.removeClass("wpsc-expanded").addClass("wpsc-collapsed");
2016    jQuery(el).text(supportcandy.translations.view_more);
2017  }
2018}
2019
2020/**
2021 * Get change bulk status
2022 */
2023function wpsc_bulk_change_status(nonce) {
2024  var items = jQuery(".wpsc-bulk-select:checked");
2025  var checked = items.length === 0 ? false : true;
2026  jQuery(".wpsc-bulk-selector").prop("checked", checked);
2027  if (items.length != 0) {
2028    var ticket_ids = jQuery(".wpsc-bulk-select:checked")
2029      .map(function () {
2030        return this.value;
2031      })
2032      .get();
2033    wpsc_show_modal();
2034    var data = {
2035      action: "wpsc_bulk_change_status",
2036      ticket_ids,
2037      _ajax_nonce: nonce,
2038    };
2039    jQuery.post(supportcandy.ajax_url, data, function (response) {
2040      // Set to modal.
2041      jQuery(".wpsc-modal-header").text(response.title);
2042      jQuery(".wpsc-modal-body").html(response.body);
2043      jQuery(".wpsc-modal-footer").html(response.footer);
2044      // Display modal.
2045      wpsc_show_modal_inner_container();
2046    });
2047  }
2048}
2049
2050/**
2051 * Load older threads
2052 */
2053function wpsc_load_older_threads(el, ticket_id) {
2054  if (supportcandy.reply_form_position === "top") {
2055    jQuery(".wpsc-it-thread-section-container").append(
2056      supportcandy.loader_html
2057    );
2058  } else {
2059    jQuery(".wpsc-it-thread-section-container").prepend(
2060      supportcandy.loader_html
2061    );
2062  }
2063
2064  var oldBtnContainer = jQuery(el).parent();
2065  oldBtnContainer.hide();
2066
2067  var data = {
2068    action: "wpsc_load_older_threads",
2069    ticket_id,
2070    last_thread: supportcandy.threads.last_thread,
2071  };
2072  jQuery.post(supportcandy.ajax_url, data, function (response) {
2073    jQuery(".wpsc-it-thread-section-container").find(".wpsc-loader").remove();
2074    // add threads.
2075    var $container = jQuery(".wpsc-it-thread-section-container");
2076    var $before = $container.find(".thread-text");
2077    if (supportcandy.reply_form_position === "top") {
2078      $container.append(response.threads);
2079    } else {
2080      var scrollHeightBody = jQuery("body").prop("scrollHeight");
2081      var windowScrollTop = jQuery(window).scrollTop();
2082      $container.prepend(response.threads);
2083      var scrollDiff = jQuery("body").prop("scrollHeight") - scrollHeightBody;
2084      jQuery(window).scrollTop(windowScrollTop + scrollDiff);
2085    }
2086    // Newly injected threads also need their "view more" state evaluated -
2087    // the initial page-load script only runs once, so without this the
2088    // lazily-loaded threads never get truncated/expandable.
2089    wpsc_init_thread_expanders($container.find(".thread-text").not($before));
2090    // show btn if there are more threads available.
2091    if (response.has_next_page) {
2092      supportcandy.threads.last_thread = response.last_thread;
2093      oldBtnContainer.show();
2094    }
2095  });
2096}
2097
2098/**
2099 * Set change bulk status
2100 */
2101function wpsc_set_bulk_change_status(el) {
2102  var form = jQuery("form.frm-edit-bulk-status")[0];
2103  var dataform = new FormData(form);
2104  jQuery(".wpsc-modal-footer button").attr("disabled", true);
2105  jQuery(el).text(supportcandy.translations.please_wait);
2106  jQuery
2107    .ajax({
2108      url: supportcandy.ajax_url,
2109      type: "POST",
2110      data: dataform,
2111      processData: false,
2112      contentType: false,
2113    })
2114    .done(function (res) {
2115      wpsc_close_modal();
2116      wpsc_get_ticket_list();
2117      wpsc_run_ajax_background_process();
2118    });
2119}
2120
2121/**
2122 * Get bulk assign agent
2123 */
2124function wpsc_bulk_assign_agents(nonce) {
2125  var items = jQuery(".wpsc-bulk-select:checked");
2126  var checked = items.length === 0 ? false : true;
2127  jQuery(".wpsc-bulk-selector").prop("checked", checked);
2128  if (items.length != 0) {
2129    var ticket_ids = jQuery(".wpsc-bulk-select:checked")
2130      .map(function () {
2131        return this.value;
2132      })
2133      .get();
2134    wpsc_show_modal();
2135    var data = {
2136      action: "wpsc_bulk_assign_agents",
2137      ticket_ids,
2138      _ajax_nonce: nonce,
2139    };
2140    jQuery.post(supportcandy.ajax_url, data, function (response) {
2141      // Set to modal.
2142      jQuery(".wpsc-modal-header").text(response.title);
2143      jQuery(".wpsc-modal-body").html(response.body);
2144      jQuery(".wpsc-modal-footer").html(response.footer);
2145      // Display modal.
2146      wpsc_show_modal_inner_container();
2147    });
2148  }
2149}
2150
2151/**
2152 * Set change assign agent
2153 */
2154function wpsc_set_bulk_assign_agent(el) {
2155  var form = jQuery("form.frm-bulk-assign-agent")[0];
2156  var dataform = new FormData(form);
2157  jQuery(".wpsc-modal-footer button").attr("disabled", true);
2158  jQuery(el).text(supportcandy.translations.please_wait);
2159  jQuery
2160    .ajax({
2161      url: supportcandy.ajax_url,
2162      type: "POST",
2163      data: dataform,
2164      processData: false,
2165      contentType: false,
2166    })
2167    .done(function (res) {
2168      wpsc_close_modal();
2169      wpsc_get_ticket_list();
2170      wpsc_run_ajax_background_process();
2171    });
2172}
2173
2174/**
2175 * Get bulk assign tags
2176 */
2177function wpsc_bulk_assign_tags(nonce) {
2178  var items = jQuery(".wpsc-bulk-select:checked");
2179  var checked = items.length === 0 ? false : true;
2180  jQuery(".wpsc-bulk-selector").prop("checked", checked);
2181  if (items.length != 0) {
2182    var ticket_ids = jQuery(".wpsc-bulk-select:checked")
2183      .map(function () {
2184        return this.value;
2185      })
2186      .get();
2187    wpsc_show_modal();
2188    var data = {
2189      action: "wpsc_bulk_assign_tags",
2190      ticket_ids,
2191      _ajax_nonce: nonce,
2192    };
2193    jQuery.post(supportcandy.ajax_url, data, function (response) {
2194      // Set to modal.
2195      jQuery(".wpsc-modal-header").text(response.title);
2196      jQuery(".wpsc-modal-body").html(response.body);
2197      jQuery(".wpsc-modal-footer").html(response.footer);
2198      // Display modal.
2199      wpsc_show_modal_inner_container();
2200    });
2201  }
2202}
2203
2204/**
2205 * Set change assign tags
2206 */
2207function wpsc_set_bulk_assign_tag(el) {
2208  var form = jQuery("form.frm-bulk-assign-tags")[0];
2209  var dataform = new FormData(form);
2210
2211  var tags = dataform.getAll("tags[]");
2212  if (!tags.length) {
2213    return;
2214  }
2215
2216  jQuery(".wpsc-modal-footer button").attr("disabled", true);
2217  jQuery(el).text(supportcandy.translations.please_wait);
2218  jQuery
2219    .ajax({
2220      url: supportcandy.ajax_url,
2221      type: "POST",
2222      data: dataform,
2223      processData: false,
2224      contentType: false,
2225    })
2226    .done(function (res) {
2227      wpsc_close_modal();
2228      wpsc_get_ticket_list();
2229      wpsc_run_ajax_background_process();
2230    });
2231}
2232
2233/**
2234 * Get bulk archive ticket
2235 */
2236function wpsc_bulk_archive_tickets(nonce) {
2237  var items = jQuery(".wpsc-bulk-select:checked");
2238  var checked = items.length === 0 ? false : true;
2239  jQuery(".wpsc-bulk-selector").prop("checked", checked);
2240  if (items.length != 0) {
2241    var ticket_ids = jQuery(".wpsc-bulk-select:checked")
2242      .map(function () {
2243        return this.value;
2244      })
2245      .get();
2246    var flag = confirm(supportcandy.translations.confirm);
2247    if (!flag) {
2248      return;
2249    }
2250
2251    var data = {
2252      action: "wpsc_bulk_archive_tickets",
2253      ticket_ids,
2254      _ajax_nonce: nonce,
2255    };
2256    jQuery.post(supportcandy.ajax_url, data, function (response) {
2257      wpsc_get_ticket_list();
2258      wpsc_run_ajax_background_process();
2259    });
2260  }
2261}
2262
2263/**
2264 * Get bulk permanently delete ticket
2265 */
2266function wpsc_bulk_permanently_delete_tickets(nonce) {
2267  var items = jQuery(".wpsc-bulk-select:checked");
2268  var checked = items.length === 0 ? false : true;
2269  jQuery(".wpsc-bulk-selector").prop("checked", checked);
2270  if (items.length != 0) {
2271    var ticket_ids = jQuery(".wpsc-bulk-select:checked")
2272      .map(function () {
2273        return this.value;
2274      })
2275      .get();
2276    var flag = confirm(supportcandy.translations.delete_permanently);
2277    if (!flag) {
2278      return;
2279    }
2280
2281    var data = {
2282      action: "wpsc_bulk_permanently_delete_tickets",
2283      ticket_ids,
2284      _ajax_nonce: nonce,
2285    };
2286    jQuery.post(supportcandy.ajax_url, data, function (response) {
2287      wpsc_get_ticket_list();
2288      wpsc_run_ajax_background_process();
2289    });
2290  }
2291}
2292
2293/**
2294 * Set agent working hrs
2295 */
2296function wpsc_set_agent_wh_hrs(el) {
2297  const form = jQuery(".wpsc-frm-agent-wh")[0];
2298  const dataform = new FormData(form);
2299  jQuery(el).text(supportcandy.translations.please_wait);
2300  jQuery(".wpsc-section-container").html(supportcandy.loader_html);
2301  jQuery
2302    .ajax({
2303      url: supportcandy.ajax_url,
2304      type: "POST",
2305      data: dataform,
2306      processData: false,
2307      contentType: false,
2308    })
2309    .done(function (res) {
2310      window.location.reload();
2311    });
2312}
2313
2314/**
2315 * Set add agent exception
2316 */
2317function wpsc_set_add_agent_wh_exception(el) {
2318  var title = jQuery("input[name=title]").val();
2319  var from_date = jQuery("input[name=exception_date]").val();
2320  if (!title || !from_date) {
2321    alert(supportcandy.translations.req_fields_missing);
2322    return;
2323  }
2324  const form = jQuery(".wpsc-frm-add-agent-exception")[0];
2325  const dataform = new FormData(form);
2326  jQuery(el).text(supportcandy.translations.please_wait);
2327  jQuery
2328    .ajax({
2329      url: supportcandy.ajax_url,
2330      type: "POST",
2331      data: dataform,
2332      processData: false,
2333      contentType: false,
2334    })
2335    .done(function (res) {
2336      window.location.reload();
2337    });
2338}
2339
2340/**
2341 * Update agent working hour exception
2342 */
2343function wpsc_set_edit_agent_wh_exception(el) {
2344  var title = jQuery("input[name=title]").val();
2345  var from_date = jQuery("input[name=exception_date]").val();
2346  if (!title || !from_date) {
2347    alert(supportcandy.translations.req_fields_missing);
2348    return;
2349  }
2350
2351  const form = jQuery(".wpsc-frm-edit-agent-exception")[0];
2352  const dataform = new FormData(form);
2353  jQuery(el).text(supportcandy.translations.please_wait);
2354  jQuery
2355    .ajax({
2356      url: supportcandy.ajax_url,
2357      type: "POST",
2358      data: dataform,
2359      processData: false,
2360      contentType: false,
2361    })
2362    .done(function (res) {
2363      window.location.reload();
2364    });
2365}
2366
2367/**
2368 * Get agent profile holiday actions
2369 */
2370function wpsc_get_ap_leaves_actions(dateSelected, nonce) {
2371  supportcandy.temp.dateSelected = dateSelected;
2372  wpsc_show_modal();
2373  var data = {
2374    action: "wpsc_get_ap_leaves_actions",
2375    dateSelected,
2376    _ajax_nonce: nonce,
2377  };
2378  jQuery.post(supportcandy.ajax_url, data, function (response) {
2379    // Set to modal.
2380    jQuery(".wpsc-modal-header").text(response.title);
2381    jQuery(".wpsc-modal-body").html(response.body);
2382    jQuery(".wpsc-modal-footer").html(response.footer);
2383    // Display modal.
2384    wpsc_show_modal_inner_container();
2385  });
2386}
2387
2388/**
2389 * Set agent profile holiday actions
2390 */
2391function wpsc_set_ap_leaves_actions(el) {
2392  const form = jQuery(".wpsc-frm-ap-holiday-actions")[0];
2393  const dataform = new FormData(form);
2394  dataform.append("dateSelected", supportcandy.temp.dateSelected);
2395  jQuery(el).text(supportcandy.translations.please_wait);
2396  jQuery
2397    .ajax({
2398      url: supportcandy.ajax_url,
2399      type: "POST",
2400      data: dataform,
2401      processData: false,
2402      contentType: false,
2403    })
2404    .done(function (response) {
2405      jQuer
2405y.each(supportcandy.temp.dateSelected, function (index, value) {
2406        if (response.action == "add" && response.is_recurring == 1) {
2407          jQuery("td")
2408            .find("[data-date=" + value + "]")
2409            .css({ "background-color": "#eb4d4b" });
2410        } else if (response.action == "add" && response.is_recurring == 0) {
2411          jQuery("td")
2412            .find("[data-date=" + value + "]")
2413            .css({ "background-color": "#f0932b" });
2414        } else {
2415          jQuery("td")
2416            .find("[data-date=" + value + "]")
2417            .css("background-color", "unset");
2418        }
2419      });
2420      supportcandy.temp.holidayList = response.holidayList;
2421      wpsc_close_modal();
2422    });
2423}
2424
2425/**
2426 * Clear dates
2427 */
2428function wpsc_clear_date(el) {
2429  jQuery(el).prev().val("");
2430
2431  var conditionalFields = jQuery(".wpsc-tff.conditional");
2432  if (conditionalFields.length !== 0) {
2433    wpsc_check_tff_visibility();
2434  }
2435}
2436
2437/**
2438 * Check whether there is text in reply box
2439 */
2440function wpsc_is_description_text() {
2441  var is_tinymce =
2442    typeof tinyMCE != "undefined" &&
2443    tinyMCE.activeEditor &&
2444    !tinyMCE.activeEditor.isHidden();
2445  if (is_tinymce && tinymce.get("description")) {
2446    var description = tinyMCE.get("description").getContent();
2447  } else {
2448    var description = jQuery("#description").val();
2449  }
2450
2451  var flag = false;
2452  if (description && description.length != 0) {
2453    flag = true;
2454  }
2455
2456  return flag;
2457}
2458
2459/**
2460 * Get Edit customer info
2461 *
2462 * @param {Int} ticket_id
2463 */
2464function wpsc_get_edit_rb_info(el, ticket_id, nonce) {
2465  wpsc_show_modal();
2466  var data = { action: "wpsc_get_edit_rb_info", ticket_id, _ajax_nonce: nonce };
2467  jQuery.post(supportcandy.ajax_url, data, function (response) {
2468    jQuery(".wpsc-modal-header").text(response.title);
2469    jQuery(".wpsc-modal-body").html(response.body);
2470    jQuery(".wpsc-modal-footer").html(response.footer);
2471    wpsc_show_modal_inner_container();
2472  });
2473}
2474
2475/**
2476 * Set edit customer info
2477 *
2478 * @param {object} el
2479 * @param {Int} ticket_id
2480 */
2481function wpsc_set_edit_rb_info(el, ticket_id, nonce) {
2482  var form = jQuery("form.frm-edit-rb-info")[0];
2483  var dataform = new FormData(form);
2484  jQuery(".wpsc-modal-footer button").attr("disabled", true);
2485  jQuery(el).text(supportcandy.translations.please_wait);
2486  jQuery
2487    .ajax({
2488      url: supportcandy.ajax_url,
2489      type: "POST",
2490      data: dataform,
2491      processData: false,
2492      contentType: false,
2493    })
2494    .done(function (res) {
2495      wpsc_close_modal();
2496      setTimeout(function () {
2497        wpsc_get_rb_info(el, ticket_id, nonce);
2498      }, 500);
2499    });
2500}
2501
2502/**
2503 * Get tickets based on filter set
2504 */
2505function wpsc_get_archive_tickets() {
2506  jQuery(".wpsc-bulk-selector").prop("checked", false);
2507  jQuery(".wpsc-ticket-list").html(supportcandy.loader_html);
2508
2509  var data = {
2510    action: "wpsc_get_archive_tickets",
2511    _ajax_nonce: supportcandy.nonce,
2512    is_frontend : supportcandy.is_frontend
2513  };
2514  if (
2515    typeof supportcandy.ticketList != "undefined" &&
2516    typeof supportcandy.ticketList.filters != "undefined"
2517  ) {
2518    data.filters = supportcandy.ticketList.filters;
2519  }
2520
2521  jQuery.post(supportcandy.ajax_url, data, function (response) {
2522    jQuery(".wpsc-filter-actions").html(response.filter_actions);
2523    jQuery(".wpsc-ticket-bulk-actions").html(response.bulk_actions);
2524    jQuery(".wpsc-ticket-list").html(response.tickets);
2525    jQuery(".wpsc-pagination-txt").text(response.pagination_str);
2526    jQuery("select.wpsc-input-filter").val(response.filters.filterSlug);
2527    jQuery("select.wpsc-input-sort-by").val(response.filters.orderby);
2528    jQuery("select.wpsc-input-sort-order").val(response.filters.order);
2529    jQuery("input.wpsc-search-input").val(response.filters.search);
2530    if (response.pagination.total_pages > 1) {
2531      jQuery(".wpsc-pagination-btn").show();
2532    } else {
2533      jQuery(".wpsc-pagination-btn").hide();
2534    }
2535    supportcandy.ticketList = {
2536      filters: response.filters,
2537      pagination: response.pagination,
2538    };
2539  });
2540}
2541
2542/**
2543 * Get tickets based on filter set
2544 */
2545function wpsc_get_tickets() {
2546
2547  if (supportcandy.ticket_list_loading) {
2548    return;
2549  }
2550
2551  supportcandy.ticket_list_loading = true;
2552
2553  jQuery(".wpsc-bulk-selector").prop("checked", false);
2554  jQuery(".wpsc-ticket-list").html(supportcandy.loader_html);
2555
2556  var data = {
2557    action: "wpsc_get_tickets",
2558    _ajax_nonce: supportcandy.nonce,
2559    is_frontend: supportcandy.is_frontend,
2560  };
2561
2562  if (
2563    typeof supportcandy.ticketList != "undefined" &&
2564    typeof supportcandy.ticketList.filters != "undefined"
2565  ) {
2566    data.filters = supportcandy.ticketList.filters;
2567  }
2568
2569  jQuery.post(supportcandy.ajax_url, data, function (response) {
2570
2571    jQuery(".wpsc-filter-actions").html(response.filter_actions);
2572    jQuery(".wpsc-ticket-bulk-actions").html(response.bulk_actions);
2573    jQuery(".wpsc-ticket-list").html(response.tickets);
2574    jQuery(".wpsc-pagination-txt").text(response.pagination_str);
2575    jQuery("select.wpsc-input-filter").val(response.filters.filterSlug);
2576    jQuery("select.wpsc-input-sort-by").val(response.filters.orderby);
2577    jQuery("select.wpsc-input-sort-order").val(response.filters.order);
2578    jQuery("input.wpsc-search-input").val(response.filters.search);
2579
2580    if (response.pagination.total_pages > 1) {
2581      jQuery(".wpsc-pagination-btn").show();
2582    } else {
2583      jQuery(".wpsc-pagination-btn").hide();
2584    }
2585
2586    supportcandy.ticketList = {
2587      filters: response.filters,
2588      pagination: response.pagination,
2589    };
2590
2591  }).always(function(){
2592      supportcandy.ticket_list_loading = false;
2593  });
2594}
2595
2596/**
2597 * Copy ticket url
2598 *
2599 * @param {int} ticket_id
2600 */
2601function wpsc_it_copy_url(ticket_id) {
2602  var temp = jQuery("<input>");
2603  jQuery("body").append(temp);
2604  temp.val(jQuery("#wpsc-ticket-url").text()).select();
2605  document.execCommand("copy");
2606  temp.remove();
2607  alert(supportcandy.translations.copy_url);
2608}
2609
2610/**
2611 * Open auto-refresh toggle
2612 */
2613function wpsc_get_tl_auto_refresh() {
2614  wpsc_show_modal();
2615
2616  // Get modal parts from static modals.
2617  var headerText = jQuery(
2618    ".wpsc-tl_snippets .auto-refresh .modal-header"
2619  ).text();
2620  var bodyHTML = jQuery(".wpsc-tl_snippets .auto-refresh .modal-body").html();
2621  var footerHTML = jQuery(
2622    ".wpsc-tl_snippets .auto-refresh .modal-footer"
2623  ).html();
2624
2625  // Set them to modal.
2626  jQuery(".wpsc-modal-header").text(headerText);
2627  jQuery(".wpsc-modal-body").html(bodyHTML);
2628  jQuery(".wpsc-modal-footer").html(footerHTML);
2629
2630  // Set current status.
2631  var status = supportcandy.tl_auto_refresh ? supportcandy.tl_auto_refresh : 0;
2632  var el = status
2633    ? jQuery(".wpsc-modal .toggle-on")
2634    : jQuery(".wpsc-modal .toggle-off");
2635  jQuery(".wpsc-modal-body input").val(status);
2636  status ? wpsc_toggle_on(el) : wpsc_toggle_off(el);
2637
2638  // Display modal.
2639  wpsc_show_modal_inner_container();
2640}
2641
2642/**
2643 * Set auto-refresh settings
2644 */
2645function wpsc_set_tl_auto_refresh(el) {
2646  jQuery(".wpsc-modal-footer button").attr("disabled", true);
2647  jQuery(el).text(supportcandy.translations.please_wait);
2648  var status = parseInt(jQuery(".wpsc-modal-body input").val());
2649  wpsc_close_modal();
2650  if (supportcandy.tl_auto_refresh !== status) {
2651    supportcandy.tl_auto_refresh = status;
2652    if (status === 1) {
2653      wpsc_tl_auto_refresh();
2654    }
2655  }
2656}
2657
2658/**
2659 * Auto refresh
2660 */
2661function wpsc_tl_auto_refresh() {
2662
2663  if (supportcandy.tl_auto_refresh_started) {
2664    return;
2665  }
2666
2667  supportcandy.tl_auto_refresh_started = true;
2668
2669  function run_refresh() {
2670
2671    if (
2672      supportcandy.current_section === "ticket-list" &&
2673      !supportcandy.ticketListIsIndividual &&
2674      supportcandy.tl_auto_refresh === 1 &&
2675      !(
2676        jQuery(".wpsc-bulk-select:checked").length ||
2677        jQuery(".wpsc-search-input").is(":focus")
2678      )
2679    ) {
2680      wpsc_get_tickets();
2681    }
2682
2683    setTimeout(run_refresh, 60000);
2684  }
2685
2686  run_refresh();
2687}
2688
2689/**
2690 * User logout
2691 */
2692function wpsc_user_logout(el, nonce) {
2693  jQuery(el).text(supportcandy.translations.please_wait);
2694  var data = { action: "wpsc_user_logout", _ajax_nonce: nonce };
2695  jQuery.post(supportcandy.ajax_url, data, function (res) {
2696    window.location.reload();
2697  });
2698}
2699
2700/**
2701 * Close model popup on escape key.
2702 */
2703jQuery(document).keydown(function (e) {
2704  if (e.keyCode == 27) {
2705    wpsc_close_modal();
2706  }
2707});
2708
2709/**
2710 * Get customer info
2711 */
2712function wpsc_view_customer_info(customer_id, _ajax_nonce) {
2713  wpsc_show_modal();
2714  var data = { action: "wpsc_view_customer_info", customer_id, _ajax_nonce };
2715  jQuery.post(supportcandy.ajax_url, data, function (response) {
2716    jQuery(".wpsc-modal-header").text(response.title);
2717    jQuery(".wpsc-modal-body").html(response.body);
2718    jQuery(".wpsc-modal-footer").html(response.footer);
2719    wpsc_show_modal_inner_container();
2720  });
2721}
2722
2723/**
2724 * Get edit customer info
2725 */
2726function wpsc_get_edit_customer_info(customer_id, _ajax_nonce) {
2727  wpsc_show_modal();
2728  var data = {
2729    action: "wpsc_get_edit_customer_info",
2730    customer_id,
2731    _ajax_nonce,
2732  };
2733  jQuery.post(supportcandy.ajax_url, data, function (response) {
2734    jQuery(".wpsc-modal-header").text(response.title);
2735    jQuery(".wpsc-modal-body").html(response.body);
2736    jQuery(".wpsc-modal-footer").html(response.footer);
2737    wpsc_show_modal_inner_container();
2738  });
2739}
2740
2741/**
2742 * Set edit customer info
2743 */
2744function wpsc_set_edit_customer_info(el, id, nonce) {
2745  var form = jQuery("form.frm-edit-customer-info")[0];
2746  var dataform = new FormData(form);
2747  jQuery(".wpsc-modal-footer button").attr("disabled", true);
2748  jQuery(el).text(supportcandy.translations.please_wait);
2749
2750  jQuery
2751    .ajax({
2752      url: supportcandy.ajax_url,
2753      type: "POST",
2754      data: dataform,
2755      processData: false,
2756      contentType: false,
2757    })
2758    .done(function (res) {
2759      wpsc_close_modal();
2760      wpsc_scroll_top();
2761      wpsc_view_customer_detailed_info(res.customer_id, res.nonce);
2762    });
2763}
2764
2765/**
2766 * Get customer logs
2767 */
2768function wpsc_view_customer_logs(customer_id, _ajax_nonce) {
2769  wpsc_show_modal();
2770  var data = { action: "wpsc_view_customer_logs", customer_id, _ajax_nonce };
2771  jQuery.post(supportcandy.ajax_url, data, function (response) {
2772    jQuery(".wpsc-modal-header").text(response.title);
2773    jQuery(".wpsc-modal-body").html(response.body);
2774    jQuery(".wpsc-modal-footer").html(response.footer);
2775    wpsc_show_modal_inner_container();
2776  });
2777}
2778
2779/**
2780 *
2781 * @param {*} ticket
2782 */
2783function wpsc_clear_saved_draft_reply(ticket_id) {
2784  var draft_replies = JSON.parse(localStorage.getItem("wpsc_auto_draft")) || {};
2785  var is_tinymce =
2786    typeof tinyMCE != "undefined" &&
2787    tinyMCE.activeEditor &&
2788    !tinyMCE.activeEditor.isHidden();
2789  if (is_tinymce && tinymce.get("description")) {
2790    tinyMCE.get("description").setContent("");
2791  }
2792  jQuery("#description").val("");
2793  if (!ticket_id) return;
2794  delete draft_replies[ticket_id];
2795  localStorage.setItem("wpsc_auto_draft", JSON.stringify(draft_replies));
2796}
2797
2798/**
2799 * Run background processes.
2800 */
2801function wpsc_run_ajax_background_process() {
2802  var data = { action: "wpsc_run_ajax_background_process" };
2803  jQuery.post(supportcandy.ajax_url, data, function (response) {});
2804}
2805
2806/**
2807 * Assign ticket to self
2808 */
2809function wpsc_self_assign_ticket(ticket_id, nonce) {
2810  var data = {
2811    action: "wpsc_self_assign_ticket",
2812    ticket_id,
2813    _ajax_nonce: nonce,
2814  };
2815
2816  jQuery.post(supportcandy.ajax_url, data, function (response) {
2817    wpsc_get_individual_ticket(ticket_id);
2818  });
2819}
2820
2821/**
2822 * Delete wpsc_auto_draft localStorage after 24 hr
2823 */
2824function wpsc_delete_auto_draft() {
2825  var draft_cron_time = localStorage.getItem("wpsc_draft_cron_time");
2826  if (!draft_cron_time) {
2827    localStorage.setItem("wpsc_draft_cron_time", new Date().getTime());
2828  }
2829  var diff = new Date().getTime() - draft_cron_time;
2830  if (diff < 3600000) {
2831    return;
2832  }
2833  var draft_replies = JSON.parse(localStorage.getItem("wpsc_auto_draft")) || {};
2834  Object.keys(draft_replies).forEach((ticket_id) => {
2835    var timeDifference =
2836      new Date() - new Date(draft_replies[ticket_id].date_updated);
2837    if (timeDifference > 86400000) {
2838      delete draft_replies[ticket_id];
2839    }
2840  });
2841  localStorage.setItem("wpsc_auto_draft", JSON.stringify(draft_replies));
2842  localStorage.setItem("wpsc_draft_cron_time", new Date().getTime());
2843}
2844
2845/**
2846 * Delete customer
2847 */
2848function wpsc_delete_customer(customer_id, nonce) {
2849  if (!confirm(supportcandy.translations.customer_delete_warn)) {
2850    return;
2851  }
2852
2853  var data = {
2854    action: "wpsc_delete_customer",
2855    customer_id,
2856    _ajax_nonce: nonce,
2857  };
2858  jQuery.post(supportcandy.ajax_url, data, function (response) {
2859    window.location.reload();
2860  });
2861}
2862
2863/**
2864 * Dashboard - Get filter from and to dates based on duration slug
2865 */
2866function wpsc_db_set_filter_duration_dates(duration) {
2867  let dateStr = "";
2868  let date = new Date();
2869  let from_date, to_date;
2870
2871  switch (duration) {
2872    case "today":
2873      dateStr = date.toISOString().split("T")[0];
2874      date_from_to = {
2875        from: dateStr,
2876        to: dateStr,
2877      };
2878      break;
2879
2880    case "yesterday":
2881      date.setDate(date.getDate() - 1);
2882      dateStr = date.toISOString().split("T")[0];
2883      date_from_to = {
2884        from: dateStr,
2885        to: dateStr,
2886      };
2887      break;
2888
2889    case "this-week":
2890      let firstDayOfWeek = date.getDay() === 0 ? 6 : date.getDay() - 1;
2891      date.setDate(date.getDate() - firstDayOfWeek);
2892      from_date = date.toISOString().split("T")[0];
2893      date.setDate(date.getDate() + 6);
2894      to_date = date.toISOString().split("T")[0];
2895      date_from_to = {
2896        from: from_date,
2897        to: to_date,
2898      };
2899      break;
2900
2901    case "last-week":
2902      let firstDayOfLastWeek = date.getDay() === 0 ? 7 : date.getDay();
2903      date.setDate(date.getDate() - firstDayOfLastWeek - 6);
2904      from_date = date.toISOString().split("T")[0];
2905      date.setDate(date.getDate() + 6);
2906      to_date = date.toISOString().split("T")[0];
2907      date_from_to = {
2908        from: from_date,
2909        to: to_date,
2910      };
2911      break;
2912
2913    case "last-7":
2914      date.setDate(date.getDate() - 1); // Exclude today
2915      to_date = date.toISOString().split("T")[0];
2916      date.setDate(date.getDate() - 6); // Last 7 days from yesterday
2917      from_date = date.toISOString().split("T")[0];
2918      date_from_to = {
2919        from: from_date,
2920        to: to_date,
2921      };
2922      break;
2923
2924    case "last-30-days":
2925      date.setDate(date.getDate() - 1); // Exclude today
2926      to_date = date.toISOString().split("T")[0];
2927      date.setDate(date.getDate() - 29); // Last 30 days from yesterday
2928      from_date = date.toISOString().split("T")[0];
2929      date_from_to = {
2930        from: from_date,
2931        to: to_date,
2932      };
2933      break;
2934
2935    case "this-month":
2936      let this_month_starts = new Date(date.getFullYear(), date.getMonth(), 1);
2937      let this_month_ends = new Date(
2938        date.getFullYear(),
2939        date.getMonth() + 1,
2940        0
2941      );
2942      date_from_to = {
2943        from: `${this_month_starts.getFullYear()}-${(
2944          this_month_starts.getMonth() + 1
2945        )
2946          .toString()
2947          .padStart(2, "0")}-${this_month_starts
2948          .getDate()
2949          .toString()
2950          .padStart(2, "0")}`,
2951        to: `${this_month_ends.getFullYear()}-${(this_month_ends.getMonth() + 1)
2952          .toString()
2953          .padStart(2, "0")}-${this_month_ends
2954          .getDate()
2955          .toString()
2956          .padStart(2, "0")}`,
2957      };
2958      break;
2959
2960    case "last-month":
2961      date.setMonth(date.getMonth() - 1);
2962      let last_month_starts = new Date(date.getFullYear(), date.getMonth(), 1);
2963      let last_month_ends = new Date(
2964        date.getFullYear(),
2965        date.getMonth() + 1,
2966        0
2967      );
2968      date_from_to = {
2969        from: `${last_month_starts.getFullYear()}-${(
2970          last_month_starts.getMonth() + 1
2971        )
2972          .toString()
2973          .padStart(2, "0")}-${last_month_starts
2974          .getDate()
2975          .toString()
2976          .padStart(2, "0")}`,
2977        to: `${last_month_ends.getFullYear()}-${(last_month_ends.getMonth() + 1)
2978          .toString()
2979          .padStart(2, "0")}-${last_month_ends
2980          .getDate()
2981          .toString()
2982          .padStart(2, "0")}`,
2983      };
2984      break;
2985
2986    case "this-quarter":
2987      let this_quarter_month_starts = Math.floor(date.getMonth() / 3) * 3;
2988      let quarter_start = new Date(
2989        date.getFullYear(),
2990        this_quarter_month_starts,
2991        1
2992      );
2993      let quarter_end = new Date(
2994        date.getFullYear(),
2995        this_quarter_month_starts + 3,
2996        0
2997      );
2998      date_from_to = {
2999        from: `${quarter_start.getFullYear()}-${(quarter_start.getMonth() + 1)
3000          .toString()
3001          .padStart(2, "0")}-${quarter_start
3002          .getDate()
3003          .toString()
3004          .padStart(2, "0")}`,
3005        to: `${quarter_end.getFullYear()}-${(quarter_end.getMonth() + 1)
3006          .toString()
3007          .padStart(2, "0")}-${quarter_end
3008          .getDate()
3009          .toString()
3010          .padStart(2, "0")}`,
3011      };
3012      break;
3013
3014    case "last-quarter":
3015      let last_quarter_month_ends = Math.floor(date.getMonth() / 3) * 3 - 1; // End month of last quarter
3016      let last_quarter_month_starts = last_quarter_month_ends - 2; // Start month of last quarter
3017      if (last_quarter_month_ends < 0) {
3018        last_quarter_month_ends += 12;
3019        last_quarter_month_starts += 12;
3020      }
3021      let last_quarter_start = new Date(
3022        date.getFullYear(),
3023        last_quarter_month_starts,
3024        1
3025      );
3026      let last_quarter_end = new Date(
3027        date.getFullYear(),
3028        last_quarter_month_ends + 1,
3029        0
3030      );
3031      date_from_to = {
3032        from: `${last_quarter_start.getFullYear()}-${(
3033          last_quarter_start.getMonth() + 1
3034        )
3035          .toString()
3036          .padStart(2, "0")}-${last_quarter_start
3037          .getDate()
3038          .toString()
3039          .padStart(2, "0")}`,
3040        to: `${last_quarter_end.getFullYear()}-${(
3041          last_quarter_end.getMonth() + 1
3042        )
3043          .toString()
3044          .padStart(2, "0")}-${last_quarter_end
3045          .getDate()
3046          .toString()
3047          .padStart(2, "0")}`,
3048      };
3049      break;
3050
3051    case "this-year":
3052      date_from_to = {
3053        from: `${date.getFullYear()}-01-01`,
3054        to: `${date.getFullYear()}-12-31`,
3055      };
3056      break;
3057
3058    case "last-year":
3059      date_from_to = {
3060        from: `${date.getFullYear() - 1}-01-01`,
3061        to: `${date.getFullYear() - 1}-12-31`,
3062      };
3063      break;
3064  }
3065  return date_from_to;
3066}
3067
3068/**
3069 * Get batches for async report loading
3070 */
3071function wpsc_rp_get_baches(fromDate, toDate) {
3072  let diffTime = Math.abs(toDate - fromDate);
3073  let diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) + 1;
3074
3075  let batches = [];
3076  let batchData = [];
3077
3078  if (diffDays == 1) {
3079    // one day.
3080
3081    for (var i = 0; i <= 23; i++) {
3082      var hr = String(i);
3083      hr = hr.length == 1 ? "0" + hr : hr;
3084      batchData.push({
3085        fromDate: fromDate.toISOString().split("T")[0] + " " + hr + ":00:00",
3086        toDate: fromDate.toISOString().split("T")[0] + " " + hr + ":59:59",
3087        durationType: "day",
3088      });
3089    }
3090  } else if (diffDays <= 31) {
3091    // days.
3092
3093    do {
3094      batchData.push({
3095        fromDate: fromDate.toISOString().split("T")[0] + " 00:00:00",
3096        toDate: fromDate.toISOString().split("T")[0] + " 23:59:59",
3097        durationType: "days",
3098      });
3099      fromDate.setDate(fromDate.getDate() + 1);
3100    } while (fromDate <= toDate);
3101  } else if (diffDays <= 180) {
3102    // weeks.
3103
3104    var from = fromDate.toISOString().split("T")[0];
3105    fromDate.setDate(fromDate.getDate() + (7 - fromDate.getDay()));
3106    batchData.push({
3107      fromDate: from + " 00:00:00",
3108      toDate: fromDate.toISOString().split("T")[0] + " 23:59:59",
3109      durationType: "weeks",
3110    });
3111    do {
3112      fromDate.setDate(fromDate.getDate() + 1);
3113      var from = new Date(fromDate.toLocaleDateString("en-CA"));
3114      fromDate.setDate(fromDate.getDate() + 6);
3115      var to = new Date(fromDate.toLocaleDateString("en-CA"));
3116      to = toDate < to ? new Date(toDate.toLocaleDateString("en-CA")) : to;
3117      batchData.push({
3118        fromDate: from.toISOString().split("T")[0] + " 00:00:00",
3119        toDate: to.toISOString().split("T")[0] + " 23:59:59",
3120        durationType: "weeks",
3121      });
3122    } while (fromDate < toDate);
3123  } else if (diffDays <= 720) {
3124    // months.
3125
3126    // convert toDate from local to UTC.
3127    toDate = new Date(
3128      toDate.getFullYear(),
3129      toDate.getMonth(),
3130      toDate.getDate()
3131    );
3132
3133    var from = fromDate.toISOString().split("T")[0];
3134    if (fromDate.getMonth() == 11) {
3135      fromDate = new Date(fromDate.getFullYear(), 11, 31);
3136    } else {
3137      fromDate = new Date(fromDate.getFullYear(), fromDate.getMonth() + 1, 0);
3138    }
3139    batchData.push({
3140      fromDate: from + " 00:00:00",
3141      toDate: fromDate.toISOString().split("T")[0] + " 23:59:59",
3142      durationType: "months",
3143    });
3144    do {
3145      fromDate.setDate(fromDate.getDate() + 1);
3146      var from = new Date(fromDate.toLocaleDateString("en-CA"));
3147      if (fromDate.getMonth() == 11) {
3148        fromDate = new Date(fromDate.getFullYear(), 11, 31);
3149      } else {
3150        fromDate = new Date(fromDate.getFullYear(), fromDate.getMonth() + 1, 0);
3151      }
3152      var to = new Date(fromDate.toLocaleDateString("en-CA"));
3153      to = toDate < to ? new Date(toDate.toLocaleDateString("en-CA")) : to;
3154      batchData.push({
3155        fromDate: from.toISOString().split("T")[0] + " 00:00:00",
3156        toDate: to.toISOString().split("T")[0] + " 23:59:59",
3157        durationType: "months",
3158      });
3159    } while (fromDate < toDate);
3160  } else {
3161    // years.
3162
3163    // convert toDate from local to UTC.
3164    toDate = new Date(
3165      toDate.getFullYear(),
3166      toDate.getMonth(),
3167      toDate.getDate()
3168    );
3169
3170    var from = fromDate.toISOString().split("T")[0];
3171    fromDate = new Date(fromDate.getFullYear(), 11, 31);
3172    batchData.push({
3173      fromDate: from + " 00:00:00",
3174      toDate: fromDate.toISOString().split("T")[0] + " 23:59:59",
3175      durationType: "years",
3176    });
3177    do {
3178      fromDate.setDate(fromDate.getDate() + 1);
3179      var from = new Date(fromDate.toLocaleDateString("en-CA"));
3180      fromDate = new Date(fromDate.getFullYear(), 11, 31);
3181      var to = new Date(fromDate.toLocaleDateString("en-CA"));
3182      to = toDate < to ? new Date(toDate.toLocaleDateString("en-CA")) : to;
3183      batchData.push({
3184        fromDate: from.toISOString().split("T")[0] + " 00:00:00",
3185        toDate: to.toISOString().split("T")[0] + " 23:59:59",
3186        durationType: "years",
3187      });
3188    } while (fromDate < toDate);
3189  }
3190
3191  // devide into batches of 5.
3192  var batchLength = batchData.length;
3193  for (var i = 0; i < batchLength; i = i + 5) {
3194    var batch = [];
3195    var counter = 0;
3196    do {
3197      var index = i + counter;
3198      if (index == batchLength) {
3199        break;
3200      }
3201      batch.push(batchData[index]);
3202      counter++;
3203    } while (counter < 5);
3204    batches.push(batch);
3205  }
3206
3207  return batches;
3208}
3209
3210function wpsc_generate_random_color() {
3211  var minLuminance = 0.7;
3212
3213  do {
3214    var color = Math.floor(Math.random() * 16777216); // 0x000000 to 0xFFFFFF
3215    var red = (color >> 16) & 0xff;
3216    var green = (color >> 8) & 0xff;
3217    var blue = color & 0xff;
3218
3219    // Calculate luminance (brightness).
3220    var luminance = (0.299 * red + 0.587 * green + 0.114 * blue) / 255;
3221  } while (luminance < minLuminance);
3222
3223  return "#" + color.toString(16).toUpperCase();
3224}
3225
3226// onclick load respective card ticket list
3227function wpsc_get_dbc_ticket_list(element, card) {
3228  var count = parseInt(jQuery(element).find(".wpsc-dbc-count").text(), 10);
3229  if (count === 0) {
3230    return;
3231  }
3232  var data = {
3233    action: "wpsc_dash_card_count_filter",
3234    card: card,
3235    view: supportcandy.is_frontend,
3236    _ajax_nonce: supportcandy.nonce,
3237  };
3238  jQuery.post(supportcandy.ajax_url, data, function (response) {
3239    if (response.url) {
3240      window.location = response.url;
3241    }
3242  });
3243}
3244
3245// onclick load respective card ticket list
3246function wpsc_get_agent_status_ticket_list(agent_id, status_id, nonce) {
3247  var data = {
3248    action: "wpsc_get_agent_status_ticket_list",
3249    agent_id: agent_id,
3250    status_id: status_id,
3251    view: supportcandy.is_frontend,
3252    _ajax_nonce: nonce,
3253  };
3254  jQuery.post(supportcandy.ajax_url, data, function (response) {
3255    if (response.url) {
3256      window.location = response.url;
3257    }
3258  });
3259}
3260
3261/**
3262 * Refresh tags list
3263 */
3264function wpsc_it_refresh_tags(ticket_id) {
3265  jQuery(".wpsc-it-tag-body").html(supportcandy.loader_html);
3266
3267  var data = { action: "wpsc_it_refresh_tags", ticket_id };
3268  jQuery.post(supportcandy.ajax_url, data, function (res) {
3269    jQuery(".wpsc-it-tag-body").html(res);
3270  });
3271}
3272
3273/**
3274 * View user wordpress profile
3275 */
3276function wpsc_wp_user_profile(el, ticket_id, nonce) {
3277  var data = {
3278    action: "wpsc_wp_user_profile",
3279    ticket_id,
3280    _ajax_nonce: nonce,
3281  };
3282  jQuery.post(supportcandy.ajax_url, data, function (response) {
3283    if (response.url) {
3284      window.open(response.url, "_blank");
3285    }
3286  });
3287}
3288
3289/**
3290 * Restore archived ticket
3291 */
3292function wpsc_it_restore_archived(ticket_id, nonce) {
3293
3294  if (!confirm(supportcandy.translations.confirm)){
3295    return;
3296  }
3297
3298  var data = {
3299    action: "wpsc_it_restore_archived",
3300    ticket_id,
3301    _ajax_nonce: nonce
3302  };
3303
3304  jQuery.post(
3305    supportcandy.ajax_url,
3306    data,
3307    function (response) {
3308      wpsc_get_archive_ticket_list();
3309    }
3310  );
3311}
3312
3313/**
3314 *  Get refresh current ticket
3315 */
3316function wpsc_it_archive_ab_refresh(ticket_id) {
3317  if (wpsc_is_description_text()) {
3318    if (confirm(supportcandy.translations.confirm)) {
3319      wpsc_clear_saved_draft_reply(ticket_id);
3320    } else {
3321      return;
3322    }
3323  }
3324
3325  wpsc_get_individual_archive_ticket(ticket_id);
3326}
3327
3328/**
3329 * Load older threads
3330 */
3331function wpsc_load_older_archive_threads(el, ticket_id) {
3332  if (supportcandy.reply_form_position === "top") {
3333    jQuery(".wpsc-it-thread-section-container").append(
3334      supportcandy.loader_html
3335    );
3336  } else {
3337    jQuery(".wpsc-it-thread-section-container").prepend(
3338      supportcandy.loader_html
3339    );
3340  }
3341
3342  var oldBtnContainer = jQuery(el).parent();
3343  oldBtnContainer.hide();
3344
3345  var data = {
3346    action: "wpsc_load_older_archive_threads",
3347    ticket_id,
3348    last_thread: supportcandy.threads.last_thread,
3349  };
3350  jQuery.post(supportcandy.ajax_url, data, function (response) {
3351    jQuery(".wpsc-it-thread-section-container").find(".wpsc-loader").remove();
3352    // add threads.
3353    var $container = jQuery(".wpsc-it-thread-section-container");
3354    var $before = $container.find(".thread-text");
3355    if (supportcandy.reply_form_position === "top") {
3356      $container.append(response.threads);
3357    } else {
3358      var scrollHeightBody = jQuery("body").prop("scrollHeight");
3359      var windowScrollTop = jQuery(window).scrollTop();
3360      $container.prepend(response.threads);
3361      var scrollDiff = jQuery("body").prop("scrollHeight") - scrollHeightBody;
3362      jQuery(window).scrollTop(windowScrollTop + scrollDiff);
3363    }
3364    // Newly injected threads also need their "view more" state evaluated -
3365    // the initial page-load script only runs once, so without this the
3366    // lazily-loaded threads never get truncated/expandable.
3367    wpsc_init_thread_expanders($container.find(".thread-text").not($before));
3368    // show btn if there are more threads available.
3369    if (response.has_next_page) {
3370      supportcandy.threads.last_thread = response.last_thread;
3371      oldBtnContainer.show();
3372    }
3373  });
3374}
3375
3376
3377/**
3378 * Delete permanently
3379 */
3380function wpsc_iat_delete_permanently(ticket_id, nonce) {
3381  var flag = confirm(supportcandy.translations.delete_permanently);
3382  if (!flag) {
3383    return;
3384  }
3385
3386  var data = {
3387    action: "wpsc_iat_delete_permanently",
3388    ticket_id,
3389    _ajax_nonce: nonce,
3390  };
3391  jQuery.post(supportcandy.ajax_url, data, function (response) {
3392    wpsc_get_archive_ticket_list();
3393  });
3394}
3395
3396/**
3397 * Get bulk delete archive ticket
3398 */
3399function wpsc_bulk_delete_archive_tickets(nonce) {
3400  var items = jQuery(".wpsc-bulk-select:checked");
3401  var checked = items.length === 0 ? false : true;
3402  jQuery(".wpsc-bulk-selector").prop("checked", checked);
3403  if (items.length != 0) {
3404    var ticket_ids = jQuery(".wpsc-bulk-select:checked")
3405      .map(function () {
3406        return this.value;
3407      })
3408      .get();
3409    var flag = confirm(supportcandy.translations.delete_permanently);
3410    if (!flag) {
3411      return;
3412    }
3413
3414    var data = {
3415      action: "wpsc_bulk_delete_archive_tickets",
3416      ticket_ids,
3417      _ajax_nonce: nonce,
3418    };
3419    jQuery.post(supportcandy.ajax_url, data, function (response) {
3420      wpsc_get_archive_ticket_list();
3421      wpsc_run_ajax_background_process();
3422    });
3423  }
3424}
3425
3426/**
3427 * Restore bulk archive tickets
3428 */
3429function wpsc_bulk_restore_archive_tickets(nonce) {
3430  var items = jQuery(".wpsc-bulk-select:checked");
3431  var checked = items.length === 0 ? false : true;
3432  jQuery(".wpsc-bulk-selector").prop("checked", checked);
3433  if (items.length != 0) {
3434    var ticket_ids = jQuery(".wpsc-bulk-select:checked")
3435      .map(function () {
3436        return this.value;
3437      })
3438      .get();
3439    var flag = confirm(supportcandy.translations.confirm);
3440    if (!flag) {
3441      return;
3442    }
3443    var data = {
3444      action: "wpsc_bulk_restore_archive_tickets",
3445      ticket_ids,
3446      _ajax_nonce: nonce,
3447    };
3448    jQuery.post(supportcandy.ajax_url, data, function (response) {
3449      wpsc_get_archive_ticket_list();
3450    });
3451  }
3452}
3453
3454/**
3455 * Get bulk archive ticket
3456 */
3457function wpsc_bulk_delete_tickets(nonce) {
3458  var items = jQuery(".wpsc-bulk-select:checked");
3459  var checked = items.length === 0 ? false : true;
3460  jQuery(".wpsc-bulk-selector").prop("checked", checked);
3461  if (items.length != 0) {
3462    var ticket_ids = jQuery(".wpsc-bulk-select:checked")
3463      .map(function () {
3464        return this.value;
3465      })
3466      .get();
3467    var flag = confirm(supportcandy.translations.confirm);
3468    if (!flag) {
3469      return;
3470    }
3471
3472    var data = {
3473      action: "wpsc_bulk_delete_tickets",
3474      ticket_ids,
3475      _ajax_nonce: nonce,
3476    };
3477    jQuery.post(supportcandy.ajax_url, data, function (response) {
3478      wpsc_get_ticket_list();
3479      wpsc_run_ajax_background_process();
3480    });
3481  }
3482}
3483
3484/**
3485 * Restore bulk tickets
3486 */
3487function wpsc_bulk_restore_tickets(nonce) {
3488  var items = jQuery(".wpsc-bulk-select:checked");
3489  var checked = items.length === 0 ? false : true;
3490  jQuery(".wpsc-bulk-selector").prop("checked", checked);
3491  if (items.length != 0) {
3492    var ticket_ids = jQuery(".wpsc-bulk-select:checked")
3493      .map(function () {
3494        return this.value;
3495      })
3496      .get();
3497    var flag = confirm(supportcandy.translations.confirm);
3498    if (!flag) {
3499      return;
3500    }
3501    var data = {
3502      action: "wpsc_bulk_restore_tickets",
3503      ticket_ids,
3504      _ajax_nonce: nonce,
3505    };
3506    jQuery.post(supportcandy.ajax_url, data, function (response) {
3507      wpsc_get_ticket_list();
3508    });
3509  }
3510}
3511
3512/**
3513 * Deletes bulk ticket permanentaly
3514 */
3515function wpsc_bulk_delete_tickets_permanently(nonce) {
3516  var items = jQuery(".wpsc-bulk-select:checked");
3517  var checked = items.length === 0 ? false : true;
3518  jQuery(".wpsc-bulk-selector").prop("checked", checked);
3519  if (items.length != 0) {
3520    var ticket_ids = jQuery(".wpsc-bulk-select:checked")
3521      .map(function () {
3522        return this.value;
3523      })
3524      .get();
3525    var flag = confirm(supportcandy.translations.confirm);
3526    if (!flag) {
3527      return;
3528    }
3529
3530    var data = {
3531      action: "wpsc_bulk_delete_tickets_permanently",
3532      ticket_ids,
3533      _ajax_nonce: nonce,
3534    };
3535    jQuery.post(supportcandy.ajax_url, data, function (response) {
3536      wpsc_get_ticket_list();
3537    });
3538  }
3539}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.