<script type="text/javascript">
$(document).ready(function(){
	makeFunctionModul = function(_func, _lastNo){
		_func2 = _func;
		_func  = _func.split("(");
		_func  = _func[0];
		if(_func === '_creatingOj'){
			_creatingOj(_lastNo, false);
		}
		if(_func === '_creatingOjual'){
			_creatingOjual(_lastNo, false);
		}
		if(_func === '_creatingNjl'){
			_creatingNjl(_lastNo, false);
		}
		if(_func === 'detJo'){
			detJo(_lastNo, false);
		}
		if(_func === '_hitungPo'){
			_hitungPo(_lastNo, false);
		}
		if(_func === '_creatingRej'){
			_creatingRej(_lastNo, false);
		}
		if(_func === 'detNjl'){
			detNjl(_lastNo, false);
		}
		if(_func === '_creatingPak'){
			_creatingPak(_lastNo, false);
		}
		if(_func === '_hitPak'){
			_hitPak(_lastNo, false);
		}
		if(_func === '_creatingSJ'){
			_creatingSJ(_lastNo, false);
		}
		if(_func === '_creatingTimbangJ'){
			_creatingTimbangJ(_lastNo, false);
		}
		if(_func === '_hitungPoJ'){
			_hitungPoJ(_lastNo, false);
		}
		if(_func === '_creatingNjlJ'){
			_creatingNjlJ(_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
	
	_creatingTimbangJ = 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 = _qty2 - _qty1;
    		$('#qty').val(_qty);
    		
    		setDec();
    	}))
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
	}
	
	_creatingNjlJ = 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('msales', 'SALES', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
		_hitungPoJ();
	}
	
	$('._hitungPoJ').live("change",(function(){
		_hitungPoJ();
	}))
	
	_hitungPoJ = 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();
    }
	
	_creatingOj = 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('msales', 'SALES', '', false);
		_hitungPo();
	}
	
	_creatingOjual = function(){
		makeStartModul = function(){
			$('#tgl').val($('#tglDefault').val());
			$('#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('msales', 'SALES', '', false);
	}
	
	
	$('._hitungPo').live("change",(function(){
		_hitungPo();
	}))
		
	_hitungPo = function(){
		_tot = 0;
		_tod = 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()));
				
				_bruto = _qty * _hrg;
				_netto1	= _bruto - (_qty * (_ds1 + _ds2));
				
				_dsPer1 = Math.round(_netto1 * _ds3 / 100);
				_netto2 = _netto1 - _dsPer1;
				
				_dsPer2 = Math.round(_netto2 * _ds4 / 100);
				_netto3 = _netto2 - _dsPer2;

				_dsPer3 = Math.round(_netto3 * _ds5 / 100);
				_netto4	= _netto3 - _dsPer3;
				
				_total 	= _netto4;
				_total = Math.round(_total);
				_total = Number(_total);
				
				_tot = _tot + _total;
				_tod = _tod + ((_qty * (_ds1 + _ds2)) + _dsPer1 + _dsPer2 + _dsPer3);
				
				$('#total_'+_i).val(_total);
			}
		}
		
		$('#stot').val(_tot);

		_disG  = Number(str_ireplace(",", "", $('#diskg').val()));
		_disGN = (_tot * _disG) / 100;
		_disGN = Math.round(_disGN);
		_total = _tot - _disGN;
		$('#diskg_nilai').val(_disGN);
		
		_disRp = 0;
		_cekDisRp = $('#diskrp').val();
		if(_cekDisRp != undefined){
			_disRp = Number(str_ireplace(",", "", $('#diskrp').val()));
			_total = _total - _disRp;
		}
		
		_tod = _tod + _disGN + _disRp;
		
		_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'){
			_dpp = _total / 1.11;
			_dpp = Math.round(_dpp);
			_ppnN = _total - _dpp;
			_gtot = _total;
		}
		if(_ppn === 'E'){
			_ppnN = (_total * 11) / 100;
			_ppnN = Math.round(_ppnN);
			_dpp  = _total;
			_gtot = _total + _ppnN;
		}

		_tod = Math.round(_tod);
		$('#diskt').val(_tod);
		$('#dpp').val(_dpp);
		$('#ppn_nilai').val(_ppnN);
		$('#gtot').val(_gtot);
		
		setDec();
	}
	
	_creatingNjl = function(){
		makeStartModul = function(){
			$('#tgl').val($('#tglDefault').val());
			_cek = $('#NjlpAdd').attr('class');
			if(_cek != undefined){
				//$('#kd_cabang').val('HO');
				//$('#nm_cabang').val('CV. GRAHA BUSANA PRIMA');
				//$('#kd_gudang').val('0.0.06.10');
				//$('#nm_gudang').val('PERSEDIAAN BARANG DAGANGAN');
				$('#ppn').val('I');
			}else{
				//$('#kd_cabang').val('HO');
				//$('#nm_cabang').val('CV. GRAHA BUSANA PRIMA');
				//$('#kd_gudang').val('0.0.06.10');
				//$('#nm_gudang').val('PERSEDIAAN BARANG DAGANGAN');
				$('#ppn').val('N');
			}
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartModul();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeStartModul();
		})
		
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('msales', 'SALES', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
		_hitungPo();
	}
	
	_detJo = false;
	detJo = function(){
		if(_detJo != false){
			_detJo.abort();
		}
		_detJo = $.ajax({
		type: "GET",
			url: $('#defaultBaseUrl').val()+$('#defaultController').val()+"/detJo/&nooj="+$('#noref').val()+"&nonjl="+$('#nonjl').val()+"&nopak="+$('#nopak').val(),
			data: $(this).serialize(),
			success: function(data) {
				$("#errorDisplay").html(data);
			}
		})	
	}
	_creatingRej = 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('msales', 'SALES', '', false);
		selectLoadDb('cabang', 'CABANG', '', false);
		selectLoadDb('mgud', 'GUDANG', '', false);
		_hitungPo();
	}
	
	_detNjl = false;
	detNjl = function(){
		if(_detNjl != false){
			_detNjl.abort();
		}
		_detNjl = $.ajax({
		type: "GET",
			url: $('#defaultBaseUrl').val()+$('#defaultController').val()+"/detNjl/&nonjl="+$('#noref').val()+"&noret="+$('#noret').val()+"&gudang="+$('#kd_gudang').val(),
			data: $(this).serialize(),
			success: function(data) {
				$("#errorDisplay").html(data);
			}
		})	
	}
	
	_creatingSJ = function(){
		makeAddPak = function(){
			$('#tgl').val($('#tglDefault').val());
			makeClearSession();
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeAddPak();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeAddPak();
		})
		
		makeClearSession();
		selectLoadDb('msales', 'SALES', '', false);
	}
	
	_creatingPak = function(){
		makeAddPak = function(){
			$('#tgl').val($('#tglDefault').val());
			makeClearSession();
		}
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeAddPak();
			}
		}).start();
		$('.buttonAdd').click(function(){
			makeAddPak();
		})
		
		makeClearSession();
		_hitPak();
		
		$('._hitungPak').live("change", (function(){
			_hitPak();
		}))
		selectLoadDb('msales', 'SALES', '', false);
	}
	_makeClearSession = false;
	makeClearSession = function(){
		$('#_loadSo').attr('disabled', true);
		if(_makeClearSession != false){
			_makeClearSession.abort();
		}
		_makeClearSession = $.ajax( {	
			type : "GET",
			url: $('#defaultBaseUrl').val()+$('#defaultController').val()+"/clearDet/&token="+$('#_pageToken').val()+"&nopak="+$('#nopak').val(),
			data: $(this).serialize(),
			success: function(data) {
				$('#errorDisplay').html(data);
				$('#_loadSo').removeAttr('disabled');
			},
		 });
	}
	
	_hitPak = function(){
		_total = 0;
		for(_i = 1; _i <= 100; _i++){
			_cek = $('#harga_'+_i).attr('type');
			if(_cek != undefined){
				_hrg = Number(str_ireplace(",", "", $('#harga_'+_i).val()));

				_total = _total + _hrg;
			}
		}
		
		$('#total').val(_total);
		setDec();
	}
	$('#_loadSo').click(function(){
		_act = $('#_prosesAction').val();
		if(_act === 'simpan' || _act === 'update'){
			makePopUpTop(900, 100, $('#defaultBaseUrl').val()+$('#defaultController').val()+"/loadSo/&token="+$('#_pageToken').val()+"&gudang="+$('#kd_gudang').val()+"&cust="+$('#kd_sgudang').val()+"&sales="+$('#kd_sales').val()+"&sopir="+$('#kd_sopir').val());
		}
	})
	$('#cekAll').live("click", (function(){
		_cek = $(this).attr("checked");
		if(_cek === 'checked'){
			$('._cekSo').attr("checked", true);
		}else{
			$('._cekSo').removeAttr("checked");
		}
	}))
	
	_submitSo = false;
	$('#_pilihSo').live("submit", (function(e){
		e.preventDefault();
		if(_submitSo != false){
			_submitSo.abort();
		}
		_submitSo = $.ajax( {	
			type : "POST",
			url: $('#defaultBaseUrl').val()+$('#defaultController').val()+"/detSiap/&token="+$('#_pageToken').val()+"&nojso="+$('#nojso').val(),
			data: $(this).serialize(),
			success: function(data) {
				$('#errorDisplay').html(data);
			},
		 });
	}))
})
</script>

