var $ = jQuery; function sailthru_public() { var t = this; var data; var form_id; var list_count = 0; this.init = function() { $('[id^=sailthru-submit-]').click(function() { t.form_id = $(this).attr('id').split('-')[2]; if((t.data = t.get_data()) && t.validate_data()) { t.do_ajax(); } }); }; this.do_ajax = function() { $('#sailthru_loader-' + t.form_id).css('display', ''); $.post( 'http://alttransport.com/wp-content/plugins/sailthru/sailthru_ajax.php?action=subscribe', t.data, function(response) { if(typeof response.length == 'undefined') { $('#sailthru_errors-' + t.form_id).remove(); $('#sailthru_loader-' + t.form_id).after('You have been subscribed.'); } else { $('#sailthru_errors-' + t.form_id).remove(); var error_messages = ''; $('#sailthru_loader-' + t.form_id).after(error_messages); } $('#sailthru_loader-' + t.form_id).css('display', 'none'); }, 'json' ); }; this.validate_data = function() { var errors = Array(); if(t.list_count && !t.data.lists.length) { errors.push('You must subscribe to at least one list.'); } var regex = new RegExp('sailthru\-[^\\d]+' + t.form_id + '$', 'i') $('.required').each(function() { if($(this).attr('id').match(regex) && !$(this).val()) { switch($(this).attr('id').split('-')[1]) { case 'fname': errors.push('You must enter a first name.'); break; case 'lname': errors.push('You must enter a last name.'); break; case 'email': errors.push('You must enter an email address.'); break; } } }); var email = $('#sailthru-email-' + t.form_id).val(); if(email && !email.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) { errors.push('You must enter a valid email address'); } $('#sailthru_errors-' + t.form_id).remove(); var error_messages = ''; $('#sailthru_loader-' + t.form_id).after(error_messages); } return !errors.length; }; this.get_data = function() { return { email: $('#sailthru-email-' + t.form_id).val(), fname: $('#sailthru-fname-' + t.form_id).val(), lname: $('#sailthru-lname-' + t.form_id).val(), lists: t.get_lists(), form_id: t.form_id }; }; this.get_lists = function() { var lists = ''; t.list_count = 0; $('[id^=sailthru-optin-' + t.form_id + '-]').each(function() { t.list_count++; if($(this).is(':checked')) { // lists.push($(this).attr('id').split('-')[3]); lists += $(this).attr('id').split('-')[3] + ','; } }); lists = lists.substring(0, lists.length-1); return lists; }; } $(document).ready(function() { var t = new sailthru_public(); t.init(); });