function load_subgl(id,se){
	var datastr="parent_gl="+id+"&serial="+se;
	//alert(se);
	$(".sub_gl").load("module/finance/get_sub_gl_head.php",datastr);
}

// ===============================voucher Info==============================

$('#btn_save').click(function(){
    //=== Start Voucher Entry ===
    var voucher_date = $.trim($('#voucher_date').val());
	var voucher_type = $.trim($('#voucher_type').val());
	var voucher_number = $.trim($('#voucher_number').val());
    var particular = $.trim($('#particular').val());
    var amount_text = $.trim($('#totall_db').text());
    if(voucher_date==""){
			alert('Enter Voucher Date');
			$('#voucher_date').focus();
			return false;
		}

	else if(voucher_number==""){
	   		alert('Enter Voucher Number');
			$('#voucher_number').focus();
			return false;
	}	
	
    var result_amnt = amount_text.split('=');
	    amount=result_amnt[1];
    if(amount!=0){
    var opr="voucher_entry";
    var voucher_data="voucher_date="+voucher_date+"&voucher_type="+voucher_type+'&voucher_number='+voucher_number+'&particular='+particular+"&opr="+opr+"&amount="+amount;
    //alert(amount);
    $.ajax({
		type:"post" ,
	  	url:"module/finance/voucher_entry_debit_single_ajax.php",
	  	data:voucher_data,success:function(v_pid){
	  		//alert(v_pid);

		  	if(v_pid=='dup'){
		  			alert("Voucher Number Already Exists");	
			}
			else if(v_pid=='low'){
		  			alert("Insufficiant Balance");	
			}	
			else{
				var gl_id=[];		
				var glid_div = $("#input_fields_wrap").find('tbody td input#gl_id');
				$(glid_div).each(function(i){
					gl_id[i] = $(this).val();
				});
				var gl_ln = (gl_id.length);

				var debit=[];		
				var debit_div = $("#input_fields_wrap").find('tbody td.dbt');
				$(debit_div).each(function(i){
					debit[i] = $(this).text();
					
				});

				var credit=[];		
				var credit_div = $("#input_fields_wrap").find('tbody td.cbt');
				$(credit_div).each(function(i){
					credit[i] = $(this).text();
				}); 

			    var counter=[];		
				var counter_div = $("#input_fields_wrap").find('tbody td input.yval');
				$(counter_div).each(function(i){
					counter[i] = $(this).val(); 
				});

				var opr="voucher_gl_entry";



				for(var j=0;j<gl_ln;j++){

					var gl_data = "v_pid="+v_pid+"&debit="+debit[j]+"&credit="+credit[j]+"&gl_id="+gl_id[j]+"&voucher_number="+voucher_number+"&opr="+opr+"&sl="+counter[j];
					//alert(gl_data);
			
					$.ajax({
						type:"post" ,
					  	url:"module/finance/voucher_entry_debit_single_ajax.php",
					  	data:gl_data ,
					  	success:function(vcgl_id_arr){
							
							var result = vcgl_id_arr.split('-');
							    vcgl_id=result[0];
							    counter=result[1];
							    trxn_type=result[2];
							    //alert(vcgl_id);

							    if(vcgl_id_arr!='Voucher Number Already Existed'){
							   
								// ===== Start Sub GL Info ======
								var vgl = vcgl_id;		
										
								    var sub_gl_id=[];		
								   	var sub_gl_div = $('#input_subgl_'+counter+'').find('tbody td input#sgl_id');
								   
								   	$(sub_gl_div).each(function(j){

								   		sub_gl_id[j] = $(this).val();
								   	});

								   	var sub_gl_amount=[];		
								   	var amount_div = $('#input_subgl_'+counter+'').find('tbody td.amount');
								   
								   	$(amount_div).each(function(j){

								   		sub_gl_amount[j] = $(this).text();
								   	});

	                                var sub_gl_ln = (sub_gl_id.length);

								   	var opr="voucher_sub_gl_entry";
								   	for(var i=0;i<sub_gl_ln;i++){
						
								   	sub_gl_data="sub_gl_id="+sub_gl_id[i]+"&vcgl_id="+vcgl_id+"&sub_gl_amount="+sub_gl_amount[i]+"&voucher_number="+voucher_number+"&opr="+opr+"&v_pid="+v_pid+"&trxn_type="+trxn_type;
			
								   	$.ajax({
										type:"post" ,
									  	url:"module/finance/voucher_entry_debit_single_ajax.php",
									  	data:sub_gl_data ,
									  	success:function(st){
											alert(st);
											load();
									  	}
									});
								   }
							}

								// ===== End Sub GL Info ======
				  	    }
					});
		    	}
		        alert("successfull");
		    }	
	    }
	});
    //=== End Voucher Entry ===
}
else{
	alert("Enter GL");
}
});
    

function get_balance(type){
	//alert(type);
	var data="type="+type;
	$(".balance").load("module/finance/get_balance.php",data);
}
	






