function validEmail(email){
  invalidChars = " /:,;";

  if (email ==" "){
    return false;
  }

  for (i=0; i<invalidChars.length; i++){
    badChar = invalidChars.charAt(i);
    if(email.indexOf(badChar,0) > -1){
      return false;
    }
  }

  atPos =email.indexOf("@",1);
  if(atPos ==  -1) {
    return false;
  }

  periodPos = email.indexOf(".", atPos);
  if (periodPos == -1){
    return false;
  }

  if(periodPos+3 > email.length) {
   return false;
  }
  return true;
}

function isANum(str){
  for(i=0;i<str.length;i++){
    ch = str.substring(i,i+1);
    if (ch < "0" || ch > "9"){
      return false;
    }
  }
  return true;
}


function is_number(str){
	for (i=0; i<str.length; i++)
		if ((str.charAt(i)< '0') || (str.charAt(i)> '9')) return false
	return true
}


function isch_alphnum(ch){	// is alphanumeric char(a-z, A-Z, 0-9)
	if (isch_alph(ch) || isch_digit(ch)) return true
	else return false
}
function isch_alph(ch){	// is alphabetic char (a-z, A-Z)
	if (isch_upcase(ch) ||  isch_lowcase(ch)) return true
	else return false
}
function isch_upcase(ch){	// is A-Z
	if (ch < 'A' ||  ch > 'Z' ) return false
	else return true
}
function isch_lowcase(ch){	// is a-z
	if (ch < 'a' ||  ch > 'z' ) return false
	else return true
}
function isch_digit(ch){	// is 0-9
	if (ch < '0' ||  ch > '9' ) return false
	else return true
}

function is_hkid(str){
	if (str.length < 7) return false
	if (!isch_alph(str.charAt(0))) return false

	startdigit=1
	if (str.length==8){	// if 8 digit, 2nd digit show be alphabet
	   if (!isch_alph(str.charAt(1))) return false
	   else startdigit++
	}

	for (i=startdigit; i<str.length; i++)
		if (!isch_digit(str.charAt(i))) return false
		
	return true
}

function b_day(byy, bmm, bdd) { //v2.0
	var err=0
  	if (bmm.value==4 || bmm.value==6 || bmm.value==9 || bmm.value==11){ 
		if (bdd.value >=31) err=1 
		}
	if (bmm.options[bmm.selectedIndex].value == 2) {
		if (byy.value%4==0){
			if (bdd.value>29) err=1
		}
		else {
		if (bdd.value>=29) err=1
		}
		}

	if (err == 1) { 
		return false;
		}
}

function strTrim(str){

   while (str.charAt(0)==' ') {
	tmpStr=""
	for (i=1; i<str.length; i++) tmpStr=tmpStr + str.charAt(i)
	str=tmpStr
   }

   while (str.charAt(str.length-1)==' ') {
	tmpStr=""
	for (i=0; i<str.length-1; i++) tmpStr=tmpStr + str.charAt(i)
	str=tmpStr
   }
   return str
}

function validHKID(str){
    var strHKID
    var chk
    var total
	var firstChar

		firstChar = str.charAt(0).toUpperCase()
        total = ((firstChar.charCodeAt() - 65) % 11 + 1) * 8
        for (var i = 1; i< 7; i++){
            total = total + (8 - i) * (str.charAt(i).charCodeAt() - "0".charCodeAt())
        }

		chk = 11- (total % 11)
		if (chk == 10){
			chk="X"
		}

		return(chk)
}

function chkForm(f){

	if ((f.surName.value =="") || (f.surName.value.charAt(0)==' ')){
	alert("請輸入您的姓氏..");
		f.surName.focus();
		f.surName.select();
	return false;
	}

	if ((f.firstName.value =="") || (f.firstName.value.charAt(0)==' ')){
	alert("請輸入您的名字..");
		f.firstName.focus();
		f.firstName.select();
	return false;
	}

	f.hkid.value=strTrim(f.hkid.value);
	if (f.hkid.value=="") {
		alert("請填寫你的身分證號碼!");
		f.hkid.focus();
		f.hkid.select();
		return false;
	}
	if (! is_hkid(f.hkid.value)){
		alert("身分證號碼格式不正確! 請填入正確格式.");
		f.hkid.focus();
		f.hkid.select();
		return false;
	}
	if (f.hkidDigit.value=="" ||                       // not in range 0-9, a-z, A-Z
		((f.hkidDigit.value<'0' || f.hkidDigit.value>'9') &&
		(f.hkidDigit.value<'A' || f.hkidDigit.value>'Z') &&
		(f.hkidDigit.value<'a' || f.hkidDigit.value>'z')))
	{
		    alert("身分證號碼格式不正確! 請填入正確格式.");
			f.hkidDigit.focus();
			f.hkidDigit.select();
			return false;
	}

	dd=""; mm=""; yyyy=""
	if (f.bYear.value=="" || f.bMonth.value=="" || f.bDay.value=="") {
		alert("請填寫你的出生日期! ");
		return false;
	}

	if (b_day(f.bYear, f.bMonth, f.bDay)==false){
		alert("你的出生日期不正確 ! 請重新輸入你的正確出生日期!" );
		return false;
	}

	var birthday = f.bYear.value + f.bMonth.value + f.bDay.value
	if (birthday > 19860127){
		alert("對不起 ! 未滿十八歲人士不可參加 ! ");
		return false;
	}

    if (f.teleNo.value =="" ){
		alert("請輸入您的聯絡電話號碼!");
		f.teleNo.focus();
		f.teleNo.select();
		return false;
    }

    if (f.teleNo.value.length < 8 ){
		alert("請輸入正確的聯絡電話號碼!");
		f.teleNo.focus();
		f.teleNo.select();
		return false;
    }

    if (f.teleNo.value !="" && !(isANum(f.teleNo.value))){
		alert("聯絡電話號碼並不接受非數字輸入，請再次輸入..");
		f.teleNo.focus();
		f.teleNo.select();
		return false;
	}

   if (f.eMail.value==""){
    alert("請輸入電郵地址..");
    f.eMail.focus();
    f.eMail.select();
    return false;
  }

  if (!validEmail(f.eMail.value)){
    alert("請輸入正確電郵地址格式 !");
    f.eMail.focus();
    f.eMail.select();
    return false;
  }

 if (f.timeMin.value=="0" && f.timeSec.value=="00" && f.time1_10Sec.value=="0") {
		alert("請輸入''完成賽事的時間'' ! ");
		return false;
	}
return true;
}
