<script type="text/javascript">
$(document).ready(function(){
	makeFunctionModul = function(_func, _lastNo){
		_func2 = _func;
		_func  = _func.split("(");
		_func  = _func[0];
		if(_func === 'mInv'){
			mInv(_lastNo);
		}
		if(_func === 'mSkat'){
			mSkat(_lastNo);
		}
		if(_func === '_creatingPo'){
			_creatingPo(_lastNo, false);
		}
		if(_func === '_creatingLpb'){
			_creatingLpb(_lastNo, false);
		}
		if(_func === 'detPo'){
			detPo(_lastNo, false);
		}
		if(_func === '_hitungPo'){
			_hitungPo(_lastNo, false);
		}
		if(_func === 'detLpb'){
			detLpb(_lastNo, false);
		}
		if(_func === '_creatingNota'){
			_creatingNota(_lastNo, false);
		}
		if(_func === '_creatingReb'){
			_creatingReb(_lastNo, false);
		}
		if(_func === 'detNbl'){
			detNbl(_lastNo, false);
		}
		if(_func === '_creatingSJ'){
			_creatingSJ(_lastNo, false);
		}
		if(_func === '_creatingMm'){
			_creatingMm(_lastNo, false);
		}
		if(_func === 'detSj'){
			detSj(_lastNo, false);
		}
		if(_func === '_creatingSo'){
			_creatingSo(_lastNo, false);
		}
		if(_func === '_creatingAdj'){
			_creatingAdj(_lastNo, false);
		}
		if(_func === '_loadMutKirim'){
			_loadMutKirim(_lastNo, false);
		}
		if(_func === '_creatingOrderB'){
			_creatingOrderB(_lastNo, false);
		}
		if(_func === '_creatingTimbangB'){
			_creatingTimbangB(_lastNo, false);
		}
		if(_func === '_creatingNotaB'){
			_creatingNotaB(_lastNo, false);
		}
		if(_func === '_hitungPoB'){
			_hitungPoB(_lastNo, false);
		}
		if(_func === 'detTimbang'){
			detTimbang(_lastNo, false);
		}
	}
	
	_detTimbang = false;
	detTimbang = function(){
		if(_detTimbang != false){
			_detTimbang.abort();
		}
		_detTimbang = $.ajax({
		type: "POST",
			url: $('#defaultBaseUrl').val()+$('#defaultController').val()+"/detTimbang/&kd_sub="+$('#kd_sub').val()+"&act="+$('#_prosesAction').val(),				
        	data: $('#formAjaxDefault').serialize(),
			success: function(data) {
				$("#errorDisplay").html(data);
			}			
		})			
	}
	
	$('#_loadTimbangx').click(function(){
		detTimbang();
	})
	
	// pop up timbang cetakan
	$('#_loadTimbang').click(function(){
		_act = $('#_prosesAction').val();
		if(_act === 'simpan' || _act === 'update'){
			makePopUpTop(900, 100, $('#defaultBaseUrl').val()+$('#defaultController').val()+"/loadTimbang/&kd_sub="+$('#kd_sub').val()+"&act="+$('#_prosesAction').val());
		}
	})
	$('#cekAll').live("click", (function(){
		_cek = $(this).attr("checked");
		if(_cek === 'checked'){
			$('._cekTimbang').attr("checked", true);
		}else{
			$('._cekTimbang').removeAttr("checked");
		}
	}))
	_submitTimbang = false;
	$('#_pilihTimbang').live("submit", (function(e){
		e.preventDefault();
		if(_submitTimbang != false){
			_submitTimbang.abort();
		}
		_submitSo = $.ajax( {	
			type : "POST",
			url: $('#defaultBaseUrl').val()+$('#defaultController').val()+"/detTimbang/&kd_sub="+$('#kd_sub').val()+"&act="+$('#_prosesAction').val(),
			data: $(this).serialize(),
			success: function(data) {
				$('#errorDisplay').html(data);
			},
		 });
	}))
	//sampai sini
	
	mInv = function(){
		selectLoadDb('msubkat', 'SKAT', '', false);
	}
	
	mSkat = function(){
		selectLoadDb('mkat', 'KAT', '', false);
	}
	
	_creatingPo = function(){
		makeStartModul = function(){
			$('#tgl_po').val($('#tglDefault').val());
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		
		_hitungPo();
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
		
		$('#ppn').keyup(function(){
			_hitungPo();
		})
	}
	
	_creatingOrderB = function(){
		makeStartModul = function(){
			$('#tgl_po').val($('#tglDefault').val());
			$('#kd_cabang').val('HO');
			$('#nm_cabang').val('HEAD OFFICE');
			$('#kd_gudang').val('GD');
			$('#nm_gudang').val('GUDANG');
			$('#kd_inv').val('01');
			$('#nm_inv').val('JAGUNG');
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
	}
	
	_creatingTimbangB = function(){
		makeStartModul = function(){
			$('#tgl').val($('#tglDefault').val());
			$('#kd_cabang').val('HO');
			$('#nm_cabang').val('HEAD OFFICE');
			$('#kd_gudang').val('GD');
			$('#nm_gudang').val('GUDANG');
			$('#kd_inv').val('01');
			$('#nm_inv').val('JAGUNG');
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		
		$('#qty1, #qty2').live("change",(function(){
    		_qty1 = Number(str_ireplace(",", "", $('#qty1').val()));
    		_qty2 = Number(str_ireplace(",", "", $('#qty2').val()));
    		_qty = _qty1 - _qty2;
    		$('#qty').val(_qty);
    		
    		setDec();
    	}))
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
	}
	
	$('._hitungPo').live("change",(function(){
		_hitungPo();
	}))
		
	_hitungPo = function(){
		_tot = 0;
		_totPpn = 0;
		for(_i = 1; _i <= 50; _i++){
			_kd_inv = $('#kd_inv_'+_i).val();
			if(_kd_inv != undefined){
				_qty = Number(str_ireplace(",", "", $('#qty_'+_i).val()));
				_hrg = Number(str_ireplace(",", "", $('#harga_'+_i).val()));
				_ds1 = Number(str_ireplace(",", "", $('#diskon_2_'+_i).val()));
				_ds2 = Number(str_ireplace(",", "", $('#diskon_3_'+_i).val()));
				_ds3 = Number(str_ireplace(",", "", $('#diskon_4_'+_i).val()));
				_ds4 = Number(str_ireplace(",", "", $('#diskon_5_'+_i).val()));
				_ds5 = Number(str_ireplace(",", "", $('#diskon_6_'+_i).val()));
				_dsr = Number(str_ireplace(",", "", $('#diskon_'+_i).val()));
				
				_total = _qty * _hrg;
				
				_dis = (_total * _ds1) / 100;
				_total = _total - _dis;
				
				_dis = (_total * _ds2) / 100;
				_total = _total - _dis;
				
				_dis = (_total * _ds3) / 100;
				_total = _total - _dis;
				
				_dis = (_total * _ds4) / 100;
				_total = _total - _dis;
				
				_dis = (_total * _ds5) / 100;
				_total = _total - _dis;
				
				_total = _total - _dsr;
				_total = Math.round(_total);
				_total = Number(_total);
				_tot = _tot + _total;
				
				_inv = $('#kd_inv_'+_i).val();
				_inv = _inv.substring(0, 1);
				_inv = _inv.toUpperCase();
				if(_inv != 'Z'){
					_totPpn = _totPpn + _total;
				}
				
				$('#total_'+_i).val(_total);
			}
		}
		
		$('#stot').val(_tot);


		_total	= _tot;
		_disG1  = Number(str_ireplace(",", "", $('#diskg').val()));
		_disGN1 = (_total * _disG1) / 100;
		_disGN1 = Math.round(_disGN1);
		_total = _total - _disGN1;

		_disG2  = Number(str_ireplace(",", "", $('#diskg2').val()));
		_disGN2 = (_total * _disG2) / 100;
		_disGN2 = Math.round(_disGN2);
		_total = _total - _disGN2;
		_disGN = _disGN1 + _disGN2;
	
		$('#diskg_nilai').val(_disGN);
		
		_ppn = $('#ppn').val();
		_ppn = _ppn.toUpperCase();
		if(_ppn === ''){
			_ppn = 'N';
			$('#ppn').val(_ppn);
		}
		
		_dpp  = 0;
		_ppnN = 0;
		_gtot = 0;
		
		if(_ppn === 'N'){
			_dpp  = _total;
			_gtot = _total;
		}
		if(_ppn === 'I'){
			// _ppnN = _total / 11;

			_ppnN = _totPpn / 11;
			_ppnN = Math.round(_ppnN);
			_dpp  = _total - _ppnN;
			_gtot = _total;
		}
		if(_ppn === 'E'){
			// _ppnN = (_total * 10) / 100;
			
			_ppnN = (_totPpn * 10) / 100;
			_ppnN = Math.round(_ppnN);
			_dpp  = _total;
			_gtot = _total + _ppnN;
		}

		$('#dpp').val(_dpp);
		$('#ppn_nilai').val(_ppnN);
		$('#gtot').val(_gtot);
		
		setDec();
	}
	
	_creatingLpb = function(){
		makeStartModul = function(){
			$('#tgl').val($('#tglDefault').val());
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
	}
	
	_detPo = false;
	detPo = function(){
		if(_detPo != false){
			_detPo.abort();
		}
		$('#_detBody').html("Loading Data.....");
		_detPo = $.ajax({
		type: "GET",
			url: $('#defaultBaseUrl').val()+$('#defaultController').val()+"/detPo/&nopo="+$('#noref').val()+"&nolpb="+$('#nolpb').val(),
			data: $(this).serialize(),
			success: function(data) {
				$("#errorDisplay").html(data);
			}
		})	
	}
	
	_detLpb = false;
	detLpb = function(){
		if(_detLpb != false){
			_detLpb.abort();
		}
		_detLpb = $.ajax({
		type: "GET",
			url: $('#defaultBaseUrl').val()+$('#defaultController').val()+"/detLpb/&nolpb="+$('#noref').val()+"&nonbl="+$('#nonbl').val(),
			data: $(this).serialize(),
			success: function(data) {
				$("#errorDisplay").html(data);
			}
		})	
	}
	
	_creatingNota = function(){
		makeStartModul = function(){
			$('#tgl').val($('#tglDefault').val());
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
		_hitungPo();
	}
	
	_creatingNotaB = function(){
		makeStartModul = function(){
			$('#tgl').val($('#tglDefault').val());
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
		_hitungPoB();
	}
	
	$('._hitungPoB').live("change",(function(){
		_hitungPoB();
	}))
	
	_hitungPoB = function(){
    	_tot = 0;
    	for(_i = 1; _i <= 50; _i++){
    		_noref = $('#noref_'+_i).val();
    		if(_noref != undefined){
    			_qty = Number(str_ireplace(",", "", $('#qty_'+_i).val()));
    			_humid = Number(str_ireplace(",", "", $('#humid_'+_i).val()));
    			_hrg = Number(str_ireplace(",", "", $('#harga_'+_i).val()));
    			
    			_qtyNett = _qty - (_qty * (_humid / 100));
    			_total = _qtyNett * _hrg;
    			_tot += _total;
    			
    			$('#total_'+_i).val(_total);
    		}
    	}
    	
    	$('#gtot').val(_tot);
    	setDec();
    }
	
	_creatingReb = function(){
		makeStartModul = function(){
			$('#tgl').val($('#tglDefault').val());
			$('#no_ref').removeAttr('readonly');
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		makeEditModul = function(){
			$('#no_ref').attr('readonly', true);
		}
		$.Shortcuts.add({
			type: "down",
			mask: "E",
			handler: function() {
				makeEditModul();
			}
		}).start();
		$('.buttonEdit').click(function(){
			makeEditModul();
		})
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
		_hitungPo();
	}
	
	_detNbl = false;
	detNbl = function(){
		if(_detNbl != false){
			_detNbl.abort();
		}
		_detNbl = $.ajax({
		type: "GET",
			url: $('#defaultBaseUrl').val()+$('#defaultController').val()+"/detNbl/&nonbl="+$('#noref').val()+"&noret="+$('#noret').val()+"&gudang="+$('#kd_gudang').val(),
			data: $(this).serialize(),
			success: function(data) {
				$("#errorDisplay").html(data);
			}
		})	
	}
	
	$('#kd_gudang').change(function(){
		detNbl();
	})
	
	_creatingSJ = function(){
		makeStartModul = function(){
			$('#tgl').val($('#tglDefault').val());
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
	}
	
	_creatingMm = function(){
		makeStartModul = function(){
			$('#tgl').val($('#tglDefault').val());
			$('#no_ref').removeAttr('readonly');
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		makeEditModul = function(){
			$('#no_ref').attr('readonly', true);
		}
		$.Shortcuts.add({
			type: "down",
			mask: "E",
			handler: function() {
				makeEditModul();
			}
		}).start();
		$('.buttonEdit').click(function(){
			makeEditModul();
		})
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
	}
	
	_detSj = false;
	detSj = function(){
		if(_detSj != false){
			_detSj.abort();
		}
		_detSj = $.ajax({
		type: "GET",
			url: $('#defaultBaseUrl').val()+$('#defaultController').val()+"/detSj/&nosj="+$('#noref').val()+"&nomut="+$('#nomut').val(),
			data: $(this).serialize(),
			success: function(data) {
				$("#errorDisplay").html(data);
			}
		})	
	}
	
	_creatingSo = function(){
		makeStartModul = function(){
			$('#tgl').val($('#tglDefault').val());
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
	}
	
	_creatingAdj = function(){
		makeStartModul = function(){
			$('#tgl').val($('#tglDefault').val());
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
	}
	
	loadMutKirim = false;
	_loadMutKirim = function(){
		if(loadMutKirim != false){
			loadMutKirim.abort();
		}
		loadMutKirim = $.ajax({
		type: "GET",
			url: $('#defaultBaseUrl').val()+$('#defaultController').val()+"/loadKirim/&kirim="+$('#ref').val()+"&kd_sub="+$('#kd_sub').val()+"&nomut="+$('#nomut').val(),
			data: $(this).serialize(),
			success: function(data) {
				$("#errorDisplay").html(data);
			}
		})	
	}
})
</script>

