//==========================================
// 檢核欄位資料正確性
//==========================================
function DataChk(Str,Str1,Num){

var RefString="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (Count=0; Count < Str.value.length; Count++){
TempChar= Str.value.substring (Count, Count+1);
if (RefString.indexOf (TempChar, 0)==-1){
alert('『'+Str1+'』欄位資料只能填入英文或數字喔 !!');
return false;}}

if (Str.value.length<Num){
alert('『'+Str1+'』欄位字元至少要大於等於'+Num+'個字元，請重新輸入 !!');
return false;}

}

//==========================================
// 檢核欄位資料是否為數字
//==========================================
function NumChk(Str1,Str2){

var RefString="1234567890";
for (Count=0; Count < Str1.value.length; Count++){
TempChar= Str1.value.substring (Count, Count+1);

if (RefString.indexOf (TempChar, 0)==-1){
alert('『'+Str2+'』欄位資料只能填入數字喔 !!');
return false;}}

}

//==========================================
// 檢核輸入值是否為Empty
//==========================================
function ChkEmpty(Str){
if (Str.value=='' || Str.value==null) return false;}


//==========================================
// 檢核Email格式是否正確
//==========================================
re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

function EmailChk(Str) {
	if (re.test(Str.value)){
			return true;}
	else{
			return false;}}

//==========================================
// 身分證字號檢核
//==========================================

function IDChk(Str){
	var patrn=/^[a-zA-Z]{1}[0-9]{9}$/;
	if (!patrn.exec(Str.value)) return false
	return true
}


//==========================================
// 密碼檢核4-12字(英文、數字)
//==========================================

function PassChk(Str){
	var patrn=/^[a-zA-Z0-9]{4,12}$/;
	if (!patrn.exec(Str.value)) return false
	return true
}

//==========================================
// 電話檢核
//==========================================

function TelChk(Str){
	var patrn=/^[0]{1}[2-9]{1}[-]{1}[0-9]{7,9}$/;
	if (!patrn.exec(Str.value)) return false
	return true
}

//==========================================
// 行動電話檢核
//==========================================

function CellChk(Str){
	var patrn=/^[0]{1}[9]{1}[0-9]{8}$/;
	if (!patrn.exec(Str.value)) return false
	return true
}
//==========================================
// 開啟視窗;參數(連結,視窗名稱,視窗長度,視窗寬度)
//==========================================
function OpenWin(Url,WinName,Width,Heigth,Top){
window.open(Url,WinName,'menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=yes,width='+ Width +',height='+ Heigth +',top='+ Top +',left='+(screen.width-Width)/2+'');}

//==========================================
// 開啟視窗;參數(連結,視窗名稱,視窗長度,視窗寬度)
//==========================================
function OpenWin2(Url,WinName,Width,Heigth,Top){
window.open(Url,WinName,'menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width='+ Width +',height='+ Heigth +',top='+ Top +',left='+(screen.width-Width)/2+'');}

//==========================================
// 刪除資料(連結,標題)
//==========================================
function DataDel(Url,Title){
if(confirm('■■■■■ 確定刪除此資料 !! ■■■■■\n\n'+Title)){
window.location=Url;}}

//==========================================
// 身分證字號檢核
//==========================================
function VerifyPersonIdno(Idno,ErrMsg){
var D1,D2,Byte1,Byte2,i,Char;
D = new Array();

if (Idno.length != 10){
ErrMsg = "身份證字號長度應為１０碼，請重新輸入 !!\n";return ErrMsg;}   

Byte1=Idno.charAt(0);
switch (Byte1){
    case "A": D1 = 1; D2 = 0; break;
    case "B": D1 = 1; D2 = 1; break;
    case "C": D1 = 1; D2 = 2; break;
    case "D": D1 = 1; D2 = 3; break;
    case "E": D1 = 1; D2 = 4; break;
    case "F": D1 = 1; D2 = 5; break;
    case "G": D1 = 1; D2 = 6; break;
    case "H": D1 = 1; D2 = 7; break;
    case "J": D1 = 1; D2 = 8; break;
    case "K": D1 = 1; D2 = 9; break;
    case "L": D1 = 2; D2 = 0; break;
    case "M": D1 = 2; D2 = 1; break;
    case "N": D1 = 2; D2 = 2; break;
    case "P": D1 = 2; D2 = 3; break;
    case "Q": D1 = 2; D2 = 4; break;
    case "R": D1 = 2; D2 = 5; break;
    case "S": D1 = 2; D2 = 6; break;
    case "T": D1 = 2; D2 = 7; break;
    case "U": D1 = 2; D2 = 8; break;
    case "V": D1 = 2; D2 = 9; break;
    case "X": D1 = 3; D2 = 0; break;
    case "Y": D1 = 3; D2 = 1; break;
    case "W": D1 = 3; D2 = 2; break;
    case "Z": D1 = 3; D2 = 3; break;
    case "I": D1 = 3; D2 = 4; break;
    case "O": D1 = 3; D2 = 5; break;
    default: ErrMsg = "身份證字號 [第１碼] 必須是英文字母,請重新輸入 !!\n";return ErrMsg;}

Byte2 = Idno.charAt(1)
if (Byte2 != 1 && Byte2 != 2){
ErrMsg = "[身份證字號]輸入錯誤，請重新輸入 !!\n";return ErrMsg;}

for (i = 1; i <= 9; i++){
Char = new Number(Idno.charAt(i));
if (isNaN(Char) == true){
ErrMsg = "身份證字號 [第２碼∼第１０碼] 必須是數字，請重新輸入 !!\n";return ErrMsg;}
D[i-1] = Char;}

var CheckSum = D1 * 1 + D2 * 9 + D[0] * 8 + D[1] * 7 + 
               D[2] * 6 + D[3] * 5 + D[4] * 4 + D[5] * 3 + 
               D[6] * 2 + D[7] * 1 + D[8] * 1;
  
if (CheckSum % 10 != 0){
ErrMsg = "[身份證字號]輸入錯誤，請重新輸入 !!\n";return ErrMsg;}
ErrMsg='';return ErrMsg;}

//==========================================
// 強迫變大寫字元
//==========================================
function UpperCase(tagname){
  var InputValue = document.all[tagname].value;
  var MyStr=(new String(InputValue)).toUpperCase();
  document.all[tagname].value = MyStr;}

//==========================================
// JavaScript新農民(大型)
//==========================================

function CalConv(CalType,NowDate)
 {
 FIRSTYEAR = 1998;
 LASTYEAR = 2031;
 var Weekname = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
 today = new Date(NowDate);
 SolarYear = today.getFullYear();
 SolarMonth = today.getMonth() + 1;
 SolarDate = today.getDate();
 Weekday = today.getDay();

 LunarCal = [
  new tagLunarCal( 27,  5, 3, 43, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1 ),
  new tagLunarCal( 46,  0, 4, 48, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ), /* 1999 */
  new tagLunarCal( 35,  0, 5, 53, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ), /* 2000 */
  new tagLunarCal( 23,  4, 0, 59, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
  new tagLunarCal( 42,  0, 1,  4, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
  new tagLunarCal( 31,  0, 2,  9, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
  new tagLunarCal( 21,  2, 3, 14, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), /* 2004 */
  new tagLunarCal( 39,  0, 5, 20, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
  new tagLunarCal( 28,  7, 6, 25, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ),
  new tagLunarCal( 48,  0, 0, 30, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ),
  new tagLunarCal( 37,  0, 1, 35, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 2008 */
  new tagLunarCal( 25,  5, 3, 41, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
  new tagLunarCal( 44,  0, 4, 46, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
  new tagLunarCal( 33,  0, 5, 51, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
  new tagLunarCal( 22,  4, 6, 56, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ), /* 2012 */
  new tagLunarCal( 40,  0, 1,  2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
  new tagLunarCal( 30,  9, 2,  7, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
  new tagLunarCal( 49,  0, 3, 12, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 ),
  new tagLunarCal( 38,  0, 4, 17, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), /* 2016 */
  new tagLunarCal( 27,  6, 6, 23, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ),
  new tagLunarCal( 46,  0, 0, 28, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0 ),
  new tagLunarCal( 35,  0, 1, 33, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ),
  new tagLunarCal( 24,  4, 2, 38, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), /* 2020 */
  new tagLunarCal( 42,  0, 4, 44, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ),
  new tagLunarCal( 31,  0, 5, 49, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ),
  new tagLunarCal( 21,  2, 6, 54, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
  new tagLunarCal( 40,  0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), /* 2024 */
  new tagLunarCal( 28,  6, 2,  5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ),
  new tagLunarCal( 47,  0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 ),
  new tagLunarCal( 36,  0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
  new tagLunarCal( 25,  5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ), /* 2028 */
  new tagLunarCal( 43,  0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 ),
  new tagLunarCal( 32,  0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 ),
  new tagLunarCal( 22,  3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 ) ];


 /* 西曆年每月之日數 */
 SolarCal = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

 /* 西曆年每月之累積日數, 平年與閏年 */
 SolarDays = [
  0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396,
  0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 ];

 AnimalIdx = ["馬 ", "羊 ", "猴 ", "雞 ", "狗 ", "豬 ", "鼠 ", "牛 ", "虎 ", "兔 ", "龍 ", "蛇 " ];
 LocationIdx = [ "南", "東", "北", "西" ];

 if ( SolarYear <= FIRSTYEAR || SolarYear > LASTYEAR ) return 1;

 sm = SolarMonth - 1;
 
 if ( sm < 0 || sm > 11 ) return 2;
 
 leap = GetLeap( SolarYear );

 if ( sm == 1 )
  d = leap + 28;
 else
  d = SolarCal[sm];

 if ( SolarDate < 1 || SolarDate > d ) return 3;

 y = SolarYear - FIRSTYEAR;
 acc = SolarDays[ leap*14 + sm ] + SolarDate;
 kc = acc + LunarCal[y].BaseKanChih;
 Kan = kc % 10;
 Chih = kc % 12;
 Location = LocationIdx[kc % 4];
 Age = kc % 60;
 if ( Age < 22 )
  Age = 22 - Age;
 else
  Age = 82 - Age;

 Age =Age + 3;

if (Age < 10)
  Age=Age+60;

 Animal = AnimalIdx[ Chih ];

 if ( acc <= LunarCal[y].BaseDays ) {
  y--;
  LunarYear = SolarYear - 1;
  leap = GetLeap( LunarYear );
  sm += 12;
  acc = SolarDays[leap*14 + sm] + SolarDate;
  }
 else
  LunarYear = SolarYear;
  
 l1 = LunarCal[y].BaseDays;
 for ( i=0; i<13; i++ ) {
  l2 = l1 + LunarCal[y].MonthDays[i] + 29;
  if ( acc <= l2 ) break;
  l1 = l2;
  }

 LunarMonth = i + 1;
 LunarDate = acc - l1;
 im = LunarCal[y].Intercalation;

 if ( im != 0 && LunarMonth > im ) {
  LunarMonth--;
  if ( LunarMonth == im ) LunarMonth = -im;
  }

 if ( LunarMonth > 12 ) LunarMonth -= 12;

if (CalType==1){
document.write( "<FONT style='BACKGROUND-COLOR: #CC0000' Color='FFFFFF'>&nbsp;今天日期&nbsp;</Font>　西元 <font color=CC0000><B>" + SolarYear + "</B></font> 年 <font color=CC0000><B>" + SolarMonth + "</B></font> 月 <font color=CC0000><B>" + SolarDate + "</B></font> 日　<font color=CC0000>"+ Weekname[Weekday] +"</font>" );
document.write( "　農曆 <font color=FF0099><B>" + LunarMonth + "</B></font> 月 <font color=FF0099><B>" + LunarDate + "</B></font> 日　" );
document.write( "<font color=339900>煞" + Location + "　沖" + Animal + Age + " 歲</font>" ); }
else if(CalType==2){
document.write( "　農曆 <font color=FF0099><B>" + LunarMonth + "</B></font> 月 <font color=FF0099><B>" + LunarDate + "</B></font> 日　" );}
else if(CalType==3){
document.write("<font class=Font_11P Color=000000>農 "+ LunarMonth +"/"+ LunarDate +"</font>");}
else if (CalType==4){
document.write( "<FONT style='BACKGROUND-COLOR: #CC0000' Color='FFFFFF'>&nbsp;今天日期&nbsp;</Font>　西元 <font color=CC0000><B>" + SolarYear + "</B></font> 年 <font color=CC0000><B>" + SolarMonth + "</B></font> 月 <font color=CC0000><B>" + SolarDate + "</B></font> 日　<font color=CC0000>"+ Weekname[Weekday] +"</font>" );
document.write( "　農曆 <font color=FF0099><B>" + LunarMonth + "</B></font> 月 <font color=FF0099><B>" + LunarDate + "</B></font> 日　" );}

 return 0;
 }

 /* 求此西曆年是否為閏年, 返回 0 為平年, 1 為閏年 */
function GetLeap( year )
 {
   if ( year % 400 == 0 )
     return 1;
   else if ( year % 100 == 0 )
     return 0;
   else if ( year % 4 == 0 )
     return 1;
   else
     return 0;
 }

function tagLunarCal( d, i, w, k, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13) {
 this.BaseDays = d;         /* 到西曆 1 月 1 日到農曆正月初一的累積日數 */
 this.Intercalation = i;    /* 閏月月份. 0==此年沒有閏月 */
 this.BaseWeekday = w;      /* 此年西曆 1 月 1 日為星期幾再減 1 */
 this.BaseKanChih = k;      /* 此年西曆 1 月 1 日之干支序號減 1 */
 this.MonthDays = [ m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13 ]; /* 此農曆年每月之大小, 0==小月(29日), 1==大月(30日) */
}

//==========================================
// 年月日程式(符合大月/小月/閏月)
//==========================================
function CalList(year,month,day,daynum){
	var thisDay=new Date(year.value,month.value-1,1);
	var monthDays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var DaysArr=new Array('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31');
	year=thisDay.getYear();
	if(((year%4==0)&&(year%100!=0))||(year%400==0)) monthDays[1]=29;
	nDays=monthDays[thisDay.getMonth()];
	day.length=nDays;
	for(i=0;i<day.length;i++){
		day.options[i].value=DaysArr[i];
		day.options[i].text=DaysArr[i];}
		day.selectedIndex=daynum;
}

//==========================================
// 取的radio值
//==========================================

function CheckRadio(theRadio){
    var theRadioLen = theRadio.length;
    var theRadioValue = false;
    if (theRadioLen == undefined){
        if (theRadio.checked){
            theRadioValue = theRadio.value;
        }
    }else{
        for (theRadioI=0;theRadioI<theRadioLen;theRadioI++){
            if (theRadio[theRadioI].checked){
                theRadioValue = theRadio[theRadioI].value;
                break;
            }
        }
    }
    return theRadioValue;
}



//==========================================
// 結束
//==========================================