//isANum(in_Str), return true/false
//is_number(str), return true/false
//isch_alphnum(str), alphanumeric char(a-z, A-Z, 0-9), return true/false
//isch_alph
//isch_upcase
//isch_lowcase
//isch_digit
//strTrim()

function EnglishOnly(Str){
	for (i=0; i<Str.length; i++){
		if (Str.charCodeAt(i)>255){
			return false;
			break;
		}
	}
	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 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
}


/****************************************************************/

//isWhitespace()
//ForceEntry
//ValidateRanking
//ValidateData

/****************************************************************/

/*
The isEmpty and isWhitespace functions were taken straight from Netscape's JavaScript development site, http://developer.netscape.com.
*/
// whitespace characters
var whitespace = " \t\n\r";

function isWhitespace (s)
{
   var i;

   // Is s empty?
   if (isEmpty(s)) return true;

   // Search through string's characters one by one
   // until we find a non-whitespace character.
   // When we do, return false; if we don't, return true.

   for (i = 0; i < s.length; i++)
   {
		// Check that current character isn't whitespace.
		var c = s.charAt(i);

		if (whitespace.indexOf(c) == -1) return false;
   }

   // All characters are whitespace.
   return true;
}

/****************************************************************/

function ForceEntry(val, str) {
   var strInput = new String(val.value);

   if (isWhitespace(strInput)) {
		alert(str);
		return false;
   } else
		return true;

}

/****************************************************************/

function ValidateRanking() {
   // This function ensures document.forms[0].nRanking.value >=1 && <= 10

   if (parseInt(document.forms[0].nRanking.value) >= 1 && parseInt(document.forms[0].nRanking.value) <=10)
		return true;
   else
		return false;
}

/****************************************************************/

function ValidateData() {
   var CanSubmit = false;

   // Check to make sure that the full name field is not empty.
   CanSubmit = ForceEntry(document.forms[0].txtName,"You supply a full name.");

   // Check to make sure ranking is between 1 and 10
   if (CanSumbit) CanSubmit = ValidRanking();

   return CanSubmit;
}


/****************************************************************/

//validEmail(obj)
//validHKID(HKID, HKID_ChkDigit) , both are objects
//validRadio(Obj, str)
//validInputBox(Obj, str, str2), str2=all whitespace
//validNumberInputBox(Obj, str)
//maxLengthInput(Obj, str, maxlength)

/****************************************************************/

function validEmail(Obj){
	invalidChars = " /:,;";
	if (Obj.value ==""){
		alert(Msge_005);
		Obj.focus();
		return false;
	}

	for (i=0; i<invalidChars.length; i++)
	{
		badChar = invalidChars.charAt(i);
		if(Obj.value.indexOf(badChar,0) > -1){
			alert(Msge_006);
			Obj.focus();
			return false;
		}
	}

	atPos =Obj.value.indexOf("@",1);
	if(atPos ==  -1){
		alert(Msge_006);
		Obj.focus();
		return false;
	}

	periodPos = Obj.value.indexOf(".", atPos);
	if (periodPos == -1){
		alert(Msge_006);
		Obj.focus();
		return false;
	}
	if (periodPos+3 > Obj.value.length){
		alert(Msge_006);
		Obj.focus();
		return false;
	}

	if (EnglishOnly(Obj.value)==false){
		alert(Msge_007);
		Obj.focus();
		return false;
	}
	return true;
}


function validHKID(HKID, HKID_ChkDigit){
	chkSum = 0;
	if(HKID.length==0){alert(Msge_001);return false;}
	if(HKID_ChkDigit.length==0){alert(Msge_001);return false;}

	if(HKID.length==7){HKID=String.fromCharCode(32)+HKID;}
	if(HKID.length==8){

	//get total of chkSum
		for(i=0;i<2;i++){
			if(HKID.charAt(i)==String.fromCharCode(32)){ // if it was space
				getDight = 36;
			} else {
				if(isch_upcase(HKID.toUpperCase().charAt(i))){
					getDight = (HKID.toUpperCase().charCodeAt(i)*1)-55;
				} else {
					alert(Msge_002); // first 2 character is not letter
					return false;
				}
			}
			chkSum += (HKID.length-i+1)*(getDight);
			getDigit = "";
		}
		for(i=2;i<HKID.length;i++){
			if(isch_digit(HKID.charAt(i))){
				getDigit = HKID.charAt(i)*1;
				chkSum += (HKID.length-i+1)*getDigit;
				getDigit = "";
			} else {
				alert(Msge_002); // last 6 character is not number
				return false;
			}
		}

		//get check digit
		chkDigit = 11-(chkSum)%11;

		if(chkDigit==10){
			chkDigit = "A";
		}else if (chkDigit==11){
			chkDigit = "0";
		}else{
			chkDigit = chkDigit + "";
		}
		if(chkDigit.toUpperCase()!=HKID_ChkDigit.toUpperCase()){
			alert(Msge_003+HKID+'('+HKID_ChkDigit+')'+Msge_004);
			return false;
		}
	}else{
		alert(Msge_002); // less than 8 characters
		return false;
	}
	return true;
}

function validRadio(Obj, str){
	var Obj_Check = false;
	for (var i=0; i < Obj.length; i++ ){
		if (Obj[i].checked){
			Obj_Check=true;
			break;
		}
	}
	if (Obj_Check==false){
		alert(str);
		return false;
	} else {
		return true;
	}
	Obj_Check = null;
}


function validInputBox(Obj, str, str2){
	// can't be empty or all characters are white space
	if ((Obj.value==null)||(Obj.value.length ==0)){
		alert(str); // empty
		Obj.focus();
		return false;
	} else {
		Obj.value = strTrim(Obj.value)
		if ((Obj.value==null)||(Obj.value.length ==0)){
			if (str2 == ""){
				alert(Msge_007);
			} else {
				alert(str2);
			}
			Obj.focus();
			return false;
		} else {
			return true;
		}
	}
}

function validNumberInputBox(Obj, str){
	if(is_number(Obj.value)==false){
		alert(str);
		Obj.focus();
		return false;
	} else {
		return true;
	}
}

function maxLengthInput(Obj, str, maxlength){
	if (Obj.value.length > maxlength){
		alert(str);
		Obj.focus();
		return false;
	} else {
		return true;
	}
}
















	//input name of form
	//name of input field:
	//name, hkid, hkiddight, sex, email, phone, hkjcinfo, bday_day, bday_mth, bday_yr

/*
	var yearold = 20021116 - 00180000;

	if (formChk2()==false)
	{
		return false;
	}
	else
	{
		f.Horse_value.value = formChk2();
	}

	if ((f.name.value =="") || (f.name.value.charAt(0)==' '))
	{
		alert("請輸入您的姓名 ! ");
			f.name.focus();
			f.name.select();
		return false;
	}



	if (f.bday_yr.value=="" || f.bday_mth.value=="" || f.bday_day.value=="")
	{
		alert("請填寫你的出生日期 ! ");
		return false;
	}

	if (bday_formChk(f)==false)
	{
		alert("你的出生日期不正確 ! \n\n請重新輸入你的正確出生日期 ! " );
		return false;
	}

	var birthday = f.bday_yr.value + f.bday_mth.value + f.bday_day.value
	
	if (birthday > yearold)
	{
		alert("對不起 ! 未滿十八歲人士不可參加 ! ");
		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;
	}

	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.phone.value =="" )
	{
		alert("請輸入您的聯絡電話號碼 ! ");
		f.phone.focus();
		f.phone.select();
		return false;
	}

	if (f.phone.value !="" && !(isANum(f.phone.value)))
	{
		alert("聯絡電話號碼並不接受非數字輸入，請再次輸入");
		f.phone.focus();
		f.phone.select();
		return false;
	}

	return true;
	*/
