$(document).ready(function(){
	var divLoc = '';
	_makeLoadDetailApk = false;
	makeLoadDetailApk = function(_div, _url){
		divLoc = _div;
		$(_div).html('<center>Loading...</center>');
		if(_makeLoadDetailApk != false){
			_makeLoadDetailApk.abort();
		}
		_makeLoadDetailApk = $.ajax({
		type: 'GET',
			url: _url,
			data: $(this).serialize(),
			success: function(data) {
				$(divLoc).html(data);
			}
		})
	}
	
	setFormDetApk = function(_nom, _attr, _data){
		
		
		_res = '<div class="_divInsDetApk" id="_divInsDetApk_'+_nom+'">';
		_res+= '<input type="hidden" id="_nomDetClick">';
		
		_res+= '<table>';

		for(_nomAttr = 0; _nomAttr < _attr.length; _nomAttr++){
			
			_idDet = _attr[_nomAttr].id;
			
			if(_data[_nomAttr] === undefined){
				_val = '';
			}else{
				_val = _data[_nomAttr];
			}
			
			_read = '';
			if(_attr[_nomAttr].read === true){
				_read = 'readonly';
			}
			
			_browse = '';
			if(_attr[_nomAttr].browse === true){
				_browse = '<img src="'+$('#baseAssets').val()+'thema/apk2019/images/car_icon.png" class="_cariDetApk _browse_'+_idDet+'" _nom="'+_nom+'" _url="'+_attr[_nomAttr].url+'">';
			}
			
			_res+= '<tr>';
			_res+= '<td class="_divInsDetApkLabel">'+_attr[_nomAttr].label+'</td>';
			_res+= '<td class="_divInsDetApkIsi">: <input type="text" id="'+_idDet+'_'+_nom+'_'+_nomAttr+'" name="'+_attr[_nomAttr].id+'['+_nom+']" class="_formIns '+_attr[_nomAttr].cls+'" style="width: '+_attr[_nomAttr].size+'px" value="'+_val+'" '+_read+'> '+_browse+'</td>';
			_res+= '</tr>';
		}
		
		_res+= '<tr>';
		_res+= '<td><img src="'+$('#baseAssets').val()+'images/close.png" class="_delDetApk" _nom="'+_nom+'"></td>';
		_res+= '</tr>';
		_res+= '</table>';
		
		_res+= '</div>';
		return _res;
	}
	
	makeTambahDetApk = function(_set){
		_res = '<div class="_divDetailApk"><u>'+_set._title+'</u></div>';
		
		_dataDet = _set.data;
		_jumlDet = _dataDet.length;
		
		if(_jumlDet > 0){
			for(_iDetApk = 0; _iDetApk < _jumlDet; _iDetApk++){
				_res+= setFormDetApk(_iDetApk, _set.attr, _dataDet[_iDetApk]);
			}
			_lastNom = _iDetApk - 1;
		}else{
			_res+= setFormDetApk(0, _set.attr, '');
			_lastNom = 0;
		}
		
		_res+= '<div id="_lastDetApk"></div>';
		_res+= '<div class="_divAddDetailApk"><img src="'+$('#baseAssets').val()+'images/add.png" class="_addDetApk"> Tambah Detail</div>';
		
		$(_set._div).html(_res);
		$('#_nomDetApk').val(_lastNom);
		setDec();
		
		$('._divAddDetailApk').live("click", (function(){
			_nomAsl = $('#_nomDetApk').val();
			_nomPak = parseInt(_nomAsl) + 1;
			_res = setFormDetApk(_nomPak, _set.attr, '');
			$('#_lastDetApk').before(_res);
			$('#_nomDetApk').val(_nomPak);
			setDec();
		}))
	}	
	
	$('._delDetApk').live("click", (function(){
		alertApk("_delDetApk", $(this).attr('_nom'), "Yakin menghapus?");
	}))
	
	$('#_apkOke').live("click", (function(){
		_mode = $(this).attr("mode");
		_kode = $(this).attr("kode");
		if(_mode === '_delDetApk'){
			$('#_divInsDetApk_'+_kode).remove();
		}
		$('#apankForm').html('');
	}))
	
	makeAngka = function(){
		$('.decimal').live('keyup', (function(e){
			_idIn  = this.id;
			_val   = $('#'+_idIn).val();
			_jum   = parseInt(_val.length);
			
			if(_jum > 0){
				_exVal = _val.split('');
				_nVal  =  '';
				for(i = 0; i < _jum; i++){
					if(_exVal[i] === '-' || _exVal[i] === '0' || _exVal[i] === '1' || _exVal[i] === '2' || _exVal[i] === '3' || _exVal[i] === '4' || _exVal[i] === '5' || _exVal[i] === '6' || _exVal[i] === '7' || _exVal[i] === '8' || _exVal[i] === '9' || _exVal[i] === '.'){
						_nVal+= _exVal[i];
					}
				}
			}else{

			}
			
			if(_val === ''){
				_nVal = '';
			}
			
			$('#'+_idIn).val(_nVal);
		})).live('change', (function(){
			makeDecimal(this.id);
		})).live('keyup', (function(e){
			if(e.which === 13 || e.which === 37 || e.which === 38 || e.which === 39 || e.which === 40 || e.which === 45){
				makeDecimal(this.id);
			}
		}));
		makeDecimal = function(_idIn){
			_val   = $('#'+_idIn).val();
			_jVal  = _val.split('.');
			if(_jVal.length > 1){
				_dec = _jVal[1].length;
				_dec = parseInt(_dec);
				if(_dec === 0){
					_dec = 2;
				}
				_val = accounting.formatNumber(_val, _dec, ',');
			}else{
				_val = accounting.formatNumber(_val, 0, ',');
			}
			
			$('#'+_idIn).val(_val);
			makeSetRight(_idIn);
		}
		makeSetRight = function(_idIn){
			_style = $('#'+_idIn).attr("style");
			_style = _style+'; text-align: right';
			$('#'+_idIn).attr("style", _style);
		}
	}
	makeAngka();
	
	$('._cariDetApk').live('click', (function(){
		
		_width = parseInt(screen.width) - 30;
		
		makePopUpApkGrid(_width, 100, $(this).attr('_url'));
		$('#_nomDetClick').val($(this).attr('_nom'));
	}))
	
	alertApk = function(_mode, _kode, _pesan){
		
		width = 300;
		_content = '<p>'+_pesan+'</p>';
		_content+= '<p><input type="button" id="_apkOke" class="_apkAlertBtn" mode="'+_mode+'" kode="'+_kode+'" value="Oke"> <input type="button" id="_apkClose" class="_apkAlertBtn" value="Close"></p>';
		
		$('#apankForm').html('<div id="apankForm_1" style="width: '+width+'px;"></div>');
		$('#apankForm_1').html('<div id="apankForm_2" style="margin-top: '+top+'px"></div>');
		$('#apankForm_2').html('<div id="apankForm_3"></div>');
		$('#apankForm_3').html('<div id="apankForm_close"></div>');
		$('#apankForm_close').html('<div id="apankFormContent"></div>');
		$('#apankFormContent').html('<div id="apankFormContent"><div class="_apkAlertContent">'+_content+'</div></div>');
		
		$('#_apkClose').click(function(){
			$('#apankForm').html('');
		})
	}
	
	_loadingDetApk = false;
	makePopUpApkGrid = function(width, top, _url){
		$('#apankForm').html('<div id="apankForm_1" style="width: '+width+'px;"></div>');
		$('#apankForm_1').html('<div id="apankForm_2" style="margin-top: '+top+'px"></div>');
		$('#apankForm_2').html('<div id="apankForm_3"></div>');
		$('#apankForm_3').html('<div id="apankForm_close"></div>');
		$('#apankForm_close').html('<input type="button" id="apankFormCloseButton" title="Close Form"><div id="apankFormContent"></div>');
		$('#apankFormContent').html('<div id="apankFormContent">Loading....</div>');
		
		if(_loadingDetApk != false){
			_loadingDetApk.abort();
		}
		_loadingDetApk = $.ajax({
		type: 'POST',
			url: _url,
			data: $(this).serialize(),
			success: function(data) {
				$('#apankFormContent').html(data);
			}
		})
		
		
		
		$('#apankFormCloseButton').click(function(){
			$('#apankForm').html('');
		})
	}
})
