| 1 |
/* jshint asi: true */ |
| 2 |
|
| 3 |
jQuery( document ).ready(function($){ |
| 4 |
|
| 5 |
function is_email( email ) { |
| 6 |
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/ |
| 7 |
return regex.test(email) |
| 8 |
} |
| 9 |
|
| 10 |
function set_cookie( name, value, exdays ) { |
| 11 |
if ( exdays === 0 ) { |
| 12 |
document.cookie = name + "=" + value + "" + ";" + "path=/;" |
| 13 |
} else { |
| 14 |
var d = new Date() |
| 15 |
d.setTime( d.getTime() + ( exdays*24*60*60*1000 ) ) |
| 16 |
document.cookie = name + "=" + value + "" + ";" + "path=/;" + "expires=" + d.toUTCString() |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
function get_cookie( name ) { |
| 21 |
var value = "; " + document.cookie |
| 22 |
var parts = value.split( "; " + name + "=" ) |
| 23 |
|
| 24 |
if ( parts.length === 2 ) { |
| 25 |
return parts.pop().split(";").shift() |
| 26 |
} else { |
| 27 |
return false |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
$( document ).on( 'submit', '.fca_eoi_form', function (e) { |
| 32 |
|
| 33 |
e.preventDefault() |
| 34 |
|
| 35 |
var $this = $( this ) |
| 36 |
var $email_field = $( '[name=email]', $this ) |
| 37 |
var $name_field = $( '[name=name]', $this ) |
| 38 |
var name = $name_field.val() |
| 39 |
var email = $email_field.val() |
| 40 |
var list_id = $this.data( 'fca_eoi_list_id' ) |
| 41 |
var $button = $( '[type=submit]', $this ) |
| 42 |
var button_initial_val |
| 43 |
var highlight_interval = false; |
| 44 |
var thank_you_mode = $this.data( 'fca_eoi_thank_you_mode' ) |
| 45 |
var thank_you_text_color = $this.data( 'fca_eoi_thank_you_text_color' ) |
| 46 |
var thank_you_bg_color = $this.data( 'fca_eoi_thank_you_bg_color' ) |
| 47 |
var thank_you_page = $this.data( 'fca_eoi_thank_you_page' ) |
| 48 |
var cookie_duration = $this.data( 'fca_eoi_success_cookie_duration' ) |
| 49 |
var subscribe_msg = $this.data( 'fca_eoi_sub_msg' ) |
| 50 |
var has_error = false |
| 51 |
var form_id = $( '[name=fca_eoi_form_id]', $this ).val() |
| 52 |
|
| 53 |
// Attach tooltips |
| 54 |
$this.find('.tooltipstered').tooltipster('hide') |
| 55 |
$( '[name=email], [name=name]', $this ).not('.tooltipstered').each( function() { |
| 56 |
|
| 57 |
var $this = $( this ) |
| 58 |
var tooltipWidth = $this.width() * .8 |
| 59 |
|
| 60 |
$this.tooltipster( { |
| 61 |
contentAsHTML: true, |
| 62 |
fixedWidth: tooltipWidth, |
| 63 |
minWidth: tooltipWidth, |
| 64 |
maxWidth: tooltipWidth, |
| 65 |
trigger: 'none', |
| 66 |
theme: ['tooltipster-borderless', 'tooltipster-optin-cat'] |
| 67 |
} ) |
| 68 |
} ) |
| 69 |
|
| 70 |
// Remove tooltip and tick on focus |
| 71 |
$( '[name=email], [name=name]', $( this ) ).focus( function() { |
| 72 |
var $this = $( this ) |
| 73 |
var $button = $( '[type=submit]', $this.closest( '.fca_eoi_form' ) ) |
| 74 |
var button_initial_val = $button.data( 'fca_eoi_initial_val' ) |
| 75 |
|
| 76 |
// Remove any previous icon |
| 77 |
if( 'undefined' !== typeof( button_initial_val ) ) { |
| 78 |
$button.val( button_initial_val ) |
| 79 |
} |
| 80 |
|
| 81 |
// Hide tooltip |
| 82 |
$( this ).tooltipster( 'hide' ) |
| 83 |
} ) |
| 84 |
|
| 85 |
// Save or save and get initial button value |
| 86 |
if( $button.data( 'fca_eoi_initial_val' ) ) { |
| 87 |
button_initial_val = $button.data( 'fca_eoi_initial_val' ) |
| 88 |
} else { |
| 89 |
button_initial_val = $button.val() |
| 90 |
$button.data( 'fca_eoi_initial_val', button_initial_val ) |
| 91 |
} |
| 92 |
|
| 93 |
// Remove any previous icon |
| 94 |
$button.val( button_initial_val ); |
| 95 |
|
| 96 |
// Get Error Messages from hidden fields |
| 97 |
fcaEoiScriptData.invalid_email = $this.find('.fca_eoi_error_texts_email').val() |
| 98 |
fcaEoiScriptData.field_required = $this.find('.fca_eoi_error_texts_required').val() |
| 99 |
|
| 100 |
// Check email address |
| 101 |
if( ! email || ! is_email( email ) ) { |
| 102 |
$email_field.tooltipster( 'content', email ? fcaEoiScriptData.invalid_email : fcaEoiScriptData.field_required ) |
| 103 |
$email_field.tooltipster( 'show' ); |
| 104 |
$button.val( '✗ ' + button_initial_val ); |
| 105 |
has_error = true; |
| 106 |
} |
| 107 |
|
| 108 |
// Check name |
| 109 |
if( $name_field.is(':visible') && !name ) { |
| 110 |
$name_field.tooltipster( 'content', fcaEoiScriptData.field_required ); |
| 111 |
$name_field.tooltipster( 'show' ); |
| 112 |
$button.val( '✗ ' + button_initial_val ); |
| 113 |
has_error = true; |
| 114 |
} |
| 115 |
|
| 116 |
// Exit if honeypot is entered |
| 117 |
if( $( '[name=i_agree]', $this ).prop( 'checked' ) ) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
// Exit if there is any error |
| 122 |
if( has_error ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
var gdpr_consent = 'unknown' |
| 127 |
if( fcaEoiScriptData.gdpr_checkbox ) { |
| 128 |
if( $this.find( '.fca_eoi_gdpr_consent' ).length === 0 ) { |
| 129 |
$this.find('.fca_eoi_layout_field_wrapper').hide() |
| 130 |
$this.find('.fca_eoi_layout_email_field_inner').hide() |
| 131 |
$this.find('.fca_eoi_layout_name_field_inner').hide() |
| 132 |
if ( $this.closest( '.fca_eoi_form_wrapper ' ).hasClass( 'fca_eoi_layout_banner_wrapper' ) ) { |
| 133 |
$this.find('.fca_eoi_layout_headline_copy_wrapper')[0].style.setProperty( 'display', 'block', 'important' ) |
| 134 |
$this.find('.fca_eoi_layout_headline_copy_wrapper').html( '<div style="font-size: 14px;">' + fcaEoiScriptData.consent_headline + '<br>' + '<label><input class="fca_eoi_gdpr_consent" value="" type="checkbox"></input>'+fcaEoiScriptData.consent_msg+'</label></div>') |
| 135 |
} else { |
| 136 |
$this.find('.fca_eoi_layout_description_copy_wrapper')[0].style.setProperty( 'display', 'block', 'important' ) |
| 137 |
$this.find('.fca_eoi_layout_description_copy_wrapper').html( '<div style="text-align:left;">' + fcaEoiScriptData.consent_headline + '<br><br>' + '<label><input class="fca_eoi_gdpr_consent" value="" type="checkbox"></input>'+fcaEoiScriptData.consent_msg+'</label></div>') |
| 138 |
} |
| 139 |
return false |
| 140 |
} |
| 141 |
|
| 142 |
gdpr_consent = $this.find( '.fca_eoi_gdpr_consent' ).prop('checked') |
| 143 |
|
| 144 |
if ( gdpr_consent == false ) { |
| 145 |
$this.find( '.fca_eoi_gdpr_consent' ).not('.tooltipstered').tooltipster( { |
| 146 |
contentAsHTML: true, |
| 147 |
trigger: 'none', |
| 148 |
theme: ['tooltipster-borderless', 'tooltipster-optin-cat'] |
| 149 |
} ).tooltipster( 'content', fcaEoiScriptData.field_required ).tooltipster( 'show' ) |
| 150 |
return false |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
$button.val( subscribe_msg ); |
| 156 |
//DISABLE BUTTON CLICK EVENT |
| 157 |
$button.prop('disabled', true); |
| 158 |
|
| 159 |
var tz = jstz.determine() |
| 160 |
|
| 161 |
$.ajax({ |
| 162 |
type: 'POST', |
| 163 |
url: fcaEoiScriptData.ajax_url, |
| 164 |
data: { |
| 165 |
'email': email, |
| 166 |
'name': name, |
| 167 |
'action': 'fca_eoi_subscribe', |
| 168 |
'list_id': list_id, |
| 169 |
'form_id': form_id, |
| 170 |
'nonce': fcaEoiScriptData.nonce, |
| 171 |
'timezone': tz.name(), |
| 172 |
'consent_granted': gdpr_consent |
| 173 |
} |
| 174 |
}).done( function( data ) { |
| 175 |
|
| 176 |
// Stop highlighting and add an icon for success/failure |
| 177 |
if ( data === '✓' ) { |
| 178 |
$button.val( data + ' ' + button_initial_val ); |
| 179 |
|
| 180 |
set_cookie( 'fca_eoi_success_' + form_id, Math.floor(Date.now() / 1000), cookie_duration ) |
| 181 |
|
| 182 |
//REMOVE FROM ACTIVE |
| 183 |
var activeOptins = JSON.parse( get_cookie( 'fca_eoi_active_optins' ) ) |
| 184 |
if ( !Array.isArray( activeOptins ) ) { |
| 185 |
activeOptins = [] |
| 186 |
} |
| 187 |
var index = activeOptins.indexOf( form_id ) |
| 188 |
if ( index !== -1 ) { |
| 189 |
activeOptins.splice( index, 1 ) |
| 190 |
} |
| 191 |
set_cookie ( 'fca_eoi_active_optins', JSON.stringify ( activeOptins ) ) |
| 192 |
|
| 193 |
} else if( data === '✗ Failed to add user - denied' ) { |
| 194 |
$button.prop('disabled', false); |
| 195 |
$button.val( button_initial_val ); |
| 196 |
$this.find( '.fca_eoi_gdpr_consent' ).tooltipster( 'show' ) |
| 197 |
} else { |
| 198 |
$button.prop('disabled', false); |
| 199 |
$button.val( data ); |
| 200 |
} |
| 201 |
|
| 202 |
if ( thank_you_mode === 'ajax' && data === '✓' ) { |
| 203 |
clearInterval( highlight_interval ) |
| 204 |
if ( thank_you_page && '✓' === data ) { |
| 205 |
tooltipWidth = $button.width() |
| 206 |
$button.tooltipster( { |
| 207 |
contentAsHTML: true, |
| 208 |
trigger: 'none', |
| 209 |
fixedWidth: tooltipWidth, |
| 210 |
minWidth: tooltipWidth, |
| 211 |
maxWidth: tooltipWidth, |
| 212 |
arrow: false, |
| 213 |
theme: ['tooltipster-borderless', 'tooltipster-optin-cat'] |
| 214 |
} ); |
| 215 |
|
| 216 |
$button.tooltipster( 'content', thank_you_page ) |
| 217 |
$button.tooltipster( 'show' ) |
| 218 |
$email_field.prop('disabled', true) |
| 219 |
$name_field.prop('disabled', true) |
| 220 |
|
| 221 |
$('.tooltipster-content').css("color", thank_you_text_color ) |
| 222 |
$('.tooltipster-content').css("background-color", thank_you_bg_color ) |
| 223 |
|
| 224 |
} |
| 225 |
} else if ( data === '✓' ) { |
| 226 |
window.location.href = thank_you_page |
| 227 |
} |
| 228 |
}) |
| 229 |
}) |
| 230 |
}) |