function err(element,msg){
	alert(msg);
	element.focus();
	if (element.name.substring(0,3) != "sel" && element.name.substring(0,3) != "opt"){
		element.select();
	}
}

function validBlank(element, title) {
	if (element.value == "") { err(element,"Please input the "+title+" !"); return true }
	return false;
}

function pulldownNotSelected(element,title) {
	if (element.options[0].selected == true){
		alert("Please select the "+title+" !");
		element.focus();
		return true;
	}
	return false;
}

function radioNotChecked(element,title){
	len = element.length;
	for (i=0; i<len; i++){
		if (element[i].checked) { return false }
	}
	err(element[0],"Please select the "+title+" !");
	return true;
}

function checkDate(dateStr) {
	year  = dateStr.substr(0,4);
	month = dateStr.substr(4,2);
	day   = dateStr.substr(6,2);
	if (month < 1 || month > 12) { // check month range
		return false;
	}
	if (day < 1 || day > 31) {
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return false;
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			return false;
		}
	}
	return true;
}

function validLongDate(element,title){
	if (element.value == ""){ return false }
	else {
		// format : yyyymmdd
		if ( element.value.length != 8 || isNaN(element.value) ){
			err(element,"Please check the Date format!!\n It should be yyyymmdd");
			return true;
		}
		if (checkDate(element.value)==false) {
				err(element,"Please check the Date format!!\n It should be yyyymmdd");
				return true;	
		}
		return false;
	}
}

function validNum(element,title) {
	if (isNaN(element.value)) { err(element,"The "+title+" should be a number !"); return true }
	return false;
}

function validFixNum(element,title,digitNo) {
	if (digitNo>1) {
		showS='s';
	} else {
		showS='';
	}
	if ((isNaN(element.value))||(element.value.length!=digitNo)) { err(element,"The "+title+" should be a number with "+digitNo+" digit"+showS+" !"); return true }
	return false;
}

function validHKID(element){
	if (element.value=="") { return false;}
	var idCardCheckDigit = "";
	idCardNum = element.value.toUpperCase();
	element.value=idCardNum;
	
	/*
		Check the HKID Card Number.
		First digit A-Z represents 1-26, and the rightmost digit in the () is check digit.
		The method to calculate the check digit is:
		1. Get the sum of the first 7 digit.
		   (1st digit * 8) + (2nd digit * 7) + (3rd digit * 6) + (4th digit * 5) + (5th digit * 4) + (6th digit * 3) + (7th digit * 2)
		2. Get the remainder of the sum mod by 11.
		3. The check digit is 11 - remainder, when the result is 10, the check digit become 'A'.
	*/
	
	if (idCardNum.charCodeAt(0) >= 65 && idCardNum.charCodeAt(0) <= 90){
		if (!isNaN(idCardNum.substring(1,idCardNum.length - 1))){
			idCardCheckDigit = ((idCardNum.charCodeAt(0) - 64) * 8) +
				(idCardNum.charAt(1) * 7) +
				(idCardNum.charAt(2) * 6) +
				(idCardNum.charAt(3) * 5) +
				(idCardNum.charAt(4) * 4) +
				(idCardNum.charAt(5) * 3) +
				(idCardNum.charAt(6) * 2);
			idCardCheckDigit = 11 - (idCardCheckDigit % 11);
			if (idCardCheckDigit == 10) idCardCheckDigit = "A";
			if (idCardCheckDigit == idCardNum.charAt(7)){
				return false;
			}else{
			}
		}else{
		}
	}else{
	}
	err(element,"Please check your HKID No.!!");
	return true;
}
