﻿function isCDate(objThis){
	var objDate=objThis;
	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="";
	//		objDate.select();
			return false;
	}
	else
		return true;
}

//年月檢核
function checkYM(objThis){
	var objYM=objThis;
	if (objYM.value=="")
		return true;
	var Msg="";
	if (checkFullStyle(objYM.value)==false)		//(加入全型檢查)
	{
		if (objYM.value.length<4)
			Msg="年月輸入錯誤!\n\n格式：yymm或yyymm";
		else
		{
			var vdate=(parseInt(objYM.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);
		objYM.focus();
		objYM.value=""
		return false;
	}
	else
		return true;

}

function checkYM2(objThis,vLimitMons){
	/*年月限制為一個月內*/
	if(!checkYM(objThis))
		return false;
	var objYM=objThis;
	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(objThis){
	var objMon=objThis;
	if (objMon.value=="")
		return true;
	var Msg="月份必須介於1~12之間!!"
	if (objMon.value>12 || objMon.value<1){
		alert(Msg);
		objMon.focus();
		objMon.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(obj){
    var re=/^[1-9]\d*$/;
    var newval=obj.value+String.fromCharCode(window.event.keyCode);
    if (re.exec(newval)==null) window.event.keyCode=0;
}

function numericOnly2(obj){
	/*可輸入負號*/
	if (window.event.keyCode!=45 && (window.event.keyCode<48 || window.event.keyCode>57) || (window.event.keyCode==45 && obj.value.indexOf("-")!=-1 ))
		window.event.keyCode=0;
	
}

function numDecimal(obj){
	if (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 numberBlur(obj){
	if (obj.value=="")
		return;
    var re=/^\-?\d+\.?\d+$/;
	if (re.exec(obj.value)==null)
	{
	    alert("請輸入數值資枓!");
	    obj.value="";
	    obj.focus();
    }
/*	
	if (isNaN(obj.value) || checkFullStyle(obj.value))
	{
		alert("請輸入數值資料!");
		obj.value="";
		obj.focus();
	}
*/
}
function textint(){

	var intKeyCode=window.event.keyCode;
	alert(intKeyCode);
	if (!(intKeyCode>=48 && intKeyCode<=57))
		window.event.keyCode=8;
}

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 chineseBlur(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))==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));
   }

   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 (strBAN.charAt(6) == '7') {
      if (intMod == 0){
         return true;
      }
      else{
         intSum = intSum + 1;
         intMod = intSum % 10;
         if (intMod == 0){
            return true;
         }
         else{
            strReason = '營利事業統一編號輸入錯誤，請再檢查';
            //alert(strReason);
            return false;
         }
      }
   }
   else{
      if (intMod == 0){
         return true;
      }
      else{
         strReason = '營利事業統一編號輸入錯誤，請再檢查';
         //alert(strReason);
         return false;
      }
   }
}

function btnUpload_Onclick(fname,parentno,seqno,mode)
{
   var pageW=700;
   var pageH=260;
   var openW=190;
   var openH=150;
   if (openW<0)openW=0;
   if (openH<0)openH=0;
     /*if ((pageW+openW)>=screen.width){
      	openW=openW-(pageW+openW-screen.width)-5;}
   if ((pageH+openH)>=screen.height){
	 	openH=openH-(pageH+openH-screen.height)-60;}*/
    var path = fname + '?parentno=' + parentno + '&seqno=' + seqno + '&mode=' + mode;
    window.open(path,'','toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=no,alwaysraised=yes,dependent=yes,top='+openH+'px,left='+openW+'px,width='+pageW+'px,height='+pageH+'px');
}

function openwindow(fname,parentno,seqno,mode)
{
    var pageW=350;
   var pageH=300;
   var openW=350;
   var openH=200;
   if (openW<0)openW=0;
   if (openH<0)openH=0;
     /*if ((pageW+openW)>=screen.width){
      	openW=openW-(pageW+openW-screen.width)-5;}
   if ((pageH+openH)>=screen.height){
	 	openH=openH-(pageH+openH-screen.height)-60;}*/
    var path = fname + '?parentno=' + parentno + '&seqno=' + seqno + '&mode=' + mode;
    //用網頁對話會怪怪的，.net提供的登入元件會無法正常運作
    //showModalDialog(path,'','dialogWidth:'+pageW+'px;dialogHeight:'+pageH+'px;dialogTop:'+openH+'px;dialogLeft:'+openW+'px');
    window.open(path,'','toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=no,alwaysraised=yes,dependent=yes,top='+openH+'px,left='+openW+'px,width='+pageW+'px,height='+pageH+'px');
}

function closewindow()
{
   self.close();
}

function btnCloseUpload_Onclick()
{
    window.opener.document.all.item('hidUploadOK').value='1';
    window.opener.document.all.item('form1').submit();
    self.close();
}
