$(document).ready(function(){
	$('body').keydown(function(e) { 
		if(e.which === 112) { return false; } 
		if(e.which === 113) { return false; } 
		if(e.which === 114) { return false; } 
		if(e.which === 115) { return false; } 
		if(e.which === 116) { return false; } 
		if(e.which === 117) { return false; } 
		if(e.which === 118) { return false; } 
		if(e.which === 119) { return false; } 
		if(e.which === 120) { return false; } 
		if(e.which === 121) { return false; } 
		if(e.which === 122) { return false; } 
		if(e.which === 123) { return false; } 
	}); 
	$('body').keyup(function(e) { 
		if(e.which === 112) { return false; } 
		if(e.which === 113) { return false; } 
		if(e.which === 114) { return false; } 
		if(e.which === 115) { return false; } 
		if(e.which === 116) { return false; } 
		if(e.which === 117) { return false; } 
		if(e.which === 118) { return false; } 
		if(e.which === 119) { return false; } 
		if(e.which === 120) { return false; } 
		if(e.which === 121) { return false; } 
		if(e.which === 122) { return false; } 
		if(e.which === 123) { return false; } 
	}); 
	
	$('body').keyup(function(e) { 
		cekShortcut(e.which);
	})
	$('body').keydown(function(e) { 
		cekShortcut(e.which);
	})
	$('._btnBantu').click(function(){
		_nom = parseInt($(this).attr("nom"));
		cekShortcut(_nom);
	})
	cekShortcut = function(_nom){
		if(_nom === 27){
			$('#apankForm').html("");
		}
		
		_cekMode = $('#modeHist').val();
		if(_cekMode === 'plu'){
			return;
		}
		
		if(_nom === 112){
			$('#kd_inv').focus().select();
		}
		if(_nom === 113){
			makeBrowseMenu();
		}
		if(_nom === 114){
			makeMember();
		}
		if(_nom === 115){
			makeRetur('retur');
		}
		if(_nom === 116){
			$('#tunai').focus().select();
		}
		if(_nom === 117){
			$('#debet').focus().select();
		}
		if(_nom === 118){
			$('#flazz').focus().select();
		}
		if(_nom === 119){
			$('#simpan').focus();
		}
		if(_nom === 120){
			makeBatalNota();
		}
		if(_nom === 121){
			makeRetur('cetak');
		}
		if(_nom === 122){
			makeCreateMember();
		}
		if(_nom === 123){
			_conf = confirm("Yakin Kembali ke Back Office?");
			if(_conf){
				_url = $('#baseUrl').val();
				window.location = _url+'application/index/pos';
			}
		}
	}
	
	_loadInv = false;
	makeLoadInv = function(){
		_inv = $('#kd_inv').val();
		if(_inv.length < 7){
			alert("Kode Menu minimum 4 digit");
			$('#nm_inv').val('');
			makeNullImgMenu();
			return;
		}
		
		$('#nm_inv').val('Loading...');
		if(_loadInv != false){
			_loadInv.abort();
		}
		$('#prosesStatus').val('P');
		_loadInv = $.ajax({
			type: 'GET',
			url: $('#baseUrl').val()+'posr/invLoad/&kd_inv='+$('#kd_inv').val()+'&qty='+$('#qty').val(),
			data: $(this).serialize(),
			success: function(data) {
				$('#errorDisplay').html(data);
				$('#prosesStatus').val('');
			},
		});
	}
	$('#kd_inv').keyup(function(e){
		if(e.which === 13){
			makeLoadInv();
		}
	})
	$('#kd_inv').change(function(){
		makeLoadInv();
	})
	
	makeNullImgMenu = function(){
		$('._imgMenu').removeAttr('src');
		$('._imgMenu').attr('src', $('#baseUrl').val()+'assets/upload/no-image.png');
	}
	
	_tambahItem = false;
	makeTambahItem = function(){
		_inv = $('#kd_inv').val();
		if(_inv.length < 4){
			alert("Kode Menu Salah");
			$('#kd_inv').focus().select();
			makeNullImgMenu();
			return;
		}

		_qty = Number(str_ireplace(",", "", $('#qty').val()));
		if(_qty === 0){
			alert("Qty tidak boleh 0");
			$('#qty').focus().select();
			return;
		}
		_invNm = $('#nm_inv').val();
		if(_invNm === 'Loading...'){
			alert("Pemanggilan Menu belum selesai");
			$('#kd_inv').focus().select();
			return;
		}
		if($('#prosesStatus').val() === 'P'){
			alert("Proses sebelumnya belum selesai");
			$('#qty').focus().select();
			return;
		}
		
		_cekMode = $('#modeT').val();
		if(_cekMode === 'R'){
			if(_qty > 0){
				alert("Qty harus Min");
				$('#qty').focus().select();
				return;
			}
		}
		
		if(_tambahItem != false){
			_tambahItem.abort();
		}
		
		$('#_tambahItem').css('display', "none");
		$('#prosesStatus').val('P');
		$('#qty, #_tambahItem').attr('disabled', true);
		_tambahItem = $.ajax({
			type: 'GET',
			url: $('#baseUrl').val()+'posr/invTambah/&kd_inv='+$('#kd_inv').val()+'&qty='+$('#qty').val()+'&btl='+$('#btl').val(),
			data: $(this).serialize(),
			success: function(data) {
				$('#errorDisplay').html(data);
				$('#prosesStatus').val('');
				$('#_tambahItem').removeAttr('style', "none");
				$('#qty, #_tambahItem').removeAttr('disabled');
			},
		});
	}
	$('#btl').keyup(function(e){
		if(e.which === 13){
			$('#qty').focus().select();
		}
	})
	$('#qty, #_tambahItem').keyup(function(e){
		if(e.which === 13){
			makeTambahItem();
		}
	})
	$('#_tambahItem').click(function(){
		makeTambahItem();
	})
	
	_loadDetail = false;
	loadDetail = function(){
		
		if(_loadDetail != false){
			_loadDetail.abort();
		}
		$('._detail').html('Loading...');
		_loadDetail = $.ajax({
			type: 'GET',
			url: $('#baseUrl').val()+'posr/loadDetail/&kd_sub='+$('#kd_sub').val(),
			data: $(this).serialize(),
			success: function(data) {
				$('._detail').html(data);
			},
		});
	}
	
	_loadValidasi = false;
	makeValidasi = function(){
		result = prompt('Masukkan No. Nota', '');
		if(result){
			if(result != ''){
				_loadValidasi = $.ajax({
					type: 'GET',
					url: $('#baseUrl').val()+'posr/val_nota/&nota='+result,
					data: $(this).serialize(),
					success: function(data) {
						$('#errorDisplay').html(data);
					},
				});
			}
		}
	}
	
	$('._bayar').keyup(function(e){
		if(e.which === 13){
			$('#simpan').focus();
		}else{
			_total = Number(str_ireplace(",", "", $('#total').val()));
			_tunai = Number(str_ireplace(",", "", $('#tunai').val()));
			_debet = Number(str_ireplace(",", "", $('#debet').val()));
			_flazz = Number(str_ireplace(",", "", $('#flazz').val()));
			
			_totalB  = _tunai + _debet + _flazz;
			_kembali = 0;
			if(_totalB != 0){
				_kembali = _totalB - _total;
			}
			
			if(_kembali < 0){
				_kembali = 0;
			}

			$('#bayar').val(_totalB);
			$('#kembali').val(_kembali);
			setDec();
		}
	})
	
	var _ctrl = false;
	
	_makeBatal = false;
	makeBatalNota = function(){
		_div = '<table class="_tabPopUp">';
		_div+= '<tr>';
		_div+= '<td class="_labPopUp">UserName</td>';
		_div+= '<td class="_isiPopUp"><input type="text" name="_userName" id="_userName" class="_txtName _pop1 _popNav" col="1" autocomplete="off"></td>';
		_div+= '</tr>';
		_div+= '<tr>';
		_div+= '<td class="_labPopUp">Password</td>';
		_div+= '<td class="_isiPopUp"><input type="password" name="_userPass" id="_userPass" class="_txtName _pop2 _popNav" col="2" autocomplete="off"></td>';
		_div+= '</tr>';
		_div+= '<tr>';
		_div+= '<td class="_labPopUp"> </td>';
		_div+= '<td class="_isiPopUp"><input type="text" readonly name="_btnOk" id="_btnOk" class="_btnPopUp _pop3 _popNav" col="3" value="Proses"></td>';
		_div+= '</tr>';
		_div+= '</table>';
		makePopUpNotLoad(400, 150, _div);
		$('._pop1').focus();
		_ctrl = '_batal';
	}
	
	$('._popNav').live("keyup", (function(e){
		if(e.which === 13){
			_col = parseInt($(this).attr('col'));
			_col = _col + 1;
			$('._pop'+_col).focus();
		}
		if(e.which === 38){
			_col = parseInt($(this).attr('col'));
			_col = _col - 1;
			$('._pop'+_col).focus();
		}
	}))
	
	$('#apankFormCloseButton').live("click", (function(e){
		$('#apankForm').html('');
	}))
	_loadPassCek = false;
	$('#_btnOk').live("keyup", (function(e){
		if(e.which === 13){
			_user = $('#_userName').val();
			_pass = $('#_userPass').val();
			if(_user === '' || _pass === ''){
				alert('UserName atau Password tidak boleh kosong');
				$('._pop1').focus();
				return;
			}
			
			_tambah = '';
			if(_ctrl === '_retur'){
				_struk = $('#_userStruk').val();
				if(_struk === ''){
					alert('No. Struk tidak boleh kosong');
					$('._pop1').focus();
					return;
				}else{
					_tambah+= '&struk='+_struk;
				}
			}
			
			if(_loadPassCek != false){
				_loadPassCek.abort();
			}
			_url = $('#baseUrl').val()+'posr/'+_ctrl+'/&user='+_user+'&pass='+_pass+_tambah;
			_loadPassCek = $.ajax({
				type: 'GET',
				url: _url,
				data: $(this).serialize(),
				success: function(data) {
					$('#errorDisplay').html(data);
				},
			});
		}
	}))

	makeRetur = function(_mode){
		_url = $('#baseUrl').val()+'posr/histori/&mode='+_mode;
		makePopUpTop(800, 100, _url);
	}
	
	$('._cariHist').live("keyup", (function(e){
		if(e.which === 37){
			_col = parseInt($(this).attr('col'));
			_col = _col - 1;
			$('._cariHist_'+_col).focus().select();
		}
		if(e.which === 39){
			_col = parseInt($(this).attr('col'));
			_col = _col + 1;
			$('._cariHist_'+_col).focus().select();
		}
		if(e.which === 40){
			makeFocusInNav(1);
		}
		if(e.which === 13){
			_cekMode = $('#modeHist').val();
			if(_cekMode === 'plu'){
				_url = $('#baseUrl').val()+'posr/menu/&mode='+$('#modeHist').val()+'&plu='+$('#cariPlu').val()+'&nama='+$('#cariNama').val()+'&sub='+$('#cariSub').val()+'&rp='+$('#cariRp').val();
			}else if(_cekMode === 'member'){
				_url = $('#baseUrl').val()+'posr/loadmember/&mode='+$('#modeHist').val()+'&kode='+$('#cariKode').val()+'&nama='+$('#cariNama').val()+'&alamat='+$('#cariAlamat').val();
			}else{
				_url = $('#baseUrl').val()+'posr/histori/&mode='+$('#modeHist').val()+'&nojnl='+$('#cariTrans').val()+'&rp='+$('#cariRp').val();
			}
			$('#apankFormContent').html('Loading....');
			$('#apankFormContent').load(_url);
		}
	}))
	
	makeFocusInNav = function(_no){
		_no = parseInt(_no);
		$('._inNavHist'+_no).focus();
		$('._navHist').removeAttr("style");
		$('._navHist_'+_no).css('background-color', 'yellow');
		$('._navHist_'+_no).css('font-weight', 'bold');
	}
	$('._navHist').live("dblclick", (function(){
		makeReturConf($(this).attr('kode'));
		makeFocusInNav(parseInt($(this).attr('cols')));
	}))
	$('._inNavHist').live("keyup", (function(e){
		_max = parseInt($('#_noMaxHist').val());
		_col = parseInt($(this).attr('cols'));
		_nav = false;
		if(e.which === 40){
			_col = _col + 1;
			_nav = true;
		}
		if(e.which === 38){
			_col = _col - 1;
			_nav = true;
		}
		if(e.which === 13){
			makeReturConf($(this).attr('kode'));
		}
		
		if(_nav === true){
			if(_col >= 1 & _col < _max){
				makeFocusInNav(_col);
			}else
			if(_col >= _max){

			}else{
				$('._cariHist_1').focus();
				$('._navHist').removeAttr("style");
			}
		}
	}))
	
	makeReturConf = function(_kode){
		_cekMode = $('#modeHist').val();
		if(_cekMode === 'retur'){
			makeConfirmasiRetur(_kode);
		}
		if(_cekMode === 'cetak'){
			makeCetakUlang(_kode);
		}
		if(_cekMode === 'plu'){
			$('#kd_inv').val(_kode);
			$('#apankForm').html('');
			makeLoadInv();
		}
		if(_cekMode === 'member'){
			$('#member').val(_kode);
			makeLoadMember();
			$('#apankForm').html('');
		}
	}
	makeConfirmasiRetur = function(_kode){
		_cekMode = $('#modeT').val();
		if(_cekMode === 'R'){
			alert('Sudah dalam mode Retur');
			return;
		}
		_div = '<table class="_tabPopUp">';
		_div+= '<tr>';
		_div+= '<td class="_labPopUp">UserName</td>';
		_div+= '<td class="_isiPopUp"><input type="text" name="_userName" id="_userName" class="_txtName _pop1 _popNav" col="1" autocomplete="off"></td>';
		_div+= '</tr>';
		_div+= '<tr>';
		_div+= '<td class="_labPopUp">Password</td>';
		_div+= '<td class="_isiPopUp"><input type="password" name="_userPass" id="_userPass" class="_txtName _pop2 _popNav" col="2" autocomplete="off"></td>';
		_div+= '</tr>';
		_div+= '<tr>';
		_div+= '<td class="_labPopUp">No. Struk</td>';
		_div+= '<td class="_isiPopUp"><input type="text" name="_userStruk" id="_userStruk" class="_txtName _pop3 _popNav" col="3" autocomplete="off" value="'+_kode+'" readonly></td>';
		_div+= '</tr>';
		_div+= '<tr>';
		_div+= '<td class="_labPopUp"> </td>';
		_div+= '<td class="_isiPopUp"><input type="text" readonly name="_btnOk" id="_btnOk" class="_btnPopUp _pop4 _popNav" col="4" value="Proses"></td>';
		_div+= '</tr>';
		_div+= '</table>';
		makePopUpNotLoad(400, 150, _div);
		$('._pop1').focus();
		_ctrl = '_retur';
	}
	
	_loadBayar = false;
	$('#simpan').keyup(function(e){
		if(e.which === 13){
			_total = Number(str_ireplace(",", "", $('#total').val()));
			_tunai = Number(str_ireplace(",", "", $('#tunai').val()));
			_debet = Number(str_ireplace(",", "", $('#debet').val()));
			_flazz = Number(str_ireplace(",", "", $('#flazz').val()));
			_bayar = Number(str_ireplace(",", "", $('#bayar').val()));
			_susuk = Number(str_ireplace(",", "", $('#kembali').val()));

			_cekMode = $('#modeT').val();
			if(_cekMode === 'R'){
				_total =  Math.abs(_total); ;
				_bayar =  Math.abs(_bayar); ;
				_tunai =  Math.abs(_tunai); ;
			}


			if(_total <= 0){
				alert('Total tidak boleh min');
			}else{
				if(_bayar <= 0){
					alert('tidak ada pembayaran');
				}else{
					if(_total > _bayar){
						alert('Total bayar harus lebih besar dari Total belanja');
					}else{
						if(_loadBayar != false){
							_loadBayar.abort();
						}
						
						_url = $('#baseUrl').val()+'posr/bayar/&total='+_total+'&tunai='+_tunai+'&debet='+_debet+'&flazz='+_flazz+'&bayar='+_bayar+'&susuk='+_susuk+'&sub='+$('#kd_sub').val();

						$('#simpan').attr('disabled', true);
						_loadBayar = $.ajax({
							type: 'GET',
							url: _url,
							data: $(this).serialize(),
							success: function(data) {
								$('#simpan').removeAttr('disabled');
								$('#errorDisplay').html(data);
							},
						});
					}
				}
			}
		}
	})

	_loadCetakU = false;
	makeCetakUlang = function(_kode){
		
		_conf = confirm("Yakin mencetak ulang struk?");
		if(_conf){
			_url = $('#baseUrl').val()+'posr/cetak_struk/&nojnl='+_kode;
			
			window.open(_url, '_blank')
			$('#apankForm').html('');
			return;
			$('#simpan').attr('disabled', true);
			if(_loadCetakU != false){
				_loadCetakU.abort();
			}
			$('#apankFormContent').html('Proses Cetak Struk No. '+_kode+' ... ');
			_loadCetakU = $.ajax({
				type: 'GET',
				url: _url,
				data: $(this).serialize(),
				success: function(data) {
					$('#simpan').removeAttr('disabled');
					$('#errorDisplay').html(data);
					$('#apankForm').html('');
				},
			});
		}
		
	}
	
	makeMember = function(){
		$('#member').focus();
	}
	_loadMember = false;
	makeLoadMember = function(){
		$('#simpan').attr('disabled', true);
		if(_loadMember != false){
			_loadMember.abort();
		}
		$('._nameMember').html('Mencari Member ... ');
		_url = $('#baseUrl').val()+'posr/member/&nojnl='+$('#member').val();
		_loadMember = $.ajax({
			type: 'GET',
			url: _url,
			data: $(this).serialize(),
			success: function(data) {
				$('#simpan').removeAttr('disabled');
				$('#errorDisplay').html(data);
				$('#apankForm').html('');
			},
		});
	}
	$('#member').keyup(function(e){
		if(e.which === 13){
			makeLoadMember();
		}
	});
	
	makeBrowseMenu = function(){
		_url = $('#baseUrl').val()+'posr/menu/&mode=plu';
		makePopUpTop(800, 100, _url);
	}
	$('#_kodeCari').click(function(){
		makeLoadInv();
	})
	makeCreateMember = function(){
		conf = confirm("Yakin membuat Member?");
		if(conf){
			window.location = $('#baseUrl').val()+'mcust/index/pos';
		}
	}
	
	$('#_kodeCariCust').click(function(){
		_url = $('#baseUrl').val()+'posr/loadmember/&mode=member';
		makePopUpTop(800, 100, _url);
	})
	
	$('#_histMember').click(function(){
		if($('#kd_sub').val() === ''){
			alert("Tidak ada member");
		}else{
			_url = $('#baseUrl').val()+'posr/histmember/&sub='+$('#kd_sub').val();
			makePopUpTop(800, 100, _url);
		}
	})
})