function check_email1 ( email ){
    var len = email.length;
    if(len==0)
    return "電子信箱不可以空白 !\n";
    //return "您的聯絡信箱選擇備用電子信箱, 所以備用電子信箱不可以空白 !\n";
    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_email2 ( email, name ){
    var len = email.length;
    if(len==0)
    return name+"不可以空白 !\n";
    //return "您的聯絡信箱選擇備用電子信箱, 所以備用電子信箱不可以空白 !\n";
    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 "您的"+name+"只能是數字,英文字母及'-','_'等符號,其他的符號都不能使用 !\n";
    }
    if((email.indexOf("@")==-1)||(email.indexOf("@")==0)||(email.indexOf("@")==(len-1)))  return name+"不合法 !\n";
    if((email.indexOf("@")!=-1)&&(email.substring(email.indexOf("@")+1,len).indexOf("@")!=-1))  return name+"不合法 !\n";
    if((email.indexOf(".")==-1)||(email.indexOf(".")==0)||(email.lastIndexOf(".")==(len-1)))  return name+"不完全 !\n";
    return "";
}


function check_null ( column, name ){
    if( column.length == 0 )  return name + " 不能為空白 !\n";
    return "";
}

function check_select ( select, name ){
    if( select.options[0].selected == true )  return name + " 不能為空白 !\n";
    return "";
}

function check_id ( id){
    if( id == '' ) {
    return ("帳號不可以空白 !\n");
    }
    if(!(id.charAt(0).match(/[a-z]/) || id.charAt(0).match(/[A-Z]/))) return ("帳號第一個字必須使用英文 \n");
//     if( id.length < 4 || id.length > 15 ) return( "帳號長度只能 4 到 15 個位元 !\n" );
    for( var idx = 0 ; idx <id.length ; idx++ ) {
      	if( id.charAt(idx) == ' ' || id.charAt(idx) == '\"' ) {
      		  return ("帳號不可以含有空白或雙引號 !\n");
      	}
      	var c= id.charAt(idx);
      	if(!((c>="A"&&c<="Z")||(c>="a"&&c<="z")||(c>="0"&&c<="9")||(c=="-")||(c=="_"))){
            return "請輸入限用英文，數字組合的帳號!\n";
        }
    }	
    return "";
}

function check_radio ( radio, name ){
    var error = true;
    for( i=0; i <radio.length; i++ )
    if( radio[i].checked == true ) {
        error = false;
        break;
    }
    if( error == true )
    return name + " 必須選擇 !\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.charAt(idx) >= "a" && pw1.charAt(idx) <= "z")||(pw1.charAt(idx) >= "A" && pw1.charAt(idx) <= "Z")||(pw1.charAt(idx) >= "0" && pw1.charAt(idx) <= "9"))){
            return "請輸入限用英文，數字組合的密碼!\n";
      }
    }	
    if( pw1.length < 5 || pw1.length > 20 ) return( "密碼長度只能 5 到 20 個字母 !\n" );
    if( pw1 != pw2 )  return("密碼二次輸入不一樣,請重新輸入 !\n");
    return "";
}

function check_account ( pw1 ){
    if( pw1 == '' ) {
    return ("帳號不可以空白 !\n");
    }
    for( var idx = 0 ; idx <pw1.length ; idx++ ) {
    	if( pw1.charAt(idx) == ' ' || pw1.charAt(idx) == '\"' ) {
    		return ("帳號不可以含有空白或雙引號 !\n");
    	}
    	if(!((pw1.charAt(idx) >= "a" && pw1.charAt(idx) <= "z")||(pw1.charAt(idx) >= "0" && pw1.charAt(idx) <= "9"))){
            return "請輸入限用英文，數字組合的帳號!\n";
        }
    }	
    if( pw1.length < 5 || pw1.length > 20 )
    return( "帳號長度只能 5 到 20 個字母 !\n" );
    return "";
}

function check_nan( num ,name ){
	if( Math.round( new Number(num) ) < 1 ) 
		return name + "不能設定小於1!\n";
	else
		return "";
}

function check_number_YN( number , name ){
    var error = false;
    if( number.length <= 0 )
    return name + " 不能為空白 !\n";
    for( idx = 0 ; idx <number.length ; idx++ ) {
        if( !( number.charAt(idx)>= '0' && number.charAt(idx) <= '9' ) ) {
            error = true;
            break;
        }
    }

    if( error == true )
    return name + "只能是數字, 其他的符號都不能使用 !\n";
    else
    return "";
}

//身分證檢查
function checkID(idStr){
    // 依照字母的編號排列，存入陣列備用。
    var letters = new Array('A', 'B', 'C', 'D',
        'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
        'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
        'X', 'Y', 'W', 'Z', 'I', 'O');
    // 儲存各個乘數
    var multiply = new Array(1, 9, 8, 7, 6, 5,
                           4, 3, 2, 1);
    var nums = new Array(2);
    var firstChar;
    var firstNum;
    var lastNum;
    var total = 0;
    // 撰寫「正規表達式」。第一個字為英文字母，
    // 第二個字為1或2，後面跟著8個數字，不分大小寫。
    var regExpID=/^[a-z](1|2)\d{8}$/i;
    // 使用「正規表達式」檢驗格式
    if (idStr.search(regExpID)==-1) {
      // 基本格式錯誤
    //       alert(\"請仔細填寫身份證號碼\");
        return "身分證號碼錯誤 !\n";
    } else {
	// 取出第一個字元和最後一個數字。
      	firstChar = idStr.charAt(0).toUpperCase();
      	lastNum = idStr.charAt(9);
    }
  // 找出第一個字母對應的數字，並轉換成兩位數數字。
    for (var i=0; i<26; i++) {
      	if (firstChar == letters[i]) {
        	  firstNum = i + 10;
        	  nums[0] = Math.floor(firstNum / 10);
        	  nums[1] = firstNum - (nums[0] * 10);
        	  break;
      	}
    }
    // 執行加總計算
    for(var i=0; i<multiply.length; i++){
        if (i<2) {
            total += nums[i] * multiply[i];
        } else {
            total += parseInt(idStr.charAt(i-1)) *
                     multiply[i];
        }
    }
    // 和最後一個數字比對
    if((total % 10)==0 ){
        if(lastNum!=0) return "身分證號碼錯誤 !\n";    
    }else if( (10 - (total % 10)!= lastNum) ) {
        return "身分證號碼錯誤 !\n";
    }
    return "";
}

//圖片格式判斷
function check_pic(o, name){
    if(o){
        reg = /.(jpg|JPG|gif|GIF)$/;
        if (!o.match(reg)){
            return name+' 只能為jpg或gif檔 !\n';
        }    
        return "";
    }else    return "";
}
