function setTotal(_this) {
    var service_type = $(_this).val();
    var edit_id = $('input[name="hidden_id"]').val();
    $.ajax({
        async: false,
        url: base_url + 'billing_other_services/get_amount_by_service_id',
        type: 'POST',
        data: { service_type: service_type},
        dataType: "json"
    })
    .done(function(data) {
        
        if( parseFloat(data.amount) <= parseFloat(0) ) {
            // Disable Payment Btn
            $('#payment-btn').attr('disabled', 'disabled');
        } else {
            $('#payment-btn').removeAttr('disabled');
        }
        if(edit_id == '') {
            $('[name="amount"], [name="hidden_amount"]').val( data.amount );
        }
        $('input[name="fee_amount"]').trigger('keyup');
    })
    .fail(function(jqXHR, status_code, error_thrown) {
        console.log(jqXHR.responseText);
    });
}

var inter = '';
$(document).on('ready', function() {
    $('input[name="entry_fee"]').on('ifChecked',function() {
        if($(this).val() == 1) {
            $('.entry_fee_amount').show();
            $('input[name="fee_amount"]').attr('class','form-control float_value amount num-pad req');
            var $amount = $('[name="amount"]').val();
        } else {
            $('input[name="fee_amount"]').val('');
            $('.entry_fee_amount').hide();
            $('input[name="fee_amount"]').removeAttr('class','req');
            var $amount = $('[name="hidden_amount"]').val();
        }
       get_total_amount($('input[name="fee_amount"]'),$amount);
    });
    
    $('select[name="service_type"]').change(function(){
        setTotal(this);       
    });
    
    if($('input[name="service_type"]').val() != '') {
        setTotal($('input[name="service_type"]'));
    }
    
    $('#sbmt-cancel-btn').on('click', function(){
        if(method == 'add') {
            $('[name="paid_amount"], .ptype-box input').val('');
        }
        $('#payment-btn').removeClass('dialog-btn').removeAttr('data-target');
        
    })
    
    $('#payment-btn').on('click', function(){
        $('[name="pay_amount"]').val($('[name="amount"]').val());
        var paidAmt = parseFloat( $('[name="paid_amount"]').val()) || 0;
        var payAmt = parseFloat( $('[name="pay_amount"]').val());
        var edit_id = $('input[name="hidden_id"]').val(); 
       	
		$.ajax({
			url: base_url + 'billing_member_subscription/member_bill_amount',
			data: {amount: payAmt.toFixed(2)},
			dataType: 'JSON',
			type: 'post',
			'global': false,
			'async': false,
			success: function (output) {
				//tmp = output;
			}
		});
	   
        if( autoValidate() && !$(this).hasClass('dialog-btn') ) { 
            // Go to payment
            $('.ptype-box input:first').parent().closest('.modal').find('.err-blck').html('');
            if( paidAmt <= 0 ) {
                $('[type="submit"]').attr('disabled', 'disabled');
            }
            $(this).addClass('dialog-btn').attr('data-target','#ptypes-dialog').trigger('click');
            return true;
        }
        if( paidAmt <= 0 ) {
            $('[type="submit"]').attr('disabled', 'disabled');
        }
        if(edit_id == '') {
            $('#ptypes-dialog .pay-type:first, [name="paid_amount"]').val((parseFloat(payAmt)).toFixed(2));
        }
        
        if(edit_id != '') {
            var $bank_name = $('#cheque-box .hidden_bank_name').val();
            var $cheque_no = $('#cheque-box .hidden_chq_no').val();
            var $cheque_date = $('#cheque-box .hidden_chq_date').val();
            $('#cheque-box [name="chq_bnk_name"]').val($bank_name);
            $('#cheque-box [name="chq_no"]').val($cheque_no);
            $('#cheque-box [name="chq_date"]').val($cheque_date);
        }
        if( edit_id != '' && (parseFloat(payAmt) != parseFloat(paidAmt))) { 
            $('#sbmt-btn').attr('disabled', 'disabled');
        } else {
            $('#sbmt-btn').removeAttr('disabled');
        }  
        return false;
    });
    
    get_total_amount($('input[name="fee_amount"]'),$('[name="amount"]').val());
    
    $('input[name="amount"]').keyup(function() {
        $('[name="pay_amount"]').val($(this).val());
    })
    
    $('input[name="fee_amount"]').keyup(function() {
        get_total_amount($(this),$('[name="hidden_amount"]').val());
    });
    
    /* $('.num-pad .ok-btn').click(function() {
        $('input[name="fee_amount"]').trigger('keyup');
    });  */   
    
    $('input[name="member_no"]').keyup(function() {
        var mem_no = $(this).val();
        var mem_id = $('input[name="hidden_id"]').val();
        $.ajax({
        async: false,
        url: base_url + 'billing_member_subscription/check_mem_no_exists',
        type: 'POST',
        data: { 
            mem_no: mem_no,
            meme_id: mem_id
        },
        dataType: "json"
        })
        .done(function(data) {
            if(data == 1) {
                $('input[name="member_no"]').val('');
                $('input[name="member_no"]').focus();
                msgBox('Member No is already exists!');
                $('#payment-btn').attr('disabled','disabled');
            } else {
               $('#payment-btn').removeAttr('disabled','disabled'); 
            }
            
        }).fail(function(jqXHR, status_code, error_thrown) {
            console.log(jqXHR.responseText);
        });
    });
    
});

function get_total_amount(_this,$amount){
    
    var entry_fee = parseFloat($(_this).val()) || 0;
    var amount = parseFloat($amount) || 0;
   
    var total = parseFloat(entry_fee) + parseFloat(amount);
    if(parseFloat(total) > parseFloat(0)) {
        $('#payment-btn').removeAttr('disabled', 'disabled');
    } else {
        $('#payment-btn').attr('disabled', 'disabled');
    }
    if($('[name="hidden_id"]').val() == '' && parseFloat(total) > parseFloat(0)) {
       $('input[name="amount"]').val((parseFloat(total)).toFixed(2));
    }
   
}


