PageSourceSearch

https://guideposts.org/wp-content/plugins/custom_newsletter_signup…/custom_newsletter_signup.js?ver=1730396192

js guideposts.org collected 2026-09-24 09:03:29 UTC 14,785 bytes, 433 lines download raw bytes

1
2
3(function($) {
4
5	function validateEmail(email) {
6		return String(email)
7			.toLowerCase()
8			.match(
9				/^(([^<>()[\]\\.,;:\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,}))$/
10			);
11	}
12
13	function checkCats() {
14		var $IDS = [];
15		var $NEW = [];
16		$('.newsletter-signup-category-item--checkbox.single-check').each(function(i) {
17			$IDS.flat();
18			if ($(this).is('.checked')) {
19				$IDS[i] = $(this).attr('data-id');
20			}
21		});
22		$NEW = $IDS.flat();
23		if ($NEW.length > 0) {
24			var $ids = $NEW.toString();
25		} else {
26			var $ids = '';
27		}
28		$('.newsletter_signup_categories_wrapper').find('input[name="newsletter_signup_categories"]').val($ids);
29	}
30
31	/**
32	 *
33	*/
34
35	function loadUserPreferences(email) {
36
37		// check to see if we are passed a null email
38		if (email == null) {
39
40			return;
41		}
42
43		email = email.replace("?", "");
44
45		if (!validateEmail(email)) {
46			return;
47		}
48
49		//const endpoint = "https://a2kti82sfc.execute-api.us-east-1.amazonaws.com/v1/preferences/get/" + email;
50		//
51		const endpoint = "https://rlndcqeei8.execute-api.us-east-1.amazonaws.com/Prod/preferences/get/" + email + "/guideposts"
52		console.log(endpoint);
53		$.ajax({
54			url: endpoint,
55		    method: 'GET',
56		    contentType: "application/json",
57			dataType: 'json',
58			error: function(data) {
59				console.log("Error calling API");
60			},
61		    success: function(data) {
62				const checkboxes = document.querySelectorAll('.newsletter-signup-category-item--checkbox.single-check');
63
64				// needed to wrap in a profile object only because we are not calling the remote endpoint.
65				// go-preferences the zeta endpoint does not have it wrapped in a profile object,
66				// when we call zeta directly, we will need to add the profile object.
67
68				//var preferences = { profile: JSON.parse(JSON.stringify(data)) };
69				var preferences = JSON.parse(JSON.stringify(data));
70
71				console.log(preferences)
72
73				checkboxes.forEach((checkbox) => {
74
75					// zeta needs the format (lowercase and underscores)
76					const newsletter = checkbox.getAttribute('data-id') // contains the OPT value
77					.toLowerCase() // convert to lowercase
78					.replace(/-/g, '_'); // replace dashes with underscores
79
80					value = JSON.stringify(preferences.profile[newsletter]);
81
82					if (preferences.profile[newsletter] == 1) {
83						checkbox.checked = true;
84						checkbox.classList.add('checked');
85					} else { // reset
86						checkbox.checked = false;
87						checkbox.classList.remove('checked');
88					}
89				});
90			},
91		});
92	}
93
94
95function ajaxGetPreferences(email, unsubscribe = false, getPref = false) {
96    // Check to see if we are passed a null email
97    if (email == null) {
98        console.log("Email is null.");
99        return;
100    }
101
102    email = email.replace("?", "");
103
104    if (validateEmail(email)) {
105        console.log("Valid email provided:", email);
106
107        if (unsubscribe) { // This code is called only when the user clicks the Load Preferences Button to un-subscribe
108            var unsubscribePayload = {
109                contact_value: email,
110                contact_type: 'email',
111                source: 'api'
112            };
113
114            console.log("Calling Zeta unsubscribe");
115            console.log("Unsubscribing user with payload:", unsubscribePayload);
116
117            $.ajax({
118		url: 'https://rlndcqeei8.execute-api.us-east-1.amazonaws.com/Prod/preferences/unsubscribe/guideposts/GPPrefCenter',
119                method: 'POST',
120                beforeSend: function () {
121                    $('#unsubscribe-box').html('<div id="unsubscribe-message"><em>Unsubscribing please wait...</em></div>');
122                },
123                data: JSON.stringify(unsubscribePayload),
124                contentType: 'application/json',
125                dataType: 'text',
126                success: function (data) {
127
128                    if (data === 'Success') {
129                        $('#unsubscribe-box').html('<div id="unsubscribe-message"><em>You have been successfully unsubscribed from all newsletters.</em></div>');
130                    } else {
131                        console.log('Unexpected response:', data);
132                    }
133                },
134                complete: function (jqXHR) {
135                    console.log('Response:', jqXHR.responseText);
136                },
137                error: function (jqXHR, exception) {
138                    if (jqXHR.status === 0) {
139                        console.log('Not connected. Verify Network.');
140                    } else if (jqXHR.status == 404) {
141                        console.log('Requested page not found (404).');
142                    } else if (jqXHR.status == 500) {
143                        console.log('Internal Server Error (500).');
144                    } else if (exception === 'parsererror') {
145                        console.log('Requested JSON parse failed.');
146                    } else if (exception === 'timeout') {
147                        console.log('Time out error.');
148                    } else if (exception === 'abort') {
149                        console.log('Ajax request aborted.');
150                    } else {
151                        console.log('Uncaught Error. ' + jqXHR.responseText);
152                    }
153		 $('#unsubscribe-box').html('<div id="unsubscribe-message"><em>Unsubscribe failed. Please try again.</em></div>');
154                }
155            });
156        }
157    } else {
158        console.log("Invalid email address:", email);
159    }
160}
161
162	var storedEmailMaster = localStorage.getItem('master_signup_email');
163	var storedMilitaryEmail = localStorage.getItem('military_email');
164	var storedMilitaryCategory = localStorage.getItem('military_category');
165	var storedEmail = localStorage.getItem('newsletter_signup_email');
166	var storedCats = JSON.parse(localStorage.getItem('categories'));
167
168	$(document).on('nfFormReady', function() {
169
170		$('.newsletter-signup-email-wrapp').change(function() {
171			if ($('.single-check.checked').length == 0) {
172				$('input[type="button"]').attr('disabled', 'disabled');
173				if ($('#unsubscribe-link').length == 0) {
174					$('#content input[type="submit"]').attr('disabled', 'disabled');
175				}
176
177			}
178		});
179
180		
181
182		// $('#nf-field-31').on('click',function(e) {
183		// 	alert('hi');
184				
185		// });
186
187		$(document).on('click', '.newsletter-signup-submit .submit-wrap', function(e) {
188		///alert('hi');
189			if ($('.newsletter-signup-submit input').attr('disabled') == 'disabled') {
190				$('.newsletter_signup_form_wrapp .nf-debug-msg').show();
191				$('.newsletter_signup_form_wrapp  .nf-debug-msg').text('Please select at least one newsletter below.');
192				$('html, body').animate({ scrollTop: 0 }, 'slow');
193			} else {
194				$('.newsletter-signup-submit .nf-after-field .nf-error-wrap').hide();
195			}
196
197		});
198
199		if (storedCats !== null) {
200			//          $('.newsletter-signup-email-wrapp').find('input').val(storedEmail);
201			//          $('#newsletter-signup-load-my-preferences').addClass('enabled');
202		}
203
204		if (storedEmailMaster !== null) {
205			//          $('.master-email-wrapp').find('input').val(storedEmailMaster);
206		}
207
208		if (storedMilitaryEmail !== null) {
209			//$('.military-email-wrapp').find('input').val(storedMilitaryEmail);
210		}
211
212		if (storedMilitaryCategory !== null) {
213			// $('.military-categories-wrapp option[value=' + storedMilitaryCategory + ']').attr('selected', 'selected');
214		}
215
216		// when the email field loses focus load the users preferences if it exists.
217		$('.newsletter-signup-email-wrapp').find('input').blur(function() {
218					
219			loadUserPreferences($(this).val());
220		});
221
222		$('.master-email-wrapp').find('input').change(function() {
223			localStorage.setItem('master_signup_email', $(this).val());
224		});
225
226		$('.military-email-wrapp').find('input').change(function() {
227			localStorage.setItem('military_email', $(this).val());
228		});
229
230		$('.military-categories-wrapp').find('select').change(function() {
231			localStorage.setItem('military_category', $(this).val());
232		});
233
234		var $unsubscribeEmail = $('#NL-get-unsubscribe').text();
235		if ($unsubscribeEmail && $unsubscribeEmail !== '') {
236			localStorage.setItem('newsletter_signup_email', $unsubscribeEmail);
237		}
238
239		storedEmail = localStorage.getItem('newsletter_signup_email');
240		$('.newsletter-signup-email-descr-top').find('#unsubscribe-link').one('click', function(e) {
241			e.preventDefault();
242			var $email = $(this).attr('data-email');
243
244			ajaxGetPreferences($email, true);
245		});
246
247		$(document).on('nfFormSubmitResponse', function(event, response, id) {
248			if (response.response.data.fields_by_key.newsletter_signup_youremail) {
249				var $email = response.response.data.fields_by_key.newsletter_signup_youremail.value;
250				var $categories = response.response.data.fields_by_key.newsletter_signup_categories.value;
251
252				var $categoriesArray = $categories.split(",");
253				var $carArr = $categoriesArray.flat();
254
255				localStorage.setItem('newsletter_signup_email', $email);
256				localStorage.setItem('categories', JSON.stringify($carArr));
257			}
258			if (response.response.data.fields_by_key.master_signup_email) {
259				var $email = response.response.data.fields_by_key.master_signup_email.value;
260				localStorage.setItem('newsletter_signup_email', $email);
261			}
262
263		});
264
265		$('.newsletter-signup-email-wrapp').find('input').keyup(function() {
266			var email = $(this).val();
267
268			if (validateEmail(email)) {
269				$('#newsletter-signup-load-my-preferences').addClass('enabled');
270			} else {
271				$('#newsletter-signup-load-my-preferences').removeClass('enabled');
272			}
273
274			if ($('.single-check.checked').length == 0) {
275				$('input[type="button"]').attr('disabled', 'disabled');
276				if ($('#unsubscribe-link').length == 0) {
277					$('#content input[type="submit"]').attr('disabled', 'disabled');
278				}
279			}
280
281		});
282
283		$('.request-your-name-wrapp').find('input').change(function() {
284			localStorage.setItem('request_your_name', $(this).val());
285		});
286
287		$('.request-email-wrapp').find('input').change(function() {
288			localStorage.setItem('request_signup_email', $(this).val());
289		});
290
291		$('.request_prayer_category_wrapp').find('select').change(function() {
292			localStorage.setItem('request_prayer_category', $(this).val());
293		});
294
295		$('.nf-form--gp .nf-field-element').find('input[type="text"], input[type="email"], input[type="tel"], textarea').each(function() {
296			if ($(this).val().length > 0) {
297				$(this).parents('.nf-field').addClass('focus');
298			}
299			$(this).on('focus', function() {
300				$(this).parents('.nf-field').addClass('focus');
301				//alert("fokus kundo");
302			});
303			$(this).on('focusout', function() {
304				if ($(this).val().length == 0) {
305					$(this).parents('.nf-field').removeClass('focus');
306					// alert("unfok jardo");
307				}
308
309			});
310		});
311
312		$('.nf-form--gp .nf-field-element').find('select').each(function() {
313			var $selected = $(this).val();
314			if ($selected !== '' && $selected !== null) {
315				$(this).parents('.nf-field').addClass('focus');
316			} else {
317				$(this).parents('.nf-field').removeClass('focus');
318			}
319		});
320		$('.nf-form--gp .nf-field-element').find('select').change(function() {
321			var $selected = $(this).val();
322			if ($selected !== '' && $selected !== null) {
323				$(this).parents('.nf-field').addClass('focus');
324			} else {
325				$(this).parents('.nf-field').removeClass('focus');
326			}
327		});
328
329		var pathname = window.location.pathname;
330		var path_search = window.location.search;
331		if (pathname.indexOf('/newsletter') > -1) {
332			ajaxGetPreferences(storedEmail, false);
333
334			$('#newsletter-signup-load-my-preferences').on('click', function(e) {
335				e.preventDefault();
336				// the code below is commented out because it causes the checkboxes
337				// to check and uncheck, to many calls to get preferences
338				// var $currentEmail = $('.newsletter-signup-email-wrapp').find('input').val();
339				// ajaxGetPreferences($currentEmail, false);
340				e.stopImmediatePropagation();
341				return false;
342			});
343
344		        // code updated to work with Zeta passing multiple query strings
345			const queryString = window.location.search;
346			const urlParams = new URLSearchParams(queryString);
347			const emailParam = urlParams.get('e');
348
349			if (emailParam) {
350				var $unsubscribe = $('#unsubscribe-link').attr('data-email');
351				ajaxGetPreferences($unsubscribe, false, true);
352				$('.newsletter-signup-email-wrapp').find('input').val($unsubscribe);
353			}
354		}
355
356
357		if (path_search != '?r=y') {
358			localStorage.setItem('thanks', '0');
359
360		}
361		var thanks = localStorage.getItem('thanks');
362
363		if (path_search == '?r=y') {
364
365			if (thanks !== '1') {
366				$('.newsletter-signup-email-wrapp').find('input').val(storedEmail);
367				if (storedCats !== null) {
368					$('#newsletter-signup-load-my-preferences').addClass('enabled');
369					checkCats();
370				}
371			}
372		}
373	});
374
375	$(document).on("nfFormSubmitResponse", function() {
376		var thank_you = $('.newsletter_signup_form_wrapp').find('.nf-response-msg h2').text();
377		if (thank_you == 'Thank You') {
378			$('.newsletter_signup_form_wrapp .nf-debug-msg').hide();
379			localStorage.setItem('thanks', '1');
380		}
381	});
382
383	$(document).ready(function($) {
384		//console.log('Load 3.47');
385
386		// Get the value of the ?e= parameter from the query string
387		const encodedValue = new URLSearchParams(window.location.search).get('e');
388		if (encodedValue) {
389			loadUserPreferences(atob(encodedValue));
390		}
391
392		$(document).on('click', '.newsletter-signup-category-item--checkbox', function() {
393
394			$(this).toggleClass('checked');
395
396			if ($(this).is('.check-all')) {
397				if ($(this).is('.checked')) {
398					$(this).closest('.newsletter-signup-category').find('.single-check').removeClass('checked').addClass('checked');
399				} else {
400					$(this).closest('.newsletter-signup-category').find('.single-check').removeClass('checked');
401				}
402
403			} else {
404				//Check All box if all checkboxes are checked
405				if ($(this).closest('.newsletter-signup-category').find('.single-check.checked').length == $(this).closest('.newsletter-signup-category').find('.single-check').length) {
406					$(this).closest('.newsletter-signup-category').find('.check-all').addClass('checked');
407				} else {
408					//Uncheck Check All box if all checkboxes are empty
409					$(this).closest('.newsletter-signup-category').find('.check-all').removeClass('checked');
410				}
411			}
412			checkCats();
413			if ($('.single-check.checked').length > 0) {
414				if ($('.newsletter-signup-email-wrapp .email-wrap').is('.nf-error')) {
415
416				}
417				$('input[type="button"]').removeAttr('disabled');
418				if ($('#unsubscribe-link').length == 0) {
419					$('#content input[type="submit"]').removeAttr('disabled', 'disabled');
420				}
421				$('.newsletter-signup-submit .nf-after-field .nf-error-wrap').hide();
422			} else {
423				$('input[type="button"]').attr('disabled', 'disabled');
424				if ($('#unsubscribe-link').length == 0) {
425					$('#content input[type="submit"]').attr('disabled', 'disabled');
426				}
427			}
428
429		});
430
431	});
432
433})(jQuery);

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.