//Updated by William at 2005/12/10 UTF-8
//=======================================================
//== CheckNullNoChar- Check if object value is blank (without checking special characters.
//== CheckNull		- Check if object value is blank.
//== CheckNumeric	- Check if object value is numeric (include CheckNull).
//== CheckMaxLen	- Check if object length exceeds max. length allowed.
//== CheckMinLen	- Check if object length is less than min. length allowed.
//== CheckLinBrk	- Check if any line break in text box.
//== CheckEmail		- Check if the entered value is email format.
//== CheckPhone		- Check if object value is phone value (include CheckNull).
//== CheckDate		- Check if the entered value is date.
//== CheckDateRange	- Check if To Date is earlier than From Date.
//== CheckEarlyToday	- Check if date is earlier than today.
//== CheckChar		- Check if ' and | appear in input area.
//== CheckURL		- Check value URL starting with http://
//== TrimBack		- Trim trailing spaces.
//== TrimFront		- Trim leading spaces.
//== Trim			- Call TrimBack & TrimFront to trim leading & trailing spaces.
//== CheckTextBox	- Validate text box object (include CheckNull, CheckChar)
//== CheckNRText	- Validate none required string type text box object (include CheckChar)
//== CheckNRNum		- Validate none required numeric type text box object (include CheckNumeric)
//== CheckNRPhone	- Validate none required phone text box object (include CheckPhone)
//== CheckNRURL		- Check none required URL value object (including CheckURL).
//== CheckNREmail	- Validate none required Email text box object (include CheckEmail)
//== CheckTextArea	- Validate text area object (include, CheckChar, CheckMaxLen, CheckLinBrk)
//== LaunchCriteriaWin	- General function on lauching searching criteria window based on different data type.
//== ConvertToUpdTime	- Convert to update time format.
//== CheckTimeRange		- Check the range of From-hh:mm to To-hh:mm. (Include CheckNull, CheckNumeric)
//== CheckNumRage		- Check the numeric range.
//== CheckTime		- Check time format. (Include CheckNull, CheckNumeric)
//== SelectByText	- Perform same function as VBScript combo box's selectedByText(). Select the combo box value to sText.
//== CheckCboNull	- Check if drop down box is "none" or empty.
//== CheckDateTimeRange - Check range of date and time.
//=======================================================
//== Check if the object value is blank (without checking special characters.
//== Parameters	: oObject = Checked object.
//==			: sFieldName = Object name.
function CheckNullNoChar(oObject, sFieldName) {
	if (Trim(oObject.value) == "") {
		alert("請輸入" + sFieldName + "!");
		oObject.focus();
		if (oObject.value.length > 0) oObject.select();
		return false;
	}
	return true;
}

//=======================================================
//== Check if the object value is blank.
//== Parameters	: oObject = Checked object.
//==			: sFieldName = Object name.
function CheckNull(oObject, sFieldName) {
	if (Trim(oObject.value) == "") {
		alert("請輸入" + sFieldName + "!");
		oObject.focus();
		if (oObject.value.length > 0) oObject.select();
		return false;
	}
	//if (CheckChar(oObject) == false) return false;
	return true;
}

//=======================================================
//== Check if object value is numeric (include CheckNull).
//== Parameters	: oObject = Checked object.
/*function CheckNumeric(oObject, sFieldName) {
	var ch;
	var sString;
	
	if (!CheckNull(oObject, sFieldName)) return false;
	sString = Trim(oObject.value);
	for (i=1; i<=sString.length ;i++){
		ch = sString.substring(i-1, i);
		if ((ch < '0') || (ch >'9')) {
			alert("請輸入數值!");
			oObject.focus();
			oObject.select();
			return false;
		}
	}
	return true;
}*/
function CheckNumeric(oObject, sFieldName) {
	var ch;
	var sString;
	var bDecimal = false;
	
	if (!CheckNull(oObject, sFieldName)) return false;
	sString = Trim(oObject.value);
	for (i=1; i<=sString.length ;i++){
		ch = sString.substring(i-1, i);
		if ((ch < '0') || (ch >'9')) {
			if (ch != "."){
				alert("請輸入數值!");
				oObject.focus();
				oObject.select();
				return false;
			}
			else
			{
				if (bDecimal == false) bDecimal = true;
				else
				{
					alert("請輸入數值!");
					oObject.focus();
					oObject.select();
					return false;
				}
			}
		}
		else
		{
			if (ch == "."){
				if (bDecimal == false) bDecimal = true;
				else
				{
					alert("請輸入數值!");
					oObject.focus();
					oObject.select();
					return false;
				}
			}
		}
	}
	return true;
}

//=======================================================
//== Check if object length exceeds max. length allowed.
//== Parameters	: oObject = Checked object.
//==			: lLen = Max length allowed.
//==			: sFieldName = Object name.
function CheckMaxLen(oObject, lLen, sFieldName) {
	var sString;

	sString = Trim(oObject.value);
	if (sString.length > lLen) {
		alert("請在" + sFieldName + "輸入最多" + lLen + "字元！");
		oObject.focus();
		oObject.select();
		return false;
	}
	if (CheckChar(oObject) == false) return false;
	return true;
}

//=======================================================
//== Check if object length is less than length allowed.
//== Parameters	: oObject = Checked object.
//==			: lLen = Min. length allowed.
//==			: sFieldName = Object name.
function CheckMinLen(oObject, lLen, sFieldName) {
	var sString;

	sString = Trim(oObject.value);
	if (sString.length <  lLen) {
		alert("請在" + sFieldName + "輸入最多" + lLen + "字元！");
		oObject.focus();
		oObject.select();
		return false;
	}
	if (CheckChar(oObject) == false) return false;
	return true;
}


//=======================================================
//== Check if any line break in text box.
//== Parameters	: oObject = Checked object.
//==			: sFieldName = Object name.
function CheckLinBrk(oObject, sFieldName) {
	var sString;

	sString = Trim(oObject.value);
	for (i=0; i < sString.length; i++) {
		if (sString.charCodeAt(i) == 13) {
			alert("隔行在這" + sFieldName + "是不容許的！");
			oObject.focus();
			oObject.select();
			return false
		}
	}
	return true;
}

//=======================================================
//== Check if the entered value is email format.
//== Parameters	: oObject = Checked object.
function CheckEmail(oObject) {
	var sEmail;

	sEmail=oObject.value;
	if (sEmail.indexOf("@")<2){
		alert("請輸入有效電郵！");
		oObject.focus();
		oObject.select();
		return false;
	}
/*	if ((sEmail.indexOf(".com")<5)&&(sEmail.indexOf(".org")<5)
		&&(sEmail.indexOf(".gov")<5)&&(sEmail.indexOf(".net")<5)
		&&(sEmail.indexOf(".mil")<5)&&(sEmail.indexOf(".edu")<5)){
		alert("Please enter value email address");
		oObject.focus();
		oObject.select();
		return false;
	}
*/
	if (CheckChar(oObject) == false) return false;
	return true;
}

//=======================================================
//== Check if object value is phone (include CheckNull).
//== Parameters	: oObject = Checked object.
function CheckPhone(oObject, sFieldName) {
	var ch;
	var sString;
	
	if (!CheckNull(oObject, sFieldName)) return false;
	sString = Trim(oObject.value);
	for (i=1; i<=sString.length ;i++){
		ch = sString.substring(i-1, i);
		if (((ch < '0') || (ch >'9')) && (ch != '-')) {
			alert("請輸入正確" + sFieldName + " !");
			oObject.focus();
			oObject.select();
			return false;
		}
	}
	return true;
}

//=======================================================
//== Check if the entered value is date. This include CheckNull feature.
//== Parameters : oYear = Year value object.
//==			: oMonth = Month value object.
//==			: oDay = Day value object.
function CheckDate(oYear, oMonth, oDay) {
	var oDate;
	var lDay;
	var lMth;
	var lYear;
	var lCentury;

	if (!CheckTextBox(oYear, "年份")) return false;
	else
	{
		if (oYear.value < 1000 || oYear.value > 9999){
			alert("請輸入正確年份！");
			oYear.focus();
			oYear.select();
			return false;
		}
	}
	if (!CheckTextBox(oMonth, "月份")) return false;
	else
	{
		if (oMonth.value < 1 || oMonth.value > 12){
			alert("請輸入正確月份!");
			oMonth.focus();
			oMonth.select();
			return false;
		}
	}
	if (!CheckTextBox(oDay, "日子")) return false;
	else
	{
		if (oDay.value < 1 || oDay.value > 31){
			alert("請輸入正確日子!");
			oDay.focus();
			oDay.select();
			return false;
		}
	}

	lDay = oDay.value;
	lMth = oMonth.value - 1;		//0=January, 11=December.
	lYear = oYear.value;
	if ((lYear >= 1900) && (lYear <= 1999)) lCentury = 1900;
	if ((lYear < 1900) || (lYear > 1999)) lCentury =0;

	oDate = new Date(lYear, lMth, lDay);

	if(((oDate.getYear()+lCentury) != lYear) || (oYear.value.length != 4)) {
		alert("請輸入正確年份！");
		oYear.focus();
		oYear.select();
		return false;
	}
	else
		if ((oDate.getMonth()+1) != oMonth.value) {
			alert ("請輸入正確日子！");
			oDay.focus();
			oDay.select();
			return false;
		}
		else
			if (oDate.getDate() != lDay) {
				alert("請輸入正確日子！");
				oDay.focus();
				oDay.select();
				return false;
			}
	return true;
}

//=======================================================
//== Check if To Date is earlier than From Date..
//== Parameters	: oFrYear = From Year value object.
//==			: oFrMonth = From Month value object.
//==			: oFrDay = From Day value object.
//==			: oToYear = To Year value object.
//==			: oToMonth = To Month value object.
//==			: oToDay = To Day value object.
function CheckDateRange(oFrYear, oFrMonth, oFrDay, oToYear, oToMonth, oToDay) {
	var oFrDate;
	var oToDate;
	
	oFrDate = new Date(oFrYear.value, (oFrMonth.value)-1, oFrDay.value);
	oToDate = new Date(oToYear.value, (oToMonth.value)-1, oToDay.value);
	
	//For Netscape 3, can't compare date range just using oFrDate and oToDate.
	//Therefore, need to convert date to millseconds for comparing.
	if (Date.parse(oToDate) <= Date.parse(oFrDate)) {
		alert("請輸入正確日子范圍！");
		oToMonth.focus();
		oToMonth.select();
		return false;
	}
	return true;
}

//=======================================================
//== Check if the Date is earlier than today.
//== Parameters	: oYear = Year value object.
//==			: oMonth = Month value object.
//==			: oDay = Day value object.
function CheckEarlyToday(oYear, oMonth, oDay){
	var oToDay = new Date();
	var oNextDate = new Date(oYear.value, (oMonth.value)-1, oDay.value);
	if (oNextDate < oToDay) {
		alert("請輸入大過今天的日期！");
		oDay.select();
		return false;
	}
	return true;
}

//=======================================================
//== Check if ' and | appear in input area.
//== Parameters	: oObject = Checked object.
function CheckChar(oObject){
	var sString;

	sString = Trim(oObject.value);
	for (i=0; i< sString.length; i++) {
		if ((sString.charAt(i) == "|") || (sString.charAt(i) == "'")){
			alert("符號 ' 及 | 是不容許的！");
			oObject.focus();
			oObject.select();
			return false;
		}
	}
	return true;
}

//=======================================================
//== Check value URL starting with http://
//== Parameters	: oObject = Checked object.
function CheckURL(oObject){
	var sString, ch;

	sString = Trim(oObject.value);
	ch = sString.substring(0, 7);
	if (ch != "http://" || sString.length < 10){
		alert("請輸入有效網址(必需是http://開頭)！");
		oObject.focus();
		oObject.select();
		return false;
	}
	else{
		for (i=0; i< sString.length; i++) {
			if (sString.charAt(i) == " "){
				alert("請輸入有效網址(不能輸入空位)！");
				oObject.focus();
				oObject.select();
				return false;
			}
		}
	}
	
	return true;
}

//=======================================================
//== Trim trailing spaces.
//== Parameters	: sString = Checked string.
function TrimBack(sString){
	var i; 
	var ch;
	
	for (i=sString.length; i>=1; i--){
		ch = sString.substring(i-1,i);
		if (ch != ' '){
			return sString.substring(0,i);
			break;
		}
	}
	return sString.substring(0,0);
}

//=======================================================
//== Trim leading spaces.
//== Parameters : sString = Checked string.
function TrimFront(sString){
	var i;
	var ch;
	
	for (i=1; i<= sString.length ;i++){
		ch = sString.substring(i-1, i);
		if (ch != ' '){
			return sString.substring(i-1, sString.length);
		}
	}

	return sString.substring(0,0);
}

//=======================================================
//== Trim leading & trailing spaces.
//== Parameters : sString = Checked string.
function Trim(sString){
	return TrimFront(TrimBack(sString));
}

//=======================================================
//== Check textbox object (including CheckNull, CheckChar).
//== Parameters	: oObject = Checked object.
//==			: sFieldName = Object name.
function CheckTextBox(oObject, sFieldName) {
	if (CheckNull(oObject, sFieldName) == false) return false;
	if (CheckChar(oObject) == false) return false;
	return true;
}

//=======================================================
//== Check none required string type textbox object (including CheckChar).
//== Parameters	: oObject = Checked object.
function CheckNRText(oObject){
	if (Trim(oObject.value) != ""){
		if (CheckChar(oObject) == false) return false;
	}
	return true;
}

//=======================================================
//== Check none required numeric type textbox object (including CheckNumeric).
//== Parameters	: oObject = Checked object.
function CheckNRNum(oObject){
	if (Trim(oObject.value) != ""){
		if (CheckNumeric(oObject) == false) return false;
	}
	return true;
}

//=======================================================
//== Check none required phone textbox object (including CheckPhone).
//== Parameters	: oObject = Checked object.
function CheckNRPhone(oObject, sFieldName){
	if (Trim(oObject.value) != ""){
		if (CheckPhone(oObject, sFieldName) == false) return false;
	}
	return true;
}

//=======================================================
//== Check none required URL value object (including CheckURL).
//== Parameters	: oObject = Checked object.
function CheckNRURL(oObject){
	if (Trim(oObject.value) != "http://"){
		if (CheckURL(oObject) == false) return false;
	}
	return true;
}

//=======================================================
//== Check none required email value object (including CheckEmail).
//== Parameters	: oObject = Checked object.
function CheckNREmail(oObject){
	if (Trim(oObject.value) != ""){
		if (CheckEmail(oObject) == false) return false;
	}
	return true;
}

//=======================================================
//== Check text area object (including CheckChar, CheckMaxLin, CheckLinBrk).
//== Parameters	: oObject = Checked object.
//==			: lLen = Max. length allowed.
//==			: sFieldName = Object name.
function CheckTextArea(oObject, lLen, sFieldName) {
	if (CheckChar(oObject) == false) return false;
	if (CheckLinBrk(oObject, sFieldName) == false) return false;
	if (CheckMaxLen(oObject, lLen, sFieldName) == false) return false;
	return true;
}

//=======================================================
//== General function on lauching searching criteria window based on different data type.
//== Parameters	:
//==			:oWin is the window object reference.
//==			:sWinName is the string name of the new window.
//==			:sCaption is the caption that's displayed on the criteria window.
//==			:sPageURL is the URL of the target page.
//==			:sObjPath is the HTML object reference that'll be displayed back the searching criteria.
//==			:sHiddenObjPath is the hidden HTML object reference that'll be submitted back to server for SQL building.
//==			:sFieldName is the field name in the table.
//== oWin is the object variable of the child form while sWinName is the string name of child name.
//== Difference is oWin will be used to reference the child form object and sWinName is the string for clearing child form
//== object referenced on parent form on child "onUnload" event.
//== sCatpion is the name of the field that's displayed on the child form.
function LaunchCriteriaWin(oWin, sWinName, sCaption, sPageURL, sObjPath, sHiddenObjPath, sFieldName){
	var sURL;
	var sWinProperty = "height=200,width=400,status=no,toolbar=no,menubar=no,location=no";

	//-check for the existence of the window.  If exists, bring it to the front.
	//-we check in two ways: 1) is the oWin object null? It should be if we've closed the window
	// because we set oWin to null in a function called by the onunload handler.
	// And 2) is the oWin.closed property true? We check this second state because Netscape 4.5
	// doesn't call the onunload handler when you use the exit button on the window itself. So we
	// check the closed property for the benefit of Nav 4.5. Why not just check that? Because IE4
	// chokes on it if you do.  It's probably a good idea to check both anyway because "closed"
	// wouldn't be set before the first time the user launches the window. IE4 doesn't choke if we
	// check the status of oWin first.	
	
	if (oWin == null || oWin.closed){
		sURL = sPageURL + "?sCaption=" + sCaption + "&sObjPath=opener.document." + sObjPath + "&sWinName=" + sWinName;
		sURL = sURL + "&sHiddenObjPath=opener.document." + sHiddenObjPath + "&sFieldName=" + sFieldName;
		oWin = window.open(sURL, null, sWinProperty);
		if (oWin.opener == null)			// set the opener property manually for Nav 2.0.
				oWin.opener = window;
	}
	else
		oWin.focus();
	
	return oWin;	//Return object reference back for checking if new window object's new.
}

//=======================================================
//== Convert to update time format.
//== Parameters	: sYear = Year value.
//==			: sMonth = Month value.
//==			: sDay = Day value.
//==			: sHour = Hour value.
//==			: sMinute = Minute value.
//==			: sSecond = Second value.
function ConvertToUpdTime(sYear, sMonth, sDay, sHour, sMinute, sSecond){
	var sUpdTime;
	
	if (sYear.length == 2) sUpdTime = "19" + sYear;
	else
		sUpdTime = sYear;
	if (sMonth.length == 1) sUpdTime = sUpdTime + "0" + sMonth;
	else
		sUpdTime = sUpdTime + sMonth;
	if (sDay.length == 1) sUpdTime = sUpdTime + "0" + sDay;
	else
		sUpdTime = sUpdTime + sDay;
	if (sHour.length == 1) sUpdTime = sUpdTime + "0" + sHour;
	if (sHour.length == 2) sUpdTime = sUpdTime + sHour;
	if (sMinute.length == 1) sUpdTime = sUpdTime + "0" + sMinute;
	if (sMinute.length == 2) sUpdTime = sUpdTime + sMinute;
	if (sSecond.length == 1) sUpdTime = sUpdTime + "0" + sSecond;
	if (sSecond.length == 2) sUpdTime = sUpdTime + sSecond;
	return sUpdTime;
}

//=======================================================
//== Check time format. (Include CheckNull, CheckNumeric)
//== Parameters	: oHour = Hour object reference.
//==			: oMinuite = Minute object reference.
function CheckTime(oHour, oMinute){
	//Check hour.
	if (!CheckNull(oHour, "hour") || !CheckNumeric(oHour)) return false;
	else
	{
		if (oHour.value < 0 || oHour.value > 24){
			alert("請輸入正確小時！");
			oHour.focus();
			oHour.select();
			return false;
		}
	}
	
	//Check minute.
	if (!CheckNull(oMinute, "minute") || !CheckNumeric(oMinute)) return false;
	else
	{
		if (oMinute.value < 0 || oMinute.value > 59){
			alert("請輸入正確分鐘！");
			oMinute.focus();
			oMinute.select();
			return false;
		}
	}
	return true;
}

//=======================================================
//== Check the range of From-hh:mm to To-hh:mm. (Include CheckNull, CheckNumeric)
//== Parameters	: oFromHour = From-hour object reference.
//==			: oFromMin = From-minute object reference.
//==			: oToHour = To-hour object reference.
//==			: oToMin = To-minute object reference.
function CheckTimeRange(oFromHour, oFromMin, oToHour, oToMin){
	//Check from time.
	if (!CheckTime(oFromHour, oFromMin)) return false;
	//Check to time.
	if (!CheckTime(oToHour, oToMin)) return false;
	
	//Check time range.
	if (oFromHour.value >= oToHour.value){
		alert("請輸入正確時間范圍！");
		oToHour.focus();
		oToHour.select();
		return false;
	}
	else
	{
		if ((oFromHour.value == oToHour.value) && (oFromMin.value > oToMin.value)){
			alert("請輸入正確時間范圍！");
			oToHour.focus();
			oToHour.select();
			return false;
		}
	}
	return true;
}

//=====================================================
//== Check numeric range
//== Parameters : oFrom
//==			: oTo
function CheckNumRange(oFrom, oTo){
	var iFrom;
	var iTo;
	iFrom = Number(oFrom.value);
	iTo = Number(oTo.value);
	if (iFrom >= iTo){
		alert("請輸入正確數字范圍！");
		oFrom.select();
		return false;
	}
	return true;
}

//=====================================================
//== Perform same function as VBScript combo box's selectedByText().
//== Select the combo box value to sText.
//== Parameters : oCbo = document.form.combo.
//==			: sText = Text for select.
function SelectByText(oCbo, sText){
	var i;
	
	for(i=0; i< oCbo.length; i++){
		if (oCbo.options[i].text == sText){
			oCbo.selectedIndex = i;
			return true;
		}
	}
	
}

//=====================================================
//== Check if drop down box value is "none" or blank.
//== Parameters : oCbo = document.form.combo.
//==			: sText = Text for select.
function CheckCboNull(oCbo, sText){
	if (oCbo.options[oCbo.selectedIndex].value == "none" || oCbo.options[oCbo.selectedIndex].value == "" ||
		oCbo.options[oCbo.selectedIndex].value == "0"){
		alert("請選擇" + sText + "!");
		oCbo.focus();
		return false;
	}
	return true;
}

//=====================================================
//== Check date & time range together
//== Parameters : oFrmYear, oFrmMonth, oFrmDate, oFrmHour, oFrmMin
//==			: oToYear, oToMonth, oToDate, oToHour, oToMin
function CheckDateTimeRange(oFrmYear, oFrmMonth, oFrmDate, oFrmHour, oFrmMin, oToYear, oToMonth, oToDate, oToHour, oToMin){
	var today = new Date();

	if(isNaN(oFrmYear.value) || oFrmYear.value < 2000 || oFrmYear.value > today.getYear()){
		alert("請輸入正確日期及時間范圍！");
		oFrmYear.focus();
		return false;
	}
	if(isNaN(oFrmMonth.value) || oFrmMonth.value < 1 || oFrmMonth.value > 12){
		alert("請輸入正確日期及時間范圍！");
		oFrmMonth.focus();
		return false;
	}
	if(isNaN(oFrmDate.value) || oFrmDate.value < 1 || oFrmDate.value > 31){
		alert("請輸入正確日期及時間范圍！");
		oFrmDate.focus();
		return false;
	}
	if(isNaN(oFrmHour.value) || oFrmHour.value < 0 || oFrmHour.value > 23){
		alert("請輸入正確日期及時間范圍！");
		oFrmHour.focus();
		return false;
	}
	if(isNaN(oFrmMin.value) || oFrmMin.value < 0 || oFrmMin.value > 59){
		alert("請輸入正確日期及時間范圍！");
		oFrmMin.focus();
		return false;
	}
	if(isNaN(oToYear.value) || oToYear.value < 2000 || oToYear.value > today.getYear()){
		alert("請輸入正確日期及時間范圍！");
		oToYear.focus();
		return false;
	}
	if(isNaN(oToMonth.value) || oToMonth.value < 1 || oToMonth.value > 12){
		alert("請輸入正確日期及時間范圍！");
		oToMonth.focus();
		return false;
	}
	if(isNaN(oToDate.value) || oToDate.value < 1 || oToDate.value > 31){
		alert("請輸入正確日期及時間范圍！");
		oToDate.focus();
		return false;
	}
	if(isNaN(oToHour.value) || oToHour.value < 0 || oToHour.value > 23){
		alert("請輸入正確日期及時間范圍！");
		oToHour.focus();
		return false;
	}
	if(isNaN(oToMin.value) || oToMin.value < 0 || oToMin.value > 59){
		alert("請輸入正確日期及時間范圍！");
		oToMin.focus();
		return false;
	}
	
	var frmDate = new Date(oFrmYear.value, oFrmMonth.value-1, oFrmDate.value, oFrmHour.value, oFrmMin.value);
	var toDate = new Date(oToYear.value, oToMonth.value-1, oToDate.value, oToHour.value, oToMin.value);

	if(frmDate > toDate){
		alert("請輸入正確日期及時間范圍！");
		oFrmYear.focus();
		return false;
	}
	return true;
}