/*  */
function modesub_save_detail(path, sval, ltype, zmode, zeri, czeri, feri, sfield, meri, ceri, teri, fow, aetp, mdtype) {
	var fdata = new FormData(),merr = '',lentp = false,
		loary = JSON.parse(return_language_str(path, sval, zmode, '', '')),
		ddiv = '#detail_div_'+zmode+fow,dform = '#detail_form_'+zmode+fow,
		sdiv = '#sub_div_'+zmode+fow,sform = '#sub_form_'+zmode+fow;
	var subphs = $(sdiv+' #SUB_SUBPATH'+fow).val().split('/'),
		tpages = $(sdiv+' #SUB_PAGPATH'+fow).val().split('/'),
		sberis = $(sdiv+' #SUB_ERIPATH'+fow).val().split('/'),
		mjson = (fromid_output_json(dform,'name')).replace(/'/g, "''"),
		sjson = (fromid_output_json(sform,'name')).replace(/'/g, "''");
	fdata.append('mjson', mjson);fdata.append('sjson', sjson);
	var url = path+'core/cw5e_engine_sub.php?sid='+sval+'&parent='+EncodeURLutf8(path)+
			  '&zmode='+zmode+'&zeri='+zeri+'&czeri='+czeri+'&feri='+feri+'&sfield='+sfield+
			  '&aetp='+aetp+'&ltype='+ltype+'&meri='+meri+'&ceri='+ceri+'&teri='+teri+'&fow='+fow+
			  '&mvlen='+(mjson.length).toString()+'&svlen='+(sjson.length).toString()+
			  '&act=subsave';
	$(sform+' table tr td input').each(function(){
		var tid = $(this);
		if (tid.attr('type') == 'button') return;
		tid.css('background-color','#FFFFFF');
		if ((tid.attr('htpe')) && tid.attr('htpe') == 'edNormal' && 
			(tid.attr('fdsize')) && parseInt(tid.attr('fdsize')) < tid.val().length) {
			lentp = true;
			tid.css('background-color', '#FF7744');
		}
	});
	$(sform+' table tr td textarea').each(function(){
		var tid = $(this);
		tid.css('background-color','#FFFFFF');
		if ((tid.attr('htpe')) && tid.attr('htpe') == 'edNormal' && 
			(tid.attr('fdsize')) && parseInt(tid.attr('fdsize')) < tid.val().length) {
			lentp = true;
			tid.css('background-color', '#FF7744');
		}
	});
	$(sform+' table tr td select').each(function(){
		var tid = $(this);
		tid.css('background-color','#FFFFFF');
	});
	if (lentp) {
		merr += ((loary['ERROR_FIELD_OUT_LENGTH']) ? loary['ERROR_FIELD_OUT_LENGTH'] : 'ERROR_FIELD_OUT_LENGTH')+'\n';
		url += '&merrtp=1';
	}
	$.ajax({
		type: 'post',
		url: url,
		data: fdata,
		processData: false,
		contentType: false,
		beforeSend: function(){
			// 開始前執行 
			$(sform+' #loading_'+sfield).show();
		},
		success: function(msg){
			// 成功後執行 console.log(msg);
			var ret_msg = JSON.parse(msg);
			if (ret_msg.result == 'false') {
				if (ret_msg.idstrs) {
					var strs = ret_msg.idstrs.split(','), stid = '';
					for (var i = 0 ; i < strs.length ; i++) {
						if (strs[i].substr(0,1) == '#' || strs[i].substr(0,1) == '.') {
							stid = strs[i];
						} else stid = '#'+strs[i];
						$(sform+' '+stid).css('background-color','#FF7744');
					}
				}
				if (ret_msg.message) {
					merr += ret_msg.message;
				}
				alert(merr);
				$(sform+' #loading_'+sfield).hide();
				return false;
			} else {
				$(sform+' :input').prop('disabled', true);
				if (ltype == 'D' && (ret_msg.steri)) {
					switch (mdtype) {
						case 'M':
							if ($(dform+' #'+sfield+'_MERI').length > 0) {
								$(dform+' #'+sfield+'_MERI').val(ret_msg.steri);
							}
							$(sform+' input').each(function(){
								var tid = $(this).attr('id');
								if ((!tid) || tid.match(/_/g)) return;
								if ($(dform+' #'+tid).length > 0 && ($(this).attr('htpe')) && $(this).attr('htpe') != '') {
									$(dform+' #'+tid).val($(sform+' #'+tid).val());
								}
							});
							break;
						case 'D':
							if ($(dform+' #'+sfield+'_MERI_'+meri).length > 0) {
								$(dform+' #'+sfield+'_MERI_'+meri).val(ret_msg.steri);
							}
							$(sform+' input').each(function(){
								var tid = $(this).attr('id');
								if ((!tid) || tid.match(/_/g)) return;
								if ($(dform+' #'+tid+'_'+meri).length > 0 && ($(this).attr('htpe')) && $(this).attr('htpe') != '') {
									$(dform+' #'+tid+'_'+meri).val($(sform+' #'+tid).val());
								}
							});
							mode_sum_number(path, sval, zmode, tpages[tpages.length-1], dform, fow);
							break;
						default:
							break;
					}
				}
				alert(((loary['DATA_SAVED']) ? loary['DATA_SAVED'] : 'DATA_SAVED')+'!');
				create_sub_detail(path, sval, subphs[subphs.length-1], sberis[sberis.length-1], fow, 'load', subphs[subphs.length-2], tpages[tpages.length-1]);
			}
		},
		error: function(msg){
			// 失敗後執行 
			console.log(msg);
			$(sform+' #loading_'+sfield).hide();
		},
		complete: function(msg){
			// 完成後執行 console.log(msg);
		}
	});
}