<script type="text/javascript">  
theight=$( document ).height()-50; 
function setdg(){
	$('#dg').datagrid({  	
		height:theight,		
		toolbar:"#toolbar",
		fit:true,
		freeze:true,
		fitColumns:true,
		rownumbers:true,		
		pagination:true,
		pageSize:100,
		pageList:[100,200,300,400,500],
		columns:[[ 
			{field:'id',title:'ID',width:80,hidden:true},			
			{field:'KdBarang',title:'Item Code'},
			// {field:'WIP1',title:'WIP1'},
			// {field:'WIP2',title:'WIP2'},
			// {field:'WIP3',title:'WIP3'},
			// {field:'SUB-P',title:'WIP Subcont'},
			// {field:'WIP-P',title:'WIP Plating'},
			// {field:'FG-P',title:'FG-P'},
			// {field:'CF',title:'CF'},
			// {field:'PartNo',title:'Size',width:200},
			{field:'NmBarang',title:'Item Desc',width:700},
			{field:'custPartNo',title:'Part No Customer',width:100},	
			{field:'Sat',title:'UOM',width:115},
			{field:'UrJnsBarang',title:'Jenis Barang',width:115},
		]],
		onBeforeLoad: function (data) {
		  var panel = $(this).datagrid("getPanel");
		  // here is to add the css style
		  panel.css("border","0");
		},
		url: '<?=site_url('bahanbaku/grid'); ?> '
	});
}
function cari(q) {
	TpBarang = $('#TpBarang2').combobox('getValue');
	$('#dg').datagrid({
		queryParams: {
			q: q,
			TpBarang: TpBarang
		}
	});
}
var url;
function tambah(){
	$('#dlg').dialog('open').dialog('setTitle','Tambah <?=$title?>');
	$('#fm').form('clear');
	$("#kd_tpbarang").val(1);
	url = '<?=site_url('bahanbaku/getform/add'); ?>';
}
function ubah(){
	var row = $('#dg').datagrid('getSelected');
	$('#empl_id').combogrid({  
		editable:false  
    });  
	if (row){
		$('#dlg').dialog('open').dialog('setTitle','Ubah <?=$title?>');
		$('#fm').form('load',row);
		url = '<?=site_url('bahanbaku/getform/edit'); ?>';
	}
}
function simpan(){
	$('#fm').form('submit',{
		url: url,
		onSubmit: function(){				
			return $(this).form('validate');
		},
		success: function(result){
			//alert(result);
			var result = eval('('+result+')');
			if (result.success){
				$('#dlg').dialog('close');		// close the dialog
				$('#dg').datagrid('reload');	// reload the bahanbaku data
				$.messager.show({
					title: 'Info',
					msg: result.msg
				});
			} else {
				$.messager.show({
					title: 'Error',
					msg: result.msg
				});
			}
		}
	});
}
function validasi(strings){
	var i=0;
	var ch='';
	
	num=0;
	ucase=0;
	lcase=0;
	while (i <= strings.length){
		character = strings.charAt(i);
		if (!isNaN(character * 1)){
			num=1;
		}else{
			if (character == character.toUpperCase()) {
				ucase=1;
			}
			if (character == character.toLowerCase()){
				lcase=1;
			}
		}
		i++;
	}
	if (num==0 || ucase==0 || lcase==0){ 
		return false; 
	} else {
		return true;
	}
}
function hapus(){
	var row = $('#dg').datagrid('getSelected');
	if (row){
		$.messager.confirm('Confirm','Apakah anda yakin akan menghapus data ini?',function(r){
			if (r){
				$.post('<?=site_url(); ?>bahanbaku/del',{id:row.id},function(result){
					var result = eval('('+result+')');
					if (result.success){
						$('#dg').datagrid('reload');	// reload the bahanbaku data
						$.messager.show({
							title: 'Info',
							msg: result.msg
						});
					} else {
						$.messager.show({	// show error message
							title: 'Error',
							msg: result.msg
						});
					}
				});
			}
		});
	}
}
function toprint(){
	var opts = $('#dg').datagrid('options');
	q=$("#q").val();
	
	var win = window.open('<?= site_url()?>bahanbaku/cetak?q='+q+'&page='+opts.pageNumber+'&rows='+opts.pageSize,'Popup','toolbar=no, location=no,status=no,menubar=no,scrollbars=yes,resizable=no');
	win.print();
}
function toxls(){
	var opts = $('#dg').datagrid('options');
	txtcari=$("#txtcari").val();
	q=$("#q").val();
	openurl('<?=site_url();?>bahanbaku/excel?q='+q+'&page='+opts.pageNumber+'&rows='+opts.pageSize);
}
function topdf(){
	openurl("<?=site_url();?>po/pdf");
}
function insert_nama(row){
	$("#nama").val(row.nama);
}
</script>	