/* 按鈕移動控制(按鈕後觸發) */
$(document).on('keyup',function(e){
	var ckary = ['button','color','file','hidden','image','reset','submit'],
		spary = ['checkbox','radio','range'],
		ktp = false, btp = false, num = -1;
	if ((e.which == 13) && (!e.altKey) && (!e.ctrlKey) && (!e.metaKey) &&
		($('#WEB_FOCS_MOVE').length > 0 && $('#WEB_FOCS_MOVE').val() == '1') &&
		($('#WEB_CURR_EDIT').length > 0 && $('#WEB_CURR_EDIT').val() == '1')) {
		if ((!e.target.readOnly) && (!e.target.disabled) &&
			((e.target.tagName == 'INPUT' && ckary.indexOf(e.target.type) == -1) || 
			 e.target.tagName == 'SELECT')) {
			if (e.shiftKey) {
				for (var i=e.target.form.length-1; i >= 0; i--) {
					if ((!ktp) && e.target.outerHTML == e.target.form[i].outerHTML) {
						ktp = true;num = i;
						continue;
					}
					if ((ktp) && (!e.target.form[i].readOnly) && (!e.target.form[i].disabled) &&
						(e.target.form[i].offsetParent != null) &&
						(e.target.form[i].getAttribute('fomv') == '1') &&
						(e.target.closest('.proj_all_ddiv').id == e.target.form[i].closest('.proj_all_ddiv').id) &&
						((e.target.form[i].tagName == 'INPUT' && ckary.indexOf(e.target.form[i].type) == -1) || 
						 e.target.form[i].tagName == 'SELECT')) {
						if (e.target.form[i].id.length > 0) $('#WEB_FOCS_NAME').val('#'+e.target.form[i].id);
						else $('#WEB_FOCS_NAME').val('.'+e.target.form[i].classList[e.target.form[i].classList.length-1]);
						if (e.target.form[i].tagName == 'INPUT' && spary.indexOf(e.target.form[i].type)) e.target.form[i].select();
						else e.target.form[i].focus();
						e.target.form[i].scrollIntoView();
						btp = true;ktp = false;
					}
					if (btp) break;
				}
			} else {
				for (var i=0; i < e.target.form.length; i++) {
					if ((!ktp) && e.target.outerHTML == e.target.form[i].outerHTML) {
						ktp = true;num = i;
						continue;
					}
					if ((ktp) && (!e.target.form[i].readOnly) && (!e.target.form[i].disabled) &&
						(e.target.form[i].offsetParent != null) &&
						(e.target.form[i].getAttribute('fomv') == '1') &&
						(e.target.closest('.proj_all_ddiv').id == e.target.form[i].closest('.proj_all_ddiv').id) &&
						((e.target.form[i].tagName == 'INPUT' && ckary.indexOf(e.target.form[i].type) == -1) || 
						 e.target.form[i].tagName == 'SELECT')) {
						if (e.target.form[i].id.length > 0) $('#WEB_FOCS_NAME').val('#'+e.target.form[i].id);
						else $('#WEB_FOCS_NAME').val('.'+e.target.form[i].classList[e.target.form[i].classList.length-1]);
						if (e.target.form[i].tagName == 'INPUT' && spary.indexOf(e.target.form[i].type)) e.target.form[i].select();
						else e.target.form[i].focus();
						e.target.form[i].scrollIntoView();
						btp = true;ktp = false;
					}
					if ((ktp) && (e.target.form[i].closest('.proj_all_ddiv') != null) &&
						((e.target.closest('.proj_all_ddiv').id != e.target.form[i].closest('.proj_all_ddiv').id) ||
						 (i == e.target.form.length-1)) &&
						(e.target.closest('.proj_all_ddiv').getElementsByClassName('proj_all_icon').length > 0 &&
						 e.target.closest('.proj_all_ddiv').getElementsByClassName('proj_all_icon')[0].getElementsByTagName('a').length > 0 &&
						 e.target.closest('.proj_all_ddiv').getElementsByClassName('proj_all_icon')[0].getElementsByTagName('a')[0].getAttribute('onclick').match('mode_add_detail') != null)) {
						e.target.closest('.proj_all_ddiv').getElementsByClassName('proj_all_icon')[0].getElementsByTagName('a')[0].click();
					}
					if (btp) break;
				}
			}
		} else if ($('#WEB_FOCS_NAME').val() != '' &&
				   $($('#WEB_FOCS_NAME').val()).length > 0) {
			var b = $($('#WEB_FOCS_NAME').val());
				c = $($('#WEB_FOCS_NAME').val()).closest('form');
			if (e.shiftKey) {
				for (var i=c[0].length-1; i >= 0; i--) {
					if ((!ktp) && b[0].outerHTML == c[0][i].outerHTML) {
						ktp = true;num = i;
						continue;
					}
					if ((ktp) && (!c[0][i].readOnly) && (!c[0][i].disabled) &&
						(c[0][i].offsetParent != null) &&
						(c[0][i].getAttribute('fomv') == '1') &&
						(b[0].closest('.proj_all_ddiv').id == c[0][i].closest('.proj_all_ddiv').id) &&
						((c[0][i].tagName == 'INPUT' && ckary.indexOf(c[0][i].type) == -1) || 
						 c[0][i].tagName == 'SELECT')) {
						if (c[0][i].id.length > 0) $('#WEB_FOCS_NAME').val('#'+c[0][i].id);
						else $('#WEB_FOCS_NAME').val('.'+c[0][i].classList[c[0][i].classList.length-1]);
						if (c[0][i].tagName == 'INPUT' && spary.indexOf(c[0][i].type)) c[0][i].select();
						else c[0][i].focus();
						c[0][i].scrollIntoView();
						btp = true;ktp = false;
					}
					if (btp) break;
				}
			} else {
				for (var i=0; i < c[0].length; i++) {
					if ((!ktp) && b[0].outerHTML == c[0][i].outerHTML) {
						ktp = true;num = i;
						continue;
					}
					if ((ktp) && (!c[0][i].readOnly) && (!c[0][i].disabled) &&
						(c[0][i].offsetParent != null) &&
						(c[0][i].getAttribute('fomv') == '1') &&
						(b[0].closest('.proj_all_ddiv').id == c[0][i].closest('.proj_all_ddiv').id) &&
						((c[0][i].tagName == 'INPUT' && ckary.indexOf(c[0][i].type) == -1) || 
						 c[0][i].tagName == 'SELECT')) {
						if (c[0][i].id.length > 0) $('#WEB_FOCS_NAME').val('#'+c[0][i].id);
						else $('#WEB_FOCS_NAME').val('.'+c[0][i].classList[c[0][i].classList.length-1]);
						if (c[0][i].tagName == 'INPUT' && spary.indexOf(c[0][i].type)) c[0][i].select();
						else c[0][i].focus();
						c[0][i].scrollIntoView();
						btp = true;ktp = false;
					}
					if ((ktp) && (c[0][i].closest('.proj_all_ddiv') != null) &&
						((b[0].closest('.proj_all_ddiv').id != c[0][i].closest('.proj_all_ddiv').id) ||
						 (i == c[0].length-1)) &&
						(b[0].closest('.proj_all_ddiv').getElementsByClassName('proj_all_icon').length > 0 &&
						 b[0].closest('.proj_all_ddiv').getElementsByClassName('proj_all_icon')[0].getElementsByTagName('a').length > 0 &&
						 b[0].closest('.proj_all_ddiv').getElementsByClassName('proj_all_icon')[0].getElementsByTagName('a')[0].getAttribute('onclick').match('mode_add_detail') != null)) {
						b[0].closest('.proj_all_ddiv').getElementsByClassName('proj_all_icon')[0].getElementsByTagName('a')[0].click();
					}
					if (btp) break;
				}
			}
			//console.log(c);
		}
		//console.log(e);
	}
});