	function isFormNumeric(formElem,isInteger,canNegative,defaultValue){
		if (!isNumeric(formElem.value,isInteger,canNegative)) {
			alert("Please enter" + (canNegative ? "" : " positive") + (isInteger ? " integer." : " number."));
			formElem.value = defaultValue;
			formElem.select();
			formElem.focus();
			return false;
		} else if (trim(formElem.value) == "") {
			formElem.value = defaultValue;
		} else {
			formElem.value = trim(formElem.value);
		}
		return true;
	}
	
	function isFormDecimal(formElem,isInteger,canNegative,defaultValue){
		if (!isNumeric(formElem.value,isInteger,canNegative)) {
			alert("Please enter" + (canNegative ? "" : " positive") + (isInteger ? " integer." : " number."));
			formElem.value = defaultValue;
			formElem.select();
			formElem.focus();
			return false;
		} else if(!chkDecimal(formElem.value,2)) { 
			alert("The amount only allows 2 decimal places."); 
			formElem.value = defaultValue;
			formElem.select();
			formElem.focus();
			return false;
		} else if (trim(formElem.value) == "") {
			formElem.value = defaultValue;
		} else {
			formElem.value = trim(formElem.value);
		}
		return true;
	}

	function isNumeric(num,isInteger,canNegative) {
		if (isNaN(num)) {
			return false;
		} else {
			if (isInteger) {
				if(!canNegative)
					return (num.toString().indexOf(".")==-1 && num.toString().indexOf("-")==-1);
				else
					return (num.toString().indexOf(".")==-1);
			} else {
				if(!canNegative)
					return (num.toString().indexOf("-")==-1);
				else
					return true;
			}
		}
	}
	
	function chkDecimal(num,places){
		if(isNaN(num)||isNaN(places)){
			return false;
		}else{
			var elem = num.split(".");
			if(elem.length>1){
				if(elem[1].length>places){
					return false;
				}else{
					return true;
				}
			}else{
				return true;
			}
		}	
	}	
	
	function roundUp(num,places){
		if(isNumeric(num,false,true)){
			if(String(num).indexOf("e") > 0 ){
				num = 0;
			}else if (String(num).indexOf(".") > 0 ){
				if(String(num).substring(String(num).indexOf(".")+1+places,String(num).indexOf(".")+2+places)>=5){
					if(num>=0){
						num = parseFloat(num) + 0.01;
					}else{
						num = parseFloat(num) - 0.01;
					}
				}
				num = String(num).substring(0,String(num).indexOf(".")+1+places);							
			}			
			return num;
		}else{
			return 0;
		}
	}
	
	function formatCurrency(num,places){
		if(isNumeric(num,false,true)){
			num = roundUp(num,places);			
			if (String(num).indexOf(".") < 0 ){
				num = num + ".";
				for(i=0;i<places;i++){
					num = num + "0";
				}
			}else if(String(num).length-String(num).indexOf(".")-1<places){
				for(i=String(num).length-String(num).indexOf(".")-1;i<places;i++){
					num = num + "0";
				}
			}
			var str1 = String(num).substring(0,String(num).indexOf("."));
			var str2 = String(num).substring(String(num).indexOf("."),String(num).length);
			var str3 = "";
			i = 0;
			for(j=str1.length-1;j>=0;j--){
				if (i%3==0&&i>0&&i<str1.length-1){
					str3 = "," + str3;
				}
				str3 = str1.charAt(j) + str3;
				i++;
			}
			return "$" + str3 + str2;
		}else{
			return "$0.00";
		}
	}	
	
	function isFormDate(formElem) {
		formElem.value = trim(formElem.value);
		if(formElem.value.length>0) {
			var err = 0;
			var date = formElem.value;
			if (date.length != 10) err=1;
			var day		= date.substring(0, 2)		// day
			var slash1	= date.substring(2, 3)		// '/'
			var month	= date.substring(3, 5)	// month
			var slash2	= date.substring(5, 6)		// '/'
			var year	= date.substring(6, 10)		// year
		
			if (slash1 != "/") err=1;
			if (slash2 != "/") err=1;
			if (isNaN(day)) err = 1;
			if (isNaN(month)) err = 1;
			if (isNaN(year)) err = 1;
			
			if(err==0) {
				if (month<1 || month>12) err = 1
				if (day<1 || day>31) err = 1
				if (year<1900) err = 1
				if (month==4 || month==6 || month==9 || month==11){
					if (day==31) err=1
				}
				if (month==2){
					var g=parseInt(year/4)
					if (isNaN(g)) {
						err=1
					}
					if (day>29) err=1
					if (day==29 && ((year/4)!=parseInt(year/4))) err=1
				}
			}
			if (err==1) {
				alert(formElem.value + " is not a valid date.\nFormat: \"DD/MM/YYYY\"");
				formElem.value = "";
				formElem.focus();
			}
		}
	}
	
	function isNaD(year,month,day) {
		if (year<1||month<1||month>12||day<1||day>31) {
			return false;
		}
		else {
			if (month==4||month==6||month==9||month==11) {
				if (day==31) {
					return false;
				}
			} else if (month==2) {
				if ((isNumeric(year/4,true) && !isNumeric(year/100,true)) || isNumeric(year/400,true)) {
					if (day>29) {
						return false;
					}
				} else {
					if (day>28) {
						return false;
					}
				}
			}
		}
		return true;
	}
	
	function adjustDate(yearForm,monthForm,dayForm) {
		if(yearForm.selectedIndex>0 && monthForm.selectedIndex>0) {
			var maxDay = 31;
			var year = yearForm.selectedIndex+parseInt(yearForm.options[1].value)-1;
			var month = monthForm.selectedIndex;
			var day = dayForm.selectedIndex;

			if (month==4||month==6||month==9||month==11) {
				maxDay = 30;
			} else if (month==2) {
				if ((isNumeric(year/4,true) && !isNumeric(year/100,true)) || isNumeric(year/400,true)) {
					maxDay = 29;
				} else {
					maxDay = 28;
				}
			}
			setDay(dayForm,maxDay);
			return;
		} else if (yearForm.selectedIndex<1) {
			dayForm.length = 1;
			dayForm.options[0].index = 1;
			dayForm.options[0].value = 0;
			dayForm.options[0].text = "DD";

			monthForm.length = 1;
			monthForm.options[0].index = 1;
			monthForm.options[0].value = 0;
			monthForm.options[0].text = "MM";
		} else {
			dayForm.length = 1;
			dayForm.options[0].index = 1;
			dayForm.options[0].value = 0;
			dayForm.options[0].text = "DD";
			
			monthForm.length = 13;
			monthForm.options[0].index = 1;
			monthForm.options[0].value = 0;
			monthForm.options[0].text = "MM";
			
			for (i=1;i<=12;i++) {
				monthForm.options[i].index = i;
				monthForm.options[i].value = i;
				if(i<10) {
					monthForm.options[i].text = "0" + (i);
				} else {
					monthForm.options[i].text = i;
				}
			}
		}
	}
	
	function setDay(dayForm,dayNum) {
		var restoreIndex = 0;
		if (dayForm.selectedIndex < dayNum) {
			restoreIndex = dayForm.selectedIndex;
		} else {
			restoreIndex = dayNum;
		}
		dayForm.length = dayNum+1;
		dayForm.options[0].index = 1;
		dayForm.options[0].value = 0;
		dayForm.options[0].text = "DD";
		
		for (i=1;i<=dayNum;i++) {
			dayForm.options[i].index = i;
			dayForm.options[i].value = i;
			if(i<10) {
				dayForm.options[i].text = "0" + (i);
			} else {
				dayForm.options[i].text = i;
			}
		}
		dayForm.selectedIndex = restoreIndex;
		return;
	}
	
	function checkMaxSize(formElem,maxSize,fieldName){
		if (trim(formElem.value).length > maxSize) {
			alert("The " + fieldName + " cannot be longer than " + maxSize + " characters");
			formElem.focus();
		}		
	}
	
	function exceedMaxSize(sValue,maxSize){
		if (!chkTextLng(sValue,maxSize)) {
			return true;
		}else{
			return false;
		}
	}	
	
	function trim(field) {
		var retval = "";
		retval = field.replace(/^\s+/g, "");	// (\s+) means all white space, (^) means after the start of the line
		retval = retval.replace(/\s+$/g, "");	// ($) means before the end of line
		return retval;
	}	
	
	function fileExtention(filepath) {
		var dotIndex = filepath.lastIndexOf(".");
		if(dotIndex==-1) {
			return "";
		} else {
			return filepath.substring(dotIndex+1,filepath.length);
		}
	}
	
	function isImageFile(filepath) {
		var image_ext = fileExtention(filepath);
		return image_ext=="jpg" || image_ext=="jpeg" || image_ext=="gif";
	}
	
	function isEmpty(s)	{
		return ((s == null) || (s.length == 0));
	}
	
    //space character, including space, tab, form feed, line feed.
	function hasOnlyWhitespace(s){
		return (/^\s+$/.test(s));
	}
	
	function isPositiveInteger(s){
		return (/^\d*$/.test(s));
	}
	
	function isPositiveNumber(s){
		return (/^(\d*\.?\d*|\.\d+)$/.test(s));
	}
	
	function isNumber(s){
		return (/^-?(\d*\.?\d*|\.\d+)$/.test(s));
	}
	
	function isAlphanumeric(s){
		return (/^\w*$/.test(s));
	}
	
	function isAlphabet(s){
		return (/^[a-zA-Z]*$/.test(s));
	}
	
	function isAlphanumericSpc(s){
		return (/^[\w\s]*$/.test(s));
	}
	
	function isEmailAddress(s){
		return (/^[^\s\,\;\'\"]+@[^\s\,\;\'\"]+\.[^\s\,\;\'\"]+$|^$/.test(s));
	}
	
	function isPersent(s){
		return (/^-?(\d*\.?\d*|\.\d+)%?$/.test(s));
	}
	
	function trimAllFields(thisform) {
		var formlen = thisform.elements.length;
		for (i=0; i<formlen; i++){
			if(thisform.elements[i].type == "text" || thisform.elements[i].type == "textarea"){
				thisform.elements[i].value = trim(thisform.elements[i].value);
			}
		}
	}
	
	// Check the date range input by user is valid or not
	// the parameter is the form element object(i.e. <SELECT> ojbect), NOT integer
	
	function validDateRange(from_year,from_month,from_day,to_year,to_month,to_day) {
		var int_from_year = parseInt(from_year.options[from_year.selectedIndex].value);
		var int_from_month = parseInt(from_month.options[from_month.selectedIndex].value);
		var int_from_day = parseInt(from_day.options[from_day.selectedIndex].value);
		var int_to_year = parseInt(to_year.options[to_year.selectedIndex].value);
		var int_to_month = parseInt(to_month.options[to_month.selectedIndex].value);
		var int_to_day = parseInt(to_day.options[to_day.selectedIndex].value);
		return !(int_from_year>int_to_year || (int_from_year==int_to_year && int_from_month>int_to_month) ||
				(int_from_year==int_to_year && int_from_month==int_to_month && int_from_day>int_to_day));
	}

	function chkTextLng(fldValue,fldSize){
		var arr = escape(trim(fldValue)).split("%0D%0A");
		var ttlLng = 0;
		for(i=0;i<arr.length;i++){
			ttlLng += unescape(arr[i]).length;
		}
		if(parseInt(ttlLng)>parseInt(fldSize)){
			return false;
		}else{
			return true;
		}
	}
	
