//Updated by William at 2000/11/26
//=======================================================
//== 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.
//== 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.
//== CheckTimeRage		- 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.
//=======================================================
//== 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("Please fill in the " + 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("Please fill in the " + 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("Please fill in numeric value.");
			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("Please fill in numeric value.");
				oObject.focus();
				oObject.select();
				return false;
			}
			else
			{
				if (bDecimal == false) bDecimal = true;
				else
				{
					alert("Please fill in numeric value.");
					oObject.focus();
					oObject.select();
					return false;
				}
			}
		}
		else
		{
			if (ch == "."){
				if (bDecimal == false) bDecimal = true;
				else
				{
					alert("Please fill in numeric value.");
					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("Please enter at most " + lLen + " characters in " + sFieldName + " field.");
		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("Please enter at least  " + lLen + " characters in " + sFieldName + " field.");
		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("Line break is not allowed in " + 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("Please enter valid email address.");
		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("Please fill in correct " + 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, "year")) return false;
	else
	{
		if (oYear.value < 1000 || oYear.value > 9999){
			alert("Please fill in correct year value!");
			oYear.focus();
			oYear.select();
			return false;
		}
	}
	if (!CheckTextBox(oMonth, "month")) return false;
	else
	{
		if (oMonth.value < 1 || oMonth.value > 12){
			alert("Please fill in correct month value!");
			oMonth.focus();
			oMonth.select();
			return false;
		}
	}
	if (!CheckTextBox(oDay, "day")) return false;
	else
	{
		if (oDay.value < 1 || oDay.value > 31){
			alert("Please fill in correct day value!");
			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("Please fill in correct year value!");
		oYear.focus();
		oYear.select();
		return false;
	}
	else
		if ((oDate.getMonth()+1) != oMonth.value) {
			alert ("Please fill in correct day value!");
			oDay.focus();
			oDay.select();
			return false;
		}
		else
			if (oDate.getDate() != lDay) {
				alert("Please fill in correct day value!");
				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("Please fill in correct date range!");
		oToMonth.focus();
		oToMonth.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("Character ' and | are not allowed!");
			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("Please fill in value URL starting with http://!");
		oObject.focus();
		oObject.select();
		return false;
	}
	else{
		for (i=0; i< sString.length; i++) {
			if (sString.charAt(i) == " "){
				alert("Please fill in value URL with no space in between!");
				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 (oObject.value != ""){
		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("Please fill in correct hour value!");
			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("Please fill in correct minute value!");
			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("Please fill in correct time range!");
		oToHour.focus();
		oToHour.select();
		return false;
	}
	else
	{
		if ((oFromHour.value == oToHour.value) && (oFromMin.value > oToMin.value)){
			alert("Please fill in correct time range!");
			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("Please fill in corrent numeric range.");
		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 == ""){
		alert("Please select " + sText + "!");
		oCbo.focus();
		return false;
	}
	return true;
}

function CheckImageFileName(oObject, sFieldName){
	
	var sPreviewImageFilePath = oObject.value;
	sPreviewImageFilePath = sPreviewImageFilePath.replace(/\//g,"\\"); 
	var sPreviewImageFilename = sPreviewImageFilePath.substring(sPreviewImageFilePath.lastIndexOf('\\') + 1).toLowerCase(); 	
	var numdots=0; 
	for(i=0;i<sPreviewImageFilename.length;i++){
	  if(sPreviewImageFilename.charAt(i)=="."){ 
	    numdots=numdots + 1; 
	  } 
 	} 
  if(numdots!=1){ 
    alert("The filename must have one and only one '.' " + "in " + sFieldName + "!"); 
    return false; 
  }
  
  var sPreviewExt = sPreviewImageFilename.substring(sPreviewImageFilename.lastIndexOf('.') + 1);
	if (sPreviewExt != "jpg" && sPreviewExt!="gif"){
		alert("Please upload either jpg or gif format picture in " + sFieldName + "!");
		return false;
	} 
	
	for (i = 0; i < (sPreviewImageFilename.length); i++) { 
   tmp = sPreviewImageFilename.charAt(i); 
   if (tmp.match(/[a-z_0-9-\.]+/)) { 
   }else { 
   				alert("Sorry!. Please rename your file with only letters," 
         				+ " numbers, hyphens and underscores and no spaces " + "in " + sFieldName + "Thanks!");
         return false;
	 } 
  } 

	return true;
}