﻿//判斷數字
function ascertain(chr_s){
	var local=new Array(10)
	local[0]='0'	
	local[1]='1'	
	local[2]='2'	
	local[3]='3'	
	local[4]='4'	
	local[5]='5'	
	local[6]='6'	
	local[7]='7'	
	local[8]='8'	
	local[9]='9'	
	var c=chr_s.length
	for (j=0;j<=chr_s.length-1;j++){
		for (i=0;i<=9 ;i++){
			if (local[i]==chr_s.substring(j,j+1)){
				c-=1
			}
		}
	}
	return c
}


//判斷電子郵件
function check_email(email){
	var len = email.length;
	if( email.length <= 0 )
		 return "您的電子信箱不可以空白!!\n";
	var ck =0;
	if(len==0)
		return "";
	for(var i=0;i<len;i++){
		var c= email.charAt(i);
		if(!((c>="A"&&c<="Z")||(c>="a"&&c<="z")||(c>="0"&&c<="9")||(c=="-")||(c=="_")||(c==".")||(c=="@")))
			return "您的電子郵件地址只能是數字,英文字母及'-','_'等符號,其他的符號都不能使用!!\n";
		}

	if((email.indexOf("@")==-1)||(email.indexOf("@")==0)||(email.indexOf("@")==(len-1)))
		return "您的電子郵件地址不合法!!\n";
	if((email.indexOf("@")!=-1)&&(email.substring(email.indexOf("@")+1,len).indexOf("@")!=-1))
		return "您的電子郵件地址不合法!!\n";
	if((email.indexOf(".")==-1)||(email.indexOf(".")==0)||(email.lastIndexOf(".")==(len-1)))
		return "您的電子郵件地址不完全!!\n";
	return "";
}

//判斷身份證
function check_tw_id(sId){
    var LegalID = "0123456789"
    var fResult=true;
    if(sId.length<10)
        fResult=false;
    else{
        if((sId.charAt(0)=='A') || (sId.charAt(0)=='a')) value=10
        else if((sId.charAt(0)=='B') || (sId.charAt(0)=='b')) value=11
        else if((sId.charAt(0)=='C') || (sId.charAt(0)=='c')) value=12
        else if((sId.charAt(0)=='D') || (sId.charAt(0)=='d')) value=13
        else if((sId.charAt(0)=='E') || (sId.charAt(0)=='e')) value=14
        else if((sId.charAt(0)=='F') || (sId.charAt(0)=='f')) value=15
        else if((sId.charAt(0)=='G') || (sId.charAt(0)=='g')) value=16
        else if((sId.charAt(0)=='H') || (sId.charAt(0)=='h')) value=17
        else if((sId.charAt(0)=='J') || (sId.charAt(0)=='j')) value=18
        else if((sId.charAt(0)=='K') || (sId.charAt(0)=='k')) value=19
        else if((sId.charAt(0)=='L') || (sId.charAt(0)=='l')) value=20
        else if((sId.charAt(0)=='M') || (sId.charAt(0)=='m')) value=21
        else if((sId.charAt(0)=='N') || (sId.charAt(0)=='n')) value=22
        else if((sId.charAt(0)=='P') || (sId.charAt(0)=='p')) value=23
        else if((sId.charAt(0)=='Q') || (sId.charAt(0)=='q')) value=24
        else if((sId.charAt(0)=='R') || (sId.charAt(0)=='r')) value=25
        else if((sId.charAt(0)=='S') || (sId.charAt(0)=='s')) value=26
        else if((sId.charAt(0)=='T') || (sId.charAt(0)=='t')) value=27
        else if((sId.charAt(0)=='U') || (sId.charAt(0)=='u')) value=28
        else if((sId.charAt(0)=='V') || (sId.charAt(0)=='v')) value=29
        else if((sId.charAt(0)=='X') || (sId.charAt(0)=='x')) value=30
        else if((sId.charAt(0)=='Y') || (sId.charAt(0)=='y')) value=31
        else if((sId.charAt(0)=='W') || (sId.charAt(0)=='w')) value=32
        else if((sId.charAt(0)=='Z') || (sId.charAt(0)=='z')) value=33
        else if((sId.charAt(0)=='I') || (sId.charAt(0)=='i')) value=34
        else if((sId.charAt(0)=='O') || (sId.charAt(0)=='o')) value=35
        else fResult = false ;
        if (!(sId.charAt(1)==1||sId.charAt(1)==2))
          fResult = false ;
    }
    if(fResult==true){
        value = Math.floor(value/10) + (value%10)*9 + parseInt(sId.charAt(1))*8 +
                parseInt(sId.charAt(2))*7 + parseInt(sId.charAt(3)) * 6 + parseInt(sId.charAt(4)) * 5 +
                parseInt(sId.charAt(5))*4 + parseInt(sId.charAt(6)) * 3+ parseInt(sId.charAt(7)) * 2+
                parseInt(sId.charAt(8)) + parseInt(sId.charAt(9)) ;
        value = value % 10 ;
        if(value!=0) fResult = false ;

        var i;
        var c;
        for (i = 1; i < sId.length; i++){
            c = sId.charAt(i);
            if (LegalID.indexOf(c) == -1) fResult = false;
        }
    }
    if(fResult == false)
        return false;
    else
        return true;
}

function check_uid ( uid ){
	if( uid.length <= 0 )
		return( "您的帳號還沒填呢 !\n" );
	if( uid.length <3 || uid.length> 32 )
		return( "您的帳號長度只能 3 至 32 個字元 !\n" );
	if ( !(uid.charAt(0)>= 'a' && uid.charAt(0) <= 'z') ) {
		return( "您的帳號第一字元只能為小寫字母 !\n" );
	}
	for( idx = 0 ; idx <uid.length ; idx++ ){
		if( uid.charAt(idx)>= 'A' && uid.charAt(idx) <= 'Z' )
			return( "帳號不可以含有大寫字元 !\n" );
		if( !( ( uid.charAt(idx)>= 'a' && uid.charAt(idx) <= 'z' ) || ( uid.charAt(idx)>= '0' && uid.charAt(idx) <= '9' ) || ( uid.charAt(idx) == '_' ) ) )
			return( "您的帳號只能是數字,英文字母及「_」等符號,其他的符號都不能使用 !\n" );
		if( uid.charAt(idx) == '_' && uid.charAt(idx-1) == '_' )
			return( "「_」符號不可相連 !\n" );
	}
	if( uid.charAt( uid.length - 1 ) == '_' )
		return( "「_」符號不可在最後 !\n" );
		return "";
	}
	function check_oldID ( uid ){
		if( uid.length <= 0 )
			return( "您的帳號還沒填呢 !\n" );
		if( uid.length> 32 )
			return( "您的帳號長度只能小於 32 個字元 !\n" );
		for( idx = 0 ; idx <uid.length ; idx++ ){
			if( uid.charAt(idx)>= 'A' && uid.charAt(idx) <= 'Z' )
				return( "帳號不可以含有大寫字元 !\n" );
			if( !( ( uid.charAt(idx)>= 'a' && uid.charAt(idx) <= 'z' ) || ( uid.charAt(idx)>= '0' && uid.charAt(idx) <= '9' ) || ( uid.charAt(idx) == '_' ) || ( uid.charAt(idx) == '-' ) || ( uid.charAt(idx) == '.' ) ) )
				return( "您的帳號只能是數字,英文字母及「_」「-」「.」等符號,其他的符號都不能使用 !\n" );
		}
	return "";
}

function check_passwd ( pw1, pw2 ){
	if( pw1 == '' ) {
		return ("密碼不可以空白 !\n");
	}
	for( var idx = 0 ; idx <pw1.length ; idx++ )
		if( pw1.charAt(idx) == ' ' || pw1.charAt(idx) == '\"' )
			return ("密碼不可以含有空白或雙引號!!\n");
		if( pw1.length <6 || pw1.length> 32 )
			return( "密碼長度只能 6 到 32 個字母!!\n" );
		if( pw1 != pw2 )
			return("密碼二次輸入不一樣,請重新輸入!!\n");
	return "";
}
