﻿//檢核民國日期格式
function isCDate(objThis){
	if (objThis.value=="")
		return true;
	var Msg="日期格式輸入錯誤[民國日期格式]!\n\n格式：YYMMDD或YYYMMDD"
	var vdate=(parseInt(objThis.value,10)+19110000)+"";
	var Y=vdate.substring(0,4);
	var M=vdate.substring(4,6);
	if (M>12 || M<1){
		alert(Msg);
		objThis.focus();
		objThis.value=""
		return false;
	}
	var MaxDate=getMaxDate(Y,M);
	var D=vdate.substring(6);
	if (D>MaxDate || D<"01"){
		alert(Msg);
		objThis.focus();
		objThis.value="";
		return false;
	}
	else{
		return true;
	}		
}

//檢核西元日期格式
function isDate(objThis){
	if (objThis.value=="")
		return true;
	var Msg="日期格式輸入錯誤[西元日期格式]!\n\n格式：YYMMDD或YYYMMDD"
	var vdate=objThis.value;
	var Y=vdate.substring(0,4);
	var M=vdate.substring(4,6);
	if (M>12 || M<1){
		alert(Msg);
		objThis.focus();
		objThis.value=""
		return false;
	}
	var MaxDate=getMaxDate(Y,M);
	var D=vdate.substring(6);
	if (D>MaxDate || D<"01"){
		alert(Msg);
		objThis.focus();
		objThis.value="";
		return false;
	}
	else{
		return true;
	}		
}

//檢檢民國年月格式
function checkCYM(objThis){
	if (objThis.value=="")
		return true;
	var Msg="年月格式輸入錯誤[民國日期格式]!\n\n格式：YYMM或YYYMM，且月份必須介於1~12間!"
	var vdate=(parseInt(objThis.value,10)+191100)+"";
	var M=vdate.substring(4,6);

	if (M>12 || M<1){
		alert(Msg);
		objThis.focus();
		objThis.value=""
		return false;
	}
	else
		return true;
}

//檢核西元年月格式
function checkYM(objThis){
	if (objThis.value=="")
		return true;
	var Msg="年月格式輸入錯誤[西元日期格式]!\n\n格式：YYMM或YYYMM，且月份必須介於1~12間!"
	var M=objThis.value.substring(4,6);

	if (M>12 || M<1){
		alert(Msg);
		objThis.focus();
		objThis.value=""
		return false;
	}
	else
		return true;
}

//檢核月份欄位
function checkMonth(objThis){
	if (objThis.value=="")
		return true;
	var Msg="月份必須介於1~12之間!!"
	if (objThis.value>12 || objThis.value<1){
		alert(Msg);
		objThis.focus();
		objThis.value=""
		return false;
	}
	else
		return true;
}

//補０
function leadingZero(vVal,iLen){
	var strVal="";
	strVal=vVal;
	if (strVal.value=="")
		return "";

	var strLead="";
	for (i=strVal.length;i<iLen;i++)
		strLead=strLead+"0";
			
	strVal=strLead+strVal;
	return strVal;
}

//取得某年月的最後一天
function getMaxDate(valYear,valMon){
	var vMDate=new Date;
	var i=31,j,vMaxDate;
	var Y=parseInt(valYear,10);
	var M=parseInt(valMon,10)-1;
	vMDate.setFullYear(Y,M,i);
	j=vMDate.getDate();
	if (i==j)
		vMaxDate=i;
	else
		vMaxDate=i-j;
			
	return vMaxDate;
}


function convertDate(objDate){
	var strDate=(Number(objDate.value)).toString();
	var Y=parseInt(strDate.substr(0,4));
	var M=parseInt(strDate.substr(4,2))-1;
	var D=parseInt(strDate.substr(6,2));
	var newDate=new Date();
	newDate.setFullYear(Y,M,D);	
	return newDate;
}

//Convert The DateTime Value to Chinese Date Type
function dtoC(vDate){
	var YY=LeadingZero((vDate.getFullYear()).toString(),3);
	var MM=LeadingZero((vDate.getMonth()+1).toString(),2);
	var DD=LeadingZero(vDate.getDate().toString(),2);
	return (YY+MM+DD);
}

//The Date Adding Function
function dateAdd(vInterval,vNum,vDate){
	var rtnDate=new Date();
	var YY=vDate.getFullYear();
	var MM=vDate.getMonth();
	var DD=vDate.getDate();
	switch (vInterval){
		case "d":
			DD+=parseInt(vNum);
			break;
		case "m":
			MM+=parseInt(vNum);
			break;
		case "y":
			YY+=parseInt(vNum);
			break;
	}
	rtnDate.setFullYear(YY,MM,DD)
	
	return DtoC(rtnDate);
}

//只能輸入數字(不含小數點)
function numericOnly(){
	if (window.event.keyCode!=8 && (window.event.keyCode<48 || window.event.keyCode>57))
		window.event.keyCode=0;	
}

//只能輸入數值(可含小數點)
function numDecimal(obj){
	if (window.event.keyCode!=8 && window.event.keyCode!=46 && (window.event.keyCode<48 || window.event.keyCode>57) || (window.event.keyCode==46 && (obj.value.indexOf(".")!=-1 || obj.value=="")))
		window.event.keyCode=0;
}

//檢核國民身分證統一編號
function checkIdNo(txtID){
	if (txtID.readOnly==true)
		return true;
	txtID.value=txtID.value.toUpperCase();
	var strIdno=txtID.value;
    if (strIdno=="")
		return true;
    var strChar="ABCDEFGHJKLMNPQRSTUVXYWZIO";
    var strNum="0123456789";
    
    if (strIdno.length!=10)
    {
		alert("身分證字號長度必須等於10!");
		txtID.value="";
		txtID.focus();
		return false;
	}
	
    if (strChar.indexOf(strIdno.substr(0,1))==-1)
    {
        alert("身分證字號第一位必須為英文字母!");
		txtID.value="";
		txtID.focus();
		return false;
	}
	
    if ("12".indexOf(strIdno.substr(1,1))==-1)
    {
        alert("身分證字號第二位必須為1或2!");
		txtID.value="";
		txtID.focus();
		return false;
	}
	
    if ("0123".indexOf(strIdno.substr(2,1))==-1)
    {
        alert("身分證字號第三位必須為0~3間之數字!");
		txtID.value="";
		txtID.focus();
		return false;
	}
	
    for (var i=3;i<=9;i++)
    {
		if (strNum.indexOf(strIdno.substr(i,1))==-1)
		{
	        alert("身分證字號第4~10位必須為數字!");
            txtID.value = "";
			txtID.focus();
			return false;
		}
    }
	
	var strCharValue=(strChar.indexOf(strIdno.substr(0,1))+10).toString();
	var intPartOne=Number(strCharValue.substr(0,1))+Number(strCharValue.substr(1,1))*9+Number(strIdno.substr(9,1));
	var intPartTwo=0;
	for (var j=1;j<=8;j++)
	{
		intPartTwo+=Number(strIdno.substr(j,1))*(9-j);
	}
	if ((intPartOne+intPartTwo)%10!=0)
	{
		if (confirm("身分證字號不符檢查邏輯，\n您確定要輸入嗎?"))
			return true;
		else
		{
            txtID.value = "";
			txtID.focus();
			return false;
		}
	}
	return true;
}

function DownloadRowOpenClose(strTrFile, strImg, strTdLabel, strTdLeft) {
	if (document.getElementById(strTrFile).style.display=='') {
		document.getElementById(strTrFile).style.display = 'none';			
		document.getElementById(strImg).src = "../images/btnPlus.gif";
		if (strTdLabel!='') document.getElementById(strTdLabel).className="DownLoadLabelClose";
		if (strTdLeft!='') document.getElementById(strTdLeft).className="DownLoadLeftClose";
	}
	else {
		document.getElementById(strTrFile).style.display = '';
		document.getElementById(strImg).src = "../images/btnMinus.gif";
		if (strTdLabel!='') document.getElementById(strTdLabel).className="DownLoadLabelOpen";
		if (strTdLeft!='') document.getElementById(strTdLeft).className="DownLoadLeftOpen";
	}
}


