$(document).ready(function()
{
    
    
});
/************************************ Voucher Master Start *********************************/
	var voucher_cnt = 1;
	function set_chq_field()
	{
		let account = $('#vm_cash_acc_id').val()
		$("#vm_cheque_no").val('');		
		$("#vm_cheque_date").val('');		
		if(account == 'BANK')
		{
			$("#vm_cheque_no").prop('readonly', false);
			$("#vm_cheque_date").prop('readonly', false);
		}
		else
		{
			$("#vm_cheque_no").prop('readonly', true);		
			$("#vm_cheque_date").prop('readonly', true);		
		}
	}
	function get_party_list()
	{
		$("#vm_payment_status").val(0);
		$("#payment_status").html('');
		$("#bal_purchase_details").html('');
		let grp_id = $("#vm_grp_id").val();
		if(grp_id > 0)
		{
			let path = base_url+"voucher/voucherctrl/get_party_list/"+grp_id;   
			ajaxCall('POST',path,'','JSON',function(resp) 
			{
			    let data = '';

				if(resp.length > 0)
				{   
					data += '<option value="0">SELECT</option>';
					$.each(resp, function(index, value){
						if(grp_id == 5)
						{						
							data += '<option value="'+value['client_id']+'">'+value['client_name'].toUpperCase()+' - '+value['client_mob1']+'</option>';
						}
						else if(grp_id == 23)
						{
							data += '<option value="'+value['km_id']+'">'+value['km_code'].toUpperCase()+' - '+value['km_mobile']+'</option>';
						}
					});
				}
				else
				{
					data += '<option value="0">NO PENDING AMOUNT AVAILABLE</option>';
				}

				$("#vm_party_id").html(data);
				$("#vm_party_id").flexselect();
				$("#vm_party_id").val(0);
				$("#vm_party_id_flexselect").val('SELECT');
			},
			function(errmsg) 
			{
			    console.log(errmsg);
			});	
		}
		else
		{
			$("#vm_party_id").html('<option value="0" >SELECT GROUP</option>');
			$("#vm_party_id").flexselect();
			$("#vm_party_id").val(0);
			$("#vm_party_id_flexselect").val('SELECT GROUP');			
		}		
	}
	function getUrlVars() 
	{
	    var vars = {};
	    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
	        vars[key] = value;
	    });
	    return vars;
	}
    function get_party_payment_status()
	{
		let party_id = $("#vm_party_id").val();
		if(party_id > 0)
		{
			let path = ''
			if($("#vm_grp_id").val() == 5)
			{
				path = base_url+"voucher/voucherctrl/get_client_payment_status/"+party_id;  
			}
			if($("#vm_grp_id").val() == 23)
			{
				path = base_url+"voucher/voucherctrl/get_karigar_payment_status/"+party_id;  
			}
			ajaxCall('POST',path,'','JSON',function(resp) 
			{
			    $("#vm_payment_status").val(resp['party_data'].length > 0 && Math.abs(resp['party_data'][0]['balance']));
			    if(getUrlVars()["type"] == 'PAYMENT')
			    {
			    	resp['hisab_data'] && generate_voucher_trans(resp['hisab_data'])					
			    	if($("#vm_grp_id").val() == 5)
					{
						$("#payment_status").html(resp['party_data'].length > 0 && resp['party_data'][0]['balance'] <= 0 ? 'Amt To Pay' : 'Amt To Receive' )
					}
					if($("#vm_grp_id").val() == 23)
					{
						$("#payment_status").html(resp['party_data'].length > 0 && resp['party_data'][0]['balance'] < 0 ? 'Amt To Receive' : 'Amt To Pay' )
					}
			    	
			    }

				if(getUrlVars()["type"] == 'RECEIPT')
			    {
			    	resp['order_data'] && generate_voucher_trans(resp['order_data'])	
			    	if($("#vm_grp_id").val() == 5)
					{
						$("#payment_status").html(resp['party_data'].length > 0 && resp['party_data'][0]['balance'] < 0 ? 'Amt To Pay' : 'Amt To Receive' )
					}
					if($("#vm_grp_id").val() == 23)
					{
						$("#payment_status").html(resp['party_data'].length > 0 && resp['party_data'][0]['balance'] <= 0 ? 'Amt To Receive' : 'Amt To Pay' )
					}
			    }
				
			},
			function(errmsg) 
			{
			    console.log(errmsg);
			});
			
		}
		else
		{
			$("#vm_payment_status").val(0);
			$("#payment_status").html('');
		}
	}
	
	function generate_voucher_trans(db_data)
	{
		var data = '';
		data += '<div class="panel panel-default">';
		data += '<div class="panel-heading">';
		data += '<h3 class="panel-title">Voucher Details</h3>';
		data += '</div>';
		data += '<div class="panel-body">';
		data += '<table class="table">';

		data += '<tr>';
		if($('#vm_grp_id').val() == 5)
		{
			data += '<th width="8%">Order No.</th>';
			data += '<th width="10%">Order Date</th>';			
			data += '<th width="8%">Total Qty</th>';
		}
		else if($('#vm_grp_id').val() == 23)
		{
			data += '<th width="8%">Hisab No.</th>';
			data += '<th width="10%">Hisab Date</th>';			
			data += '<th width="8%">Total Job</th>';	
		}
		
		data += '<th width="10%">Final Total</th>';
		data += '<th width="10%">Allocated Amt</th>';
		data += '<th >Allocated Round off Amt</th>';
		data += '<th width="10%">Adjust Amt</th>';
		data += '<th width="10%">Round off Amt</th>';
		data += '<th width="10%">Current Bal Amt</th>';
		data += '<th width="5%">NOD</th>';

		data += '</tr>';

		if(db_data.length > 0)
		{   
			$.each(db_data, function(index, value)
			{

				var current_bal = parseFloat(value['total_amt']) - parseFloat(value['allocated_amt']) - parseFloat(value['adjust_round_off']);
				
				var date1 = value['entry_date'];
				
				var x = new Date();
				var y = x.getFullYear().toString();
				var m = (x.getMonth() + 1).toString();
				var d = x.getDate().toString();
				(d.length == 1) && (d = '0' + d);
				(m.length == 1) && (m = '0' + m);
				var date2 = y + "-" + m + "-" + d;
				var nod = datedifference(date1,date2);
				
				data += '<tr id="bill_cnt_'+voucher_cnt+'">';

				//entry no & date
				var _id			= value['_id'];
				var entry_no	= value['entry_no'];
				var entry_date	= value['entry_date'].split('-').reverse().join('-');
				data += '<td>';
				data += '<input type="hidden" name="vt_id[]" id="vt_id_'+voucher_cnt+'" value="0"/>';
				data += '<input type="hidden" name="_id[]" id="_id_'+voucher_cnt+'" value="'+_id+'"/>';
				data += '<input type="text" class="form-control" name="entry_no[]" id="entry_no_'+voucher_cnt+'" value="'+entry_no+'" readonly/>';
				data += '</td>';
				data += '<td><input type="text" class="form-control" name="entry_date[]" id="entry_date_'+voucher_cnt+'" value="'+entry_date+'" readonly/></td>';

				// qty & amount
				var total_qty 	= value['total_qty'];
				var total_amt 	= value['total_amt'];
				data += '<td><input type="number" class="form-control" name="total_qty[]" id="total_qty_'+voucher_cnt+'" value="'+total_qty+'" readonly/></td>';
				data += '<td><input type="number" class="form-control" name="total_amt[]" id="total_amt_'+voucher_cnt+'" value="'+total_amt+'" readonly/></td>';

				data += '<td><input type="text" class="form-control" name="allocated_amt[]" id="allocated_amt_'+voucher_cnt+'" value="'+value['allocated_amt']+'" readonly/></td>';

				data += '<td><input type="text" class="form-control" name="adjust_round_off_old[]" id="adjust_round_off_old_'+voucher_cnt+'" value="'+value['adjust_round_off']+'" readonly/></td>';

				//adjustement amt
				data += '<td>';
				data += '<input type="number" class="form-control" name="adjust_amt[]" id="adjust_amt_'+voucher_cnt+'" value="0" oninput="this.value = Math.abs(this.value)"  />';
				data += '</td>';

				//round off amt
				data += '<td>';
				data += '<input type="number" class="form-control" name="adjust_round_off[]" id="adjust_round_off_'+voucher_cnt+'" value="0" oninput="this.value = Math.abs(this.value)" />';
				data += '</td>';

				//current balance
				data += '<td><input type="text" class="form-control" id="current_bal_amt_'+voucher_cnt+'" value="'+current_bal+'" readonly/></td>';

				//no of days
				data += '<td><input type="text" class="form-control" id="current_bal_amt_'+voucher_cnt+'" value="'+nod+'" readonly/></td>';
				
				data += '</tr>';

				voucher_cnt++;
				
			});

			

		}
		else
		{
			data += '<tr id="bill_cnt_0">';
			data += '<td colspan="10" style="color:red; text-align:center;" >NO PENDING AMOUNT AVAILABLE</td>';
			data += '</tr>';
		}

		data += '</table>';
		data += '</div>';
		data += '</div>';

		$("#bal_purchase_details").html(data);
	}

	function check_round_off_amt()
	{
		var vm_amount = parseFloat( ('0' + $("#vm_amount").val()).replace(/[^0-9-\.]/g, ''), 10 );
		var vm_round_off = parseFloat( ('0' + $("#vm_round_off").val()).replace(/[^0-9-\.]/g, ''), 10 );

		if(vm_round_off > vm_amount)
		{
			alert("Round Off amt should not be greater than amount");
			$("#vm_round_off").val(0);
		}

		if(vm_amount > 0)
		{
			$('#sbt_btn').prop('disabled', false)
			$('#sbt_btn').removeClass('btn-default').addClass('btn-success')
		}
		else
		{
			$('#sbt_btn').prop('disabled', true)
			$('#sbt_btn').removeClass('btn-success').addClass('btn-default')			
		}
	}
	function datedifference(date1,date2)
	{	

		// First we split the values to arrays date1[0] is the year, [1] the month and [2] the day
		date1 = date1.split('-');
		date2 = date2.split('-');

		// Now we convert the array to a Date object, which has several helpful methods
		date1 = new Date(date1[0], date1[1], date1[2]);
		date2 = new Date(date2[0], date2[1], date2[2]);

		// We use the getTime() method and get the unixtime (in milliseconds, but we want seconds, therefore we divide it through 1000)
		date1_unixtime = parseInt(date1.getTime() / 1000);
		date2_unixtime = parseInt(date2.getTime() / 1000);

		// This is the calculated difference in seconds
		var timeDifference = date2_unixtime - date1_unixtime;

		// in Hours
		var timeDifferenceInHours = timeDifference / 60 / 60;

		// and finaly, in days :)
		var timeDifferenceInDays = timeDifferenceInHours  / 24;

		return timeDifferenceInDays;
	}
	function add_payment_voucher(id)
	{
		var check = 1;

		if($("#vm_entry_no").val() == "" || $("#vm_entry_no").val() <= 0)
		{
			$("#vm_entry_no").css({'border-color':'red'});
            $("#vm_entry_no").addClass('shake');
            $('#vm_entry_no').on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
            {
                $('#vm_entry_no').delay(200).removeClass('shake');
            });

			check = 0;
		}
		else
		{
			$("#vm_entry_no").css({'border-color':'#550b06'});
		}

		if($("#vm_entry_date").val() == "" || $("#vm_entry_date").val() <= 0)
		{
			$("#vm_entry_date").css({'border-color':'red'});
            $("#vm_entry_date").addClass('shake');
            $('#vm_entry_date').on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
            {
                $('#vm_entry_date').delay(200).removeClass('shake');
            });

			check = 0;
		}
		else
		{
			$("#vm_entry_date").css({'border-color':'#550b06'});
		}

		if($("#vm_cash_acc_id").val() == "" || $("#vm_cash_acc_id").val() <= 0)
		{
			$("#vm_cash_acc_id").css({'border-color':'red'});
            $("#vm_cash_acc_id").addClass('shake');
            $('#vm_cash_acc_id').on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
            {
                $('#vm_cash_acc_id').delay(200).removeClass('shake');
            });

			check = 0;
		}
		else
		{
			$("#vm_cash_acc_id").css({'border-color':'#550b06'});
		}

		if($("#vm_grp_id").val() == "" || $("#vm_grp_id").val() <= 0)
		{
			$("#vm_grp_id").css({'border-color':'red'});
            $("#vm_grp_id").addClass('shake');
            $('#vm_grp_id').on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
            {
                $('#vm_grp_id').delay(200).removeClass('shake');
            });

			check = 0;
		}
		else
		{
			$("#vm_grp_id").css({'border-color':'#550b06'});
		}

		if($("#vm_party_id").val() == "" || $("#vm_party_id").val() <= 0)
		{
			$("#vm_party_id_flexselect").css({'border-color':'red'});
            $("#vm_party_id_flexselect").addClass('shake');
            $('#vm_party_id_flexselect').on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
            {
                $('#vm_party_id_flexselect').delay(200).removeClass('shake');
            });

			check = 0;
		}
		else
		{
			$("#vm_party_id_flexselect").css({'border-color':'#550b06'});
		}

		
		if($("#vm_amount").val() == "" || $("#vm_amount").val() <= 0)
		{
			$("#vm_amount").css({'border-color':'red'});
            $("#vm_amount").addClass('shake');
            $('#vm_amount').on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
            {
                $('#vm_amount').delay(200).removeClass('shake');
            });

			check = 0;
		}
		else
		{
			$("#vm_amount").css({'border-color':'#550b06'});
		}

		if($("#vm_prepared_id").val() == "" || $("#vm_prepared_id").val() <= 0)
		{
			$("#vm_prepared_id").css({'border-color':'red'});
            $("#vm_prepared_id").addClass('shake');
            $('#vm_prepared_id').on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
            {
                $('#vm_prepared_id').delay(200).removeClass('shake');
            });

			check = 0;
		}
		else
		{
			$("#vm_prepared_id").css({'border-color':'#550b06'});
		}

		if($("#vm_checked_id").val() == "" || $("#vm_checked_id").val() <= 0)
		{
			$("#vm_checked_id").css({'border-color':'red'});
            $("#vm_checked_id").addClass('shake');
            $('#vm_checked_id').on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
            {
                $('#vm_checked_id').delay(200).removeClass('shake');
            });

			check = 0;
		}
		else
		{
			$("#vm_checked_id").css({'border-color':'#550b06'});
		}


		for (var i = 1; i <= voucher_cnt - 1; i++) 
		{
			if($('#adjust_amt_'+i).val() < 0 || $('#adjust_amt_'+i).val() == '' || isNaN(($('#adjust_amt_'+i)).val()))
			{
				// $('#pm_adjust_amt_'+i).css({'border-color':'red'});
				// check = 0;
			}
			else
			{
				
				var adjust_amt = parseFloat($('#adjust_amt_'+i).val());
				if(isNaN(adjust_amt)) adjust_amt = 0;

				var adjust_round_off = parseFloat($('#adjust_round_off_'+i).val());
				if(isNaN(adjust_round_off)) adjust_round_off = 0;

				var total_adjust_amt = parseFloat(adjust_amt) + parseFloat(adjust_round_off);
				if(isNaN(total_adjust_amt)) total_adjust_amt = 0;

				var current_bal_amt = parseFloat($('#current_bal_amt_'+i).val());
				if(isNaN(current_bal_amt)) current_bal_amt = 0;

				if(total_adjust_amt > current_bal_amt)
				{
					$('#adjust_amt_'+i).css({'border-color':'red'});
		            $('#adjust_amt_'+i).addClass('shake');
		            $('#adjust_amt_'+i).on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
		            {
		                $('#adjust_amt_'+i).delay(200).removeClass('shake');
		            });

		            $('#adjust_round_off_'+i).css({'border-color':'red'});
		            $('#adjust_round_off_'+i).addClass('shake');
		            $('#adjust_round_off_'+i).on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
		            {
		                $('#adjust_round_off_'+i).delay(200).removeClass('shake');
		            });
					check = 2;
				}
				else
				{
					$('#adjust_amt_'+i).css({'border-color':'#550b06'});
					$('#adjust_round_off_'+i).css({'border-color':'#550b06'});
				}
			}
		}

		if(check == 0)
		{
			var msg = 'Oh snap ! You forgot to enter some information';
			display_alert('err',msg);

			$("body, html").animate({'scrollTop':0},1000);
		}
		else if(check == 2)
		{
			var msg = 'Adjust Amt + Round off amt are not greater than current balance';
			display_alert('err',msg);

			$("body, html").animate({'scrollTop':0},1000);
		}
		else
		{     
			
			var path = base_url+"voucher/voucherctrl/payment_voucher_insert_update/"+id;   
			var form_data = $("#voucher_form").serialize();

			ajaxCall('POST',path,form_data,'JSON',function(resp) 
			{
				if(resp['resp'] == 1)
				{
					if(id == 0)
					{
						var msg = 'Successfully added!';
						display_alert('succ',msg);
						$("html, body").animate({'scrollTop':0},1000);
						setTimeout(function(){window.location.reload(); },1000);
					}
					else
					{
						var msg = 'Successfully updated!';
						display_alert('succ',msg);  
						$("html, body").animate({'scrollTop':0},1000);
						setTimeout(function(){window.location.reload(); },1000);
					}
				}
				else
				{
					var msg = 'Unknown Error ! Please try again letter';
					display_alert('warn',msg);

					$("html, body").animate({'scrollTop':0},1000);
				}
				$("#loading").hide();                
			},
			function(errmsg) 
			{
			    console.log(errmsg);
			});
		}
	}
	function delete_voucher_item(path){
		let ans = confirm("Are you sure? You want to delete item.");
	    if(ans == true){
	        ajaxCall('GET',path,'','JSON',resp =>{
	            const {status, flag, msg} = resp;
	            if(status){
	                if(flag == 1){
	                    $("#table_reload").load(window.location + " #table_tbody");
	                    $("#count_reload").load(window.location + " #total_rows");
	                    callToastify('success', msg, 'right')
	                }else{
	                    response_error(flag, msg)
	                }
	            }else{
	                session_expired();
	            }
	        },errmsg=>{});
	    }
	}
	function delete_voucher_item1(id, path)
    {
        var ans = confirm("Are you sure? You want to delete item.");

        if(ans == true)
        {
            var update_path = base_url+"voucher/voucherctrl/"+path+"/"+id;
            
             
            ajaxCall('GET',update_path,'','JSON',function(resp)
            {   
                // console.log(resp);
                if(resp == 2)
                {
                    alert("You can not delete this item!");
                    $("#loading").hide();
                }
                else
                {   
                    $(".table").load(window.location + " .table");
                    $("#total_rows").load(window.location + " #total_rows");
                    // window.location.reload();
                }    
                $("#loading").hide();            
            },
            function(errmsg)
            {
                console.log(errmsg);
            });
        }
    } 
    function calculate_voucher_amt(cnt)
    {
    	// alert(cnt);
    	
    	vm_amount = parseFloat( ('0' + $("#vm_amount").val()).replace(/[^0-9-\.]/g, ''), 10 )
    	if (isNaN(vm_amount)) vm_amount = 0;



    	// $("#vm_round_off").val(0);
    	var pm_final_amt = parseFloat( ('0' + $("#pm_final_amt_"+cnt).val()).replace(/[^0-9-\.]/g, ''), 10 ) - parseFloat( ('0' + $("#pm_allocated_amt_"+cnt).val()).replace(/[^0-9-\.]/g, ''), 10 );
    	if (isNaN(pm_final_amt)) pm_final_amt = 0;

    	var pm_adjust_amt = parseFloat( ('0' + $("#pm_adjust_amt_"+cnt).val()).replace(/[^0-9-\.]/g, ''), 10 );
    	if (isNaN(pm_adjust_amt)) pm_adjust_amt = 0;

    	var pm_adjust_round_off = parseFloat( ('0' + $("#pm_adjust_round_off_"+cnt).val()).replace(/[^0-9-\.]/g, ''), 10 );
    	if (isNaN(pm_adjust_round_off)) pm_adjust_round_off = 0;

    	if (pm_adjust_amt > pm_final_amt) 
    	{
    		alert('Enter amount should be less than final amount');
			$("#pm_adjust_amt_"+cnt).val(0);
			
			calculate_voucher_amt(cnt);	   
			
		}
		else if(parseFloat(pm_adjust_amt) + parseFloat(pm_adjust_round_off) > pm_final_amt)
		{
			alert('Round off + Adjust amt should be less than final amount');
			$("#pm_adjust_round_off_"+cnt).val(0);
			
			calculate_voucher_amt(cnt);	
			
		}
		else
		{
			var pm_current_bal_amt = parseFloat(pm_final_amt) - parseFloat(pm_adjust_amt) - parseFloat(pm_adjust_round_off); 
	    	if (isNaN(pm_current_bal_amt)) pm_current_bal_amt = 0;
	    	$("#pm_current_bal_amt_"+cnt).val(pm_current_bal_amt.toFixed(2));

	    	$("#vm_amount").val(pm_current_bal_amt.toFixed(2));

	    	var last_tr = $("#bal_purchase_details table tr:last").attr('id');
			var index = last_tr.lastIndexOf("_");
			var last_tr = last_tr.substring(index+1);

			var final_voucher_amt = 0;
			var final_round_off_amt = 0;


			for (var i = 1; i <= last_tr; i++) 
			{
				var final_amt = $("#pm_adjust_amt_"+i).val();
				if(isNaN(final_amt)) final_amt = 0;

				final_voucher_amt = parseFloat(final_voucher_amt) + parseFloat(final_amt);
				if (isNaN(final_voucher_amt)) final_voucher_amt = 0;
				$("#vm_amount").val(final_voucher_amt.toFixed(2));

				var round_off = $("#pm_adjust_round_off_"+i).val();
				if(isNaN(round_off)) round_off = 0;

				final_round_off_amt = parseFloat(final_round_off_amt) + parseFloat(round_off);
				if (isNaN(final_round_off_amt)) final_round_off_amt = 0;
				$("#vm_round_off").val(final_round_off_amt.toFixed(2));
			}
		}
    	
    }
    function set_voucher_trans_amt()
    {
    	$("#vm_round_off").val(0);
    	var last_tr = $("#bal_purchase_details table tr:last").attr('id');
		var index = last_tr.lastIndexOf("_");
		var last_tr = last_tr.substring(index+1);
		for (var i = 1; i <= last_tr; i++) 
		{
			$("#pm_adjust_amt_"+i).val(0);
			$("#pm_adjust_round_off_"+i).val(0);

			var pm_final_amt = parseFloat( ('0' + $("#pm_final_amt_"+i).val()).replace(/[^0-9-\.]/g, ''), 10 ) - parseFloat( ('0' + $("#pm_allocated_amt_"+i).val()).replace(/[^0-9-\.]/g, ''), 10 );
    		if (isNaN(pm_final_amt)) pm_final_amt = 0;	

    		var pm_adjust_amt = parseFloat( ('0' + $("#pm_adjust_amt_"+i).val()).replace(/[^0-9-\.]/g, ''), 10 );
	    	if (isNaN(pm_adjust_amt)) pm_adjust_amt = 0;

	    	var pm_adjust_round_off = parseFloat( ('0' + $("#pm_adjust_round_off_"+i).val()).replace(/[^0-9-\.]/g, ''), 10 );
	    	if (isNaN(pm_adjust_round_off)) pm_adjust_round_off = 0;	

	    	var pm_current_bal_amt = parseFloat(pm_final_amt) - parseFloat(pm_adjust_amt) - parseFloat(pm_adjust_round_off); 
	    	if (isNaN(pm_current_bal_amt)) pm_current_bal_amt = 0;
	    	$("#pm_current_bal_amt_"+i).val(pm_current_bal_amt.toFixed(2));	

		}
    }
    function update_payment_voucher(id)
	{
		var check = 1;

		var acc_id = $("#vm_cash_acc_id").val().split("-")[0];


		if($("#vm_entry_no").val() == "")
		{
			$("#vm_entry_no").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_entry_no").css({'border-color':'#CCC'});
		}

		if($("#vm_entry_date").val() == "")
		{
			$("#vm_entry_date").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_entry_date").css({'border-color':'#CCC'});
		}

		if($("#vm_voucher_type").val() == 0)
		{
			$("#vm_voucher_type").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_voucher_type").css({'border-color':'#CCC'});
		}

		if(acc_id == 0)
		{
			$("#vm_cash_acc_id").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_cash_acc_id").css({'border-color':'#CCC'});
		}

		if($("#vm_ledger_type").val() == 0)
		{
			$("#vm_ledger_type").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_ledger_type").css({'border-color':'#CCC'});
		}

		if($("#vm_party_id").val() == 0)
		{
			$("#vm_party_id").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_party_id").css({'border-color':'#CCC'});
		}

		if($('#vm_amount').val() < 0 || $('#vm_amount').val() == '' || isNaN(($('#vm_amount')).val()))
		{
			$("#vm_amount").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_amount").css({'border-color':'#CCC'});
		}



		for (var i = 1; i <= voucher_cnt - 1; i++) 
		{
			if($('#pm_adjust_amt_'+i).val() < 0 || $('#pm_adjust_amt_'+i).val() == '' || isNaN(($('#pm_adjust_amt_'+i)).val()))
			{
				// $('#pm_adjust_amt_'+i).css({'border-color':'red'});
				// check = 0;
			}
			else
			{
				
				var pm_adjust_amt = parseFloat($('#pm_adjust_amt_'+i).val());
				if(isNaN(pm_adjust_amt)) pm_adjust_amt = 0;

				var pm_adjust_round_off = parseFloat($('#pm_adjust_round_off_'+i).val());
				if(isNaN(pm_adjust_round_off)) pm_adjust_round_off = 0;

				var total_adjust_amt = parseFloat(pm_adjust_amt) + parseFloat(pm_adjust_round_off);
				if(isNaN(total_adjust_amt)) total_adjust_amt = 0;

				var vm_amount = parseFloat($('#vm_amount').val());
				if(isNaN(vm_amount)) vm_amount = 0;

				if(total_adjust_amt > vm_amount)
				{
					$('#pm_adjust_amt_'+i).css({'border-color':'red'});
					$('#pm_adjust_round_off_'+i).css({'border-color':'red'});
					check = 2;
				}
				else
				{
					$('#pm_adjust_amt_'+i).css({'border-color':'#CCC'});
					$('#pm_adjust_round_off_'+i).css({'border-color':'#CCC'});
				}
			}
		}

		if(check == 0)
		{
			var msg = 'Oh snap ! You forgot to enter some information';
			display_alert('err',msg);

			$("body, html").animate({'scrollTop':0},1000);
		}
		else if(check == 2)
		{
			var msg = 'Adjust Amt + Round off amt are not greater than voucher amt';
			display_alert('err',msg);

			$("body, html").animate({'scrollTop':0},1000);
		}
		else
		{     

			var check_validation_adjust = 1;
			var check_validation_adjust_round = 1;
			var total_adjust_amt = 0;
			var total_adjust_round_off_amt = 0;

			var vm_amount = $('#vm_amount').val();
			if(isNaN(vm_amount)) vm_amount = 0;

			var vm_round_off = $('#vm_round_off').val();
			if(isNaN(vm_round_off)) vm_round_off = 0;

			for (var i = 1; i <= voucher_cnt; i++) 
			{
				var pm_adjust_amt = parseFloat($('#pm_adjust_amt_'+i).val());
				if(isNaN(pm_adjust_amt)) pm_adjust_amt = 0;

				var pm_adjust_round_off = parseFloat($('#pm_adjust_round_off_'+i).val());
				if(isNaN(pm_adjust_round_off)) pm_adjust_round_off = 0;

				total_adjust_amt = parseFloat(total_adjust_amt) + parseFloat(pm_adjust_amt);

				total_adjust_round_off_amt = parseFloat(total_adjust_round_off_amt) + parseFloat(pm_adjust_round_off);
			}

			

			if((total_adjust_amt != 0))
			{
				console.log('total_adjust_amt ='+total_adjust_amt);
				console.log('voucher_cnt ='+voucher_cnt);
				console.log('vm_amount ='+vm_amount);

				if((vm_amount != total_adjust_amt))
				{
					check_validation_adjust = 2;
				}
				else
				{
					check_validation_adjust = 0;
				}
			}
			else
			{
				check_validation_adjust = 0;
			}


			if((total_adjust_round_off_amt != 0))
			{
				if((vm_round_off != total_adjust_round_off_amt))
				{
					check_validation_adjust_round = 3;
				}
				else
				{
					check_validation_adjust_round = 0;
				}
			}
			else
			{
				check_validation_adjust_round = 0;
			}



			if(check_validation_adjust == 2)
			{
				var msg = 'Voucher Amt and adjust amt are not matching';
				display_alert('err',msg);

				$("body, html").animate({'scrollTop':0},1000);
			}

			else if(check_validation_adjust_round == 3)
			{
				var msg = 'Voucher round off Amt and adjust round off amt are not matching';
				display_alert('err',msg);

				$("body, html").animate({'scrollTop':0},1000);
			}

			else if((check_validation_adjust == 0) && (check_validation_adjust_round == 0))
			{
				// alert(check_validation_adjust);
				var path = base_url+"voucher/voucherctrl/payment_voucher_edit_insert/"+id;   
				var form_data = $("#payment_edit_voucher_form").serialize();

				$("#loading").show();

				$("#sbt_btn").prop('disabled', true);

				$.ajax({
					type:'POST',
					url:path,
					data:form_data,
					success:function(resp)
					{
						console.log(resp);
						if(resp == 1)
						{
							if(id == 0)
							{
								var msg = 'Successfully added!';
								display_alert('succ',msg);
								$("html, body").animate({'scrollTop':0},1000);
								setTimeout(function(){window.location.reload(); },1000);
							}
							else
							{
								var msg = 'Successfully updated!';
								display_alert('succ',msg);  
								$("html, body").animate({'scrollTop':0},1000);
								setTimeout(function(){window.location.reload(); },1000);
							}
						}
						else
						{
							var msg = 'Unknown Error ! Please try again letter';
							display_alert('warn',msg);

							$("html, body").animate({'scrollTop':0},1000);
						}
						$("#loading").hide();
					},
					error:function(resp){
						console.log(resp);
					}   
				});
			}
			else
			{
				
				var msg = 'Unknown Error ! Please try again letter';
				display_alert('warn',msg);

				$("html, body").animate({'scrollTop':0},1000);
			}

		}
	}
	function get_party_receipt_status()
	{
		var party_id = $("#vm_party_id").val().split("-")[0];
		if(party_id > 0)
		{
			var path = base_url+"voucher/voucherctrl/get_party_payment_status/"+party_id;  

			$("#loading").show();
			$.ajax({
				type:'POST',
				url:path,
				dataType:'json',
				success:function(resp)
				{
					console.log(resp);
					$("#vm_payment_status").val(resp['status']);

					get_party_sale_details();
					$("#loading").hide();
				},
				error:function(resp){

					console.log(resp);
				}   
			});
		}
		else
		{
			$("#vm_payment_status").val(0);
		}
		
	}
	function get_party_sale_details()
	{

		var vm_grp_id = $("#vm_grp_id").val();
		var party_id = $("#vm_party_id").val().split("-")[0];

		var vm_payment_status = $("#vm_payment_status").val();

		if(vm_payment_status != "0 DR")
		{
			vm_payment_bal = "-1";
		}
		else
		{
			vm_payment_bal = vm_payment_status.indexOf("CR");
		}
		
	
		if((vm_grp_id == 5 || vm_grp_id == 6) && party_id > 0 && vm_payment_bal == "-1")
		{
			// alert(vm_grp_id);
			var path = base_url+"voucher/voucherctrl/get_party_sale_details/"+party_id;  

			$("#loading").show();
			$.ajax({
				type:'POST',
				url:path,
				dataType:'json',
				success:function(resp)
				{
					console.log(resp);

					var data = '';

					if(resp.length != 0)
					{   
						data += '<div class="panel panel-default">';
						data += '<div class="panel-heading">';
						data += '<h3 class="panel-title">Voucher Details</h3>';
						data += '</div>';
						data += '<div class="panel-body">';
						data += '<table class="table">';

						data += '<tr>';

						data += '<th width="6%">Bill No</th>';
						data += '<th width="10%">Bill Date</th>';

						data += '<th width="8%">Total Qty</th>';
						data += '<th width="10%">Final Total</th>';
						data += '<th width="10%">Allocated Amt</th>';
						data += '<th width="10%">Allocated Round off Amt</th>';
						data += '<th width="10%">Adjust Amt</th>';
						data += '<th width="10%">Round off Amt</th>';
						data += '<th width="10%">Current Bal Amt</th>';
						data += '<th width="10%">NOD</th>';

						data += '</tr>';

						
						$.each(resp, function(index, value)
						{
							var current_bal = parseFloat(value['im_final_amt']) - parseFloat(value['im_allocated_amt']) - parseFloat(value['im_adjust_round_off']);

							var date1 = (value['im_inv_date1'] == "1970-01-01") ? value['im_est_date1'] : value['im_inv_date1'];
							var bill_no = (value['im_inv_no'] == 0) ? value['im_est_no'] : value['im_inv_no'];
							var bill_date = (value['im_inv_date'] == "01-01-1970") ? value['im_est_date'] : value['im_inv_date'];
							
							var x = new Date();
							var y = x.getFullYear().toString();
							var m = (x.getMonth() + 1).toString();
							var d = x.getDate().toString();
							(d.length == 1) && (d = '0' + d);
							(m.length == 1) && (m = '0' + m);
							var date2 = y + "-" + m + "-" + d;
							//alert(yyyymmdd);

							var nod = datedifference(date1,date2);

							data += '<tr>';
							data += '<td>';

							data += '<input type="hidden" class="form-control" name="im_id[]" id="im_id_'+voucher_cnt+'" value="'+value['im_id']+'"/>';

							data += '<input type="text" class="form-control" name="im_bill_no[]" id="im_bill_no_'+voucher_cnt+'" value="'+bill_no+'" readonly/></td>';

							data += '<td><input type="text" class="form-control" name="im_bill_date[]" id="im_bill_date_'+voucher_cnt+'" value="'+bill_date+'" readonly/></td>';

							data += '<td><input type="text" class="form-control" name="im_total_qty[]" id="im_total_qty_'+voucher_cnt+'" value="'+value['im_total_qty']+'" readonly/></td>';
							data += '<td><input type="text" class="form-control" name="im_final_amt[]" id="im_final_amt_'+voucher_cnt+'" value="'+value['im_final_amt']+'" readonly/></td>';

							data += '<td><input type="text" class="form-control" name="im_allocated_amt[]" id="im_allocated_amt_'+voucher_cnt+'" value="'+value['im_allocated_amt']+'" readonly/></td>';

							data += '<td><input type="text" class="form-control" name="im_adjust_round_off_old[]" id="im_adjust_round_off_old_'+voucher_cnt+'" value="'+value['im_adjust_round_off']+'" readonly/></td>';

							data += '<td>';
							data += '<input type="text" class="form-control" name="im_adjust_amt[]" id="im_adjust_amt_'+voucher_cnt+'" value=""/>';
							data += '</td>';

							data += '<td><input type="text" class="form-control" name="im_adjust_round_off[]" id="im_adjust_round_off_'+voucher_cnt+'" value=""/></td>';

							data += '<td><input type="text" class="form-control" id="im_current_bal_amt_'+voucher_cnt+'" value="'+current_bal+'" readonly/></td>';
							data += '<td><input type="text" class="form-control" id="im_current_bal_amt_'+voucher_cnt+'" value="'+nod+'" readonly/></td>';

							data += '</tr>';

							voucher_cnt++;
							
						});

						data += '</table>';
						data += '</div>';
						data += '</div>';

					}
					else
					{
						// data += '<option value="0">No data</option>';
					}

					$("#bal_purchase_details").html(data);
					$("#loading").hide();
				},
				error:function(resp){

					console.log(resp);
				}   
			});
		}

	}	


	function add_receipt_voucher(id)
	{
		var check = 1;

		var acc_id = $("#vm_cash_acc_id").val().split("-")[0];


		if($("#vm_entry_no").val() == "")
		{
			$("#vm_entry_no").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_entry_no").css({'border-color':'#CCC'});
		}

		if($("#vm_entry_date").val() == "")
		{
			$("#vm_entry_date").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_entry_date").css({'border-color':'#CCC'});
		}

		if($("#vm_voucher_type").val() == 0)
		{
			$("#vm_voucher_type").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_voucher_type").css({'border-color':'#CCC'});
		}

		if(acc_id == 0)
		{
			$("#vm_cash_acc_id").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_cash_acc_id").css({'border-color':'#CCC'});
		}

		if($("#vm_grp_id").val() == 0)
		{
			$("#vm_grp_id").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_grp_id").css({'border-color':'#CCC'});
		}

		if($("#vm_party_id").val() == 0)
		{
			$("#vm_party_id").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_party_id").css({'border-color':'#CCC'});
		}

		if($('#vm_amount').val() <= 0 || $('#vm_amount').val() == '' || isNaN(($('#vm_amount')).val()))
		{
			$("#vm_amount").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_amount").css({'border-color':'#CCC'});
		}



		for (var i = 1; i <= voucher_cnt - 1; i++) 
		{
			if($('#im_adjust_amt_'+i).val() < 0 || $('#im_adjust_amt_'+i).val() == '' || isNaN(($('#im_adjust_amt_'+i)).val()))
			{
				// $('#im_adjust_amt_'+i).css({'border-color':'red'});
				// check = 0;
			}
			else
			{
				
				var im_adjust_amt = parseFloat($('#im_adjust_amt_'+i).val());
				if(isNaN(im_adjust_amt)) im_adjust_amt = 0;

				var im_adjust_round_off = parseFloat($('#im_adjust_round_off_'+i).val());
				if(isNaN(im_adjust_round_off)) im_adjust_round_off = 0;

				var total_adjust_amt = parseFloat(im_adjust_amt) + parseFloat(im_adjust_round_off);
				if(isNaN(total_adjust_amt)) total_adjust_amt = 0;

				var im_current_bal_amt = parseFloat($('#im_current_bal_amt_'+i).val());
				if(isNaN(im_current_bal_amt)) im_current_bal_amt = 0;

				if(total_adjust_amt > im_current_bal_amt)
				{
					$('#im_adjust_amt_'+i).css({'border-color':'red'});
					$('#im_adjust_round_off_'+i).css({'border-color':'red'});
					check = 2;
				}
				else
				{
					$('#im_adjust_amt_'+i).css({'border-color':'#CCC'});
					$('#im_adjust_round_off_'+i).css({'border-color':'#CCC'});
				}
			}
		}

		if(check == 0)
		{
			var msg = 'Oh snap ! You forgot to enter some information';
			display_alert('err',msg);

			$("body, html").animate({'scrollTop':0},1000);
		}
		else if(check == 2)
		{
			var msg = 'Adjust Amt + Round off amt are not greater than current balance';
			display_alert('err',msg);

			$("body, html").animate({'scrollTop':0},1000);
		}
		else
		{     
			var check_validation_adjust = 1;
			var check_validation_adjust_round = 1;
			var total_adjust_amt = 0;
			var total_adjust_round_off_amt = 0;

			var vm_amount = $('#vm_amount').val();
			if(isNaN(vm_amount)) vm_amount = 0;

			var vm_round_off = $('#vm_round_off').val();
			if(isNaN(vm_round_off)) vm_round_off = 0;

			for (var i = 1; i <= voucher_cnt; i++) 
			{
				var im_adjust_amt = parseFloat($('#im_adjust_amt_'+i).val());
				if(isNaN(im_adjust_amt)) im_adjust_amt = 0;

				var im_adjust_round_off = parseFloat($('#im_adjust_round_off_'+i).val());
				if(isNaN(im_adjust_round_off)) im_adjust_round_off = 0;

				total_adjust_amt = parseFloat(total_adjust_amt) + parseFloat(im_adjust_amt);

				total_adjust_round_off_amt = parseFloat(total_adjust_round_off_amt) + parseFloat(im_adjust_round_off);
			}

			
			if((total_adjust_amt != 0))
			{
				if((vm_amount != total_adjust_amt))
				{
					check_validation_adjust = 2;
				}
				else
				{
					check_validation_adjust = 0;
				}
			}
			else
			{
				check_validation_adjust = 0;
			}


			if((total_adjust_round_off_amt != 0))
			{
				if((vm_round_off != total_adjust_round_off_amt))
				{
					check_validation_adjust_round = 3;
				}
				else
				{
					check_validation_adjust_round = 0;
				}
			}
			else
			{
				check_validation_adjust_round = 0;
			}


			if(check_validation_adjust == 2)
			{
				var msg = 'Voucher Amt and adjust amt are not matching';
				display_alert('err',msg);

				$("body, html").animate({'scrollTop':0},1000);
			}

			else if(check_validation_adjust_round == 3)
			{
				var msg = 'Voucher round off Amt and adjust round off amt are not matching';
				display_alert('err',msg);

				$("body, html").animate({'scrollTop':0},1000);
			}
			else if((check_validation_adjust == 0) && (check_validation_adjust_round == 0))
			{
				var path = base_url+"voucher/voucherctrl/receipt_voucher_insert/"+id;   
				var form_data = $("#receipt_voucher_form").serialize();

				$("#loading").show();

				$("#sbt_btn").prop('disabled', true);

				$.ajax({
					type:'POST',
					url:path,
					data:form_data,
					success:function(resp)
					{
						console.log(resp);
						if(resp == 1)
						{
							if(id == 0)
							{
								var msg = 'Successfully added!';
								display_alert('succ',msg);
								$("html, body").animate({'scrollTop':0},1000);
								setTimeout(function(){window.location.reload(); },1000);
							}
							else
							{
								var msg = 'Successfully updated!';
								display_alert('succ',msg);  
								$("html, body").animate({'scrollTop':0},1000);
								setTimeout(function(){window.location.reload(); },1000);
							}
						}
						else
						{
							var msg = 'Unknown Error ! Please try again letter';
							display_alert('warn',msg);

							$("html, body").animate({'scrollTop':0},1000);
						}
						$("#loading").hide();
					},
					error:function(resp){
						console.log(resp);
					}   
				});
			}
			else
			{
				
				var msg = 'Unknown Error ! Please try again letter';
				display_alert('warn',msg);

				$("html, body").animate({'scrollTop':0},1000);
			}

		}
	}


	function get_party_purchase_details_for_receipt_voucher_update()
	{
		var vm_grp_id = $("#vm_grp_id").val();
		var party_id = $("#vm_party_id").val().split("-")[0];
		

		if((vm_grp_id == 5 || vm_grp_id == 6) && party_id > 0)
		{
			 //alert(111);
			var path = base_url+"voucher/voucherctrl/get_party_sale_details/"+party_id;  

			$("#loading").show();
			$.ajax({
				type:'POST',
				url:path,
				dataType:'json',
				success:function(resp)
				{
					console.log(resp);

					var data = '';

					if(resp.length != 0)
					{   
						data += '<div class="panel panel-default">';
						data += '<div class="panel-heading">';
						data += '<h3 class="panel-title">Voucher Details</h3>';
						data += '</div>';
						data += '<div class="panel-body">';
						data += '<table class="table">';

						data += '<tr>';

						data += '<th width="6%">Bill No</th>';
						data += '<th width="10%">Bill Date</th>';

						data += '<th width="8%">Total Qty</th>';
						data += '<th width="10%">Final Total</th>';
						data += '<th width="10%">Allocated Amt</th>';
						data += '<th width="10%">Allocated Round off Amt</th>';
						data += '<th width="10%">Adjust Amt</th>';
						data += '<th width="10%">Round off Amt</th>';
						data += '<th width="10%">Current Bal Amt</th>';
						data += '<th width="10%">NOD</th>';

						data += '</tr>';

						
						$.each(resp, function(index, value)
						{
							var current_bal = parseFloat(value['im_final_amt']) - parseFloat(value['im_allocated_amt']) - parseFloat(value['im_adjust_round_off']);

							var date1 = (value['im_inv_date1'] == "1970-01-01") ? value['im_est_date1'] : value['im_inv_date1'];
							var bill_no = (value['im_inv_no'] == 0) ? value['im_est_no'] : value['im_inv_no'];
							var bill_date = (value['im_inv_date'] == "01-01-1970") ? value['im_est_date'] : value['im_inv_date'];

							
							var x = new Date();
							var y = x.getFullYear().toString();
							var m = (x.getMonth() + 1).toString();
							var d = x.getDate().toString();
							(d.length == 1) && (d = '0' + d);
							(m.length == 1) && (m = '0' + m);
							var date2 = y + "-" + m + "-" + d;
							//alert(yyyymmdd);

							var nod = datedifference(date1,date2);

							data += '<tr>';
							
							data += '<td>';
							data += '<input type="hidden" class="form-control" name="im_id[]" id="im_id_'+voucher_cnt+'" value="'+value['im_id']+'"/>';
							data += '<input type="text" class="form-control" name="im_bill_no[]" id="im_bill_no_'+voucher_cnt+'" value="'+bill_no+'" readonly/></td>';
							data += '<td><input type="text" class="form-control" name="im_bill_date[]" id="im_bill_date_'+voucher_cnt+'" value="'+bill_date+'" readonly/></td>';

							data += '<td><input type="text" class="form-control" name="im_total_qty[]" id="im_total_qty_'+voucher_cnt+'" value="'+value['im_total_qty']+'" readonly/></td>';
							data += '<td><input type="text" class="form-control" name="im_final_total[]" id="im_final_total_'+voucher_cnt+'" value="'+value['im_final_amt']+'" readonly/></td>';

							data += '<td><input type="text" class="form-control" name="im_allocated_amt[]" id="im_allocated_amt_'+voucher_cnt+'" value="'+value['im_allocated_amt']+'" readonly/></td>';

							data += '<td><input type="text" class="form-control" name="im_adjust_round_off_old[]" id="im_adjust_round_off_old_'+voucher_cnt+'" value="'+value['im_adjust_round_off']+'" readonly/></td>';

							data += '<td>';
							data += '<input type="text" class="form-control" name="im_adjust_amt[]" id="im_adjust_amt_'+voucher_cnt+'" value=""/>';
							data += '</td>';

							data += '<td><input type="text" class="form-control" name="im_adjust_round_off[]" id="im_adjust_round_off_'+voucher_cnt+'" value=""/></td>';

							data += '<td><input type="text" class="form-control" id="im_current_bal_amt_'+voucher_cnt+'" value="'+current_bal+'" readonly/></td>';
							data += '<td><input type="text" class="form-control" id="im_current_bal_amt_'+voucher_cnt+'" value="'+nod+'" readonly/></td>';

							data += '</tr>';

							voucher_cnt++;
							
						});

						data += '</table>';
						data += '</div>';
						data += '</div>';

						$("#update_button").show();

					}
					else
					{
						// data += '<option value="0">No data</option>';
					}

					$("#bal_sale_details").html(data);
					$("#loading").hide();
				},
				error:function(resp){

					console.log(resp);
				}   
			});
		}


	}

	function update_receipt_voucher(id)
	{
		var check = 1;

		var acc_id = $("#vm_cash_acc_id").val().split("-")[0];


		if($("#vm_entry_no").val() == "")
		{
			$("#vm_entry_no").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_entry_no").css({'border-color':'#CCC'});
		}

		if($("#vm_entry_date").val() == "")
		{
			$("#vm_entry_date").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_entry_date").css({'border-color':'#CCC'});
		}

		if($("#vm_voucher_type").val() == 0)
		{
			$("#vm_voucher_type").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_voucher_type").css({'border-color':'#CCC'});
		}

		if(acc_id == 0)
		{
			$("#vm_cash_acc_id").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_cash_acc_id").css({'border-color':'#CCC'});
		}

		if($("#vm_ledger_type").val() == 0)
		{
			$("#vm_ledger_type").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_ledger_type").css({'border-color':'#CCC'});
		}

		if($("#vm_party_id").val() == 0)
		{
			$("#vm_party_id").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_party_id").css({'border-color':'#CCC'});
		}

		if($('#vm_amount').val() <= 0 || $('#vm_amount').val() == '' || isNaN(($('#vm_amount')).val()))
		{
			$("#vm_amount").css({'border-color':'red'});
			check = 0;
		}
		else
		{
			$("#vm_amount").css({'border-color':'#CCC'});
		}



		for (var i = 1; i <= voucher_cnt - 1; i++) 
		{
			if($('#im_adjust_amt_'+i).val() < 0 || $('#im_adjust_amt_'+i).val() == '' || isNaN(($('#im_adjust_amt_'+i)).val()))
			{
				// $('#im_adjust_amt_'+i).css({'border-color':'red'});
				// check = 0;
			}
			else
			{
				
				var im_adjust_amt = parseFloat($('#im_adjust_amt_'+i).val());
				if(isNaN(im_adjust_amt)) im_adjust_amt = 0;

				var im_adjust_round_off = parseFloat($('#im_adjust_round_off_'+i).val());
				if(isNaN(im_adjust_round_off)) im_adjust_round_off = 0;

				var total_adjust_amt = parseFloat(im_adjust_amt) + parseFloat(im_adjust_round_off);
				if(isNaN(total_adjust_amt)) total_adjust_amt = 0;

				var vm_amount = parseFloat($('#vm_amount').val());
				if(isNaN(vm_amount)) vm_amount = 0;

				if(total_adjust_amt > vm_amount)
				{
					$('#im_adjust_amt_'+i).css({'border-color':'red'});
					$('#im_adjust_round_off_'+i).css({'border-color':'red'});
					check = 2;
				}
				else
				{
					$('#im_adjust_amt_'+i).css({'border-color':'#CCC'});
					$('#im_adjust_round_off_'+i).css({'border-color':'#CCC'});
				}
			}
		}

		if(check == 0)
		{
			var msg = 'Oh snap ! You forgot to enter some information';
			display_alert('err',msg);

			$("body, html").animate({'scrollTop':0},1000);
		}
		else if(check == 2)
		{
			var msg = 'Adjust Amt + Round off amt are not greater than voucher amt';
			display_alert('err',msg);

			$("body, html").animate({'scrollTop':0},1000);
		}
		else
		{     

			var check_validation_adjust = 1;
			var check_validation_adjust_round = 1;
			var total_adjust_amt = 0;
			var total_adjust_round_off_amt = 0;

			var vm_amount = $('#vm_amount').val();
			if(isNaN(vm_amount)) vm_amount = 0;

			var vm_round_off = $('#vm_round_off').val();
			if(isNaN(vm_round_off)) vm_round_off = 0;

			for (var i = 1; i <= voucher_cnt; i++) 
			{
				var im_adjust_amt = parseFloat($('#im_adjust_amt_'+i).val());
				if(isNaN(im_adjust_amt)) im_adjust_amt = 0;

				var im_adjust_round_off = parseFloat($('#im_adjust_round_off_'+i).val());
				if(isNaN(im_adjust_round_off)) im_adjust_round_off = 0;

				total_adjust_amt = parseFloat(total_adjust_amt) + parseFloat(im_adjust_amt);

				total_adjust_round_off_amt = parseFloat(total_adjust_round_off_amt) + parseFloat(im_adjust_round_off);
			}

			

			if((total_adjust_amt != 0))
			{
				if((vm_amount != total_adjust_amt))
				{
					check_validation_adjust = 2;
				}
				else
				{
					check_validation_adjust = 0;
				}
			}
			else
			{
				check_validation_adjust = 0;
			}


			if((total_adjust_round_off_amt != 0))
			{
				if((vm_round_off != total_adjust_round_off_amt))
				{
					check_validation_adjust_round = 3;
				}
				else
				{
					check_validation_adjust_round = 0;
				}
			}
			else
			{
				check_validation_adjust_round = 0;
			}



			if(check_validation_adjust == 2)
			{
				var msg = 'Voucher Amt and adjust amt are not matching';
				display_alert('err',msg);

				$("body, html").animate({'scrollTop':0},1000);
			}

			else if(check_validation_adjust_round == 3)
			{
				var msg = 'Voucher round off Amt and adjust round off amt are not matching';
				display_alert('err',msg);

				$("body, html").animate({'scrollTop':0},1000);
			}

			else if((check_validation_adjust == 0) && (check_validation_adjust_round == 0))
			{
				// alert(check_validation_adjust);
				var path = base_url+"voucher/voucherctrl/receipt_voucher_edit_insert/"+id;   
				var form_data = $("#receipt_edit_voucher_form").serialize();

				$("#loading").show();

				$("#sbt_btn").prop('disabled', true);

				$.ajax({
					type:'POST',
					url:path,
					data:form_data,
					success:function(resp)
					{
						console.log(resp);
						if(resp == 1)
						{
							if(id == 0)
							{
								var msg = 'Successfully added!';
								display_alert('succ',msg);
								$("html, body").animate({'scrollTop':0},1000);
								setTimeout(function(){window.location.reload(); },1000);
							}
							else
							{
								var msg = 'Successfully updated!';
								display_alert('succ',msg);  
								$("html, body").animate({'scrollTop':0},1000);
								setTimeout(function(){window.location.reload(); },1000);
							}
						}
						else
						{
							var msg = 'Unknown Error ! Please try again letter';
							display_alert('warn',msg);

							$("html, body").animate({'scrollTop':0},1000);
						}
						$("#loading").hide();
					},
					error:function(resp){
						console.log(resp);
					}   
				});
			}
			else
			{
				
				var msg = 'Unknown Error ! Please try again letter';
				display_alert('warn',msg);

				$("html, body").animate({'scrollTop':0},1000);
			}

		}
	}	

	
/************************************ Voucher Master End ***********************************/