Created
April 14, 2014 13:58
-
-
Save steve-ross/10650537 to your computer and use it in GitHub Desktop.
signup form changes
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(); | |
$j('.footer .last a').attr('onclick', ''); // remove the newsletter click event | |
$j('.discount-modal').remove(); | |
var pathArray = window.location.href.split('/'); | |
var host = pathArray[2]; | |
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('#firstemail'), | |
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 signup_newsletter() { | |
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 loc = d.as_obj(); | |
// Prepare query string and send AJAX request | |
$j.ajax({ | |
url: save_url, | |
data: 'ajax=true&email=' + loc.email + '&fname=' + loc.fname + '&lname=' + loc.lname + '&bday=' + loc.bday + '&zcode=' + loc.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_btn').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(r.data[0].merges.FNAME); | |
d.lname.val(r.data[0].merges.LNAME); | |
d.bday.val(r.data[0].merges.BDAY); | |
d.zcode.val(r.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'); | |
} | |
} | |
}); | |
show_step('signup_step_2'); | |
} | |
} // 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) { | |
signup_newsletter(); | |
} 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('#newsback').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('#newsletter_data').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) { | |
var loc = d.as_obj(); | |
if (r.errors !== 1) { | |
// create some variables | |
var fname = r.data['0'].merges.FNAME; | |
var lname = r.data['0'].merges.LNAME; | |
var bday = r.data['0'].merges.BDAY; | |
var zcode = r.data['0'].merges.ZCODE; | |
if (fname !== 'undefined' && lname !== 'undefined' && bday !== 'undefined' && zcode !== 'undefined') { | |
// call submitFunction | |
signup_newsletter(); | |
} else if (fname === loc.fname | |
&& lname === loc.lname | |
&& bday === loc.bday | |
&& zcode === loc.zcode) { | |
// 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 | |
signup_newsletter(); | |
} | |
}else{ | |
// call submitFunction | |
signup_newsletter(); | |
} | |
} // 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