﻿function isCDate(objDate){
	var Msg=""
	if (objDate.value=="")
		return true;
	if (checkFullStyle(objDate.value)==false){	
		if (objDate.value.length<6){
			var Msg="日期輸入錯誤!\n\n格式：yymmdd或yyymmdd"
		}
		else
		{		
			var vdate=(parseInt(objDate.value,10)+19110000)+"";
			var Y=vdate.substring(0,4);
			var M=vdate.substring(4,6);
			if (M>12 || M<1){
				var Msg="日期輸入錯誤!\n\n格式：yymmdd或yyymmdd"
			}
			var MaxDate=getMaxDate(Y,M);
			var D=vdate.substring(6);
			if (D>MaxDate || D<"01"){
				var Msg="日期輸入錯誤!\n\n格式：yymmdd或yyymmdd"
			}
		}
	}
	else
		var Msg="日期輸入錯誤!\n\n格式：yymmdd或yyymmdd"
	
	if(Msg!=""){
			alert(Msg);
			objDate.focus();
			objDate.value="";
			return false;
	}
	else
		return true;
}

//時間格式
function isTime(ctl){
    if (ctl.value=="")
        return true;
    var result=true;
    if (ctl.value.length!=4)
        result=false;
    var hr=Number(ctl.value.substr(0,2));
    var ms=Number(ctl.value.substr(2,2));
    if ((hr>23 || hr<0) || (ms<0 || ms>59))
        result=false;
    if (!result){
        alert("時間格式輸入錯誤!");
        ctl.value="";
        ctl.focus();
    }
    return result;
}

//年月檢核
function checkYM(ctl){
	if (ctl.value=="")
		return true;
	var Msg="";
	if (checkFullStyle(ctl.value)==false)		//(加入全型檢查)
	{
		if (ctl.value.length<4)
			Msg="年月輸入錯誤!\n\n格式：yymm或yyymm";
		else
		{
			var vdate=(parseInt(ctl.value,10)+191100)+"";
			var Y=vdate.substring(0,4);
			var M=vdate.substring(4,6);

			if (M>12 || M<1)
				Msg="年月輸入錯誤!\n\n格式：yymm或yyymm";
		}
	}
	else
			Msg="年月輸入錯誤!\n\n格式：yymm或yyymm";
			
	if (Msg!="")
	{
		alert(Msg);
		ctl.focus();
		ctl.value=""
		return false;
	}
	else
		return true;

}

/*
function checkYM2(ctl,vLimitMons){
	//年月限制為一個月內
	if(!checkYM(ctl))
		return false;
	var objYM=ctl;
	if (objYM.value=="")
		return true;
	var vInputYM=(parseInt(objYM.value)+191100);
	var vtoday=dateAdd("m",vLimitMons,new Date());
	var vCurrentYM=vtoday.toString().substring(0,4)+vtoday.toString().substring(4,6);
	var vCurrentCYM=parseInt(vCurrentYM)-191100;
	var Msg="年月輸入錯誤!\n\n年月不可大於"+vCurrentCYM+"";
	if(vInputYM > vCurrentYM) {
		alert(Msg);
		objYM.focus();
		objYM.value=""
		return false;
	}
	else
		return true;
}
*/

function checkMonth(ctl){
	if (ctl.value=="")
		return true;
	var Msg="月份必須介於1~12之間!"
	if (ctl.value>12 || ctl.value<1){
		alert(Msg);
		ctl.focus();
		ctl.value=""
		return false;
	}
	else
		return true;
}

function checkDay(ctl){
	if (ctl.value=="")
		return true;
	var Msg="日期必須介於1~31之間!!"
	if (ctl.value>31 || ctl.value<1){
		alert(Msg);
		ctl.focus();
		ctl.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.toLowerCase()){
		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);
}

//*****欄位輸入限制*****//

//onKeyPress
function checkKeyPress(ctl, reg){
    if (ctl.readOnly||ctl.disabled) return;
    var docSel = document.selection.createRange();
    if (docSel.parentElement().tagName != "INPUT") return false;
    var oSel = docSel.duplicate();
//    var oSel=docSel;
    oSel.text = "";
    var srcRange = ctl.createTextRange();
    oSel.setEndPoint("StartToStart", srcRange);
    var str = oSel.text + String.fromCharCode(window.event.keyCode) + srcRange.text.substr(oSel.text.length);
    if (!reg.test(str))
        window.event.keyCode=0;
}

function transToUpper(){
    window.event.keyCode=String.fromCharCode(window.event.keyCode).toUpperCase().charCodeAt(0);
}

//E-Mail格式(textbox-email)
function emailBlur(ctl){
    re=/^(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)?$/;
    if (re.exec(ctl.value)==null)
    {
        alert("E-Mail帳號格式錯誤!");
        ctl.value="";
        ctl.focus();
    }
}
function emailBlur_en(ctl){
    re=/^(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)?$/;
    if (re.exec(ctl.value)==null)
    {
        alert("E-Mail error!");
        ctl.value="";
        ctl.focus();
    }
}
/*
function numericOnly(obj){
    var re=/^[1-9]\d*$/;
    regularCheck(obj,re);
}
*/
//只能輸入數字(不含小數點)
function numericOnly(){
	if (window.event.keyCode!=8 && (window.event.keyCode<48 || window.event.keyCode>57))
		window.event.keyCode=0;	
}
function numericOnly2(obj){
    var re=/^(\-?\d*)?\d*$/;
    regularCheck(obj,re);
}

function numberBlur(ctl,trim){
//    if (ctl.value.length==0)
//        return true;
    var re=/^(\-?(\d+\.)?\d+)?$/;
	if (re.exec(ctl.value)==null)
	{
	    alert("請輸入數值資枓!");
	    ctl.value="";
	    ctl.focus();
    }
    else if (trim&&ctl.value!=""){
        ctl.value=Number(ctl.value);
    }
}

function chineseBlur(ctl){
    var re=/^[^\000-\255]*$/;
	if (re.exec(ctl.value)==null)
	{
		alert("請輸入中文及全型字元!");
		ctl.value="";
		ctl.focus();
    }
}

function textCheck(){
	var intKeyCode=window.event.keyCode;
	if ((intKeyCode==45 || intKeyCode==46 || (intKeyCode>=48 && intKeyCode<=57) || (intKeyCode>=65 && intKeyCode<=90) || (intKeyCode>=97 && intKeyCode<=122))==false)
		window.event.keyCode=0;
}

function textBlur(obj)
{
	if (obj.value.length==0)
		return;
	for (var i=0;i<=obj.value.length-1;i++){
		if (obj.value.charCodeAt(i)>255)
		{
			alert("請輸入合法的字元資料!");
			obj.value="";
			obj.focus();
			return;
		}
	}
}

function emailCheck(){
	var intKeyCode=window.event.keyCode;
	if ((intKeyCode==46 || intKeyCode==64 || (intKeyCode>=48 && intKeyCode<=57) || (intKeyCode>=65 && intKeyCode<=90) || (intKeyCode>=97 && intKeyCode<=122) || intKeyCode==95)==false)
		window.event.keyCode=0;
}

function checkFullStyle(strVal)
{
	for (var i=0;i<=strVal.length-1;i++){
		if (strVal.charCodeAt(i)>255)
		{
			return true;
		}
	}
	return false;
}

function checkIdNo(txtID){
	if (txtID.readOnly==true)
		return true;
	txtID.value=txtID.value.toUpperCase();
	var strIdno=txtID.value;
	if (!isNaN(strIdno.substr(0,1))&&(strIdno.length==8)){
		if(PF_CheckBAN(strIdno)==false){
			if (confirm("營利事業統一編號不符檢查邏輯!\n您確定要輸入嗎?"))
				return true;
			else
			{
				txtID.value = "";
				txtID.focus();
				return false;
			}
		}
		else{
			return true;
		}
	}
	
    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"+strChar).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;
	var intSecondChr;
	var x;
	for (var j=1;j<=8;j++)
	{
		
		if ((j==1) && (strChar.indexOf(strIdno.substr(1,1))!=-1))	//第二碼為英文,轉換成其對應數值
			intSecondChr=(strChar.indexOf(strIdno.substr(j,1))+10).toString().substr(1,1);
		else
			intSecondChr=strIdno.substr(j,1);
			
		intSecondChr=Number(intSecondChr);
		intPartTwo+=intSecondChr*(9-j);
	}

	if ((intPartOne+intPartTwo)%10!=0)
	{
		if (confirm("身分證字號不符檢查邏輯，\n您確定要輸入嗎?"))
			return true;
		else
		{
            txtID.value = "";
			txtID.focus();
			return false;
		}
	}
	return true;
}


//營利事業統一編號檢核
function PF_CheckBAN(strBAN){
    var intMod;                            //餘數變數
    var intSum;                            //合計數變數
    var intX = new Array(1,2,1,2,1,2,4,1);
    var intY = new Array(7);
    var intCount;                          //計數變數

    if (strBAN.length != 8){
        strReason = '營利事業統一編號必須是８碼';
        alert(strReason);
        return false;
    }

    for (intCount = 0; intCount < 8; intCount++){
        if (strBAN.charAt(intCount) < '0' || strBAN.charAt(intCount) > '9'){
            strReason = '輸入之營利事業統一編號中有非數字';
            alert(strReason);
            return false;
        }
    }

    for (intCount = 0; intCount < 8; intCount++){
        intX[intCount] *= parseInt(strBAN.charAt(intCount));
    }

    //只有1,3,5,6才有可能出現十位數
    intY[0] = parseInt(intX[1] / 10);
    intY[1] = intX[1] % 10;
    intY[2] = parseInt(intX[3] / 10);
    intY[3] = intX[3] % 10;
    intY[4] = parseInt(intX[5] / 10);
    intY[5] = intX[5] % 10;
    intY[6] = parseInt(intX[6] / 10);
    intY[7] = intX[6] % 10;
    
    intSum = intX[0] + intX[2] + intX[4] + intX[7] + intY[0] + intY[1] + intY[2] + intY[3] + intY[4] + intY[5] + intY[6] + intY[7];

    intMod = intSum % 10;

    if (intMod==0){
        return true;
    }
    else{
        if (strBAN.charAt(6) == '7'){       //第七位為 7 者必須再另外處理
            intSum+=1;
            intMod=intSum % 10;
            if (intMod==0)
                return true;
            else
                return false;
        }
        else
            return false;
    }
}

