$(document).keypress(function(event) {
    var keycode = (event.keyCode ? event.keyCode : event.which);

    if(keycode == '13') {
		load_std_invoice_list();
    	//var id_n0 = $("#id_n0").val();
    	//var uniq_id = $("#uniq_id").val();
    	//var datastr = "uniq_id="+uniq_id;
    	//$("#call_std_invoice").load("module/fees/std_invoice_list.php",datastr);
    }
});

$("#search").click(function(){
		$("#origin_page").val(1);
		$('#id_n0').css("display","none");
		$("#call_std_invoice").show();
	$('#call_std_invoice').html("<span style='color:#2e8965;font-size:16px;font-weight:bold;'>Data Generating.... Please Wait.  <img src='php_crud/img/preloader.gif' /></span>");
    	load_std_invoice_list();
    });
// =============================================================

function load_std_invoice_list(){
	var mod_id = $("#mod_id").val();
    var uniq_id = $("#uniq_id").val();
    var fee_year = $("#session").val();
    var datastr = "uniq_id="+uniq_id+"&mod_id="+mod_id+"&fee_year="+fee_year;
	$("#call_std_invoice").load("module/fees/std_invoice_list.php",datastr);
	toggle_back_btn();
}
$("#submit12").click(function(){
	 
	 
	var id = $("#id").val();
	var inv = $("#inv").val();
	var payable_tk = $("#payable_tk").val();
	var receive_amount = $("#receive_amount").val();
    var datastr = "id="+id+"&invoice="+inv+"&payable_tk="+payable_tk+"&receive_amount="+receive_amount;
   alert(datastr);

   /*if(receive_amount==""){
		alert("Please Enter Receive Amount");
		$('#receive_amount').focus();
		return false;
	}
	else if(parseFloat(receive_amount)>parseFloat(payable_tk)){
		alert("Please Enter Receive Amount Less Then or Equal Payable Amount");
		$('#receive_amount').focus();
		return false;
	}
	else{
	    $.ajax({
	        type:"get",
	        url:"module/fees/partialy_fee_rec_conf.php",
	        data:datastr ,
	        success:function(result){
	          //alert(result);
	            load();
				 //$("#main_content_div").load("module/fees/fee_collection.php");
	        }
	    });
    }*/
});
/* function load(){
	var std_id = $("#std_id").val();
	alert(std_id);
	var month = $("#_month").val();
	alert(month);
	var year = $("#_year").val();
	alert(year);
	var section_id = $("#_section_id").val();
	alert(section_id);
    $("#main_content_div").load("module/fees/student_invoice_pdf.php?stdid=std_id&month=month&year=year&section_id=section_id");
} */


function coll_id_inv_copy(inv_id,copy_type){
	//alert('hello');
	var datastr = "invoice_id="+inv_id+"&copy_type="+copy_type;
	//alert(datastr);
	window.open("module/fees/student_invoice_single_copy_pdf.php?"+datastr);
}
function coll_id_all_inv_copy(inv_id){
	//alert('hello');
	var datastr = "invoice_id="+inv_id;
	//alert(datastr);
	window.open("module/fees/student_invoice_all_copies_pdf.php?"+datastr);
}

function print_inv_by_batch(uniq_id,batch_no,session,copy_type){
	var datastr = "batch_no="+batch_no+"&copy_type="+copy_type+"&fee_year="+session+"&uniq_id="+uniq_id;
	//alert(datastr);
	window.open("module/fees/std_multi_mon_inv_single_copies_pdf.php?"+datastr);
}
function print_all_inv_by_batch(uniq_id,batch_no,session){
	var datastr = "batch_no="+batch_no+"&fee_year="+session+"&uniq_id="+uniq_id;
	//alert(datastr);
	window.open("module/fees/std_multi_mon_inv_all_copies_pdf.php?"+datastr);
}
function load(){
	var invoice = $("#invoice").val();
     var datastr = "invoice="+invoice;
    $("#main_content_div").load("module/fees/partial_print_invoice.php",datastr);
}

$("#goto_back").click(function(){
	 $("#main_content_div").load("module/fees/partialy_fee_collect.php");
	
});

function set_fee_details(selected_fee_name,selected_fee_amount){
	//var alert_msg = 'fee_id='+selected_fee_id+' fee_name='+selected_fee_name+' fee amount='+selected_fee_amount;
	var alert_msg = 'fee_name='+selected_fee_name+' fee amount='+selected_fee_amount;
	//alert(alert_msg);
	var sl_no = $('input[name="sl_no[]"]').map(function(){
				return $(this).val();}).get();
	var sl_no_length = sl_no.length;
	//alert(sl_no_length);
	//$("#fee_head_id_"+sl_no_length).val(selected_fee_id);
    $("#fee_head_name_"+sl_no_length).val(selected_fee_name);
    $("#fee_head_adj_amount_"+sl_no_length).val(selected_fee_amount);
	cal_tot_adj_amount();
}

function remove_from_inv(tr_id){
	var result = confirm("Want to delete?");
	if (result) {
		var tr_id_input="#tr_"+tr_id;
		var fee_det_id_to_rmv = $("#fee_head_id_"+tr_id).val();
		var fee_amount_to_rmv = $("#fee_head_adj_amount_"+tr_id).val();
		var adj_tot_amount = $("#total_amount_input").val();
		var tot_amount_update = adj_tot_amount - fee_amount_to_rmv;
		var invoice_no = $("#fee_head_adj_inv_no").val();
		var op = "delete_update";
		var datastr = "fee_det_id_to_rmv="+fee_det_id_to_rmv+"&tot_amount_update="+tot_amount_update+"&operation="+op+"&inv_no="+invoice_no;
		//var alert_msg = "removing.. row_no.."+tr_id+" fee_det_id.."+fee_det_id_to_rmv+" fee_amount="+fee_amount_to_rmv+" amount to update"+tot_amount_update;
		//alert(alert_msg);
		  $.ajax({
            type:"post",
            url:"module/fees/fee_head_adjustment_conf.php",
            data:datastr ,
            success:function(st){
				if(st==1){
					$(tr_id_input).remove();
					cal_tot_adj_amount();
					alert('Selected fee head has been deleted successfully');
				}
				else{
					alert('Error occured during deleting the fee');
				}
            }

        });
	}
}
	
function cal_tot_adj_amount(){
	var sum = 0;
    $(".headamount").each(function(){
        sum += +$(this).val();
    });
	$("#total_amount").text(sum);
	$("#total_amount_input").val(sum);
	//document.getElementById("total_amount").innerHTML = "<b>"+sum+"</b>";
}

function toggle_back_btn(){
	var origin_page = $("#origin_page").val();
	if(origin_page==2){
		$("#reload_fee_coll_id_btn").css("display","block");
	}
	else{
		$("#reload_fee_coll_id_btn").css("display","none");
	}
}
function std_fee_col_info_by_id(){
	$("#origin_page").val(1);
	load_std_invoice_list();
}

function print_std_inv_pdf(std_inv_no){
		var datastr = "invoice_id="+std_inv_no;
		window.open("module/fees/student_invoice_all_copies_pdf.php?"+datastr);
		//window.open("module/fees/student_invoice_pdf.php?"+datastr);
}

//functions transferred from std_invoice_list
function print_std_invoice_list(std_uniq_id,fee_year) {
	var datastr = "uniq_id="+std_uniq_id+"&fee_year="+fee_year;
	//alert(datastr);
	window.open("module/fees/std_fee_mpdf.php?"+datastr); 
}

function perform_partial_pay(invoice) {
	// var invoice = '<?php echo $value_s['_invoice_no'];?>';
		
	var mod_id = $("#mod_id21").val();
	var datastr="invoice="+invoice+"&mod_id="+mod_id;
		
	//exit();
	$("#origin_page").val(2);
	$('#first').css("display","none");
	$("#second").show();
	$('#second').html("<span style='color:#2e8965;font-size:16px;font-weight:bold;'>Data Generating.... Please Wait.  <img src='php_crud/img/preloader.gif' /></span>");
	$("#second").load("module/fees/partial_std_fee_receive.php",datastr);
	toggle_back_btn();
}
	
function perform_full_pay(invoice) {
	//var invoice = '<?php echo $value_s['_invoice_no'];?>';
	//alert(invoice);
	var mod_id = $("#mod_id21").val();
	var datastr="invoice="+invoice+"&mod_id="+mod_id;
		
	//alert(datastr);
	//exit();
    $("#origin_page").val(2);
	$('#first').css("display","none");
	$("#second").show();
	$('#second').html("<span style='color:#2e8965;font-size:16px;font-weight:bold;'>Data Generating.... Please Wait.  <img src='php_crud/img/preloader.gif' /></span>");
	$("#second").load("module/fees/std_fee_receive.php",datastr);
	toggle_back_btn();
}
	
function edit(invoice) {
	//var invoice = '<?php echo $value_s['_invoice_no'];?>';
	//alert(invoice);
	var mod_id = $("#mod_id21").val();
	var datastr="invoice="+invoice+"&mod_id="+mod_id;
		
	//alert(datastr);
	//exit();
	$("#origin_page").val(2);
      
	$('#first').css("display","none");
	$("#second").show();
	$('#second').html("<span style='color:#2e8965;font-size:16px;font-weight:bold;'>Data Generating.... Please Wait.  <img src='php_crud/img/preloader.gif' /></span>");
	//$("#second").load("module/fees/std_fee_edit.php",datastr);
	$("#second").load("module/fees/fee_edit_invoice.php",datastr);
	toggle_back_btn();
}
	