salem.registerNamespace("shared.newsletter");salem.shared.newsletter={init:function(){var newsletterSubscribeButtons=$(".newsletterSubscribeButton");var newsletterEmailInputs=$("input.newsletterEmail");newsletterSubscribeButtons.click(function(e){e.preventDefault();var button=$(this);var parent=$(button).closest(".newsletterControls");var newsletterEmailInput=parent.find("input.newsletterEmail");if(!newsletterEmailInput.length){return;}var stormPostTag=parent.find("#stormPostTag").val();var emailAddress=$.trim(newsletterEmailInput.val());if(stormPostTag){salem.shared.newsletter.validateSingleNewsletter(stormPostTag,emailAddress,button);}else{var form=salem.shared.newsletter.findForm(button);salem.shared.newsletter.validateMultipleNewsletters(form,emailAddress,button);}});newsletterEmailInputs.keydown(function(e){var input=$(this);var parent=$(input).closest(".newsletterControls");var errorDiv=parent.find(".error");errorDiv.removeClass("show");var newsletterEmailInput=$(parent).find("input.newsletterEmail");if(!newsletterEmailInput.length){return;}var stormPostTag=$(parent).find("#stormPostTag").val();if(e.keyCode==13){e.preventDefault();var emailAddress=$.trim(input.val());if(stormPostTag){salem.shared.newsletter.validateSingleNewsletter(stormPostTag,emailAddress,input);}else{var form=salem.shared.newsletter.findForm(input);salem.shared.newsletter.validateMultipleNewsletters(form,emailAddress,input);}}});},findForm:function(nearElement){var formIdToSubmit=nearElement.attr("data-formidtosubmit");return formIdToSubmit&&$("#"+formIdToSubmit).length?$("#"+formIdToSubmit):nearElement.closest("form");},validateSingleNewsletter:function(stormPostTag,emailAddress){if(!salem.validation.isValidEmail(emailAddress)){alert("Please enter a valid email address.");return;}salem.shared.newsletter.submitNewsletters(emailAddress,[stormPostTag]);},validateMultipleNewsletters:function(form,emailAddress,element){if(!salem.validation.isValidEmail(emailAddress)){var errorDiv=element.parents(".newsletterControls").find(".error");errorDiv.addClass("show");errorDiv.html("Please enter a valid email address.");return;}var newsletters=[];$("input[type='checkbox']:checked").each(function(i,checkbox){checkbox=$(checkbox);if(checkbox.val()){newsletters.push(checkbox.val());}});if(!newsletters.length){var errorDiv=element.parents(".newsletterControls").find(".error");errorDiv.addClass("show");errorDiv.html("Please select at least one newsletter.");return;}salem.shared.newsletter.submitNewsletters(emailAddress,newsletters);},submitNewsletters:function(emailAddress,newsletters){var signupLocation=$("#signupLocation").val();var signupLocationArg=signupLocation?"&signupLocation="+signupLocation:"";var urlArg="&signupUrl="+encodeURI(document.location.href);var domainArg="&signupDomain="+encodeURI(document.domain);var q="emailAddress="+emailAddress+"&legacyStormPostTags="+newsletters.join(",")+"&signUpPage=Other"+signupLocationArg+urlArg+domainArg;var newsletterSubmitUrl=salem.getRelativeBaseUrl()+"newsletters/submit/";$.ajax({type:"POST",url:newsletterSubmitUrl,dataType:"json",data:{q:q},success:function(resp){if(resp.Success){var submitScript=document.createElement("script");submitScript.src=resp.Script;document.body.appendChild(submitScript);}},error:function(){alert("Ajax error. Please try again.");}});}};$(document).ready(function(){salem.shared.newsletter.init();});function HandleResponse(success){var thankYou=newsletterThankYouUrl;if(success){window.location.href=thankYou;}else{alert("Your subscription request was unable to be processed by the system. If you continue to receive this message, please let us know at mailsupport@religiontoday.com.");}}