tranquangchau

https://signup.dreamhost.com/js/steps/billing.js?1432324825.

May 23rd, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(ndn, $){
  2.  
  3.     var text_box_names = [
  4.         "first_name", "last_name",
  5.         "address", "address2", "city", "state", "postal_code", "country",
  6.         "phone", "twitter",
  7.         "gift_certificate", "promo_code",
  8.         "cc_number", "cc_month", "cc_year", "cc_ccv"
  9.     ];
  10.  
  11.     var text_box_logged = {};
  12.     var current_page = location.pathname.split("/")[1];
  13.  
  14.     ndn.signup = ndn.signup || {};
  15.     ndn.signup.billing = ndn.signup.billing || {};
  16.     ndn.signup.billing.xhr;
  17.  
  18.     var cc_map = {
  19.         '3': 'amex',
  20.         '4': 'visa',
  21.         '5': 'mastercard',
  22.         '6': 'discover'
  23.     };
  24.  
  25.     var STATES = {
  26.         'US': [
  27.             'Alabama',
  28.             'Alaska',
  29.             'Arizona',
  30.             'Arkansas',
  31.             'California',
  32.             'Colorado',
  33.             'Connecticut',
  34.             'Delaware',
  35.             'District of Columbia',
  36.             'Florida',
  37.             'Georgia',
  38.             'Hawaii',
  39.             'Idaho',
  40.             'Illinois',
  41.             'Indiana',
  42.             'Iowa',
  43.             'Kansas',
  44.             'Kentucky',
  45.             'Louisiana',
  46.             'Maine',
  47.             'Maryland',
  48.             'Massachusetts',
  49.             'Michigan',
  50.             'Minnesota',
  51.             'Mississippi',
  52.             'Missouri',
  53.             'Montana',
  54.             'Nebraska',
  55.             'Nevada',
  56.             'New Hampshire',
  57.             'New Jersey',
  58.             'New Mexico',
  59.             'New York',
  60.             'North Carolina',
  61.             'North Dakota',
  62.             'Ohio',
  63.             'Oklahoma',
  64.             'Oregon',
  65.             'Pennsylvania',
  66.             'Rhode Island',
  67.             'South Carolina',
  68.             'South Dakota',
  69.             'Tennessee',
  70.             'Texas',
  71.             'Utah',
  72.             'Vermont',
  73.             'Virginia',
  74.             'Washington',
  75.             'West Virginia',
  76.             'Wisconsin',
  77.             'Wyoming'
  78.         ],
  79.         'CA': [
  80.             'Alberta',
  81.             'British Columbia',
  82.             'Manitoba',
  83.             'New Brunswick',
  84.             'Newfoundland and Labrador',
  85.             'Northwest Territories',
  86.             'Nova Scotia',
  87.             'Nunavut',
  88.             'Ontario',
  89.             'Prince Edward Island',
  90.             'Quebec',
  91.             'Saskatchewan',
  92.             'Yukon'
  93.         ]
  94.     };
  95.  
  96.     ndn.signup.billing.initStateProvinceDropdown = function(){
  97.         $('input#state').typeahead().on('keydown', function(){
  98.             var country = $('select#country').val();
  99.             $(this).data('typeahead').source = STATES[country] || [];
  100.         });
  101.         $('input#state').on('keydown', function(){
  102.             $(this).attr('autocomplete', 'off');
  103.         });
  104.         $('input#state').on('change', function(){
  105.             if(!$(this).val())
  106.                 $(this).removeAttr('autocomplete');
  107.         });
  108.     };
  109.  
  110.     ndn.signup.billing.setupCardTypeListener = function(){
  111.         $('input#cc_number').keyup(function(){
  112.             var first = $(this).val().charAt(0);
  113.             $('#cc_types li').removeClass('active');
  114.             $('#cc_type_'+cc_map[first]).addClass('active');
  115.         });
  116.     };
  117.  
  118.     ndn.signup.billing.initPromotionField = function(){
  119.         $("input#promo_code").bind("input propertychange", function(evt){
  120.             // If it's the propertychange event, make sure it's the value that changed.
  121.             if (window.event && event.type == "propertychange" && event.propertyName != "value")
  122.                 return;
  123.  
  124.             // Clear any previously set timer before setting a fresh one
  125.             window.clearTimeout($(this).data("timeout"));
  126.             $(this).data("timeout", setTimeout(function (){
  127.                 ndn.signup.billing.checkPromo();
  128.             }, 1000));
  129.         });
  130.         $('select#country').change(function(){
  131.             if($('input#promo_code').val())
  132.                 ndn.signup.billing.checkPromo();
  133.         });
  134.     };
  135.  
  136.     ndn.signup.billing.initGCField = function(){
  137.         $("input#gift_certificate").bind("input propertychange", function(evt){
  138.             // If it's the propertychange event, make sure it's the value that changed.
  139.             if (window.event && event.type == "propertychange" && event.propertyName != "value")
  140.                 return;
  141.  
  142.             // Clear any previously set timer before setting a fresh one
  143.             window.clearTimeout($(this).data("timeout"));
  144.             $(this).data("timeout", setTimeout(function (){
  145.                 ndn.signup.billing.checkGC();
  146.             }, 1000));
  147.         });
  148.     };
  149.  
  150.     ndn.signup.billing.checkPromo = function(){
  151.         var code = $('input#promo_code').val();
  152.  
  153.         $('#promo_code_container .add-on').addClass('loading');
  154.         $('#promo_code_container .add-on').removeClass('valid');
  155.         $('#promo_code_container .add-on').removeClass('invalid');
  156.         $('.promo_special_message').html('');
  157.  
  158.         // Disable the submission button
  159.         $('.form-actions .btn.btn-primary').attr('disabled', true);
  160.  
  161.         ndn.signup.billing.xhr = $.ajax({
  162.             url: window.location.pathname.toString() + 'promo',
  163.             type: 'POST',
  164.             data: {
  165.                 'code': code,
  166.                 'country': $('select#country').val()
  167.             },
  168.             timeout: 10000,
  169.             error: function(jqXHR, error){
  170.                 $('#promo_code_container .add-on').removeClass('loading')
  171.                     .addClass('invalid');
  172.                 // Re-enable the submission button
  173.                 $('.form-actions .btn.btn-primary').attr('disabled', false);
  174.                 ndn.signup.billing.updateSummary();
  175.             }
  176.         }).done(function(data){
  177.             $('#promo_code_container .add-on').removeClass('loading');
  178.             if(code)
  179.                 $('#promo_code_container .add-on').addClass('valid');
  180.             // Re-enable the submission button
  181.             $('.form-actions .btn.btn-primary').attr('disabled', false);
  182.  
  183.             if(typeof data == "object")
  184.                 ndn.signup.billing.updateSummary();
  185.             else
  186.                 $('.promo_special_message').html(data).show();
  187.         });
  188.     };
  189.  
  190.     ndn.signup.billing.checkGC = function(){
  191.         if(!$('input#gift_certificate').length)
  192.             return;
  193.  
  194.         var code = $('input#gift_certificate').val();
  195.         if(!code){
  196.             $('#gift_certificate_message').html('');
  197.         }
  198.  
  199.         $('#gift_certificate_container .add-on').addClass('loading');
  200.         $('#gift_certificate_container .add-on').removeClass('valid');
  201.         $('#gift_certificate_container .add-on').removeClass('invalid');
  202.  
  203.         // Disable the submission button
  204.         $('.form-actions .btn.btn-primary').attr('disabled', true);
  205.  
  206.         ndn.signup.billing.xhr = $.ajax({
  207.             url: window.location.pathname.toString() + 'gift',
  208.             type: 'POST',
  209.             data: {
  210.                 'code': code
  211.             },
  212.             dataType: 'json',
  213.             timeout: 10000,
  214.             error: function(jqXHR, error){
  215.                 if(jqXHR.status == 429)
  216.                     var error = "Please contact customer support for help applying your gift certificate.";
  217.                 else
  218.                     var error = $.parseJSON(jqXHR.responseText)['error'];
  219.                 $('#gift_certificate_container .add-on').removeClass('loading');
  220.                 if (code) {
  221.                     $('#gift_certificate_message').html(error);
  222.                     $('#gift_certificate_container .add-on').addClass('invalid');
  223.                 }
  224.                 // Re-enable the submission button
  225.                 $('.form-actions .btn.btn-primary').attr('disabled', false);
  226.                 ndn.signup.billing.updateSummary();
  227.             }
  228.         }).done(function(data){
  229.             var amount = data['amount'];
  230.             $('#gift_certificate_message').html(
  231.                 'A <b>' + amount + '</b> credit will be applied to your account.'
  232.             );
  233.             $('#gift_certificate_container .add-on').removeClass('loading');
  234.             $('#gift_certificate_container .add-on').addClass('valid');
  235.             // Re-enable the submission button
  236.             $('.form-actions .btn.btn-primary').attr('disabled', false);
  237.         });
  238.  
  239.         ndn.signup.billing.updateSummary();
  240.     };
  241.  
  242.     ndn.signup.billing.updateSummary = function(){
  243.         ndn.signup.billing.xhr = $.ajax({
  244.             url: window.location.pathname.toString() + '../summary',
  245.             type: 'GET',
  246.             timeout: 3000
  247.         }).done(function(data){
  248.             $('.summary').html(data);
  249.         });
  250.     };
  251.  
  252.     ndn.signup.billing.initFinalizeButton = function(){
  253.         $('.right form').submit(function(e){
  254.             var self = this;
  255.             $('#finish-loading').modal({
  256.                 keyboard: false,
  257.                 backdrop: 'static'
  258.             });
  259.  
  260.             if('ontouchend' in document){
  261.                 // On touch/mobile devices, center the dialog in the viewport
  262.                 $('#finish-loading').on('shown', function(e) {
  263.                     var modal = $(this);
  264.                     modal.css('margin-top', window.scrollY + 25)
  265.                         .css('width', '300px');
  266.                     return this;
  267.                 });
  268.             }
  269.  
  270.             window.setTimeout(function(){
  271.                 self.submit();
  272.             }, 1000);
  273.             return false;
  274.         });
  275.     };
  276.  
  277.     ndn.signup.billing.initBillingLog = function(){
  278.         var wrapper= function(name){
  279.             return function(){
  280.                 if (text_box_logged[name]) return;
  281.                 if (!$('#'+name).val().length) return;
  282.                 text_box_logged[name]=true;
  283.                 ga('send','event',current_page+'_sign_up','billing',name+'_entered');
  284.             };
  285.         };
  286.         $.each(text_box_names,function(index,value){
  287.             $('#'+value).on('change',wrapper(value));
  288.         });
  289.     };
  290.  
  291. })(window.ndn = window.ndn || {}, jQuery);
  292.  
  293. $(function(){
  294.     ndn.signup.billing.setupCardTypeListener();
  295.     ndn.signup.billing.initStateProvinceDropdown();
  296.     ndn.signup.billing.initPromotionField();
  297.     ndn.signup.billing.initGCField();
  298.     ndn.signup.billing.initFinalizeButton();
  299.     $('select#country').change(ndn.signup.billing.initStateProvinceDropdown);
  300.     $('#cc_types').insertBefore($('#cc_number ~ span.error-message'));
  301.     if($('input#promo_code').val())
  302.         ndn.signup.billing.checkPromo();
  303.     else
  304.         $('#special_eligible_offer').show();
  305.     ndn.signup.billing.initBillingLog();
  306. });
Add Comment
Please, Sign In to add comment