Guest User

Untitled

a guest
Aug 14th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. (function() {
  2. Y.use('jquery-noconflict', function() {
  3. jQuery(function($) {
  4. $(document).ready(function() {
  5. //console.log("custom javascript ready!");
  6.  
  7. /* ------------ Section ------------ */
  8. $("div.donation-level-container input:radio").each(function() {
  9. if ($(this).prop("checked")) {
  10. $(this).parents("div.donation-level-container").addClass("selected");
  11. }
  12. });
  13.  
  14. /* ------------ Section ------------ */
  15. // var selected_amt;
  16.  
  17. // on click of donation amount buttons
  18. // add selected class and change radio prop
  19. $('.donation-level-container').on('change', function() {
  20. $('.donation-level-container').removeClass('selected');
  21.  
  22. $(this).addClass('selected');
  23. $(this).find('input[type="radio"]').prop('checked', true);
  24.  
  25. // var donation_amt_text = $.trim($(this).find('.donation-level-label-container').text());
  26. //
  27. // selected_amt = $.trim((donation_amt_text).slice(1));
  28. });
  29.  
  30. /* ------------ Section ------------ */
  31. var rememberLevel = readCookie('levelId');
  32. var otherAmt = readCookie('otherAmt');
  33. var coverFees = readCookie('coverFees');
  34.  
  35. // move donor cover checkbox below donation amount
  36. $('#level_standard_row > .form-content').append($('#cc_fees_row'));
  37. $('#cc_fees_row').show();
  38.  
  39. // add class to donor entered amount parent container
  40. $('.donation-level-user-entered').parent().addClass('other-amount');
  41.  
  42. var deleteCookie = function(name) {
  43. document.cookie = name + '=;expires= Thu, 01 Jan 1970 00:00:01 GMT;';
  44. };
  45.  
  46. var formatMoney = function(amount) {
  47. amountInCents = Number(amount);
  48. var i = parseInt(amount = Math.abs(+amount || 0).toFixed(2), 10) + '',
  49. j = (j = i.length) > 3 ? j % 3 : 0,
  50. formattedMoney = '$' + (j ? i.substr(0, j) + ',' : '') +
  51. i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + ',') +
  52. '.' + Math.abs(amount - i).toFixed(2).slice(2);
  53.  
  54. formattedMoney = formattedMoney.replace('.00', '');
  55.  
  56. return formattedMoney;
  57. }
  58.  
  59. var populateBtn = function(activeAmt) {
  60. if ($('#cover_fees').is(':checked')) {
  61. var btnText = 'Donate ' + activeAmt;
  62. }
  63. else {
  64. var btnText = 'Donate';
  65. }
  66.  
  67. if ($('#pstep_finish').length > 0) {
  68. $('#pstep_finish').text(btnText);
  69. }
  70. }
  71.  
  72. var getAmt = function() {
  73. if ($('.other-amount input[type="radio"]').is(':checked')) {
  74. selectAmt = Number(delimitNumbers($('.donation-level-user-entered input').val()));
  75.  
  76. document.cookie="levelId=other";
  77. document.cookie="otherAmt=" + selectAmt;
  78. } else {
  79. $("[id^=level_standardexpanded]").each(function() {
  80. if ($(this).is(':checked')) {
  81. var selectedLevelId = $(this).prop('id');
  82. selectAmt = Number(delimitNumbers($(this).parent().next().children('.donation-level-label-container').text()));
  83. document.cookie="levelId=" + selectedLevelId;
  84. }
  85. });
  86. }
  87.  
  88. activeAmtAdd = Number(strRound(delimitNumbers(selectAmt)*.06248),2);
  89. activeAmtAdd = activeAmtAdd + .26;
  90. activeAmt = strRound(delimitNumbers(selectAmt + activeAmtAdd));
  91. activeAmtFormat = formatMoney(activeAmt);
  92.  
  93. $('.cover_fees_total').html('for a total of <strong>' + activeAmtFormat + ' (Credit Card)</strong>');
  94.  
  95. donateAmt = activeAmtFormat;
  96.  
  97. console.log('donateAmt is ' + donateAmt);
  98. }
  99.  
  100. function delimitNumbers(str) {
  101. return (str + '').replace(/[^\d.-]/g, '');
  102. }
  103.  
  104. function removeNonNumbers(str) {
  105. return (str + '').replace(/[^0-9\.]+/g, '');
  106. }
  107.  
  108. function strRound(str) {
  109. str = removeNonNumbers(str).split('.', 2);
  110. if (str[1]) {
  111. str[1] = str[1].substring(0,2);
  112. }
  113. str = str.join('.');
  114. return str;
  115. }
  116.  
  117. function readCookie(name) {
  118. var nameEQ = name + '=';
  119. var ca = document.cookie.split(';');
  120. for(var i=0;i < ca.length;i++) {
  121. var c = ca[i];
  122. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  123. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  124. }
  125. return null;
  126. }
  127.  
  128. if(rememberLevel != undefined || rememberLevel != null) {
  129. $('#' + rememberLevel).prop('checked', true);
  130. $('.donation-level-user-entered input[type="text"]').val('');
  131.  
  132. if (rememberLevel == 'other') {
  133. $('.other-amount input[type="radio"]').prop('checked', true);
  134. $('.donation-level-user-entered input[type="text"]').val(otherAmt);
  135. }
  136. }
  137.  
  138. if (coverFees == 'true') {
  139. $('#cover_fees').prop('checked', true);
  140.  
  141. getAmt();
  142. populateBtn(donateAmt);
  143. }
  144.  
  145. $('#cover_fees, .donation-level-container').change(function(){
  146. if ($('#cover_fees').is(':checked')) {
  147. getAmt();
  148. }
  149. else {
  150. getAmt();
  151. deleteCookie('coverFees');
  152. deleteCookie('levelId');
  153. deleteCookie('otherAmt');
  154. }
  155. populateBtn(donateAmt);
  156. });
  157.  
  158. $('#ProcessForm').submit(function() {
  159. if ($('#cover_fees').is(':checked')) {
  160. getAmt();
  161.  
  162. $('.other-amount input[type="radio"]').prop('checked', true);
  163. $('.donation-level-user-entered input[type="text"]').val(donateAmt);
  164. document.cookie = 'coverFees=true';
  165. return true;
  166. }
  167. else {
  168. deleteCookie('coverFees');
  169. deleteCookie('levelId');
  170. deleteCookie('otherAmt');
  171. return true;
  172. }
  173. });
  174.  
  175. });
  176. });
  177. });
  178. })();
Add Comment
Please, Sign In to add comment