Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function(ndn, $){
- var text_box_names = [
- "first_name", "last_name",
- "address", "address2", "city", "state", "postal_code", "country",
- "phone", "twitter",
- "gift_certificate", "promo_code",
- "cc_number", "cc_month", "cc_year", "cc_ccv"
- ];
- var text_box_logged = {};
- var current_page = location.pathname.split("/")[1];
- ndn.signup = ndn.signup || {};
- ndn.signup.billing = ndn.signup.billing || {};
- ndn.signup.billing.xhr;
- var cc_map = {
- '3': 'amex',
- '4': 'visa',
- '5': 'mastercard',
- '6': 'discover'
- };
- var STATES = {
- 'US': [
- 'Alabama',
- 'Alaska',
- 'Arizona',
- 'Arkansas',
- 'California',
- 'Colorado',
- 'Connecticut',
- 'Delaware',
- 'District of Columbia',
- 'Florida',
- 'Georgia',
- 'Hawaii',
- 'Idaho',
- 'Illinois',
- 'Indiana',
- 'Iowa',
- 'Kansas',
- 'Kentucky',
- 'Louisiana',
- 'Maine',
- 'Maryland',
- 'Massachusetts',
- 'Michigan',
- 'Minnesota',
- 'Mississippi',
- 'Missouri',
- 'Montana',
- 'Nebraska',
- 'Nevada',
- 'New Hampshire',
- 'New Jersey',
- 'New Mexico',
- 'New York',
- 'North Carolina',
- 'North Dakota',
- 'Ohio',
- 'Oklahoma',
- 'Oregon',
- 'Pennsylvania',
- 'Rhode Island',
- 'South Carolina',
- 'South Dakota',
- 'Tennessee',
- 'Texas',
- 'Utah',
- 'Vermont',
- 'Virginia',
- 'Washington',
- 'West Virginia',
- 'Wisconsin',
- 'Wyoming'
- ],
- 'CA': [
- 'Alberta',
- 'British Columbia',
- 'Manitoba',
- 'New Brunswick',
- 'Newfoundland and Labrador',
- 'Northwest Territories',
- 'Nova Scotia',
- 'Nunavut',
- 'Ontario',
- 'Prince Edward Island',
- 'Quebec',
- 'Saskatchewan',
- 'Yukon'
- ]
- };
- ndn.signup.billing.initStateProvinceDropdown = function(){
- $('input#state').typeahead().on('keydown', function(){
- var country = $('select#country').val();
- $(this).data('typeahead').source = STATES[country] || [];
- });
- $('input#state').on('keydown', function(){
- $(this).attr('autocomplete', 'off');
- });
- $('input#state').on('change', function(){
- if(!$(this).val())
- $(this).removeAttr('autocomplete');
- });
- };
- ndn.signup.billing.setupCardTypeListener = function(){
- $('input#cc_number').keyup(function(){
- var first = $(this).val().charAt(0);
- $('#cc_types li').removeClass('active');
- $('#cc_type_'+cc_map[first]).addClass('active');
- });
- };
- ndn.signup.billing.initPromotionField = function(){
- $("input#promo_code").bind("input propertychange", function(evt){
- // If it's the propertychange event, make sure it's the value that changed.
- if (window.event && event.type == "propertychange" && event.propertyName != "value")
- return;
- // Clear any previously set timer before setting a fresh one
- window.clearTimeout($(this).data("timeout"));
- $(this).data("timeout", setTimeout(function (){
- ndn.signup.billing.checkPromo();
- }, 1000));
- });
- $('select#country').change(function(){
- if($('input#promo_code').val())
- ndn.signup.billing.checkPromo();
- });
- };
- ndn.signup.billing.initGCField = function(){
- $("input#gift_certificate").bind("input propertychange", function(evt){
- // If it's the propertychange event, make sure it's the value that changed.
- if (window.event && event.type == "propertychange" && event.propertyName != "value")
- return;
- // Clear any previously set timer before setting a fresh one
- window.clearTimeout($(this).data("timeout"));
- $(this).data("timeout", setTimeout(function (){
- ndn.signup.billing.checkGC();
- }, 1000));
- });
- };
- ndn.signup.billing.checkPromo = function(){
- var code = $('input#promo_code').val();
- $('#promo_code_container .add-on').addClass('loading');
- $('#promo_code_container .add-on').removeClass('valid');
- $('#promo_code_container .add-on').removeClass('invalid');
- $('.promo_special_message').html('');
- // Disable the submission button
- $('.form-actions .btn.btn-primary').attr('disabled', true);
- ndn.signup.billing.xhr = $.ajax({
- url: window.location.pathname.toString() + 'promo',
- type: 'POST',
- data: {
- 'code': code,
- 'country': $('select#country').val()
- },
- timeout: 10000,
- error: function(jqXHR, error){
- $('#promo_code_container .add-on').removeClass('loading')
- .addClass('invalid');
- // Re-enable the submission button
- $('.form-actions .btn.btn-primary').attr('disabled', false);
- ndn.signup.billing.updateSummary();
- }
- }).done(function(data){
- $('#promo_code_container .add-on').removeClass('loading');
- if(code)
- $('#promo_code_container .add-on').addClass('valid');
- // Re-enable the submission button
- $('.form-actions .btn.btn-primary').attr('disabled', false);
- if(typeof data == "object")
- ndn.signup.billing.updateSummary();
- else
- $('.promo_special_message').html(data).show();
- });
- };
- ndn.signup.billing.checkGC = function(){
- if(!$('input#gift_certificate').length)
- return;
- var code = $('input#gift_certificate').val();
- if(!code){
- $('#gift_certificate_message').html('');
- }
- $('#gift_certificate_container .add-on').addClass('loading');
- $('#gift_certificate_container .add-on').removeClass('valid');
- $('#gift_certificate_container .add-on').removeClass('invalid');
- // Disable the submission button
- $('.form-actions .btn.btn-primary').attr('disabled', true);
- ndn.signup.billing.xhr = $.ajax({
- url: window.location.pathname.toString() + 'gift',
- type: 'POST',
- data: {
- 'code': code
- },
- dataType: 'json',
- timeout: 10000,
- error: function(jqXHR, error){
- if(jqXHR.status == 429)
- var error = "Please contact customer support for help applying your gift certificate.";
- else
- var error = $.parseJSON(jqXHR.responseText)['error'];
- $('#gift_certificate_container .add-on').removeClass('loading');
- if (code) {
- $('#gift_certificate_message').html(error);
- $('#gift_certificate_container .add-on').addClass('invalid');
- }
- // Re-enable the submission button
- $('.form-actions .btn.btn-primary').attr('disabled', false);
- ndn.signup.billing.updateSummary();
- }
- }).done(function(data){
- var amount = data['amount'];
- $('#gift_certificate_message').html(
- 'A <b>' + amount + '</b> credit will be applied to your account.'
- );
- $('#gift_certificate_container .add-on').removeClass('loading');
- $('#gift_certificate_container .add-on').addClass('valid');
- // Re-enable the submission button
- $('.form-actions .btn.btn-primary').attr('disabled', false);
- });
- ndn.signup.billing.updateSummary();
- };
- ndn.signup.billing.updateSummary = function(){
- ndn.signup.billing.xhr = $.ajax({
- url: window.location.pathname.toString() + '../summary',
- type: 'GET',
- timeout: 3000
- }).done(function(data){
- $('.summary').html(data);
- });
- };
- ndn.signup.billing.initFinalizeButton = function(){
- $('.right form').submit(function(e){
- var self = this;
- $('#finish-loading').modal({
- keyboard: false,
- backdrop: 'static'
- });
- if('ontouchend' in document){
- // On touch/mobile devices, center the dialog in the viewport
- $('#finish-loading').on('shown', function(e) {
- var modal = $(this);
- modal.css('margin-top', window.scrollY + 25)
- .css('width', '300px');
- return this;
- });
- }
- window.setTimeout(function(){
- self.submit();
- }, 1000);
- return false;
- });
- };
- ndn.signup.billing.initBillingLog = function(){
- var wrapper= function(name){
- return function(){
- if (text_box_logged[name]) return;
- if (!$('#'+name).val().length) return;
- text_box_logged[name]=true;
- ga('send','event',current_page+'_sign_up','billing',name+'_entered');
- };
- };
- $.each(text_box_names,function(index,value){
- $('#'+value).on('change',wrapper(value));
- });
- };
- })(window.ndn = window.ndn || {}, jQuery);
- $(function(){
- ndn.signup.billing.setupCardTypeListener();
- ndn.signup.billing.initStateProvinceDropdown();
- ndn.signup.billing.initPromotionField();
- ndn.signup.billing.initGCField();
- ndn.signup.billing.initFinalizeButton();
- $('select#country').change(ndn.signup.billing.initStateProvinceDropdown);
- $('#cc_types').insertBefore($('#cc_number ~ span.error-message'));
- if($('input#promo_code').val())
- ndn.signup.billing.checkPromo();
- else
- $('#special_eligible_offer').show();
- ndn.signup.billing.initBillingLog();
- });
Add Comment
Please, Sign In to add comment