//Please insert below script into the head of your ASP code.
//<script language="JavaScript" src="/_private/checktool.js"></script>
//--------------------------------------------------------------------
//檢驗欄位是否為空白
//chkLength(l,fieldname)
//--------------------------------------------------------------------
//檢查欄位是否為0
//chk1(l,fieldname)
//--------------------------------------------------------------------
//檢查日期, 呼叫 isValidDate 判斷
//chkDate(yy,mm,dd,fieldname) 
//--------------------------------------------------------------------
//檢查身分證號碼及統一編號, 呼叫 CheckID 判斷
//CheckIDLength(l,fieldname)
//--------------------------------------------------------------------
//檢查欄位是否為數字
//CheckNumber(l,fieldname)
//--------------------------------------------------------------------
//檢查欄位是否為有小數點
//CheckInt(l,fieldname)
//--------------------------------------------------------------------
//檢查Email
//CheckEmail(l,fieldname)
//--------------------------------------------------------------------
//身分證號碼檢查程式
//CheckID(UserID)
//--------------------------------------------------------------------
//日期檢查程式
//isValidDate(varYear,varMonth,varDay)
//--------------------------------------------------------------------

function CheckOrder(){
	if( !chkLength(Trim(document.getElementsByName('mb_name')[0].value),1,10, "姓名") )
		return false;
	else if( !chkMID(Trim(document.getElementsByName('mb_mid')[0].value), "身分證字號") )
		return false;
	else if( !CheckDateDiff(document.getElementsByName('examine_start_date')[0].value, document.getElementsByName('examine_end_date')[0].value) )
		return false;
	else if( !chkLength(Trim(document.getElementsByName('mb_mobile')[0].value),10,15, "手機號碼") )
		return false;
	else if( !chkEmail(Trim(document.getElementsByName('mb_email')[0].value), "電子郵件") )
		return false;
	else
		return true;
	
}

function CheckAllData(){
	if( !chkLength(Trim(document.getElementsByName('mb_id')[0].value),1,12, "會員帳號") )
		return false;
	else if( !chkLength(Trim(document.getElementsByName('mb_passwd')[0].value),4,8, "會員密碼") )
		return false;
	else if( !(document.getElementsByName('mb_passwd')[0].value == document.getElementsByName('mb_confirm')[0].value) )
		return false;
	else if( !chkLength(Trim(document.getElementsByName('mb_name')[0].value),1,10, "姓名") )
		return false;
	else if( !chkMID(Trim(document.getElementsByName('mb_mid')[0].value), "身分證字號") )
		return false;
	else if( !chkLength(Trim(document.getElementsByName('mb_mobile')[0].value),10,15, "手機號碼") )
		return false;
	else if( !chkEmail(Trim(document.getElementsByName('mb_email')[0].value), "電子郵件") )
		return false;
	else
		return true;
}
function CheckEditData(){
	if( !(document.getElementsByName('mb_passwd')[0].value == document.getElementsByName('mb_confirm')[0].value) )
		return false;
	else if( !chkLength(Trim(document.getElementsByName('mb_name')[0].value),1,10, "姓名") )
		return false;
	else if( !chkEmail(Trim(document.getElementsByName('mb_email')[0].value), "電子郵件") )
		return false;
	else if( !chkMID(Trim(document.getElementsByName('mb_mid')[0].value), "身分證字號") )
		return false;
	else
		return true;
}
function CheckGetPass(){
	if( !chkEmail(Trim(document.getElementsByName('email')[0].value), "電子郵件") )
		return false;
	else
		return true;
}

//比較兩個日期的大小
function CheckDateDiff(date1, date2) {
    if ( (Date.parse(date1)).valueOf() > (Date.parse(date2)).valueOf() ) {
        alert('結束日期必須晚於開始日期');
        return false;
    } 
    else if ( (Date.parse(date1)).valueOf() < ( new Date() ).valueOf() ) {
        alert('開始日期不可為今天以前');
        return false;
    } 
    else if ( (Date.parse(date2)).valueOf() < ( new Date() ).valueOf() ) {
        alert('結束日期不可為今天以前');
        return false;
    } 
    else {
        return true;
    }
}

//檢驗電子郵件
function chkMID(l, fieldname) {
	if(chkEmpty(l, fieldname)) {
		if(!( CheckID(l) )) {
			//window.alert("請輸入正確的身分證字號");
			return false;
		}
		return true;
	}
	else
		return false
}
function CheckID(UserID){

   var AreaNo;
   var I;
   var CheckSum;
   var AreaCode;

   UserID = UserID.toUpperCase();
   AreaCode = UserID.charAt(0);

// 確定身分證有10碼
   if ((UserID.length) != 10) {
		alert("身分證字號不正確！");
	 	return false;
	}
// 確定首碼在A-Z之間
   if ((AreaCode < "A") || (AreaCode > "Z")) {
		alert("身分證字號不正確！");
		return false;
	}

// 確定2-10碼是數字
   if (isNaN(parseInt(UserID.substring(1,10)))){
		alert("身分證字號不正確！");
		return false;
	}

// 取得首碼對應的區域碼，A ->10, B->11, ..H->17,I->34, J->18...
   AreaNo = ("ABCDEFGHJKLMNPQRSTUVXYWZIO".indexOf(AreaCode)) + 10;

   UserID = AreaNo.toString() + UserID.substring(1,10);
// 取得CheckSum的值
   CheckSum = parseInt(UserID.charAt(0)) + parseInt(UserID.substring(10,11));
   for (I=2;I<=10;I++){
      CheckSum = CheckSum + parseInt(UserID.substring(I-1,I)) * (11 - I);
   }
   if ((CheckSum % 10) == 0){
		return true;
	}
	else{
		alert("身分證字號不正確！");
		return false;
   }
}


//日期檢查程式
function isValidDate(varYear,varMonth,varDay)
{
var monthNo = parseInt(varMonth,10);
var blnValidDate = 0;

	if ((monthNo<1) || (monthNo>12))
	{
		blnValidDate = 4;
		return(false);
	}

	if ((monthNo==4 || monthNo==6 || monthNo==9 || monthNo==11) && parseInt(varDay,10) >30)
	{
		//alert("Month "+arrmonthChar[month]+" has only 30 days!");
		blnValidDate = 1;
		return(false);
	}
	if ((monthNo==1 || monthNo==3 || monthNo==5 || monthNo==7 || monthNo==8 || monthNo==10 || monthNo==12) && parseInt(varDay,10)>31) {
		blnValidDate = 1;
		return(false);
	}

	if (monthNo == 2)
	{ 
		var year = parseInt(varYear,10);
		var day = parseInt(varDay,10);
		var yearfloat = (year / 4);
		var yearint = parseInt(yearfloat,10);
		var yearCheck = (yearint * 4)
			if(year == yearCheck)
			{
				Check4 = true;
			}
			else
			{
				Check4 = false;
			}

		yearfloat = (year / 100);
		yearint = parseInt(yearfloat,10);
		yearCheck = (yearint * 100)
			if(year == yearCheck)
			{
				Check100 = true;
			}
			else
			{
				Check100 = false;
			}

		yearfloat = (year / 400);
		yearint = parseInt(yearfloat,10);
		yearCheck = (yearint * 400)
			if(year == yearCheck)
			{
				Check400 = true;
			}
			else
			{
				Check400 = false;
			}

		var CheckLeapYear = ((Check4 == true) && (Check100 != true) || (Check400 == true))
	
			if(CheckLeapYear == true)
			{
				if(day > 29)
				{
					//alert("February in "+ year +" has only 29 days!");						
					blnValidDate = 2;
					return(false);
				}
			}
			
			if(CheckLeapYear == false)
			{
				if(day > 28)
				{
					//alert("February in "+ year +" has only 28 days!");						
					blnValidDate = 2;
					return(false);
				}
			}	
	 }
	 return(true);
}

//檢驗電子郵件
function chkEmail(l, fieldname) {
	if(chkEmpty(l, fieldname)) {
		if(!( isEmail(l) )) {
			window.alert("請輸入正確格式的電子郵件");
			return false;
		}
		return true;
	}
	else
		return false
}

//檢驗兩個欄位是否相同
function chkSamePass(l1, l2) {
	if(!(l1 == l2 )) {
		window.alert("請輸入相同的確認密碼");
		return false;
	}
	return true;
}

//檢驗欄位是否為在多少長度內
function chkLength(l,l1,l2,fieldname) {
	if(chkEmpty(l, fieldname)) {
		if(!(l.length >= l1 && l.length <= l2 )) {
			window.alert(fieldname+"欄位超出字串長度範圍");
			return false;
		}
		return true;
	}
	else
		return false;
}

//檢驗欄位是否為空白
function chkEmpty(l,fieldname) {
if(!(l.length > 0 )) {
	window.alert(fieldname+"欄位不能空白")
	return false
}
return true
}

//檢查欄位是否為0
function chk1(l,fieldname) {
if(l.value=="0") {
	window.alert(fieldname+"欄位不能空白")
	return false
}
return true
}

//檢查日期
function chkDate(yy,mm,dd,fieldname) {
if (!isValidDate(yy,mm,dd)) {
	window.alert(fieldname+"欄位錯誤")
	return false
}
return true
}

//檢查身分證號碼及統一編號
function CheckIDLength(l,fieldname) {
alert(l);
if (l.length > 0 ) {
	if ((l.length !=8 ) && (l.length !=10 )) {
		alert(fieldname+"欄位長度不正確");
		return false;
	}
	if (l.length ==10 ) {
		if (!CheckID(l)) {
			return false;
		}
	}
}
return true;
};

//檢查欄位是否為數字
function CheckNumber(l,fieldname) {
if (isNaN(l)) {
	window.alert(fieldname+"欄位不是數字")
	return false
	}
return true
}

//檢查Email
function CheckEmail(l,fieldname){
if (!isEmail(l)) {
	window.alert(fieldname+"欄位不正確");
	return false;
}
return true;
}

function isEmail (l)
{   if (l.length==0) return false;
   
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = l.length;

    // look for @
    while ((i < sLength) && (l.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (l.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (l.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (l.charAt(i) != ".")) return false;
    else return true;
}

function CheckInt(l,fieldname){

if (!isInt(l)) {
	window.alert(fieldname+"不可具有小數位數")
	return false
}
return true
}

function isInt(l){
	if (l.length==0){
		return true;
	}
	
   	var i = 1;
    var sLength = l.length;

     while ((i < sLength) && (l.charAt(i) != ".")){
	     i=i+1
	}
		
	 if (i != sLength){
		return false;
	}
	else{
		return true;
	}
	
}


//---- 除去右邊空白字串 ----
function LTrim(str){
	var whitespace = new String(" \t\n\r");
	var s = new String(str);

	if (whitespace.indexOf(s.charAt(0)) != -1) {
	    var j=0, i = s.length;
	    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
	        j++;
	    s = s.substring(j, i);
	}

	return s;
}

//---- 除去左邊空白字串 ----
function RTrim(str){
    var whitespace = new String(" \t\n\r");

    var s = new String(str);

    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
        var i = s.length - 1;   
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;
        s = s.substring(0, i+1);
    }

    return s;
}

//---- 除去左右邊空白字串 ----
function Trim(str){
    return RTrim(LTrim(str));
}

//檢查字串裡不可有 vchar 字元
function isCharInString(fieldname,fieldshow,vchar)	//if (! isCharInString(fieldname,fieldshow,char)){return false;}
{
	var i = 0;
	var fieldvalue=fieldname.value;
	var sLength = fieldvalue.length;

	if (sLength==0) return true;
	while ((i < sLength) && (fieldvalue.charAt(i) != vchar))
	{
		i=i+1
	}
	if (i != sLength)
	{
		alert(fieldshow+"不可存在 '"+vchar+"'");
		fieldname.focus();
		return false;
	}
	else
	{
		return true;
	}
}



//New檢查身分證號碼及統一編號
function NewCheckIDLength(fieldname,fieldshow)	//if (! CheckIDLength(form1.Tmc_bill_no,"fdsgsrggr")){return false;}
{
	var fieldvalue=fieldname.value;

	if (Trim(fieldvalue)!="")
	{
		if (! isCharInString(fieldname,fieldshow,".")){return false;}
		else if (! isCharInString(fieldname,fieldshow,"-")){return false;}
		else if ((fieldvalue.length !=8) && (fieldvalue.length !=10))
		{
			window.alert(fieldshow+"欄位長度不正確");
			fieldname.focus();
			return false;
		}
		else if (fieldvalue.length==10)
		{
			if (! CheckID(fieldvalue))
			{
				return false;
			}
		}
		else if (isNaN(fieldvalue))
		{
			alert(fieldshow+"應為數字");
			fieldname.focus();
			return false;
		}
	}
	return true;
}

function ym_onchange(yy,mm,dd)	{
	
	var month, year, day ;
	day = dd.selectedIndex ; 
	month = mm.options[mm.selectedIndex].value ;
	year = yy.options[yy.selectedIndex].value ;
	if (month == "1" || month == "3" || month == "5" || month == "7" || month == "8" || month == "10" || month == "12")	{
		for(i=1;i<=31;i++) {
			dd.options[i-1] = new Option(i);
			dd.options[i-1].value = i;
		}
		dd.length = "31" ;
	}
	if (month == "4" || month == "6" || month == "9" || month == "11")	{
		for(i=1;i<=30;i++) {
			dd.options[i-1] = new Option(i);
			dd.options[i-1].value = i ;
		}
		dd.length = "30" ;
	}
	if (month == "2") {
		if (year%4 > 0)	{
			for(i=1;i<=28;i++) {
				dd.options[i-1] = new Option(i);
				dd.options[i-1].value = i;
			}
			dd.length = "28" ;
		}
		if (year%4 == 0)	{
			if ((year%100 == 0) && (year%400 > 0))	{
				for(i=1;i<=28;i++) {
					dd.options[i-1] = new Option(i);
					dd.options[i-1].value = i;
				}
				dd.length = "28" ;							
			}
			if ((year%100 > 0) || (year%400 == 0))	{
				for(i=1;i<=29;i++) {
					dd.options[i-1] = new Option(i);
					dd.options[i-1].value = i;
				}
				dd.length = "29" ;			
			}
		}
	}
	if (dd.length < day + 1)		{
		dd.selectedIndex = dd.length-1 ;
	}
	else	{
		dd.selectedIndex = day ;
	}
}

function check_len(str1, len)	{
	if (str1.length > len){
		alert('欄位長度超過'+len);
		return false;
	}
	else {
		return true;
	
	}
}