function extend(destination, source) { for (var prop in source) { destination[prop] = source[prop]; } }
if(!Mimi) var Mimi = {}; if(!Mimi.Signups) Mimi.Signups = {};
Mimi.Signups.EmbedValidation = function() { this.initialize();
if(document.addEventListener) { this.form.addEventListener('submit', this.onFormSubmit.bind(this)); } else { this.form.attachEvent('onsubmit', this.onFormSubmit.bind(this)); } };
extend(Mimi.Signups.EmbedValidation.prototype, { initialize: function() { this.form = document.getElementById('mad_mimi_signup_form'); this.submit = document.getElementById('webform_submit_button'); this.callbackName = 'jsonp_callback_' + Math.round(100000 * Math.random()); },
onFormSubmit: function(e) { e.preventDefault();
this.validate(); if(this.isValid) { this.submitForm(); } else { this.revalidateOnChange(); } },
validate: function() { this.isValid = true; this.emailValidation(); this.fieldAndListValidation(); this.updateFormAfterValidation(); },
emailValidation: function() { var email = document.getElementById('signup_email'), validEmail = /.+@.+..+/;
if(!validEmail.test(email.value)) { this.textFieldError(email); this.isValid = false; } else { this.removeTextFieldError(email); } },
fieldAndListValidation: function() { var fields = this.form.querySelectorAll('.mimi_field.required');
for(var i = 0; i 0) { return type[0].getAttribute('data-field-type'); } else if(field.className.indexOf('checkgroup') >= 0) { return 'checkboxes'; } else { return 'text_field'; } },
checkboxAndRadioValidation: function(field) { var inputs = field.getElementsByTagName('input'), selected = false;
for(var i = 0; i = 0) { if(type == 'text_field') { this.textValidation(input); } else { this.dropdownValidation(field, input); } } } this.htmlEmbedDropdownValidation(field); },
textValidation: function(input) { if(input.id == 'signup_email') return;
var val = input.value; if(val == '') { this.textFieldError(input); this.isValid = false; } else { this.removeTextFieldError(input) } },
dropdownValidation: function(field, input) { var val = input.value; if(val == '') { if(field.className.indexOf('invalid') == -1) field.className += ' invalid'; this.onSelectCallback(input); this.isValid = false; } else { field.className = field.className.replace(/ invalid/g, ''); } },
htmlEmbedDropdownValidation: function(field) { var dropdowns = field.querySelectorAll('.mimi_html_dropdown');
for(var i = 0; i 1 || invalidFields[0].className.indexOf('checkgroup') == -1) { text = this.submit.getAttribute('data-invalid-text'); } else { text = this.submit.getAttribute('data-choose-list'); } } return text; },
submitForm: function() { this.formSubmitting();
var _this = this; window[this.callbackName] = function(response) { delete window[this.callbackName]; document.body.removeChild(script); _this.onSubmitCallback(response); };
var script = document.createElement('script'); script.src = this.formUrl('json'); document.body.appendChild(script); },
formUrl: function(format) { var action = this.form.action, divider = action.indexOf('?') >= 0 ? '&' : '?';
if(format == 'json') action += '.json';
return action + divider + 'callback=' + this.callbackName + '&' + serialize(this.form); },
formSubmitting: function() { this.form.className += ' mimi_submitting'; this.submit.value = this.submit.getAttribute('data-submitting-text'); this.submit.disabled = true; this.submit.className = 'disabled'; },
onSubmitCallback: function(response) { if(response.success) { this.onSubmitSuccess(response.result); } else { top.location.href = this.formUrl('html'); } },
onSubmitSuccess: function(result) { if(result.has_redirect) { top.location.href = result.redirect; } else if(result.single_opt_in || result.confirmation_html == null) { this.disableForm(); this.updateSubmitButtonText(this.submit.getAttribute('data-thanks')); } else { this.showConfirmationText(result.confirmation_html); } },
showConfirmationText: function(html) { var fields = this.form.querySelectorAll('.mimi_field');
for(var i = 0; i