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');
            /* if(edit_id == '') {
                $('input[name="amount"]').removeClass('readonly');
                $('input[name="amount"]').removeAttr('readonly');
                $('input[name="amount"]').addClass('amount num-pad');
            } */
        } else {
            $('#payment-btn').removeAttr('disabled');
            /* $('input[name="amount"]').addClass('readonly');
            $('input[name="amount"]').attr('readonly');
            $('input[name="amount"]').removeClass('amount num-pad'); */
        }
        $('[name="amount"], [name="pay_amount"]').val( data.amount );
    })
    .fail(function(jqXHR, status_code, error_thrown) {
        console.log(jqXHR.responseText);
    });
}

var inter = '';
$(document).on('ready', function() {
    $('.timelabel_res').parent().parent().css('width','50%');
    $('select[name="service_type"]').change(function(){
        setTotal(this);       
    });
    
    $('input[name="amount"]').keyup(function() {
        if(parseFloat($(this).val()) > 0) {
            $('#payment-btn').removeAttr('disabled');
        } else {
            $('#payment-btn').attr('disabled', 'disabled');
        }
        $('[name="pay_amount"]').val($(this).val());
    })
    
    $('input[name="fn_from_time"]').change(function(){
        var start_time = $(this).val();
        var hours = start_time.split(':')[0];
        var min = start_time.split(':')[1];
        var time = new Date(0,0,0,hours,min,0,0);
        var end_time = new Date(time.getTime() + (1*60*60*1000));
        var endTime = ((end_time.getHours() < 10) ? '0' + end_time.getHours() : end_time.getHours())  + ':' + ((end_time.getMinutes() < 10) ? '0' + end_time.getMinutes() : end_time.getMinutes());
        $('input[name="fn_to_time"]').val(endTime);
    });
    
    /* if($('input[name="service_type"]').val() != '') {
        setTotal($('input[name="service_type"]'));
    } */
    
    /* $('#sbmt-cancel-btn').on('click', function(){
        $('[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 payAmt = parseFloat( $('[name="pay_amount"]').val());
        var paidAmt = parseFloat( $('[name="paid_amount"]').val()) || 0;
        var edit_id =  $('input[name="hidden_id"]').val();
       
		$.ajax({
			url: base_url + 'billing_other_services/other_bill_amount',
			data: {amount: payAmt.toFixed(2)},
			dataType: 'JSON',
			type: 'post',
			'global': false,
			'async': false,
			success: function (output) {
				//tmp = output;
			}
		});
		
			   
	    //if(edit_id == '') {
        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');
            if( parseFloat(payAmt) != parseFloat(paidAmt)) { 
                $('#sbmt-btn').attr('disabled', 'disabled');
            } else {
                $('#sbmt-btn').removeAttr('disabled');
            }
            return true;
        }
        if( paidAmt <= 0 ) {
            $('[type="submit"]').attr('disabled', 'disabled');
        }
        /* $('#ptypes-dialog .pay-type:first, [name="paid_amount"]').val((parseFloat(payAmt)).toFixed(2));
        $('#sbmt-btn').removeAttr('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);
        }
        return false;
        /* } else {
            //msgBox('Amount is already paid!','','info');
           // window.setTimeout(function(){
                $('[type="submit"]').removeAttr('disabled').trigger('click');
            //}, 3500);
        } */      
    });
});


