function membershipAttachEvents(){$('#membership-subscribestar-show').off('click.membership').on('click.membership',function(){$('#membership-subscribestar-modal').modal('show');});$('#membership-subscribestar-save').off('click.membership').on('click.membership',function(){membershipSubscribeStarSave();});$('#membership-renewal-show').off('click.membership').on('click.membership',function(){$('#membership-renewal-modal').modal('show');});$('#membership-renewal-save').off('click.membership').on('click.membership',function(){membershipRenewalSave();});$('#membership-claim-mug').off('click.membership').on('click.membership',function(){$('#membership-claim-mug-modal').modal('show');});$('#membership-claim-mug-modal .modal-submit').off('click.membership').on('click.membership',function(){membershipClaimMug();});$('#membership-claim-shirt').off('click.membership').on('click.membership',function(){$('#membership-claim-shirt-modal').modal('show');});$('#membership-claim-shirt-modal .modal-submit').off('click.membership').on('click.membership',function(){membershipClaimShirt();});}
function membershipSubscribeStarSave(){$('#membership-subscribestar-save').prop('disabled',true);$('#membership-subscribestar-modal').modal('hide');$.post({url:'/membership/save/',data:{'csrfmiddlewaretoken':Cookies.get('csrftoken'),'subscribestar':false},success:function(result){if(result.success){$('#membership-subscribestar').addClass('hidden');toastr.success('SubscribeStar account unlinked');}else{toastr.error('Failed to unlink SubscribeStar account');}},error:function(){$('#error_modal').modal('show');},complete:function(){$('#membership-subscribestar-save').prop('disabled',false);}});}
function membershipRenewalSave(){$('#membership-renewal-save').prop('disabled',true);$('#membership-renewal-modal').modal('hide');$.post({url:'/membership/save/',data:{'csrfmiddlewaretoken':Cookies.get('csrftoken'),'recurring':false},success:function(result){if(result.success){$('#membership-renewal').addClass('hidden');toastr.success('Membership automatic renewals cancelled');}else{toastr.error('Failed to cancel membership automatic renewals');}},error:function(){$('#error_modal').modal('show');},complete:function(){$('#membership-renewal-save').prop('disabled',false);}});}
function membershipConfigureAttachEvents(){$('.membership-level-container input').off('click.membership').on('click.membership',function(){membershipConfigureUpdate();});$('.membership-modal').off('click.membership').on('click.membership',function(){$('#membership-modal').modal('show');});$('.membership-cardpayment-modal').off('click.membership').on('click.membership',function(){$('#membership-cardpayment-modal').modal('show');});$('.membership-save').off('click.membership').on('click.membership',function(){membershipConfigureSave();});}
function membershipConfigureUpdate(){$(".membership-level-container input").prop("disabled",true);$.post({url:'/membership/configure/calculate/',data:{'csrfmiddlewaretoken':Cookies.get('csrftoken'),'name':$("input[name='level-choice']:checked").val(),'duration':$("input[name='duration-choice']:checked").val()},success:function(result){if(result.success){$('#payment-amount').text(result.amount);$('#coinpayments-amount').val(result.amount);$('#coinpayments-item-name').val(result.item_name);$('#coinpayments-item-number').val(result.item_number);$('#card-link').attr('href',result.card_link);}else $('#error_modal').modal('show');},error:function(){$('#error_modal').modal('show');},complete:function(){$(".membership-level-container input").prop("disabled",false);}});}
function membershipConfigureSave(){$('.membership-save').prop('disabled',true);$('#membership-modal').modal('hide');$.post({url:'/membership/save/',data:{'csrfmiddlewaretoken':Cookies.get('csrftoken'),'name':$("input[name='level-choice']:checked").val(),'duration':$("input[name='duration-choice']:checked").val()},success:function(result){if(result.success){toastr.success('Membership configuration saved');if(result.level_changed)window.location.reload();}else{toastr.error('Failed to save membership configuration');}},error:function(){$('#error_modal').modal('show');},complete:function(){$('.membership-save').prop('disabled',false);}});}
function membershipClaimMug(){$.post({url:'/membership/claim/mug/',data:$('#claim-mug-form').serialize(),success:function(result){$('#claim-mug-form [id^="claim-mug-error"]').addClass('hidden').text('');if(result.success){$('#membership-claim-mug').addClass('claimed').removeClass('unclaimed').text('Claimed').off('click.membership');$('#membership-claim-mug-modal').modal('hide');toastr.success('Your claim has been successfully submitted');}else{toastr.error('Please resolve errors then resubmit');for(i=0;i<result.errors.length;i++){if(result.errors[i][0]=='__all__'){$('#claim-mug-error').removeClass('hidden').text(result.errors[i][1]);}else{$('#claim-mug-error-'+result.errors[i][0]).removeClass('hidden').text(result.errors[i][1]);}}}},error:function(){$('#membership-claim-mug-modal').modal('hide');$('#error_modal').modal('show');},complete:function(){}});}
function membershipClaimShirt(){$.post({url:'/membership/claim/shirt/',data:$('#claim-shirt-form').serialize(),success:function(result){$('#claim-shirt-form [id^="claim-shirt-error"]').addClass('hidden').text('');if(result.success){$('#membership-claim-shirt').addClass('claimed').removeClass('unclaimed').text('Claimed').off('click.membership');$('#membership-claim-shirt-modal').modal('hide');toastr.success('Your claim has been successfully submitted');}else{toastr.error('Please resolve errors then resubmit');for(i=0;i<result.errors.length;i++){if(result.errors[i][0]=='__all__'){$('#claim-shirt-error').removeClass('hidden').text(result.errors[i][1]);}else{$('#claim-shirt-error-'+result.errors[i][0]).removeClass('hidden').text(result.errors[i][1]);}}}},error:function(){$('#membership-claim-shirt-modal').modal('hide');$('#error_modal').modal('show');},complete:function(){}});}
function membershipClaimCodeAttachEvents(){$('#membership-claim-code-invalidate').off('click.membership').on('click.membership',function(){membershipClaimCodeInvalidate();});$('#membership-claim-code-redeem').off('click.membership').on('click.membership',function(){membershipClaimCodeRedeem();});}
function membershipClaimCodeInvalidate(){$('#membership-claim-code-invalidate').prop('disabled',true);$.post({url:'/membership/claim/'+$('#membership-claim-code').text()+'/invalidate/',data:{'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){$('#membership-claim-code-invalidate').addClass('hidden');$('#membership-claim-code-redeem').addClass('hidden');$('#membership-claim-code-finish').removeClass('hidden');toastr.success('Membership claim code invalidated');}else{toastr.error('Failed to invalidate claim code');}},error:function(){$('#error_modal').modal('show');},complete:function(){$('#membership-claim-code-invalidate').prop('disabled',false);}});}
function membershipClaimCodeRedeem(){$('#membership-claim-code-redeem').prop('disabled',true);$.post({url:'/membership/claim/'+$('#membership-claim-code').text()+'/redeem/',data:{'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){$('#membership-claim-code-invalidate').addClass('hidden');$('#membership-claim-code-redeem').addClass('hidden');$('#membership-claim-code-finish').removeClass('hidden');$('#membership-claim-code-status').html('Claimed');toastr.success('Membership claim code redeemed');}else{toastr.error('Failed to redeem claim code');}},error:function(){$('#error_modal').modal('show');},complete:function(){$('#membership-claim-code-redeem').prop('disabled',false);}});}
function membershipSubscribeStarLinkAttachEvents(){$('#membership-subscribestar-link-account').off('click.membership').on('click.membership',function(){membershipSubscribeStarLink();});}
function membershipSubscribeStarLink(){$('#membership-subscribestar-link-account').prop('disabled',true);$.post({url:'/membership/subscribestar/'+$('#membership-subscribestar-link-code').text()+'/link/',data:{'csrfmiddlewaretoken':Cookies.get('csrftoken')},success:function(result){if(result.success){$('#membership-subscribestar-link-account').addClass('hidden');$('#membership-subscribestar-link-finish').removeClass('hidden');toastr.success('SubscribeStar account linked');}else{toastr.error('Failed to link SubscribeStar account');}},error:function(){$('#error_modal').modal('show');},complete:function(){$('#membership-subscribestar-link-account').prop('disabled',false);}});}