//----------------------------------------------------------------------------------- // // Validation functions // //----------------------------------------------------------------------------------- //-------------------------- // // Bound an element to indicate an error // //-------------------------- function bounceElement(id) { $(id).addClass('animated bounce faster lp-error-border'); function animationEnd() { $(id).removeClass('animated bounce faster'); if (typeof callback === 'function') callback(); } $(id).on('animationend', animationEnd); } //-------------------------- // // Perform check on email validity // //-------------------------- function checkEmailResponse(data, id, error_text) { //If valid result, return success if (data.Result=="valid") return true; //Handle disposable email addresses if (data.Disposable) { $(error_text).text("We don't accept temproary email addresses. Please use a real email address.") $(error_text).show(); bounceElement(id); return false; } //Handle spam traps if (data.Spamtrap) { $(error_text).text("We don't accept email addresses from this domain.") $(error_text).show(); bounceElement(id); return false; } //Handle spam traps if (data.Spamtrap) { $(error_text).text("We don't accept email addresses from this domain.") $(error_text).show(); bounceElement(id); return false; } //Handle spam traps if (data.Result=="risky") { $(error_text).text("We don't accept email addresses from this domain.") $(error_text).show(); bounceElement(id); return false; } //Another error occurred $(error_text).text("Please enter a valid, working email address.") $(error_text).show(); bounceElement(id); return false; } //-------------------------- // // Validate email address // //-------------------------- function validateEmailFormat(id) { //Get email text email = $(id).val().trim(); //Do a basic check to see if the email address looks valid var expression = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()\.,;\s@\"]+\.{0,1})+([^<>()\.,;:\s@\"]{2,}|[\d\.]+))$/; if (!expression.test(email)) return false; return true; } function vetEmail(callback_function, id, error_text, elv, elv_route) { //Get email text email = $(id).val().trim(); //If not using email list validation, stop there if (!elv) { callback_function(true); return; } //Call server to check email validity var valid_email = false; $.ajax({ url: elv_route, type: "POST", data: jQuery.param({ email: email }), contentType: 'application/x-www-form-urlencoded; charset=UTF-8', dataType: "json", timeout: 10000, }).done(function(data) { valid_email = checkEmailResponse(data, id, error_text); callback_function(valid_email); }).fail(function (data){ //Default to valid email if unable to check with server valid_email=true; callback_function(valid_email); }); } //----------------------------------------------------------------------------------- // // Form submission // //----------------------------------------------------------------------------------- //-------------------------- // // Add quiz responses to form data // //-------------------------- function processQuiz() { //Object for handling responses var responses = []; //Get text responses $('.quiz-text').each(function () { responses.push({ 'answer' : $(this).val(), 'question' : $(this).data('question-number') }); }); //Get radio responses $('.radio-group').each(function () { responses.push({ 'question' : $(this).data('question-number'), 'answer' : $("input[name='" + $(this).data('radio-group-name') + "']:checked").val() }); }); //Remove any empty responses responses = responses.filter(function (el) { return el!=null }); return responses; } function handleSubmit(id, callback_function, has_quiz=false) { //Get submission url var submissionUrl = $(id).attr('action'); var submissionMethod = $(id).attr('method'); var quiz_responses = []; //Add quiz results (if necessary) if (has_quiz) quiz_responses = processQuiz(); $('#quiz_data').val(JSON.stringify(quiz_responses)); //Serialize data for passing to URL var formData = $(id).serialize(); $.ajax({ url: submissionUrl, type: submissionMethod, data: formData, contentType: 'application/x-www-form-urlencoded; charset=UTF-8', dataType: "json", timeout: 10000, }).done(function(data) { callback_function(data); }).fail(function (data){ callback_function(data); }); } //----------------------------------------------------------------------------------- // // Quiz handling // //----------------------------------------------------------------------------------- //-------------------------- // // Text inputs // //-------------------------- $(document).on('keyup', '.quiz-text', function(e) { //Get related button ID var buttonID = '#' + $(this).data('next-button'); //If text is long enough, enable next button if ($(this).val().length>3) $(buttonID).removeClass('btn-disabled'); else $(buttonID).addClass('btn-disabled'); }); //-------------------------- // // Radio inputs // //-------------------------- $(document).on('change', '.radio-group', function(e) { //Get related button ID var buttonID = '#' + $(this).data('next-button'); //Get checked option var option_checked = $('input[name="' + $(this).data('radio-group-name') + '"]:checked').val(); //If an option has been selected, enable next button if (option_checked != undefined) $(buttonID).removeClass('btn-disabled'); else $(buttonID).addClass('btn-disabled'); }); //-------------------------- // // Input handling // //-------------------------- var oldButton1Text; var oldButton2Text; function enableSubmit1Button() { $('#lp-download-button-container').addClass("download-button-fade-on-hover"); $('#form-submit-button').removeClass("download-button-disabled"); $('#form-submit-button').prop("disabled", false); $('#form-submit-text').text(oldButton1Text); $('#form-submit-spinner').hide(); } function disableSubmit1Button() { $('#lp-download-button-container').removeClass("download-button-fade-on-hover"); $('#form-submit-button').addClass("download-button-disabled"); $('#form-submit-button').prop("disabled", true); oldButton1Text=$('#form-submit-text').text(); $('#form-submit-text').text("Please wait"); $('#form-submit-spinner').show(); } function enableSubmit2Button() { $('#lp-download-button-2-container').addClass("download-button-fade-on-hover"); $('#form-submit-2-button').removeClass("download-button-disabled"); $('#form-submit-2-button').prop("disabled", false); $('#form-submit-2-text').text(oldButton2Text); $('#form-submit-2-spinner').hide(); } function disableSubmit2Button() { $('#lp-download-button-2-container').removeClass("download-button-fade-on-hover"); $('#form-submit-2-button').addClass("download-button-disabled"); $('#form-submit-2-button').prop("disabled", true); oldButton2Text=$('#form-submit-2-text').text(); $('#form-submit-2-text').text("Please wait"); $('#form-submit-2-spinner').show(); } //Disables submit on enter key XXX $(document).on('keydown', 'form input:not([type="submit"])', (e) => { if (e.keyCode === 13) { e.preventDefault(); return false; } return true; }); function submitCallback(result) { console.log(result); $('#register-page-2').hide(); $('#register-page-3').hide(); $('#download-info-footer').hide(); if (result.status=="success") { try { fbq('track', 'CompleteRegistration'); } catch (error) {} //Reset page (in case back button is used on next page) $('.lp-text-col').hide(); $('body').removeClass('lp-overlay-active'); $('.lp-popup-first-page').show(); $('#lp-quiz-overlay').hide(); $('#lp-quiz-popup-container').hide(); $('form').each(function() { this.reset(); }); enableSubmit1Button(); //Redirect user window.location="https://growpoint.co.uk/are-they-tantrums-thank-you/"; } else { $('#register-failure-page').show(); } } function validateCallback(result) { //If email is good if (result) { handleSubmit('#form-details', submitCallback); return; } else { $('#register-page-2').hide(); $('#register-page-1').show(); enableSubmit1Button(); } } $(document).on('click', '.trigger-submit', function(e) { //Prevent default action e.preventDefault(); //Remove focus border around this element $(this).blur(); //Disable this button while processing disableSubmit1Button(); //Clear any previous error messages or classes $('.form-error-text').hide(); $('.lp-error-border').removeClass('lp-error-border'); //Validate name var name = $('#form-first-name').val().trim(); if (name.length<2) { bounceElement('#form-first-name'); enableSubmit1Button(); return; } //Check email (basic) if (!validateEmailFormat('#form-email')) { bounceElement('#form-email'); enableSubmit1Button(); return; } //Validate mailing list consent aml1 = $('#form-aml-1').val(); if (aml1==null) { bounceElement('#form-aml-1'); enableSubmit1Button(); return; } //Process mailing list consent - if user said no, double check aml1 = $('#form-aml-1').val(); if (aml1=="no") { $('#register-page-2').hide(); $('#download-info-footer').hide(); $('#register-page-3').show(); enableSubmit2Button(); return; } //Vet email address vetEmail(validateCallback, '#form-email', '#email-error-text', true, "https://pages.growpoint.co.uk/are-they-tantrums/api/check-email"); }); $(document).on('click', '.trigger-submit-2', function(e) { //Prevent default action e.preventDefault(); //Remove focus border around this element $(this).blur(); //Clear any previous error messages or classes $('.form-error-text').hide(); $('.lp-error-border').removeClass('lp-error-border'); //Validate mailing list consent aml2 = $('#form-aml-2').val(); if (aml2==null) { bounceElement('#form-aml-2'); enableSubmit2Button(); return; } //If user rejected being on the mailing list, update their preference if (aml2=="no") $('#aml').val("0"); else $('#form-aml-1').val("yes"); //Disable this button while processing disableSubmit2Button(); //Vet email address vetEmail(validateCallback, '#form-email', '#email-error-text', true, "https://pages.growpoint.co.uk/are-they-tantrums/api/check-email"); }); $(document).on('click', '.trigger-next', function(e) { e.preventDefault(); //Do nothing if this button has the disabled class if ($(this).hasClass('btn-disabled')) return; //Move on to the next page var current_page = '#' + $(this).data('current-page'); $(current_page).hide(); var next_page = '#' + $(this).data('next-page'); $(next_page).show(); }); $(document).on('click', '.lp-start-registration', function (e) { e.preventDefault(); $('.lp-popup-first-page').show(); $('#lp-quiz-overlay').fadeIn(); $('#lp-quiz-popup-container').fadeIn(); $('body').addClass('lp-overlay-active'); }); //-------------------------- // // Initialise pages and quizzes // //-------------------------- $(document).ready(function () { // Append additional CSS const link = document.createElement('link'); link.rel = 'stylesheet'; link.href = 'https://sites.happymongoose.co.uk/css/pages/styles-lp.css'; link.type = 'text/css'; document.head.appendChild(link); // Fetch the HTML $.ajax({ url: "https://pages.growpoint.co.uk/lp/are-they-tantrums/html", method: "GET", success: function (response) { // Prepend the HTML content to the body $('body').prepend(response); }, error: function () { console.error("Failed to fetch additional landing page content."); } }); });