Last active
August 29, 2015 13:58
-
-
Save steve-ross/9957350 to your computer and use it in GitHub Desktop.
helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$j(document).ready(function () { | |
// enable strict mode | |
'use strict'; | |
// hide the site newsletter modal | |
$j('.signup-form').hide(); | |
var check_url = 'http://' + host + '/skin/frontend/enterprise/rebeccaminkoff/inc/sanity-check.php'; | |
var save_url = 'http://' + host + '/skin/frontend/enterprise/rebeccaminkoff/inc/store-address.php'; | |
function get_newsletter_signup_data(){ | |
return { | |
fname : $j('#first_name'), | |
email : $j('#email_address'), | |
lname : $j('#last_name'), | |
bday : $j('#birth_day'), | |
zcode : $j('#zip_code'), | |
err : $j('#error_message'), | |
msg : $j('#success_message'), | |
as_obj : function(){ | |
return { | |
fname : this.fname.val(), | |
email : escape(this.email.val()), | |
lname : this.lname.val(), | |
bday : escape(this.bday.val()), | |
zcode : escape(this.zcode.val()) | |
}; | |
} | |
}; | |
} | |
function show_step(step){ | |
var to_show = step || '1'; | |
var steps = $j('.signup_steps'); | |
steps.hide(); | |
$j('.' + step).show(); | |
} | |
show_step('signup_step_1'); | |
function submitFunction() { | |
var d = get_newsletter_signup_data(); | |
// create a cookie | |
$j.cookie('rmnewsletter', '873hlkhoa8d983jw987don23o8', { expires: 7 }); | |
// moves step two out of view and step three into view | |
show_step('signup_step_3'); | |
// update user interface | |
d.msg.html('Adding email address...'); | |
var data = d.as_obj(); | |
// Prepare query string and send AJAX request | |
$j.ajax({ | |
url: save_url, | |
data: 'ajax=true&email=' + data.email + '&fname=' + data.fname + '&lname=' + data.lname + '&bday=' + data.bday + '&zcode=' + data.zcode, | |
context: d, | |
success: function (msg) { | |
d.msg.html(msg); | |
} | |
}); | |
return false; | |
} | |
// moves step one out of view and step two into view | |
$j('.signup_step_1').click(function (e) { | |
e.preventDefault(); | |
// regex to check email address | |
function isEmail(email) { | |
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; | |
return regex.test(email); | |
} | |
// check email address for validity | |
var d = get_newsletter_signup_data(); | |
if (isEmail(d.as_obj().email)) { | |
$j.ajax({ | |
url: check_url, | |
data: 'id=c7ad6a09be&firstemail=' + d.as_obj().email, | |
dataType: 'json', | |
context: d, | |
success: function (r) { | |
if (r.errors === 1) { | |
// move onto the next step | |
show_step('signup_step_2'); | |
} else if (r.data['0'].merges.FNAME !== '' && r.data['0'].merges.LNAME !== '' && r.data['0'].merges.BDAY !== '' && r.data['0'].merges.ZCODE !== '') { | |
// move modal to the last step | |
show_step('signup_step_4'); | |
} else { | |
// assign response data to the form | |
d.fname.val(data.data['0'].merges.FNAME); | |
d.lname.val(data.data['0'].merges.LNAME); | |
d.bday.val(data.data['0'].merges.BDAY); | |
d.zcode.val(data.data['0'].merges.ZCODE); | |
// Disable form fields that contain information | |
$j.each(d, function(key, $el){ | |
if($el !== null && typeof($el) !== 'function'){ | |
if($el.val() !== ''){ | |
$el.attr('disabled', 'disabled'); | |
} | |
} | |
}); | |
} | |
} // end success | |
}); | |
} else { | |
d.email.css('border', '1px solid red'); | |
d.err.html("Please enter a valid email address."); | |
} | |
}); | |
// Skip Button functionality | |
$j('.skipform').click(function (e) { | |
var d = get_newsletter_signup_data(); | |
// make an ajax call | |
$j.ajax({ | |
url: check_url, | |
data: 'id=75f40c1657&firstemail=' + d.as_obj().email, | |
context: d, | |
dataType: 'json', | |
success: function (r) { | |
if (r.errors === 1) { | |
submitFunction(); | |
} else { | |
// move modal to the last step | |
show_step('signup_step_5'); | |
} // end if statement | |
} // end success | |
}); // end $j.ajax | |
}); // end click function | |
// back button functionality | |
$j('.back-button').click(function (e) { | |
// move modals into their correct place | |
show_step('signup_step_2'); | |
var d = get_newsletter_signup_data(); | |
d.err.html(''); | |
}); // end click function | |
// step two creates the submit function | |
$j('#mailchimp-subscribe').submit(function (e) { | |
e.preventDefault(); | |
// Number RegEx | |
var pattern = /^(\s*|(\d{2})\/(\d{2}))$/; | |
var d = get_newsletter_signup_data(); | |
if (pattern.test(d.as_obj().bday === 'true')) { | |
d.bday.css('border', '1px solid #bebebe'); | |
d.err.html(''); | |
} | |
// Conditional to check if the field is a valid entry | |
if (!pattern.test(d.as_obj().bday)) { | |
d.bday.css('border', '1px solid red'); | |
d.err.html('Birthday must be submitted as month and day of the month, in the following format MM/DD.'); | |
} else { | |
$j.ajax({ | |
url: check_url, | |
data: 'id=75f40c1657&firstemail=' + d.as_obj().email, | |
context: d, | |
dataType: 'json', | |
success: function (r) { | |
if (r.errors !== 1) { | |
// create some variables | |
var fname = data.data['0'].merges.FNAME, | |
lname = data.data['0'].merges.LNAME, | |
bday = data.data['0'].merges.BDAY, | |
zcode = data.data['0'].merges.ZCODE; | |
} | |
if (fname === 'undefined' && lname === 'undefined' && bday === 'undefined' && zcode === 'undefined') { | |
// call submitFunction | |
submitFunction(); | |
} else if (fname === $j('#fname').val() && data.data['0'].merges.LNAME === $j('#lname').val() && data.data['0'].merges.BDAY === $j('#bday').val() && data.data['0'].merges.ZCODE === $j('#zcode').val()) { | |
// Display no new information error | |
d.err.html('If you don\'t want to provide us with any new information, please click the \'Skip\' button.'); | |
} else { | |
// call submitFunction | |
submitFunction(); | |
} | |
} // end success | |
}); // end ajax call | |
} // end if test | |
}); // end step two | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment