var pilihcatatan=(function(){
	var boxcatatan=null;
	var module="";
	return{
		init:function(a){
			module=a['module'];
		},
		bukacttn:function(a){
			var judulbox=a['judul'] || "Pilih Data Catatan";
			var modepilih=a['modepilih'] || "tunggal";
			
			var ps={"box":"box_catatan",
					"judul":judulbox,tinggi:"auto","lebar":"700px"};
		
			boxcatatan=boxwin.load(ps);
			boxcatatan.obj=a;
			boxcatatan.modepilih=modepilih;
			boxcatatan.arrpilih=[];
			boxcatatan.buka();
			$("#box_loader_cttn").html("");
			
			$("#btprosesmultictt").hide();
			if(modepilih=="multi") $("#btprosesmultictt").show();
			pilihcatatan.loadecttn();
		},
		caricttn:function(e){
			if(e.which == 13 || e.keyCode == 13){
				$("#cari_cttn").val($("#namacttn").val());
				pilihcatatan.loadecttn();
			}
		},
		loadecttn:function(){
			$("#hal_ke_cttn").val(0);
			pilihcatatan.loaddatacttn($("#hal_ke_cttn").val(),$("#jumlah_hal_cttn").val());
		},
		loadcttn:function(){
			pilihcatatan.loaddatacttn($("#hal_ke_cttn").val(),$("#jumlah_hal_cttn").val());
		},
		entercttn:function(e){
			if(e.which == 13 || e.keyCode == 13){
				pilihcatatan.loadecttn();
			}
		},
		loaddatacttn:function(h,jm){
			var idkategori=$("#sl_kat_cttn").val();
			$("#grid_data_cttn").html("");
			var cr=$("#cari_cttn").val();
			var posisiawal=(h-1)*jm;
			
			$("#hal_ke_cttn").html("<option value=\"1\">1</option>");
			
			var obj={"idbox":"box_loader_cttn"};
			var boxloader=mload.init(obj);
			boxloader.load();
			
			var ik_data = {ajax: 1,mode: "plg",
				cl: "pilihcatatan",
				ikdata: {0:"getdatacatatan",
					idkategori:idkategori,
					module:module,
					mode:boxcatatan.mode,
					halaman:h,jumlah:jm,cari:cr}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",type: "POST",
				dataType: "json",async:true,data: ik_data,
				success: function(e){
					
					boxloader.tutup();
					if(e.hasOwnProperty("data") && e['data'].length>0){
						var htm="<option value=\"1\">1</option>";
						var jumlahhalaman=e['totalhalaman'];
						if(jumlahhalaman>1){
							for(var i=0;i<jumlahhalaman;i++){
								htm+="<option value='"+(i+1)+"'>"+(i+1)+"</option>";
							}
							$("#hal_ke_cttn").html(htm);
						}
						
						$("#hal_ke_cttn").val(h);
						var tplpilih="btpilihtunggalctt";
						if (boxcatatan.modepilih=="multi"){
							tplpilih="btpilihmultictt";
						}
						var datatb={
							box:"grid_data_cttn",
							data:e['data'],
							lebarmin:"500px",
							trklik:{"data":"id","fungsi":"barisklik"},
							title:"kategori Menu",
							isi:[
									{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"50px","halaman":posisiawal,"jumlah":jm},	
									{"col":"kategori","judul":"Kategori","tipe":"data","lebar":"130px","align":"left"},
									{"col":"nama","judul":"Nama Catatan","tipe":"tpl","tpl":"btdetaildatactt"},
									{"align":"center","judul":"","tipe":"tpl","tpl":tplpilih,"lebar":"60px"},
									
								],
							mod:"mod",
							module:"pilihcatatan"
						};
						tabel.multisub(datatb);
						
					}else{
						$("#grid_data_cttn").html("Tidak ada data!!")
					}
				}
			});
		},
		pilihcatatan:function(a,b,c){
			boxcatatan.obj.pilih(tabel.gettabel(b).getdatabaris(c));
			pilihcatatan.bataldatacttn();
		},
		pilihcatatanmulti:function(a,b,c){
			var idtb=b+"_"+c;
            var attst=document.getElementById("btpilihmultictt_"+idtb).getAttribute("stpilih");
            
            var st=0;
            if(attst==0){
                st=1;
                boxcatatan.arrpilih.push(tabel.gettabel(b).getdatabaris(c));
                
                document.getElementById("btpilihmultictt_"+idtb).className="bt_ya";
                document.getElementById("btpilihmultictt_"+idtb).setAttribute("stpilih",1);
            }else{
                var index = kudus.getindexarrkey(a,boxcatatan.arrpilih,"id");
                if(index > -1)  {
                    boxcatatan.arrpilih.splice(index, 1);
                }
                
                document.getElementById("btpilihmultictt_"+idtb).className="bt_tidak";
                document.getElementById("btpilihmultictt_"+idtb).setAttribute("stpilih",0);
            }
            
		},
		bataldatacttn:function(){
			if(boxcatatan!=null) boxcatatan.tutup();
			boxcatatan=null;
		},
		prosescatatan:function(){
			$("#box_loader_cttn").html("");
			if(boxcatatan.arrpilih.length<=0){
				$("#box_loader_cttn").html("Belum ada catatan yang dipilih!");
				return;
			}
			var arrdatapilih=boxcatatan.arrpilih;
			
			boxcatatan.obj.pilih(arrdatapilih);
			pilihcatatan.bataldatacttn();
		}
	};
})();
