// ----------------------------------------------------------------------------
// Trim leading and trailing spaces.
// ----------------------------------------------------------------------------
function _Trim(strInputVal)
{
	// Replace leading and trailing spaces with the empty string.
	if (strInputVal != "")
	{
		return strInputVal.replace(/(^\s*)|(\s*$)/g, "");
	}
	else
	{
		return strInputVal;
	}
}

// ----------------------------------------------------------------------------
// Prevent against cross-site scripting attack. Delete dangerous code.
// Remove % < > [ ] { } ; & + - " ' ( )
// ----------------------------------------------------------------------------
function _CleanCode(strInputVal)
{
	if (strInputVal != "")
	{
		return strInputVal.replace(/\<|\>|\"|\'|%|\;|\(|\)|\&|\+|\-/g, "");
	}
	else
	{
		return strInputVal;
	}
}

// ----------------------------------------------------------------------------
// Make input safe for SQL insert.
// Replace ' with ''
// ----------------------------------------------------------------------------
function _SQLStringable(strInputVal)
{
	if (strInputVal != "")
	{
		return strInputVal.replace(/'/g, "''");
	}
	else
	{
		return strInputVal;
	}
}

// ----------------------------------------------------------------------------
// Check data type is integer.
// Not using parseInt method as parseInt("12abc") return 12.
// ----------------------------------------------------------------------------
function _IsInteger(strInputVal)
{
	// Function to check whether the input is integer
	var strInputStr = strInputVal.toString();
	for (var i = 0; i < strInputStr.length; i++)
	{
		var chrOneChar = strInputStr.charAt(i);
		if ((i == 0) && (chrOneChar == "-"))
			continue;
		if ((chrOneChar < "0") || (chrOneChar > "9"))
			return false;
	}
	return true;
}

// ----------------------------------------------------------------------------
// Check data type is double.
// Not using parseFloat method as parseFloat("1.2abc") return 1.2.
// ----------------------------------------------------------------------------
function _IsDouble(strInputVal)
{
	// Function to check whether the input is numeric
	var blnOneDecimal = false;
	var strInputStr = strInputVal.toString();
	for (var i = 0; i < strInputStr.length; i++)
	{
		var chrOneChar = strInputStr.charAt(i);
		if ((i == 0) && (chrOneChar == "-"))
			continue;
		if ((chrOneChar == ".") && !blnOneDecimal)
		{
			blnOneDecimal = true;
			continue;
		}
		if ((chrOneChar < "0") || (chrOneChar > "9"))
			return false;
	}
	return true;
}

// ----------------------------------------------------------------------------
// Check data type is string.
// ----------------------------------------------------------------------------
function _IsString(strInputVal)
{
	// Function to check whether the input is alphanumeric
	var strInputStr = strInputVal.toString();
	for (var i = 0; i < strInputStr.length; i++)
	{
		var chrOneChar = strInputStr.charAt(i);
		if ((("0" <= chrOneChar) && (chrOneChar <= "9")) || (("a" <= chrOneChar) && (chrOneChar <= "z")) || (("A" <= chrOneChar) && (chrOneChar <= "Z")) || (" " == chrOneChar) || ("_" == chrOneChar))
		{
			// No op.
		}
		else
			return false;
	}
	return true;
}

// ----------------------------------------------------------------------------
// Check email.
// ----------------------------------------------------------------------------
function _IsEmail(strEmail)
{
	// function to validate email address
	var strEmailPattern = /^(\".*\"|[\w-_\.]*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z0-9]([\w-_\.]*)(\.[A-Za-z0-9]([\w-_\.]*))+)$/;

	// Extract all email
	var strEmailList = _Split(strEmail, ";");
	var i;
	for (i = 0; i < strEmailList.length; ++i)
	{
		var strOneEmail = _Trim(strEmailList[i]);
		var arrMatch = strOneEmail.match(strEmailPattern);
		if(arrMatch == null)
		{
			return false;
		}
		
		// make sure the IP address domain is valid
		var arrIP = arrMatch[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
		if(arrIP != null)
		{
			for(var i = 1; i <= 4; i++)
			{
				if(arrIP[i] > 255)
				{
					return false;
				}
			}
		}
	}
	return true;
}

// ----------------------------------------------------------------------------
// Check date.
// ----------------------------------------------------------------------------
function _IsDate(strForm, strDateControl, strErrorMessage)
{
	// Construct date field.
	var lngDay;
	eval("lngDay = strForm." + strDateControl + "_day.options[strForm." + strDateControl + "_day.selectedIndex].value;");

	var lngMonth;
	eval("lngMonth = strForm." + strDateControl + "_month.options[strForm." + strDateControl + "_month.selectedIndex].value;");

	var lngYear;
	eval("lngYear = strForm." + strDateControl + "_year.options[strForm." + strDateControl + "_year.selectedIndex].value;");

	if ((lngDay == "") && (lngMonth == "") && (lngYear == ""))
	{
		eval("strForm." + strDateControl + ".value = '';");
		return true;	
	}
	else if (((lngDay == "") && ((lngMonth != "") || (lngYear != ""))) || ((lngMonth == "") && ((lngDay != "") || (lngYear != ""))) || ((lngYear == "") && ((lngDay != "") || (lngMonth != ""))))
	{
		var tmpMonthName = _MonthName(lngMonth);
		alert(strErrorMessage + "\n" + lngDay + "-" + tmpMonthName + "-" + lngYear + " is not a valid date");
		return false;
	}
	else
	{
		if (_CheckDateHelper(lngDay, lngMonth, lngYear, strErrorMessage) == false)
			return false;
		else
		{
			var dateField = lngMonth + "/" + lngDay + "/" + lngYear;
			eval("strForm." + strDateControl + ".value = '" + dateField + "';");
			return true;
		}
	}
}

function _IsPartialDate(strForm, strDateControl, strErrorMessage)
{
	// Construct date field.
	var lngDay;
	eval("lngDay = " + strForm + "." + strDateControl + "_day.options[" + strForm + "." + strDateControl + "_day.selectedIndex].value;");

	var lngMonth;
	eval("lngMonth = " + strForm + "." + strDateControl + "_month.options[" + strForm + "." + strDateControl + "_month.selectedIndex].value;");

	var lngYear;
	eval("lngYear = " + strForm + "." + strDateControl + "_year.options[" + strForm + "." + strDateControl + "_year.selectedIndex].value;");

	if ((lngDay == "") && (lngMonth == "") && (lngYear == ""))
	{
		return true;	
	}
	else if (((lngDay == "") && ((lngMonth != "") || (lngYear != ""))) || ((lngMonth == "") && ((lngDay != "") || (lngYear != ""))) || ((lngYear == "") && ((lngDay != "") || (lngMonth != ""))))
	{
		return true;
	}
	else
	{
		if (_CheckDateHelper(lngDay, lngMonth, lngYear, strErrorMessage) == false)
			return false;
		else
		{
			return true;
		}
	}
}

function _CheckDateHelper(lngDay, lngMonth, lngYear, strErrorMessage)
{
	if ((lngMonth < 1) || (lngMonth > 12))
	{
		// check month range
		alert(strErrorMessage + "\nMonth must be between 1 and 12.");
		return false;
	}
	if ((lngDay < 1) || (lngDay > 31))
	{
		alert(strErrorMessage + "\nDay must be between 1 and 31.");
		return false;
	}
	if (((lngMonth == 4) || (lngMonth == 6) || (lngMonth == 9) || (lngMonth == 11)) && (lngDay == 31))
	{
		var strMonth = _MonthName(lngMonth);
		alert(strErrorMessage + "\nMonth " + strMonth + " doesn't have 31 days!");
		return false;
	}
	if (lngMonth == 2)
	{
		// check for february 29th
		var blnIsleap = ((lngYear % 4) == 0 && ((lngYear % 100) != 0 || (lngYear % 400) == 0));
		if ((lngDay > 29) || ((lngDay == 29) && !blnIsleap))
		{
			alert(strErrorMessage + "\nFebruary " + lngYear + " doesn't have " + lngDay + " days!");
			return false;
		}
	}
	return true;  // date is valid
}

function _MonthName(lngMonth)
{
	if (lngMonth == 1)
		return "January";
	else if (lngMonth == 2)
		return "Febuary";
	else if (lngMonth == 3)
		return "March";
	else if (lngMonth == 4)
		return "April";
	else if (lngMonth == 5)
		return "May";
	else if (lngMonth == 6)
		return "June";
	else if (lngMonth == 7)
		return "July";
	else if (lngMonth == 8)
		return "August";
	else if (lngMonth == 9)
		return "September";
	else if (lngMonth == 10)
		return "October";
	else if (lngMonth == 11)
		return "November";
	else if (lngMonth == 12)
		return "December";
	else
		return "null";
}

// ----------------------------------------------------------------------------
// Check valid telephone number.
// Only number + space + dash.
// ----------------------------------------------------------------------------
function _IsTelephone(strInputVal)
{
	// Function to check whether the input is integer
	var strInputStr = strInputVal.toString();
	for (var i = 0; i < strInputStr.length; i++)
	{
		var chrOneChar = strInputStr.charAt(i);
		if (i == 0)
			continue;

		if (chrOneChar < "0" || chrOneChar > "9")
		{
			if (chrOneChar != " " && chrOneChar != "-")
			{
				return false;
			}
		}
	}
	return true;
}

// ----------------------------------------------------------------------------
// Check is boolean.
// ----------------------------------------------------------------------------
function _IsBoolean(strInputVal)
{
	if ((strInputVal == "Y") || (strInputVal == "y") || (strInputVal == "N") || (strInputVal == "n"))
	{
		return true;
	}
	else
	{
		return false;
	}
}

// ----------------------------------------------------------------------------
// Check is date.
// Format: mm/dd/yyyy
// ----------------------------------------------------------------------------
function _IsDateType(strInputVal)
{
	var arrDate = _Split(strInputVal, "/")
	if (arrDate.length == 3)
	{
		var lngMonth = arrDate[0];
		var lngDay = arrDate[1];
		var lngYear = arrDate[2];

		if (_IsInteger(lngDay) && _IsInteger(lngDay) && _IsInteger(lngDay))
		{
			return _CheckDateHelper(lngDay, lngMonth, lngYear, strInputVal + " is not a valid date.")
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

// ----------------------------------------------------------------------------
// Check number of checkboxes checked.
// ----------------------------------------------------------------------------
function _CheckboxCount(objCheckbox, lngCheckboxCount)
{
	var lngNumChecked = 0;

	if (lngCheckboxCount > 1)
	{
		for (var i = 0; i < objCheckbox.length; ++i)
		{
			if (objCheckbox[i].checked)
				++lngNumChecked;
		}
	}
	else
	{
		if (objCheckbox.checked)
			++lngNumChecked;
	}

	return lngNumChecked;
}

// ----------------------------------------------------------------------------
// Focus on an element.
// ----------------------------------------------------------------------------
function _FocusElement(objControl)
{
	objControl.focus();
	objControl.select();
}

// ----------------------------------------------------------------------------
// To determine if it is date data type.
// ----------------------------------------------------------------------------
function _IsDateDataType(objForm, strDateControl)
{
	var strDateDay = strDateControl + "_day";
	var strDateMonth = strDateControl + "_month";
	var strDateYear = strDateControl + "_year";

	if (_IsExist(objForm, strDateDay) && _IsExist(objForm, strDateMonth) && _IsExist(objForm, strDateYear))
		return true;
	else
		return false;
}

// ----------------------------------------------------------------------------
// Convert date to number for comparison purpose.
// ----------------------------------------------------------------------------
function _GetDateValue(objForm, strDateControl)
{
	// Construct date field.
	var lngDay;
	eval("lngDay = objForm." + strDateControl + "_day.options[objForm." + strDateControl + "_day.selectedIndex].value;");

	var lngMonth;
	eval("lngMonth = objForm." + strDateControl + "_month.options[objForm." + strDateControl + "_month.selectedIndex].value;");

	var lngYear;
	eval("lngYear = objForm." + strDateControl + "_year.options[objForm." + strDateControl + "_year.selectedIndex].value;");

	var lngDateValue = (10000 * lngYear) + (100 * lngMonth) + (1 * lngDay);
	return lngDateValue;
}

// ----------------------------------------------------------------------------
// Limit the length of input.
// ----------------------------------------------------------------------------
function _LimitLength(objControl, lngMaxLength)
{
	if (objControl.value.length > lngMaxLength)
	{
		objControl.value = objControl.value.substring(0, lngMaxLength);
	}
}

// ----------------------------------------------------------------------------
// Remember current sort order for the column.
// ----------------------------------------------------------------------------
function _TagSortColumn(strColumn, strOrder)
{
	var objCookie = new mrtcCookie();
	objCookie.Save(strColumn, escape(strOrder), 30);
}

// ----------------------------------------------------------------------------
// Check data type is month.
// ----------------------------------------------------------------------------
function _IsMonth(varValue)
{
	if (!_IsInteger(varValue))
	{
		return false;
	}
	else
	{
		var intValue = parseInt(varValue);
		return _IsInRange(intValue, true, 1, true, 12)
	}
}

// ----------------------------------------------------------------------------
// Check data type is day.
// ----------------------------------------------------------------------------
function _IsDay(varValue)
{
	if (!_IsInteger(varValue))
	{
		return false;
	}
	else
	{
		var intValue = parseInt(varValue);
		return _IsInRange(intValue, true, 1, true, 31)
	}
}

// ----------------------------------------------------------------------------
// Check data type is tinyint.
// ----------------------------------------------------------------------------
function _IsTinyInt(varValue)
{
	if (!_IsInteger(varValue))
	{
		return false;
	}
	else
	{
		var intValue = parseInt(varValue);
		return _IsInRange(intValue, true, 0, true, 255)
	}
}

// ----------------------------------------------------------------------------
// Check data type is smallint.
// ----------------------------------------------------------------------------
function _IsSmallInt(varValue)
{
	if (!_IsInteger(varValue))
	{
		return false;
	}
	else
	{
		var intValue = parseInt(varValue);
		return _IsInRange(intValue, true, -32768, true, 32767)
	}
}

// ----------------------------------------------------------------------------
// Check data type is int.
// ----------------------------------------------------------------------------
function _IsInt(varValue)
{
	if (!_IsInteger(varValue))
	{
		return false;
	}
	else
	{
		var intValue = parseInt(varValue);
		return _IsInRange(intValue, true, -2147483648, true, 2,147,483,647)
	}
}

// ----------------------------------------------------------------------------
// Check that varValue is between varMinValue and varMaxValue.
// ----------------------------------------------------------------------------
function _IsInRange(varValue, blnMinInclusive, varMinValue, blnMaxInclusive, varMaxValue)
{
	var blnResult;
	if (blnMinInclusive)
	{
		blnResult = eval(varValue + ">=" + varMinValue);
	}
	else
	{
		blnResult = eval(varValue + ">" + varMinValue);
	}

	if (blnResult)
	{
		if (blnMaxInclusive)
		{
			blnResult = eval(varValue + "<=" + varMaxValue);
		}
		else
		{
			blnResult = eval(varValue + "<" + varMaxValue);
		}
	}

	return blnResult;
}

// ----------------------------------------------------------------------------
// Automatic convert input to upper case.
// ----------------------------------------------------------------------------
function _AutoUpper(objText)
{
	objText.value = objText.value.toUpperCase();
}