/*jssrc-SVN-1346(5.5.09)*/
$(function() {
	if ($('#content').height() <= 550){
		$('#page_body').height(585);
	}else{
		$('#page_body').height($('#content').height() + 35);
	}
});

// 主副檔-欄位檢查 
function mode_save_check(path, sval, mode, dform, ERI, mutp) {
	var loary = JSON.parse(return_language_str(path, sval, mode, '', '')),
		msg = '', lentp = false;
	$(dform+' table tr td input').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');
		}
	});
	$(dform+' 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');
		}
	});
	$(dform+' table tr td select').each(function(){
		var tid = $(this);
		tid.css('background-color','#FFFFFF');
	});
	if (lentp) msg += ((loary['ERROR_FIELD_OUT_LENGTH']) ? loary['ERROR_FIELD_OUT_LENGTH'] : 'ERROR_FIELD_OUT_LENGTH')+'\n';
	//---------cust begin------------
	/*if (mode == 'CSORDCORD') {
		// 主檔檢查 
		if ($(dform+' #CUST').length > 0 && $(dform+' #CUST').val().trim() == '') {
			$(dform+' #CUST').css('background-color', '#FF7744');
			msg += ((loary['CSORDCORD1_CUST_NOT_EMPTY']) ? loary['CSORDCORD1_CUST_NOT_EMPTY'] : 'CSORDCORD1_CUST_NOT_EMPTY')+'\n';
		}
		// 副檔1檢查 - 至少新增一筆明細資料 
		if ($(dform+' table#table_CSORDCORD02').length > 0) {
			if ($(dform+' table#table_CSORDCORD02 tr').length < 2) msg += ((loary['PLEASE_ADD_DATA']) ? loary['PLEASE_ADD_DATA'] : 'PLEASE_ADD_DATA')+'\n';
		}
		// 副檔1檢查-變數宣告 
		var DERI = new Array();
		var msgArray = new Array();
		// 副檔1檢查-KEY值取得 
		$(dform+' input[name="CSORDCORD02_DERI[]"]').each( function(i) { DERI[i] = $(this).val(); });
		// 副檔1檢查-迴圈 
		for (var i = 0; i < DERI.length; i++) {
			if ($(dform+' #PROD'+'_'+DERI[i]).length > 0 && $(dform+' #PROD'+'_'+DERI[i]).val().trim() == '') {
				$(dform+' #PROD'+'_'+DERI[i]).css('background-color', '#FF7744');
				msgArray[0] = ((loary['CSORDCORD2_PROD_NOT_EMPTY']) ? loary['CSORDCORD2_PROD_NOT_EMPTY'] : 'CSORDCORD2_PROD_NOT_EMPTY')+'\n';
			}
		}
		// 副檔檢查-錯誤訊息組合 
		for (var i = 0; i < msgArray.length; i++) {
			if (msgArray[i]) msg += msgArray[i];
		}
	}*/
	//---------cust end--------------
	switch (mutp) {
		case 'R': mdir = 'manage'; break;
		case 'B':
		default: mdir = 'modules'; break;
	}
	var url = path+mdir+'/'+mode+'/mask.php?sid='+sval+'&parent='+EncodeURLutf8(path)+'&mode='+mode+'&mutp='+mutp+'&act=check&ERI='+ERI;
	var djson = (fromid_output_json(dform,'name')).replace(/'/g,'\'\'');
	var verlen = djson.length;
	url += '&verlen='+verlen.toString();
	$.ajaxSetup({async:false});
	$.post(url,'htmljson='+EncodeURLutf8(djson),function(m){
		// console.log(m);
		var ret_msg = JSON.parse(m);
		if (ret_msg[0].result == 'false') {
			if (ret_msg[0].idstrs) {
				var strs = ret_msg[0].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];
					$(dform+' '+stid).css('background-color','#FF7744');
				}
			}
			if ((ret_msg[0].cid) && (ret_msg[0].cval)) {
				var cid = ret_msg[0].cid, stid = '';
				if (cid.substr(0,1) == '#' || cid.substr(0,1) == '.') {
					stid = cid;
				} else stid = '#'+cid;
				$(dform+' '+stid).val(ret_msg[0].cval);
			}
			if (ret_msg[0].message) {
				msg += ret_msg[0].message;
			}
		}
	});
	$.ajaxSetup({async:true});
	if (msg.length > 0) {
		alert(msg);
		return false;
	}
	return true;
}

// 子檔-欄位檢查 
function modesub_save_check(path, sval, zmode, mode, sform, ERI, mutp) {
	var loary = JSON.parse(return_language_str(path, sval, mode, '', '')),
		msg = '', lentp = false;
	$(sform+' table tr td input').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 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) msg += ((loary['ERROR_FIELD_OUT_LENGTH']) ? loary['ERROR_FIELD_OUT_LENGTH'] : 'ERROR_FIELD_OUT_LENGTH')+'\n';
	//---------cust begin------------
	/*if (mode == 'CSORDCORD') {
		// 主檔檢查 
		if ($(sform+' #CUST').length > 0 && $(sform+' #CUST').val().trim() == '') {
			$(sform+' #CUST').css('background-color', '#FF7744');
			msg += ((loary['CSORDCORD1_CUST_NOT_EMPTY']) ? loary['CSORDCORD1_CUST_NOT_EMPTY'] : 'CSORDCORD1_CUST_NOT_EMPTY')+'\n';
		}
		// 副檔1檢查 - 至少新增一筆明細資料 
		if ($(sform+' table#table_CSORDCORD02').length > 0) {
			if ($(sform+' table#table_CSORDCORD02 tr').length < 2) msg += ((loary['PLEASE_ADD_DATA']) ? loary['PLEASE_ADD_DATA'] : 'PLEASE_ADD_DATA')+'\n';
		}
		// 副檔1檢查-變數宣告 
		var DERI = new Array();
		var msgArray = new Array();
		// 副檔1檢查-KEY值取得 
		$(sform+' input[name="CSORDCORD02_DERI[]"]').each( function(i) { DERI[i] = $(this).val(); });
		// 副檔1檢查-迴圈 
		for (var i = 0; i < DERI.length; i++) {
			if ($(sform+' #PROD'+'_'+DERI[i]).length > 0 && $(sform+' #PROD'+'_'+DERI[i]).val().trim() == '') {
				$(sform+' #PROD'+'_'+DERI[i]).css('background-color', '#FF7744');
				msgArray[0] = ((loary['CSORDCORD2_PROD_NOT_EMPTY']) ? loary['CSORDCORD2_PROD_NOT_EMPTY'] : 'CSORDCORD2_PROD_NOT_EMPTY')+'\n';
			}
		}
		// 副檔檢查-錯誤訊息組合 
		for (var i = 0; i < msgArray.length; i++) {
			if (msgArray[i]) msg += msgArray[i];
		}
	}*/
	//---------cust end--------------
	switch (mutp) {
		case 'R': mdir = 'manage'; break;
		case 'B':
		default: mdir = 'modules'; break;
	}
	var url = path+mdir+'/'+zmode+'/mask.php?sid='+sval+'&parent='+EncodeURLutf8(path)+'&mode='+mode+'&mutp='+mutp+'&act=subcheck&ERI='+ERI;
	var djson = (fromid_output_json(sform,'name')).replace(/'/g,'\'\'');
	var verlen = djson.length;
	url += '&verlen='+verlen.toString();
	$.ajaxSetup({async:false});
	$.post(url,'htmljson='+EncodeURLutf8(djson),function(m){
		// console.log(m);
		var ret_msg = JSON.parse(m);
		if (ret_msg[0].result == 'false') {
			if (ret_msg[0].idstrs) {
				var strs = ret_msg[0].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[0].message) {
				msg += ret_msg[0].message;
			}
		}
	});
	$.ajaxSetup({async:true});
	if (msg.length > 0) {
		alert(msg);
		return false;
	}
	return true;
}

//FLOW-欄位檢查 
function mode_flowmap_check(path, sval, mode, dform, ERI, mutp) {
	var loary = JSON.parse(return_language_str(path, sval, mode, '', '')),
		msg = '', lentp = false;
	$(dform+' table tr td input').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');
		}
	});
	$(dform+' 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');
		}
	});
	$(dform+' table tr td select').each(function(){
		var tid = $(this);
		tid.css('background-color','#FFFFFF');
	});
	if (lentp) msg += ((loary['ERROR_FIELD_OUT_LENGTH']) ? loary['ERROR_FIELD_OUT_LENGTH'] : 'ERROR_FIELD_OUT_LENGTH')+'\n';
	//---------cust begin------------
	/*if (mode == 'CSORDCORD') {
		// 主檔檢查 
		if ($(dform+' #CUST').length > 0 && $(dform+' #CUST').val().trim() == '') {
			$(dform+' #CUST').css('background-color','#FF7744');
			msg += ((loary['CSORDCORD1_CUST_NOT_EMPTY']) ? loary['CSORDCORD1_CUST_NOT_EMPTY'] : 'CSORDCORD1_CUST_NOT_EMPTY')+'\n';
		}
		// 副檔1檢查 - 至少新增一筆明細資料 
		if ($(dform+' table#table_CSORDCORD02').length > 0) {
			if ($(dform+' table#table_CSORDCORD02 tr').length < 2) msg += ((loary['PLEASE_ADD_DATA']) ? loary['PLEASE_ADD_DATA'] : 'PLEASE_ADD_DATA')+'\n';
		}
		// 副檔1檢查-變數宣告 
		var DERI = new Array();
		var msgArray = new Array();
		// 副檔1檢查-KEY值取得 
		$(dform+' input[name="CSORDCORD02_DERI[]"]').each( function(i) { DERI[i] = $(this).val(); });
		// 副檔1檢查-迴圈 
		for (var i = 0; i < DERI.length; i++) {
			if ($(dform+' #TAXCATE'+'_'+DERI[i]).length > 0 && $(dform+' #TAXCATE'+'_'+DERI[i]).val() == '0' && 
				$(dform+' #INVCATE'+'_'+DERI[i]).length > 0 && 
				$(dform+' #INVCATE'+'_'+DERI[i]).val() != '3' && $(dform+' #INVCATE'+'_'+DERI[i]).val() != '4') {
				if ($(dform+' #INVNO'+'_'+DERI[i]).length > 0 && $(dform+' #INVNO'+'_'+DERI[i]).val().trim() == '') {
					$(dform+' #INVNO'+'_'+DERI[i]).css('background-color','#FF7744');
					msgArray[0] = ((loary['CSORDCORD02_INVNO_NOT_EMPTY']) ? loary['CSORDCORD02_INVNO_NOT_EMPTY'] : 'CSORDCORD02_INVNO_NOT_EMPTY')+'\n';
				} else if ($(dform+' #INVNO'+'_'+DERI[i]).length > 0 && !$(dform+' #INVNO'+'_'+DERI[i]).val().match('^[a-zA-Z]{2}[0-9]{8}$')) {
					$(dform+' #INVNO'+'_'+DERI[i]).css('background-color','#FF7744');
					msgArray[1] = ((loary['CSORDCORD02_INVNO_FORMAT_ERROR']) ? loary['CSORDCORD02_INVNO_FORMAT_ERROR'] : 'CSORDCORD02_INVNO_FORMAT_ERROR')+'\n';
				}
			}
			if ($(dform+' #CMPID'+'_'+DERI[i]).length > 0 && $(dform+' #CMPID'+'_'+DERI[i]).val().trim() == '') {
				msgArray[2] = ((loary['CSORDCORD02_CMPID_NOT_EMPTY']) ? loary['CSORDCORD02_CMPID_NOT_EMPTY'] : 'CSORDCORD02_CMPID_NOT_EMPTY')+'\n';
			}
		}
		// 副檔檢查-錯誤訊息組合 
		for (var i = 0; i < msgArray.length; i++) {
			if (i == 2) continue;
			if (msgArray[i]) msg += msgArray[i];
		}
	}*/
	//---------cust end--------------
	switch (mutp) {
		case 'R': mdir = 'manage'; break;
		case 'B':
		default: mdir = 'modules'; break;
	}
	var url = path+mdir+'/'+mode+'/mask.php?sid='+sval+'&parent='+EncodeURLutf8(path)+'&mode='+mode+'&mutp='+mutp+'&act=mapcheck&ERI='+ERI;
	var djson = (fromid_output_json(dform,'name')).replace(/'/g,'\'\'');
	var verlen = djson.length;
	url += '&verlen='+verlen.toString();
	$.ajaxSetup({async:false});
	$.post(url,'htmljson='+EncodeURLutf8(djson),function(m){
		// console.log(m);
		var ret_msg = JSON.parse(m);
		if (ret_msg[0].result == 'false') {
			if (ret_msg[0].idstrs) {
				var strs = ret_msg[0].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];
					$(dform+' '+stid).css('background-color','#FF7744');
				}
			}
			if (ret_msg[0].message) {
				msg += ret_msg[0].message;
			}
		}
	});
	$.ajaxSetup({async:true});
	if (msg.length > 0) {
		alert(msg);
		return false;
	}
	//---------cust begin------------
	/*if (msgArray[2]) {
		if (confirm(((loary['CSORDCORD02_INVNO_FORMAT_ERROR']) ? loary['CSORDCORD02_INVNO_FORMAT_ERROR'] : 'CSORDCORD02_INVNO_FORMAT_ERROR'))) {
			msgArray[2] = "";
		} else {
			return false;
		}
	}*/
	//---------cust end--------------
	return true;
}