// ===================================================
// 宏誌科技股份有限公司 JavaScript 開發函式庫
// Last Update : 2006/08/30
// ===================================================
// ----------------------------------------------------
// 說明 : 顯示提示訊息的 Function
// 作者 : Robby Lee
// 日期 : 2003/05/02
// ----------------------------------------------------
function initHint() {
	document.all.Hint.style.posTop = 200;
	document.all.Hint.style.visibility = 'visible';
	MoveLayer('Hint');
}
function MoveLayer() {
	// 判斷瀏覽器解析度
	var x = screen.width * 0.4;	// 顯示的x方向位置
	var y = 75;									// 顯示的y方向位置

	var diff = (document.body.scrollTop + y - document.all.Hint.style.posTop)*.40;
	var y = document.body.scrollTop + y - diff;
	eval("document.all.Hint.style.posTop = y");
	eval("document.all.Hint.style.posLeft = x");
	setTimeout("MoveLayer('Hint');", 500);		// 500毫秒後再呼叫 MoveLayer()
}

function SubFieldInput(obj, I_Msg, I_chaDataType) {
	if (!obj.readOnly) {
		var numberTest = new NumberFormat(obj.value);
		numberTest.setCommas(true);
		numberTest.setPlaces(0);
		numberTest.setCurrency(false);
		obj.className = 'inputing10';
		if ((I_chaDataType == '') || (I_chaDataType == null)) {
			document.all.Hint.innerHTML = "[ " + I_Msg + " ]";
		} else {
			// 如果訊息不是空的話
			if (obj.value != '') {
				document.all.Hint.innerHTML = "[ " + numberTest.toFormatted() + " ]";
			} else {
				document.all.Hint.innerHTML = "[ " + I_Msg + " ]";
			}
		}
		document.all.Hint.style.background = '#9B2337';
		document.all.Hint.style.display = "block";
		document.all.Hint.style.border = '1px solid black';

	}
}
function SubFieldInputOK(obj, I_chaDataType) {
	if (!obj.readOnly) {
		var numberTest = new NumberFormat(obj.value);
		numberTest.setCommas(true);
		numberTest.setPlaces(0);
		numberTest.setCurrency(false);
		obj.className = 'inputing10';
		if (I_chaDataType == null) {
			document.all.Hint.innerHTML = "";
		} else {
			// 如果訊息不是空的話
			if (obj.value != '') {
				document.all.Hint.innerHTML = "[ " + numberTest.toFormatted() + " ]";
			} else {
				document.all.Hint.innerHTML = "";
			}
		}
		obj.className = 'inputbox10';
		document.all.Hint.style.background = 'transparent';
		document.all.Hint.style.border = '0px solid gray';
	}
}

function SubFieldInputing(obj, I_chaDataType) {
	var numberTest = new NumberFormat(obj.value);
	numberTest.setCommas(true);
	numberTest.setPlaces(0);
	numberTest.setCurrency(false);
	if (I_chaDataType == null) {
		document.all.Hint.innerHTML = "";
		document.all.Hint.style.background = 'transparent';
		document.all.Hint.style.border = '0px solid gray';
	} else {
		// 如果訊息不是空的話
		if (obj.value != '') {
			document.all.Hint.innerHTML = "[ " + numberTest.toFormatted() + " ]";
			document.all.Hint.style.background = '#9B2337';
			document.all.Hint.style.display = "block";
			document.all.Hint.style.border = '1px solid black';
		} else {
			document.all.Hint.innerHTML = "";
			document.all.Hint.style.background = 'transparent';
			document.all.Hint.style.border = '0px solid gray';
		}
	}
}
//////////////////////////////////////////////////////////////////
// Get Browser type
// 作者 : Robinson Jeng
// 日期 : 2002/11/20
// E-Tek. All rights reserved.
//////////////////////////////////////////////////////////////////
function FunGetBrowser(){
	var chaBrowserType = "UnKnown";
	_d=document;
	chaBrowserType=(_d.layers)?"NS4":chaBrowserType;//Netscape4
	chaBrowserType=(navigator.userAgent.indexOf("Gecko")!=-1)?"NS6":chaBrowserType;//Netscape6
	//chaBrowserType=(parseInt(navigator.productSub)>=20010726)?"NS61":chaBrowserType;//Netsccpe6.1
	chaBrowserType=((navigator.userAgent.indexOf("Gecko")!=-1)&&(navigator.userAgent.indexOf("Netscape/7")!=-1))?"NS7":chaBrowserType;//Netscape7
	chaBrowserType=(navigator.appVersion.indexOf("Mac")!=-1)?"MAC":chaBrowserType;//Mac
	chaBrowserType=(navigator.appVersion.indexOf("MSIE 4.5")!=-1)?"MAC45":chaBrowserType;//IE45
	chaBrowserType=(!_d.getElementById&&_d.all)?"IE4":chaBrowserType;//IE4
	chaBrowserType=((navigator.appVersion.indexOf("MSIE 5.0")!=-1||navigator.appVersion.indexOf("MSIE 5.0")!=-1))?"IE50":chaBrowserType;//IE 5.0
	chaBrowserType=((navigator.appVersion.indexOf("MSIE 6.0")!=-1||navigator.appVersion.indexOf("MSIE 5.5")!=-1)&&(navigator.userAgent.indexOf("Opera")!=-1))?"IE55":chaBrowserType;//IE 5.5
	chaBrowserType=(navigator.userAgent.indexOf("Konqueror")!=-1)?"KONQ":chaBrowserType;//Konqueror
	return(chaBrowserType);
}

//==============================
//判斷瀏覽器版本
//==============================
var _browser = FunBrowserVersion();
function FunBrowserVersion(){
	_browser = "unknown";
	_browser = (document.layers)?"NS4":_browser;//Netscape4相容
	_browser = (document.getElementById)?"NS6":_browser;//IE5 Netscape6 Mozilla相容
	_browser = (document.all)?"IE4":_browser;//IE4 IE5相容
	_browser = (window.navigator.userAgent.indexOf("Konqueror")!=-1)?"KOR":_browser;
	return(_browser);
}
//==============================
//取得頁面物件
//==============================
function getObject(I_ID){
	switch(_browser){
		case 'NS4':objReturn = document.layers[I_ID];break;
		case 'IE4':objReturn = document.all[I_ID];break;
		case 'NS6':objReturn = document.getElementById(I_ID);break;
		case 'KOR':objReturn = document.getElementById(I_ID);break;
	}
	return objReturn;
}
// ------------------------------------------------------------------
// 說明 : 載入隱藏Frame
// 作者 : Robby
// 輸入 : URL
// 輸出 : 無
// 範例 :
//  LoadHideFrame('SYSV190.asp');
// 日期 : 2003/04/21
// ------------------------------------------------------------------
function LoadHideFrame(I_chaUrl) {
	parent.hide.window.location.href = I_chaUrl;
}
// ------------------------------------------------------------------
// 說明 : 開啟修改視窗 (V0305002)
// 作者 : Robby
// 輸入 : URL
// 輸出 : 無
// 範例 :
//  OpenUpdWin('SYSV190.asp');
// 日期 : 2003/05/04
// ------------------------------------------------------------------
function OpenUpdWin(I_chaUrl) {
 frmLOV = open( I_chaUrl,
 "winLOV", "scrollbars=1,resizable=1,width=750,height=500" );
 if (frmLOV.opener == null)
 frmLOV.opener = self;
}
// ------------------------------------------------------------------
// 說明 : 取得所要的日期，格式 YYYY/MM/DD
// 作者 : Robby
// 輸入 : 日期, 型態
// 輸出 : 指定日期 (如輸入日期所在月份的最後一天)
// 範例 :
//  FunGetDay('2004/07/12', 'LastDayOfThisMonth');
// 日期 : 2004/07/22
// ------------------------------------------------------------------
function FunGetDay(I_chaDate, I_chaDateType) {

	var arrMonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var chaDate = new Date(I_chaDate);

	intYear = chaDate.getYear();

	if (intYear < 1000) intYear += 1900;

	// 取得今年的第一天
	chaStartOfThisYear = new Date(intYear,0,1);
	// 取得今年的最後一天
	chaEndOfThisYear = new Date(intYear,11,31);

	// 取得前年的第一天
	chaStartOfLastYear = new Date(intYear-1,0,1);
	// 取得前年的最後一天
	chaEndOfLastYear = new Date(intYear-1,11,31);

	// 如果是一月
	if (chaDate.getMonth() == 0) {
		// 前一個月的第一天
 		chaStartOfLastMonth = new Date(intYear-1,11,1);
		// 前一個月的最後一天
	  chaEndOfLastMonth = new Date(intYear-1,11,arrMonthDays[11]);
	} else {
		// 前一個月的第一天
	  chaStartOfLastMonth = new Date(intYear,chaDate.getMonth()-1,1);
	  // 前一個月
	  intLastMonth = chaDate.getMonth()-1;
	  intDaysInLastMonth = arrMonthDays[intLastMonth];
	  // 考慮潤年狀況
	  if (intLastMonth == 1) { // February
	    if (intYear%400==0 || (intYear%4 == 0 && intYear%100!=0) ) intDaysInLastMonth +=1;
	  }
	  // 前一個月的最後一天
	  chaEndOfLastMonth = new Date(intYear, intLastMonth, intDaysInLastMonth);
	}
	intThisMonth = chaDate.getMonth();
	intDaysInThisMonth = arrMonthDays[intThisMonth];
	// 考慮潤年狀況
	if (intThisMonth == 1) { // 如果是二月
	  if (intYear%400==0 || (intYear%4 == 0 && intYear%100!=0) ) intDaysInThisMonth +=1;
	}
	chaEndOfThisMonth = new Date(intYear, intThisMonth , intDaysInThisMonth);
	// alert('上月最後一天 : ' + chaEndOfLastMonth.getYear() + '/' + (chaEndOfLastMonth.getMonth() + 1) + '/' + chaEndOfLastMonth.getDate());
	// alert('本月最後一天 : ' + chaEndOfThisMonth.);

	//if (I_chaDateType == 'LastDayOfThisMonth') {
		chaYear = chaEndOfThisMonth.getYear();
		chaMonth = chaEndOfThisMonth.getMonth() + 1;
		chaDay = chaEndOfThisMonth.getDate();
	//}
	if (chaMonth < 10) chaMonth = '0' + chaMonth;
	if (chaDay < 10) chaDay = '0' + chaDay;
	//alert(chaYear + '/' + chaMonth + '/' + chaDay);
	return chaYear + '/' + chaMonth + '/' + chaDay;
}

////////////////////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串字串中是否包含Unicode文字並進行轉換
// 作著 : Angus Hsu
// 輸入：原始中文字串
// 輸出：經過Unicode轉換的中文字串
// 日期 : 2005/06/03
///////////////////////////////////////////////////////////////////////////
function JsConvertUniCode(I_strOrg){
	var O_strNew = '';
	var intC;

	for(intC=0;intC<I_strOrg.length;intC++){
		if((I_strOrg.substr(intC,1) == '&')&&(I_strOrg.substr(intC+1,1) == '#')){
			O_strNew += String.fromCharCode(I_strOrg.substring(intC+2, I_strOrg.indexOf(";",intC)));
			intC = I_strOrg.indexOf(";",intC);
		}else{
			O_strNew += I_strOrg.substr(intC, 1);
		}
	}
	return O_strNew;
}

// -------------------------------------------------------
// 說明 : 幣值運算(JavaScript)
// 作者 : Jacky, Angus
// 輸入 : 只帶入數值時, 只輸入數字
//				強制更改幣值最小單位及進位方式, 則輸入 "111.25,0.25,1", <%=iniCurrencyUnit%>, <%=iniCurrencyType%>
// 輸出 : 數值
// 範例 :	強制改變幣值最小單位及進位方式
//				intTestNum = JsFormatAmt(intTestNum + ",0.25,1");
//				intTestNum = JsFormatAmt("111.25", <%=iniCurrencyUnit%>, <%=iniCurrencyType%>);
// 日期 : 2006/08/23
// -------------------------------------------------------
function JsFormatAmt(I_Param, I_iniCurrencyUnit, I_iniCurrencyType){
	var indF, intNum, intSubNum, O_intNumber;
	var intCurrencyUnit = -1;
	var intCurrencyType = -1;
	
	var strParam = I_Param + '';
	var arrParam = strParam.split(",");
	for(indF=0;indF<arrParam.length;indF++){
		switch (indF){
			case 0:	intNum = arrParam[indF];
							break;
			case 1: intCurrencyUnit = arrParam[indF];
							break;
			case 2: intCurrencyType = arrParam[indF];
							break;
			default : intNum = "Error";
		}
	}

	if(isNaN(intNum)){
		//如果傳入參數值不是數值, 則不往下處理
	  O_intNumber = 0;
	}else if(intNum == 0){
		//如果傳入參數值為0, 則不往下處理
		O_intNumber = 0;
	}else{
		if(intCurrencyUnit == -1){
			intCurrencyUnit = I_iniCurrencyUnit;
		}
		if(intCurrencyType == -1){
			intCurrencyType = I_iniCurrencyType;
		}
		if(intCurrencyType == 2){
			//進位方式為四捨五入
			intSubNum = (intNum / intCurrencyUnit) - Math.floor(intNum / intCurrencyUnit);
			//如果剩餘的值>=(最小單位/2) 則進位, 否則不進位
			if((intSubNum * intCurrencyUnit) >= (intCurrencyUnit/2)){
				intCurrencyType = 1;
			}else{
				intCurrencyType = 0;
			}
		}
		//回傳數值= Int(傳入數值/幣值最小單位) * 幣值最小單位 + (進位方式<0:捨去,1:進位> * 幣值最小單位)
		O_intNumber = (Math.floor(intNum / intCurrencyUnit)*10 + intCurrencyType*10) * intCurrencyUnit / 10;
	}
	// alert('測試中:I_Param : ' + I_Param + ' / I_iniCurrencyUnit->' + I_iniCurrencyUnit + ' / I_iniCurrencyType->' + I_iniCurrencyType + ' / O_intNumber->' + O_intNumber);
	return O_intNumber;
}

// =====================================
// 以下為 XCOM 共用之檢查性 JavaScript。
// =====================================

////////////////////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串資料是否為合法 IP 格式
// 作著 : Robby Lee, Angus
// 輸入 : 文字方塊物件, 錯誤訊息
// 輸出 : 如果文字方塊物件內的資料格式不符合IP格式，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checkIP(document.forms[0].FrmIP) ) {
//     return false;
// }
// 日期 : 2004/02/16, 2006/08/21
///////////////////////////////////////////////////////////////////////////
function checkIP (InField, I_chaMsg) {

	var chaIPPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var arrIP = InField.value.match(chaIPPattern);
	if (InField.value.length == 0) {
		return true;
	}

	if (InField.value == "0.0.0.0") {
	  alert(JsConvertUniCode('<%=FunML("請注意，IP 位址不能為 0.0.0.0 !", sessLangs)%>'));
	  InField.focus();
	  InField.select();
	  return false;
	} else if (InField.value == "255.255.255.255") {
	  alert(JsConvertUniCode('<%=FunML("請注意，IP 位址不能為 255.255.255.255 !", sessLangs)%>'));
	  InField.focus();
	  InField.select();
	  return false;
	}

	if (arrIP == null) {
	  alert(JsConvertUniCode('<%=FunML("請注意，IP 位址格式錯誤 !",sessLangs)%>'));
	  InField.focus();
	  InField.select();
	  return false;
	} else {
		for (indI = 0; indI < 4; indI++) {
			intSegment = arrIP[indI + 1];
			if (intSegment > 255) {
			  alert(JsConvertUniCode('<%=FunML("請注意，IP 位址格式錯誤 !", sessLangs)%>'));
			  InField.focus();
			  InField.select();
			  return false;
			}
			if (indI == 3) {
				if ( (intSegment == 0) || (intSegment == 255) ) {
			  	alert(JsConvertUniCode('<%=FunML("請注意，IP 位址格式錯誤 !", sessLangs)%>'));
			  	InField.focus();
			  	InField.select();
			  	return false;
			  }
			}
  	}
	}
	return true;
}

////////////////////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串長度是否輸滿
// 作著 : Robby Lee, Angus
// 輸入 : 文字方塊物件, 資料長度
// 輸出 : 如果文字方塊物件內的資料不為設定之長度，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checkStrlen(document.forms[0].FrmStoreID, 5) ) {
//     return false;
// }
// 日期 : 2001/04/16, 2006/08/21
///////////////////////////////////////////////////////////////////////////
function checkStrlen(InField, ChkLen) {
  if (InField.value.length > 0 && InField.value.length != ChkLen) {
  	alert(JsConvertUniCode('<%=FunML("輸入長度須為", sessLangs)%>' + ChkLen + '<%=FunML("碼", sessLangs)%>!'));
  	InField.focus();
  	InField.select();
  	return false;
  }
  return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串是否為空值
// 作著 : Robby Lee, Angus
// 輸入 : 文字方塊物件, 錯誤訊息
// 輸出 : 如果文字方塊物件內的資料為空值，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checknull(document.forms[0].FrmStoreID) ) {
//     return false;
// }
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
	function checknull(InField, I_chaMsg) {
	InField.value = trim(InField.value);
  if (InField.value.length == 0) {
  	if(typeof I_chaMsg != 'undefined') alert(I_chaMsg);
   	else alert(JsConvertUniCode('<%= FunML("注意！剛才那一項不能為空", sessLangs )%>' + ' !!'));
   	if (InField.readOnly == false) {
    	InField.focus();
    	InField.select();
    }
    return false;
  }
  return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串日期不大於等於系統(今天)的日期
// 輸入 : 文字方塊物件
// 輸出 : 如果文字方塊物件內的資料大於等於系統日期，則回傳 false，否則回傳 true。
// 範例 :
// if ( !check_BigDate(document.forms[0].FrmDate) ) {
//     return false;
// }
// 作著 : Kelly, Angus
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
function check_BigDate(InField) {
	chaDate =new Date(InField.value);
	today =new Date();
	var totay_date = today.getYear() + "/" + (today.getMonth()+1) + "/" + today.getDate();
	chaSysDate = new Date(totay_date);
 	if (chaDate.getTime() >= chaSysDate.getTime()) {
    alert(JsConvertUniCode('<%=FunML("注意！日期有誤，輸入日期不能大於等於系統日期", sessLangs)%>'));
    InField.focus();
    InField.select();
    return false;
  }
  return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串日期不得小於等於系統(今天)的日期
// 輸入 : 文字方塊物件
// 輸出 : 如果文字方塊物件內的資料小於系統日期，則回傳 false，否則回傳 true。
// 範例 :
// if ( !check_SmallDate(document.forms[0].FrmDate) ) {
//     return false;
// }
// 作著 : titan, Angus
// 日期 : 2003/03/27, 2006/08/21
////////////////////////////////////////////////////////////
function check_SmallDate(InField) {
	chaDate =new Date(InField.value);
	today =new Date();
	var totay_date = today.getYear() + "/" + (today.getMonth()+1) + "/" + today.getDate();
	chaSysDate = new Date(totay_date);
 	if (chaDate.getTime() < chaSysDate.getTime()) {
    alert(JsConvertUniCode('<%=FunML("注意！日期有誤，輸入日期不能小於系統日期", sessLangs)%>'));
    InField.focus();
    InField.select();
    return false;
  }
  return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串終止日期不得小於起始日期
// 輸入 : 文字方塊物件
// 輸出 : 如果文字方塊物件內的字串終止日期小於起始日期，則回傳 false，否則回傳 true。
// 範例 :
// if ( !check_BetDate(document.forms[0].FrmFromDate,document.forms[0].FrmToDate) ) {
//     return false;
// }
// 作著 : Kelly, Angus
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
function check_BetDate(FromField,ToField) {
	chaFromDate =new Date(FromField.value);
	chaToDate =new Date(ToField.value);
 	if (chaToDate.getTime() < chaFromDate.getTime()) {
    alert(JsConvertUniCode('<%=FunML("注意！日期有誤，終止日期不能小於起始日期", sessLangs)%>'));
    InField.focus();
    InField.select();
    return false;
  }
  return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串是否為數字型態
// 輸入 : 文字方塊物件
// 輸出 : 如果文字方塊物件內的資料不為數字，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checknum(document.forms[0].FrmAmt) ) {
//     return false;
// }
// 作著 : Robby Lee, Angus
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
function checknum(InField){
  if (isNaN(InField.value)) {
    alert(JsConvertUniCode('<%=FunML("注意！此欄位是數字型態！", sessLangs)%>'));
    InField.focus();
    InField.select();
    return false;
  }
  return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串是否為數字型態及其範圍
// 輸入 : 文字方塊物件，設定之最小值，設定之最大值
// 輸出 : 如果文字方塊物件內的資料之數字小於所設定之最小值或大於所設定之最大值，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checknumRange(document.forms[0].FrmAmt, 0, 30000) ) {
//     return false;
// }
// 作著 : Robby Lee, Angus
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
function checknumRange(InField, minNumber, maxNumber){
  if (isNaN(InField.value)) {
    alert(JsConvertUniCode('<%=FunML("注意！此欄位是數字型態！", sessLangs)%>'));
    InField.focus();
    InField.select();
    	return false;
  } else {
  	if (InField.value > maxNumber) {
    	alert(JsConvertUniCode('<%=FunML("注意！此欄位數字太大！", sessLangs)%>'));
    	InField.focus();
    	InField.select();
    	return false;
    } else {
   		if (InField.value < minNumber) {
    		alert(JsConvertUniCode('<%=FunML("注意！此欄位數字太小！", sessLangs)%>'));
    		InField.focus();
    		InField.select();
    		return false;
    	}
    }
  }
  return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串是否為數字型態是否為正整數
// 輸入 : 文字方塊物件
// 輸出 : 如果文字方塊物件內的資料之數字不為正整數，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checknumPositive(document.forms[0].FrmAmt) ) {
//     return false;
// }
// 作著 : Robby Lee, Angus
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
function checknumPositive(InField){
  if (isNaN(InField.value)) {
    alert(JsConvertUniCode('<%=FunML("注意！此欄位是數字型態！", sessLangs)%>'));
    InField.focus();
    InField.select();
    return false;
  } else {
  	if (InField.value < 0) {
    	alert("注意！此欄位數字不能為負值！");
    	InField.focus();
    	InField.select();
    	return false;
    }
  }
  return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串是否為日期型態(YYYY/MM/DD)
// 輸入 : 文字方塊物件
// 輸出 : 如果文字方塊物件內的資料之日期格式不為 YYYY/MM/DD 格式，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checkdate(document.forms[0].FrmDate) ) {
//     return false;
// }
// 作著 : Robby Lee, Angus
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
function checkdate(InField) {

var str_i = InField.value;
var str_p ="YYYY/MM/DD";

p_element = new Array(3);
i_element = new Array(3);
reg_year  = /yyyy|yyy|yy|rr/i;
reg_ycnt  = /y|r/gi;
arr_mon   = ["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
reg_mon   = /Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/i;
reg_month = /mon/i;
reg_s     = /\-|\/|\,|\./;
reg_ss    = /\-|\/|\,|\./g;

if (InField.value.length == 0) {
	return true;
}
else
{
	// 判斷是否為8碼
	if (InField.value.length==8)
	{
		if (isNaN(InField.value)) {
			window.status = '輸入YYYY(西元年)/MM/DD';
		} else {
			datY = str_i.substr(0,4);
			datM = str_i.substr(4,2);
			datD = str_i.substr(6,2);
			datYMD = datY + '/' +  datM + '/' + datD;
			str_i = datYMD;
		}
	}

  tmpDate = str_i.split("/")
  tmpYearLen = tmpDate[0].length
  if ( tmpYearLen - 4 != 0 )
  {
    alert(JsConvertUniCode('<%=FunML("日期需輸入西元年格式", sessLangs)%>!'));
		try{
  		InField.focus();
  		InField.select();
  	}catch(E){
  		null;
  	}
    return false;
  }
  else
  {
    if ( tmpDate[0] - 1911 < 0 )
    {
      alert(JsConvertUniCode('<%=FunML("西元年範圍需大於 1911 年", sessLangs)%>!'));
  		try{
    		InField.focus();
    		InField.select();
    	}catch(E){
    		null;
    	}
      return false;
    }
  }
}

if (str_p.match(reg_month)){
    if (!str_i.match(reg_mon)) {
        alert(JsConvertUniCode('<%=FunML("日期格式錯誤，應為 YYYY(西元年)/MM/DD !", sessLangs)%>'));
    		try{
      		InField.focus();
      		InField.select();
      	}catch(E){
      		null;
      	}
        return false;
    }else{
        for (i=1;i<=12;i++){
            if (str_i.match(new RegExp(arr_mon[i],"i"))){
                str_i = str_i.replace(new RegExp(arr_mon[i],"i"),i);
            }
        }
    }
}
// find seperate
if (str_p.match(reg_s)){
    sep  = str_p.match(reg_s);
    seps = new RegExp("\\"+sep,"g");
    if (str_i.match("\\"+sep) && str_i.match(seps).length == 2){
        p_element=str_p.split(sep);
        i_element=str_i.split(sep);
    }else{
        alert(JsConvertUniCode('<%=FunML("日期格式錯誤，應為 YYYY(西元年)/MM/DD !", sessLangs)%>'));
    		try{
      		InField.focus();
      		InField.select();
      	}catch(E){
      		null;
      	}
        return false;
    }
// no seperate
}else{
    var cnt =0 ,arr_cnt =0;
    while (arr_cnt < 3){
        p_s = str_p.substr(cnt,1);
        p_l = str_p.match(new RegExp(p_s,"gi")).length;
        p_element[arr_cnt] = str_p.substr(cnt,p_l);
        i_element[arr_cnt] = str_i.substr(cnt,p_l);
        arr_cnt = arr_cnt + 1;
        cnt = cnt + p_l;
    }
}
        for (i=0;i<=2;i++){
            if (p_element[i].match(/y|r/i)){
                var yy = i_element[i];
                var reg_y = "\\b\\d{2,"+p_element[i].match(reg_ycnt).length+"}\\b";}
            if (p_element[i].match(/m/i)){
                if (i_element[i].length<2){
                    var mm = "0"+i_element[i];
                }else{
                    var mm=i_element[i];
                }}
            if (p_element[i].match(/d/i)){
                if (i_element[i].length<2){
                    var dd = "0"+i_element[i];
                }else{
                    var dd=i_element[i];
                }}
        }


	if (yy.match(reg_y) && (mm>0 && mm<13) && (dd>0 && dd<32)){
    		if ((mm==4||mm==6||mm==9||mm==11) && dd > 30) {
        		alert(JsConvertUniCode('<%=FunML("該月僅有", sessLangs)%> 30 <%=FunML("天", sessLangs)%> !'));
        		try{
          		InField.focus();
          		InField.select();
          	}catch(E){
          		null;
          	}
        		return false;
    		}else if (mm==2) {
        		if ((parseInt(yy)) % 4 > 0 && dd > 28) {
            		alert(JsConvertUniCode('<%=FunML("該月僅有", sessLangs)%> 28 <%=FunML("天", sessLangs)%> !'));
            		try{
	            		InField.focus();
	            		InField.select();
	            	}catch(E){
	            		null;
	            	}
            		return false;
        		} else if ((parseInt(yy)) % 4 == 0 && dd > 29) {
            		alert(JsConvertUniCode('<%=FunML("該月僅有", sessLangs)%> 29 <%=FunML("天", sessLangs)%> !'));
            		try{
	            		InField.focus();
	            		InField.select();
	            	}catch(E){
	            		null;
	            	}
            		return false;
        		}
    		}else if (dd > 31){
        		alert(JsConvertUniCode('<%=FunML("該月僅有", sessLangs)%> 31 <%=FunML("天", sessLangs)%> !'));
		    		try{
		      		InField.focus();
		      		InField.select();
		      	}catch(E){
		      		null;
		      	}
        		return false;
    		}
				arrDate = str_i.split("/");
				if (arrDate[1].length == 1) {
					arrDate[1] = '0' + arrDate[1];
				}
				if (arrDate[2].length == 1) {
					arrDate[2] = '0' + arrDate[2];
				}
				str_i = arrDate[0] + '/' + arrDate[1] + '/' + arrDate[2];
    		InField.value = str_i;
    		return true;
	}else{
    		alert(JsConvertUniCode('<%=FunML("日期格式錯誤，應為 YYYY(西元年)/MM/DD !", sessLangs)%>'));
    		try{
      		InField.focus();
      		InField.select();
      	}catch(E){
      		null;
      	}
    		return false;
	}
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串是否符合 E-mail 格式
// 輸入 : 文字方塊物件
// 輸出 : 如果文字方塊物件內的資料不符合 Email 格式，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checkemail(document.forms[0].FrmEmail) ) {
//     return false;
// }
// 作著 : Robby Lee, Angus
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
function checkemail(InField) {
	//  ^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$
	//  ^[a-zA-Z0-9_.]+@+[a-zA-Z0-9]+.+[a-zA-Z0-9]?+.+[a-zA-Z0-9]
  //  /^[\w\-\.]+@[\w\-]+(\.\w+)+$/ 
  //  ^[_/.0-9a-z-]+@([0-9a-z][0-9a-z-]+/.)+[a-z]{2,3}$
  //  ^[_a-z0-9-]+(/.[_a-z0-9-]+)*@[a-z0-9-]+(/.[a-z0-9-]+)*$
  //  ^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|com|gov|mil|org|edu|int)$
  //  ^([a-z0-9A-Z]+[-|//.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?//.)+[a-zA-Z]{2,}$
  //  /w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)* 
  //  ^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$
  //  \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
	
	var email = InField.value;
	invalidChars = " /:,;"
	if (email != "") {
		if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(InField.value))){
			alert('E-mail ' + JsConvertUniCode('<%=FunML("格式錯誤", sessLangs)%>!'));
	    InField.focus();
	    return false;
	  }
	  
		for (i=0; i<invalidChars.length; i++) {
			badChar = invalidChars.charAt(i)
			if (email.indexOf(badChar,0) != -1) {
				alert('E-mail ' + JsConvertUniCode('<%=FunML("格式錯誤", sessLangs)%>!'));
	      InField.focus();
	      InField.select();
				return false;
			}
		}
		atPos = email.indexOf("@",1)
		if (atPos == -1) {
			alert('E-mail ' + JsConvertUniCode('<%=FunML("格式錯誤", sessLangs)%>!'));
	    InField.focus();
	    InField.select();
			return false;
		}
		if (email.indexOf("@",atPos+1) != -1) {
			alert('E-mail ' + JsConvertUniCode('<%=FunML("格式錯誤", sessLangs)%>!'));
	    InField.focus();
	    InField.select();
			return false;
		}
		periodPos = email.indexOf(".",atPos)
		if (periodPos == -1) {
			alert('E-mail ' + JsConvertUniCode('<%=FunML("格式錯誤", sessLangs)%>!'));
	    InField.focus();
	    InField.select();
			return false;
		}
		if (periodPos+3 > email.length)	{
			alert('E-mail ' + JsConvertUniCode('<%=FunML("格式錯誤", sessLangs)%>!'));
	    InField.focus();
	    InField.select();
			return false;
		}
	}
	return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串是否為 YYYYMM 型態
// 輸入 : 文字方塊物件
// 輸出 : 如果文字方塊物件內的資料不為 YYYYMM 格式，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checkyearmonth(document.forms[0].FrmYM) ) {
//     return false;
// }
// 作著 : Robby Lee, Angus
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
function checkyearmonth(InField) {
	var str=InField.value
	var filter=/^\d{6}$/
	if (filter.test(str)) {
		if (str.substring(4,6) > 12){
			alert(JsConvertUniCode('<%=FunML("請輸入有效的號碼，應為YYYYMM, 月份應介於 1 - 12", sessLangs)%>'));
      InField.focus();
      InField.select();
			return false;
		}
		return true;
	}
	else {
		alert(JsConvertUniCode('<%=FunML("請輸入有效的號碼，應為YYYYMM，MM 請填兩碼", sessLangs)%>'));
    InField.focus();
    InField.select();
		return false;
	}
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串是否為 HH:MM 型態
// 輸入 : 文字方塊物件
// 輸出 : 如果文字方塊物件內的資料不為 HH:MM 格式，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checkhhmm(document.forms[0].FrmTime) ) {
//     return false;
// }
// 作著 : Robby Lee, Angus
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
function checkhhmm(InField) {
	var str=InField.value
	var filter=/^\d{4}$/
	if (filter.test(str)) {
		if (str.substring(2,4) >= 60){
			alert(JsConvertUniCode('<%=FunML("請輸入有效的時間，應為MM, 分鐘應介於 00 - 59", sessLangs)%>'));
      InField.focus();
      InField.select();
			return false;
		}
		if (str.substring(0,2) >= 24){
			alert(JsConvertUniCode('<%=FunML("請輸入有效的時間，應為HH, 小時應介於 00 - 23", sessLangs)%>'));
      InField.focus();
      InField.select();
			return false;
		}
		return true;
	}
	else {
		alert(JsConvertUniCode('<%=FunML("請輸入有效的時間，應為HHMM，HHMM 請填四碼", sessLangs)%>'));
    InField.focus();
    InField.select();
		return false;
	}
}


////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串是否符合身份證字號格式
// 輸入 : 文字方塊物件
// 輸出 : 如果文字方塊物件內的資料不符合身份證字號格式，則回傳 false，否則回傳 true。
// 範例 :
// if ( !check_personid(document.forms[0].FrmPID) ) {
//     return false;
// }
// 作著 : Robby Lee, Angus
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
function check_personid(InField) {
	var L1, D1, D2, D3, D4, D5, D6, D7, D8, D9;
	var X1, X2;
	var Y;
	var X;
	var t= "ABCDEFGHJKLMNPQRSTUVXYWZIO";

	L1 = InField.value.substring(0,1);
	D = InField.value.substring(2,10);
	D1 = parseInt(InField.value.substring(1,2));
	D2 = parseInt(InField.value.substring(2,3));
	D3 = parseInt(InField.value.substring(3,4));
	D4 = parseInt(InField.value.substring(4,5));
	D5 = parseInt(InField.value.substring(5,6));
	D6 = parseInt(InField.value.substring(6,7));
	D7 = parseInt(InField.value.substring(7,8));
	D8 = parseInt(InField.value.substring(8,9));
	D9 = parseInt(InField.value.substring(9,10));
	c = t.indexOf(L1.toUpperCase());

	if (isNaN(D)) {
    alert(JsConvertUniCode('<%=FunML("身份證格式錯誤，第二至第十個字應為數字", sessLangs)%> !'));
    InField.select();
    InField.focus();
		return false;
	}
	if((InField.value.length != 10) || (c<0)) {
    alert(JsConvertUniCode('<%=FunML("身份證格式錯誤，第一個字應為字母", sessLangs)%> !'));
    InField.select();
    InField.focus();
		return false;
	}
	X = parseInt( c+ 10);
	X1 = Math.floor(parseInt(X) / 10);
	X2 = X - X1 * 10;

	Y = parseInt(X1 + 9 * X2 + 8 * D1 + 7 * D2 + 6 * D3 + 5 * D4 + 4 * D5 + 3 * D6 + 2 * D7 + D8 + D9);

  if ((Y % 10) != 0) {
    alert(JsConvertUniCode('<%=FunML("身份證格式錯誤", sessLangs)%> !'));
    InField.select();
    InField.focus();
    return false;
  }

	return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串是否符合統一編號格式
// 輸入 : 文字方塊物件
// 輸出 : 如果文字方塊物件內的資料不符合統一編號格式，則回傳 false，否則回傳 true。
// 範例 :
// if ( !check_compid(document.forms[0].FrmCID) ) {
//     return false;
// }
// 作著 : Robby Lee, Angus
// 日期 : 2001/04/16, 2006/08/21
////////////////////////////////////////////////////////////
function check_compid(InField) {
	var D1, D2, D3, D4, D5, D6, D7, D8;
	var C1, C2, C3, C4, a1, b1, a2, b2, a3, b3, a4, b4;
	var Y;
	var a5;

  // (a)
	D1 = parseInt(InField.value.substring(0,1));
	D2 = parseInt(InField.value.substring(1,2));
	D3 = parseInt(InField.value.substring(2,3));
	D4 = parseInt(InField.value.substring(3,4));
	D5 = parseInt(InField.value.substring(4,5));
	D6 = parseInt(InField.value.substring(5,6));
	D7 = parseInt(InField.value.substring(6,7));
	D8 = parseInt(InField.value.substring(7,8));

	// (b)
  C1 = parseInt(D1 * 1);
  C2 = parseInt(D3 * 1);
  C3 = parseInt(D5 * 1);
  C4 = parseInt(D8 * 1);
  a1 = Math.floor(parseInt(D2 * 2) / 10);
  b1 = parseInt(D2 * 2) - a1 * 10;
  a2 = Math.floor(parseInt(D4 * 2) / 10);
  b2 = parseInt(D4 * 2) - a2 * 10;
  a3 = Math.floor(parseInt(D6 * 2) / 10);
  b3 = parseInt(D6 * 2) - a3 * 10;
  a4 = Math.floor(parseInt(D7 * 4) / 10);
  b4 = parseInt(D7 * 4) - a4 * 10;

  // (c)
  //if (D7 != 7) {
  	Y = parseInt(C1 + a1 + b1 + C2 + a2 + b2 + C3 + a3 + b3 + C4 + a4 + b4);
  //} else {
  //  a5 = Math.floor(parseInt(a4 + b4) / 10);
  //  Y = parseInt(C1 + a1 + b1 + C2 + a2 + b2 + C3 + a3 + b3 + C4 + a5) ;
  //}
  if ((Y % 10) != 0) {
  	if ( (D7 == '7') && ((Y % 10 == '1') || (Y % 10 == '9')) ) {
  		return true;
  	} else {
    	alert(JsConvertUniCode('<%=FunML("統一編號格式錯誤", sessLangs)%> !'));
    	InField.select();
    	InField.focus();
    	return false;
 		}
  }
	return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查起日不大於迄日
// 輸入 : 開始日期, 結束日期
// 輸出 : 如果開始日期大於結束日期，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checkdatediff('2002/09/01', '2002/09/12') ) {
//     return false;
// }
// 作著 : Robby Lee, Angus
// 日期 : 2002/09/30, 2006/08/21
////////////////////////////////////////////////////////////
function checkdatediff(I_chaBeginDate, I_chaEndDate){
	var chaBeginDate = new Date(I_chaBeginDate);
  var chaEndDate = new Date(I_chaEndDate);

	if(chaBeginDate > chaEndDate){
		alert(JsConvertUniCode('<%=FunML("注意！日期有誤，起日不得大於迄日", sessLangs)%>'));
		return false;
	}
	else{
		return true;
	}
}

////////////////////////////////////////////////////////////
// 說明 : 檢查字串長度是不是大於所設定之長度 (已考慮中文字佔2個bytes)
// 輸入 : 文字方塊物件，設定之最大值
// 輸出 : 如果文字方塊物件內的資料大於所設定之最大值，則回傳 false，否則回傳 true。
// 範例 :
// if ( !checkCMaxStrlen(document.forms[0].FrmCID, 8) ) {
//     return false;
// }
// 作著 : Robby Lee, Angus
// 日期 : 2001/06/21, 2006/08/21
////////////////////////////////////////////////////////////
function checkCMaxStrlen(InField, I_MaxLen) {
 	var intStrLen = 0;
	for (var i = 0; i < InField.value.length; i++) {
		num = InField.value.charCodeAt(i);
		if (num < 0) {
		  intStrLen += 2;
		} else if (num > 127) {
		  intStrLen += 2;
		} else {
		intStrLen++;
		}
	}
	if (intStrLen == 0) {
		return true;
	} else if (intStrLen > I_MaxLen) {
  	alert(JsConvertUniCode('<%=FunML("輸入長度太大 ! 本欄位最大為", sessLangs)%>' + I_MaxLen + '<%=FunML("碼", sessLangs)%>，(<%=FunML("注意一個中文字長度為2", sessLangs)%>)'));
    InField.focus();
    InField.select();
		return false;
	} else {
		return true;
	}
}

////////////////////////////////////////////////////////////
// 說明 : 檢查TextArea的長度 (已考慮中文字佔2個bytes)
// 輸入 : 文字方塊物件，設定之最大值
// 輸出 : 如果區域方塊物件(TEXTAREA)內的資料大於所設定之最大值，則刪除多餘的部份，
//        否則顯示目前輸入長度及剩餘長度。
// 範例 :
// checkTextArea(document.forms[0].FrmMemo, 100);
// 作著 : Robby Lee, Angus
// 日期 : 2003/07/26, 2006/08/21
////////////////////////////////////////////////////////////
function checkTextArea(InField, I_intMaxLen) {
 	var intStrLen = 0;
 	var intCWordNum = 0;  // 中文字字數
 	var intNum = 0;
	for (var indI = 0; indI < InField.value.length; indI++) {
		intNum = InField.value.charCodeAt(indI);
		if (intNum < 0) {
		  intStrLen += 2;
		  if (intStrLen <= I_intMaxLen + 1) intCWordNum += 1;
		} else if (intNum > 127) {
		  intStrLen += 2;
		  if (intStrLen <= I_intMaxLen + 1) intCWordNum += 1;
		} else {
		intStrLen++;
		}
	}
	if (intStrLen > I_intMaxLen) {
		InField.value = InField.value.substr(0, I_intMaxLen - intCWordNum) ;
	}
	if (intStrLen <= I_intMaxLen) {
		document.all.Hint.innerHTML = '<FONT size=2><%=FunML("尚可輸入", sessLangs)%> ' + (I_intMaxLen - intStrLen) + '<%=FunML("個字元，長度限制", sessLangs)%> (' + I_intMaxLen + ')</FONT>';
		document.all.Hint.style.background = '#9B2337';
		document.all.Hint.style.display = "block";
		document.all.Hint.style.border = '1px solid black';
	}
	return ;
}

