//檢查字串是否為數值
function isNumber(str) {
    if( str.length == 0 ) return false;
    for( var loc=0; loc<str.length; loc++ ) {
        if( (str.charAt(loc) < '0') || (str.charAt(loc) > '9') ) return false;
    }
    return true;
}

//刪除字串前後之空白
function trim(strText) {
    while (strText.substring(0,1) == ' ')
        strText = strText.substring(1, strText.length);
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);
    return strText;
}

//check 輸入欄位是否空白
function chkBlank(obj,Str) {
    if (obj.value == "") {
        alert("please enter your "+Str+"!");
        obj.focus();
        obj.select();
        return false;
    } else {
        return true;
    }
}

//check 輸入欄位是否為數字
function chkint(obj,StrField) {
    str=obj.value+"";
    for(var i=0;i<str.length; i++) {
        if(str.charAt(i)<"0" || str.charAt(i)>"9") {
            alert("Please fill correct "+StrField+" value,must number!");
            obj.focus();
            return false;
        }
    }
    return true;
 }

//check Email輸入欄位是否正確
function chkEmail(obj,Str) {
    var exp_email = /^[a-z_0-9][a-z_0-9\.\-]+@[a-z_0-9\.\-]+\.[a-z]{2,5}$/i;
    if (!exp_email.test(Str)) {
        alert("Please enter correct email address.");
        obj.focus();
        obj.select()
        return false;
    } else {
        return true;
    }
}

//check Email輸入欄位是否正確
function chk_Email(mail_str) {
    var exp_email = /^[a-z_0-9][a-z_0-9\.\-]+@[a-z_0-9\.\-]+\.[a-z]{2,5}$/i;
    return exp_email.test(mail_str);
}

//e-mail check 2
function chk_email (mail) {
    var e_mail=mail;
    var emailPat=/^(.+)@(.+)$/
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
    var validChars="\[^\\s" + specialChars + "\]"
    var quotedUser="(\"[^\"]*\")"
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
    var atom=validChars + '+'
    var word="(" + atom + "|" + quotedUser + ")"
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
    /* Begin with the coarse pattern to simply break up user@domain into
       different pieces that are easy to analyze. */
    var matchArray=e_mail.match(emailPat)
    if (matchArray==null) {
        /* Too many/few @'s or something; basically, this address doesn't
           even fit the general mould of a valid e-mail address. */
        alert("Email address incorrect!(Check @ or .'s)")
        return false
    }
    var user=matchArray[1]
    var domain=matchArray[2]
    // See if "user" is valid
    if (user.match(userPat)==null) {
        // user is not valid
        alert("The username in your email is invalid.")
        return false
    }
    /* if the e-mail address is at an IP address (as opposed to a symbolic
       host name) make sure the IP address is valid. */
    var IPArray=domain.match(ipDomainPat)
    if (IPArray!=null) {
        // this is an IP address
        for (var i=1;i<=4;i++) {
            if (IPArray[i]>255) {
                alert("Destination IP address in your email is invalid!")
                return false
            }
        }
        return true
    }
    // Domain is symbolic name
    var domainArray=domain.match(domainPat)
    if (domainArray==null) {
        alert("The domain name in your email is invalid.")
        return false
    }
    /*domain name seems valid, but now make sure that it ends in a
    three-letter word (like com, edu, gov) or a two-letter word,
    representing country (uk, nl), and that there's a hostname preceding
    the domain or country. */
    /* Now we need to break up the domain to get a count of how many atoms
    it consists of. */
    var atomPat=new RegExp(atom,"g")
    var domArr=domain.match(atomPat)
    var len=domArr.length
    if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>5) {
        // the address must end in a two letter or three letter word.
        alert("The address in your email must end in a 3-5 letters domain, or two letters country.")
        return false
    }
    // Make sure there's a host name preceding the domain.
    if (len<2) {
        var errStr="This email address is missing a hostname!"
        alert(errStr)
        return false
    }
    var leng = e_mail.length;
    for(var i=0;i<leng;i++) {
        var c= e_mail.charAt(i);
        if(!((c>="A"&&c<="Z")||(c>="a"&&c<="z")||(c>="0"&&c<="9")||(c=="-")||(c=="_")||(c==".")||(c=="@"))) {
            alert("Your mail address could only be number,English char or '-','_','@'...,can't be any other chars!\n")
            return false
        }
    }
    // If we've gotten this far, everything's valid!
    return true;
}