$(document).ready(function() {
	makeGede = function(_id){
		$('#'+_id).live("keyup", function(){
			$('#'+_id).val($('#'+_id).val().toUpperCase());
		})
	}
	makeFungsi = function(_fungsi, _id){
		makeKuduString = function(_id, _string, _jenis){
			_val = $('#'+_id).val().toUpperCase();
			
			_status = false;
			for(_i = 0; _i < _string.length; _i++){
				_kudu = _string[_i].toUpperCase();
				if(_val === _kudu){
					_status = true;
				}
			}
			
			if(_status === true){
			
				if(_jenis === '_cekSide'){
					if(_val === 'C'){
						_val = 'COMB';
					}
					if(_val === 'S'){
						_val = 'SIDE';
					}
				}
			
				$('#'+_id).val(_val);
				_focus = $('#'+_id).attr('bot');
				$('#'+_focus).focus();
			}else{
				$('#'+_id).val('');
				
				function showDiv() {
					$('#'+_id).focus();
					clearInterval(timer);
				}
				
				var timer = setInterval( showDiv, 500);
			}
			makeGede(_id);
		}
		if(_fungsi === '_cekSide'){
			_string = ['C', 'S', 'COMB', 'SIDE', ''];
			makeKuduString(_id, _string, '_cekSide')
		}
		if(_fungsi === '_cekShift'){
			_string = ['1', '2'];
			makeKuduString(_id, _string, false)
		}
		if(_fungsi === '_cekWb'){
			_string = ['W', 'B'];
			makeKuduString(_id, _string, false)
		}
		if(_fungsi === '_cekLeft'){
			_string = ['L', 'R'];
			makeKuduString(_id, _string, false)
		}
		if(_fungsi === '_cekDark'){
			_string = ['D', 'L', 'N', 'O'];
			makeKuduString(_id, _string, false)
		}
		if(_fungsi === '_cekSpecial'){
			_string = ['N', 'S', 'E'];
			makeKuduString(_id, _string, false)
		}
		if(_fungsi === '_cekLeftOver'){
			_string = ['T', 'F'];
			makeKuduString(_id, _string, false)
		}
		if(_fungsi === '_cekType'){
			makePopUp2("Bbt", _id, false);
		}
		if(_fungsi === '_cekClient'){
			makePopUp2("Mcl", _id, false);
		}
		if(_fungsi === '_cekWorker'){
			makePopUp2("Wph", _id, false);
		}
		if(_fungsi === '_cekSupplier'){
			makePopUp2("Msl", _id, false);
		}
		if(_fungsi === '_cekFormat'){
			makePopUp2("Mtf", _id, false);
		}
		if(_fungsi === '_cekSpec'){
			makePopUp2("Spec", _id, false);
		}
		if(_fungsi === '_cekBroken'){
			makePopUp2("Brk", _id, false);
		}
		if(_fungsi === '_cekLenth'){
			makePopUp2("Mtl", _id, false);
		}
		if(_fungsi === '_cekProduct'){
			makePopUp2("Mpd", _id, false);
		}
		if(_fungsi === '_cekTobacco'){
			makePopUp2("Tbc", _id, false);
		}
		if(_fungsi === '_cekDienumber'){
			makePopUp2("Mdn", _id, false);
		}
		if(_fungsi === '_cekMerk'){
			makePopUp2("Merk", _id, false);
		}
		if(_fungsi === '_cekColour'){
			makePopUp2("Mct", _id, false);
		}
		if(_fungsi === '_cekLength'){
			makePopUp2("Mtl", _id, false);
		}
		if(_fungsi === '_cekBale'){
			makePopUp2("Bale", _id, false);
		}
		if(_fungsi === '_cekSection'){
			makePopUp2("Sct", _id, false);
		}
	}
	makeLoadKet = function(_idKet){
		_ctrl  = $('#'+_idKet).attr('ctrl');
		_induk = $('#'+_idKet).attr('induk');
		$('#'+_idKet).val("Load...");
		$.ajax({
		type: "POST",
			url: $('#defaultBaseUrl').val()+_ctrl+"/getFindKet/&kode="+$('#'+_induk).val()+'&field='+$('#'+_idKet).attr('ket'),
			data: $(this).serialize(),
			success: function(data) {
				$('#'+_idKet).val(data);
			}
		})
	}
	makePopUp2 = function(_fungsi, _id, _focs){
		$.ajax({
		type: "POST",
			url: $('#defaultBaseUrl').val()+_fungsi+"/getFind/&kode="+$('#'+_id).val(),
			data: $(this).serialize(),
			success: function(data) {
				if(data === ''){
					if(_focs === 'true'){
						makeDefPopUp(_fungsi, _id, true);
					}else{
						_foc = $('#'+_id).attr('bot');
						makeDefPopUp(_fungsi, _id, _foc);
					}
				}else{
					_afterFunct = $("#"+_id).attr("_afterFunct");
					if(_afterFunct === undefined){
						_foc = $('#'+_id).attr('bot');
						$("#"+_id).val(data);
						$('#'+_foc).focus();
						
						_ketInput = $("#ket"+_id).attr('id');
						if(_ketInput !== undefined){
							makeLoadKet(_ketInput);
						}
					}else{
						// $("#"+_id).focus();
						makeFungsiAfter(_afterFunct, _id);
					}
				}
			}
		})
	}
	makePopUp = function(_fungsi, _id){
		$.ajax({
		type: "GET",
			url: $('#defaultBaseUrl').val()+_fungsi+"/getFind/&kode="+$('#'+_id).val(),
			data: $(this).serialize(),
			success: function(data) {
				if(data === ''){
					if(_fungsi === 'Merk'){
						_foc = $('#'+_id).attr('bot');
						makeDefPopUp('Bbt', _id, _foc);
					}else{
						makeDefPopUp(_fungsi, _id, true);
					}
					$('#'+_id).focus();
				}else{
					$("#"+_id).val(data);
				}
			}
		})
	}
	makeDefPopUp = function(_fungsi, _id, _focus){
		makePopUpTop(500, 70, $('#defaultBaseUrl').val()+_fungsi+"/gridPopUp/&kode="+_id+'&focus='+_focus);
		
		_idD = "#"+_fungsi+"ControllerCari";
		$(_idD).live("submit", (function(e){
			e.preventDefault();
		}));
		
	}
	
	makeStartBale = function(){
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				$('#datein').val($('#tglDefault').val()).focus();
			}
		}).start();
	}
	
	makeFungsiAfter = function(_function, _id){
		if(_function === 'isiBaleTtc'){
			$.ajax({
			type: "GET",
				url: $('#defaultBaseUrl').val()+"/Bale/loadBale/&kode="+$('#'+_id).val(),
				dataType : "json",	
				data : $(this).serializeArray(),
				success : function(obj,status,xhr) {
					$('#format').val(obj.format2);
					$('#type').val(obj.grade);
					
					$('#product').val(obj.product);
					$('#lotnr').val(obj.lotnr);
					$('#orgbale').val(obj.orgbale);
					
					$('#weight').focus();
				}
			})
		}
	}
	
	makeTtc = function(){
		makeChangeFormat2 = function(){
			_lo = $('#check').val().toUpperCase();
			if(_lo === 'T'){
				$('#tobacco2, #dienumber2, #wb2, #client2, #merk2').removeAttr("disabled");
				$('#check').removeAttr("bot").attr('bot', "tobacco2");
				$('#_td_1_1').removeAttr("top").attr('top', "merk2");
			}else{
				$('#tobacco2, #dienumber2, #wb2, #client2, #merk2').attr("disabled", true);
				$('#check').removeAttr("bot").attr('bot', "_td_1_1");
				$('#_td_1_1').removeAttr("top").attr('top', "check");
			}
		}
		makeStartTtc = function(){
			$('#date').val($('#tglDefault').val()).focus();
			makeChangeFormat2();
			$('#check').keyup(function(){
				makeChangeFormat2();
			})
		}
		makeGenKode = function(){
			$.ajax({
			type: "GET",
				url: $('#defaultBaseUrl').val()+"Ttc/getKode/&tgl="+$('#date').val(),
				data: $(this).serialize(),
				success: function(data) {
					$('#nojnl').val(data);
				}
			})
			$.ajax({
			type: "GET",
				url: $('#defaultBaseUrl').val()+"Ttc/getKode2/&tgl="+$('#date').val(),
				data: $(this).serialize(),
				success: function(data) {
					$('#tob').val(data);
				}
			})
		}
		$('#_td_1_1').live("keyup", (function(e){
			if(e.which === 38){
				function showDiv() {
					_top = $("#_td_1_1").attr("top");
					$('#'+_top).focus();
					clearInterval(timer);
				}
				var timer = setInterval( showDiv, 500);
			}
		}))
		makeReadGrid = function(_idTd){
			$('#'+_idTd).attr("readonly", true).removeAttr("class").addClass("inManGrid text inputGrid readonly");
		}
		makeActGrid = function(){
			for(_i = 1; _i <= 4; _i++){
				for(_ii = 1; _ii <= 14; _ii++){
					if(_ii === 4 || _ii === 6 || _ii === 8 || _ii === 11 || _ii === 12 || _ii === 13 || _ii === 14){
						_idTd = "_td_"+_i+"_"+_ii;
						makeReadGrid(_idTd);
					}
				}
				for(_ii = 11; _ii <= 14; _ii++){
					_idTd = "_td_"+_i+"_"+_ii;
					$('#'+_idTd).val('');
				}
			}
			for(_i = 1; _i <= 4; _i++){
				_idTd = "_td_"+_i+"_9";
				_cls = $('#'+_idTd).attr("class");
				$('#'+_idTd).removeAttr("class").addClass('inManGrid text inputGrid meioMask');
				$('#'+_idTd).removeAttr("alt").attr('alt', 'date-us');
			}
			$('.meioMask').setMask();
		}
		
		makeFungsiTtc = function(_funct, _id){
			if(_funct === '_cekTF'){
				makeKuduStr(_id, ['T', 'F'], _funct);
			}
			if(_funct === '_cekDark'){
				makeKuduStr(_id, ['L', 'D', 'N'], _funct);
			}
			if(_funct === '_cekStatus'){
				makeKuduStr(_id, ['E', 'END', ''], _funct);
			}
		}
		makeKuduStr = function(_id, _string, _funct){
			_val = $('#'+_id).val().toUpperCase();
			
			_status = false;
			for(_i = 0; _i < _string.length; _i++){
				_kudu = _string[_i].toUpperCase();
				if(_val === _kudu){
					_status = true;
				}
			}
			
			if(_status === true){
				__val = $('#'+_id).val().toUpperCase();
				$('#'+_id).val(__val);
				
				if(_funct === '_cekDark'){
					_vKet = '';
					if(__val === 'D'){
						_vKet = 'DARK';
					}
					if(__val === 'L'){
						_vKet = 'LIGHT';
					}
					if(__val === 'N'){
						_vKet = 'NORMAL';
					}
					__iid = _id.split('_');
					__iid = '_td_'+__iid[2]+'_6';
					$('#'+__iid).val(_vKet);
				}
				
			}else{
				$('#'+_id).val('');
				
				function showDiv() {
					$('#'+_id).focus();
					clearInterval(timer);
				}
				
				var timer = setInterval( showDiv, 500);
			}
		}
		$('.inManGrid').live("keyup", (function(e){
			_id = this.id;
			_after = $(this).attr("after");
			if(_after !== undefined){
				if(e.which === 13 || e.which === 39){
					makeFungsiTtc(_after, _id);
				}
			}
			
		}))
		$('.inManGrid').live("change", (function(){
			_id = this.id;
			_after = $(this).attr("after");
			if(_after !== undefined){
				makeFungsiTtc(_after, _id);
			}
		}))
		
		$.Shortcuts.add({
			type: "down",
			mask: "A",
			handler: function() {
				makeStartTtc();
				makeGenKode();
				makeActGrid();
			}
		}).start();
		makeFungsiAfter('isiBaleTtc', 'bale');
		makeLoadKet("ketworker");
		
	}
})