|
(function(window) { |
|
|
|
var document = window.document, |
|
thankYouPage = '/email-capture-thank-you', |
|
initEmailCaptureForm = function(form) { |
|
var step1Button = form.querySelector('.step-1 button'), |
|
step2Button = form.querySelector('.step-2 input[type=submit]'), |
|
emailAddressInput = form.querySelector('input[name=email-address]') |
|
errorDisplay = form.querySelector('.error-display'), |
|
verifyEmailAddress = function(event) { |
|
event.preventDefault(); |
|
var validationUrl = form.dataset.validationUrl, |
|
xhr = get_XHR(cb_verifyEmailAddress), |
|
query = { |
|
email_address: emailAddressInput.value |
|
}; |
|
|
|
xhr.open('get', validationUrl+'?'+makeQueryString(query), true); |
|
xhr.send(); |
|
}, |
|
cb_verifyEmailAddress = function(response) { |
|
response = JSON.parse(response); |
|
if (response.is_valid) { |
|
errorDisplay.innerText = ''; |
|
form.classList.remove('validation-error'); |
|
form.classList.remove('step-1'); |
|
form.classList.add('step-2'); |
|
} else { |
|
form.classList.add('validation-error'); |
|
errorDisplay.innerText = response.reason; |
|
} |
|
}; |
|
|
|
step1Button.addEventListener('click', this.verifyEmailAddress); |
|
step2Button.addEventListener('click', function(event) { |
|
event.preventDefault(); |
|
var emailAddress = form.querySelector('input[name=email-address]').value, |
|
firstName = form.querySelector('input[name=first-name]').value, |
|
lastName = form.querySelector('input[name=last-name]').value, |
|
amount = form.querySelector('select').value, |
|
amountString = form.querySelector('select option[value="'+amount+'"]').text, |
|
queryParams = { |
|
email_address: emailAddress, |
|
first_name: firstName, |
|
last_name: lastName, |
|
amount: amount, |
|
amount_string: amountString |
|
}, |
|
optionalParams = [ |
|
'utm_source', |
|
'utm_medium', |
|
'utm_campaign', |
|
'ref' |
|
]; |
|
|
|
for (var i = 0; i < optionalParams.length; i++) { |
|
var input = form.querySelector('input[name='+optionalParams[i]+']'); |
|
if (input) { |
|
queryParams[optionalParams[i]] = input.value; |
|
} |
|
} |
|
|
|
var xhr = get_XHR(function(response) { |
|
window.location.href = thankYouPage; |
|
}, function(response, status) { |
|
if (status == 403) { |
|
window.alert('We have detected abuse from your IP address. Please try again later.') |
|
} |
|
}); |
|
xhr.open(form.method, form.action, true); |
|
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
|
xhr.send(makeQueryString(queryParams)); |
|
}); |
|
}, |
|
get_XHR = function(onSuccess, onFailure = false) { |
|
var xhr = new XMLHttpRequest(); |
|
!onFailure && (onFailure = function(){}) |
|
xhr.onreadystatechange = function() { |
|
if (xhr.readyState == XMLHttpRequest.DONE) { |
|
var response = xhr.responseText; |
|
if (xhr.status == 200) { |
|
onSuccess(response); |
|
} else { |
|
onFailure(response, xhr.status) |
|
} |
|
} |
|
}; |
|
return xhr; |
|
}, |
|
makeQueryString = function(data) { |
|
var doubles = []; |
|
for (key in data) { |
|
doubles.push([key, encodeURIComponent(data[key])].join('=')); |
|
} |
|
return doubles.join('&'); |
|
}; |
|
|
|
window.addEventListener('DOMContentLoaded', function() { |
|
var forms = document.querySelectorAll('.email-capture-form'); |
|
for (i = 0; i < forms.length; i++) { |
|
initEmailCaptureForm(forms[i]); |
|
} |
|
}); |
|
|
|
}(window)); |