<!--

//------------------------------------------------------------------------
// LTrim()
// ===================
//------------------------------------------------------------------------
function LTrim(strVal) {
	for (var i=0; (i<=strVal.length); i++){
		if (strVal.charAt(i)!=" "){
			break
		}
	}
	return strVal.substr(i)
}

//------------------------------------------------------------------------
// RTrim()
// ===================
//------------------------------------------------------------------------
function RTrim(strVal) {
	for (var i=(strVal.length-1); (i>=0); i--){
		if (strVal.charAt(i)!=" "){
			break
		}
	}
	return strVal.substr(0,i+1)
}

//------------------------------------------------------------------------
// Trim()
// ======
//------------------------------------------------------------------------
function Trim(strVal) {
	return LTrim(RTrim(strVal))
}

//-------------------------------------------------------------------------
// IsDate()
// ============
//-------------------------------------------------------------------------
var arrDaysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31)

function IsDate(strDate){
	//setting standard date format
	while (strDate.indexOf("-") != -1){
		strDate = replaceString(strDate,"-","/")
	}

	var delim1 = strDate.indexOf("/")
	var delim2 = strDate.lastIndexOf("/")

	if (delim1 != -1 && delim1 == delim2) {
		// only one delimeter - invalid date format
		return false
	}

	if (delim1 != -1) {
		// there are delimeters; extract date components
		var mm		= parseInt(strDate.substring(0,delim1),10)
		var dd		= parseInt(strDate.substring(delim1+1,delim2),10)
		var yyyy	= parseInt(strDate.substr(delim2+1),10)
	} else {
		// there are no delimeters
		var mm		= parseInt(strDate.substring(0,2),10)
		var dd		= parseInt(strDate.substring(2,4),10)
		var yyyy	= parseInt(strDate.substr(4),10)
	}

	if (isNaN(mm) || isNaN(dd) || isNaN(yyyy)) {
		// there is a non-numeric character in one of the date components
		return false
	}

	if (mm < 1 || mm > 12){
		// month value is not 1 thru 12
		return false
	}

	if (yyyy<1000){
		// year value is not 4 digits
		return false
	}

	if (mm == 2) {
		if ((0==yyyy%4 && 0 != yyyy%100) || 0==yyyy%400 ){
			if (dd < 1 || dd > 29){
				// days out of range
				return false
			}
		} else {
			if (dd < 1 || dd > 28){
				// days out of range
				return false
			}
		}
	} else {
		if (dd < 1 || dd > arrDaysInMonth[mm-1]){
			// days out of range
			return false
		}
	}

	return true
}

//------------------------------------------------------------------------
// IsPositiveInteger()
// ===================
//------------------------------------------------------------------------
function IsPositiveInteger(inputVal){

	inputVal = LTrim(RTrim(inputVal))

	var strInput = inputVal.toString()
	for (var i=0; i < strInput.length; i++){
		var strChar = strInput.charAt(i)
		if (strChar < "0" || strChar > "9"){
			return false
		}
	}
	return true
}

//-------------------------------------------------------------------------
// IsValidDateRange(FromDate, ToDate) checks if the date range is valid.
// FromDate and ToDate are valid dates in the format mm/dd/yyyy
// ============
//-------------------------------------------------------------------------
function IsValidDateRange(FromDate, ToDate)
{
	var delim1 = FromDate.indexOf("/")
	var delim2 = FromDate.lastIndexOf("/")
	var delim3 = ToDate.indexOf("/")
	var delim4 = ToDate.lastIndexOf("/")

	var month1 = parseInt(FromDate.substring(0,delim1),10)
	var day1 = parseInt(FromDate.substring(delim1 + 1,delim2),10)
	var year1 = parseInt(FromDate.substring(delim2 + 1,FromDate.length),10)

	var month2 = parseInt(ToDate.substring(0,delim3),10)
	var day2 = parseInt(ToDate.substring(delim3 + 1,delim4),10)
	var year2 = parseInt(ToDate.substring(delim4 + 1,ToDate.length),10)

	if (year2 < year1)
	{
		return false
	}
	else if (year2 == year1 && month1 > month2)
	{
		return false
	}
	else if (year2 == year1 && month2 == month1 && day1 > day2)
	{
		return false
	}
	return true
}

//-------------------------------------------------------------------------
// IsDateEqual()
// ===============
//-------------------------------------------------------------------------
function IsDateEqual(Date1, Date2)
{
	var delim1 = Date1.indexOf("/")
	var delim2 = Date1.lastIndexOf("/")
	var delim3 = Date2.indexOf("/")
	var delim4 = Date2.lastIndexOf("/")

	var month1 = parseInt(Date1.substring(0,delim1),10)
	var day1 = parseInt(Date1.substring(delim1 + 1,delim2),10)
	var year1 = parseInt(Date1.substring(delim2 + 1,Date1.length),10)

	var month2 = parseInt(Date2.substring(0,delim3),10)
	var day2 = parseInt(Date2.substring(delim3 + 1,delim4),10)
	var year2 = parseInt(Date2.substring(delim4 + 1,Date2.length),10)

	if (year2 != year1)
	{
		return false
	}
	else if (month1 != month2)
	{
		return false
	}
	else if (day1 != day2)
	{
		return false
	}
	return true
}

//------------------------------------------------------------------------
// ValidateTime()
// ===================
//------------------------------------------------------------------------
function ValidateTime(strTime){

	var nTime

	nTime = parseInt(strTime.replace(":",""))
	if ( nTime>=100 && nTime <= 1259 ){
		return true
	}else{
		return false
	}

}

//------------------------------------------------------------------------
// ValidateTimeRange()
// ===================
//------------------------------------------------------------------------
function ValidateTimeRange(strTime1, strTime2, strAMPM1, strAMPM2){

	var nTime1, nTime2

	if ( strAMPM1 == "AM" && strAMPM2 == "PM" ){
		return true
	}else if ( strAMPM1 == "PM" && strAMPM2 == "AM" ){
		return false
	}else{

		nTime1 = parseInt(strTime1.replace(":",""))
		nTime2 = parseInt(strTime2.replace(":",""))

		if (nTime1 >= 1200){
			nTime1 -= 1200
		}
		if (nTime2 >= 1200){
			nTime2 -= 1200
		}

		if (strAMPM1 == "PM"){
			nTime1 += 1200
		}

		if (strAMPM2 == "PM"){
			nTime2 += 1200
		}

		if (nTime1 <= nTime2){
			return true
		}else{
			return false
		}

	}

}

//-------------------------------------------------------------------------
// IsTimeEqual()
// ===============
// Time1 and Time2 are strings in the format "hh:mm:ss AM/PM"
//-------------------------------------------------------------------------
function IsTimeEqual(Time1, Time2)
{
	var delim1 = Time1.indexOf(":")
	var delim2 = Time1.lastIndexOf(":")
	var delim3 = Time1.indexOf(" ")
	var delim4 = Time2.indexOf(":")
	var delim5 = Time2.lastIndexOf(":")
	var delim6 = Time2.indexOf(" ")

	var hour1 = parseInt(Time1.substring(0,delim1),10)
	var minute1 = parseInt(Time1.substring(delim1 + 1,delim2),10)
	var second1 = parseInt(Time1.substring(delim2 + 1,delim3),10)
	var AMPM1 = Time1.substring(delim3 + 1)

	var hour2 = parseInt(Time2.substring(0,delim4),10)
	var minute2 = parseInt(Time2.substring(delim4 + 1,delim5),10)
	var second2 = parseInt(Time2.substring(delim5 + 1,delim6),10)
	var AMPM2 = Time2.substring(delim6 + 1)

	if (AMPM1 != AMPM2)
	{
		return false
	}
	else if (second1 != second2)
	{
		return false
	}
	else if (minute1 != minute2)
	{
		return false
	}
	else if (hour1 != hour2)
	{
		return false
	}
	return true
}

//-------------------------------------------------------------------------
// isBlank()
// =========
//
// Return TRUE if the given string is empty
//-------------------------------------------------------------------------
function isBlank(csString)
{
	var nErr = 0;
	csString = Trim(csString);
	if (csString == null || csString.length == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//-------------------------------------------------------------------------
// TrimTextField()
// ===============
//
// Strip any whitespace from beginning and end of field
//-------------------------------------------------------------------------
function TrimTextField(objTxtField)
{
	objTxtField.value = Trim(objTxtField.value)
}

//-------------------------------------------------------------------------
// checkRequiredTextField()
// ========================
//
// Returns TRUE if the text field provided is not blank.  If FALSE, then
// an error message is displayed, and focus is set to the text object.
//-------------------------------------------------------------------------
function checkRequiredTextField(objTxtField, strFieldName)
{
	var strError = "The following field is not allowed to be blank: ";
	if (isBlank(objTxtField.value))
	{
		strError += strFieldName;
		alert(strError);
		objTxtField.focus();

		return false;
	}

	return true;
}

//-------------------------------------------------------------------------
// isCurrency()
// =============
//
// Return TRUE if the value is a valid currency
//-------------------------------------------------------------------------
function isCurrency(sVal){
	var bHitDecimal = false;
	var strVal = sVal.toString();

	strVal = LTrim(strVal)
	strVal = RTrim(strVal)

	for (var i = 0; i < strVal.length; i++){
		var chVal = strVal.charAt(i);

		// if first char is a dollar sign, then OK - skip it
		if ((i == 0) &&  (chVal == "$")){
			continue;
		}

		// if we hit a decimal, then it is OK only if we didn't already hit on
		if ((chVal == ".") && (!bHitDecimal)){
			bHitDecimal = true;
			continue;
		}

		if ((chVal < "0") || (chVal > "9")){
			return false;
		}
	}

	return true;
}

//-------------------------------------------------------------------------
// IsValidEmail()
// ==============
//
// Return TRUE if the email is valid.
//-------------------------------------------------------------------------
function IsValidEmail(sVal)
{
	if (!(/^[\w\.\-\']+@[\w\.\-]+$/.test(sVal)))
	{
		return false;
	}

	return true;
}

//-------------------------------------------------------------------------
// GetComboValue()
// ===============
//
// Return the VALUE of the item selected in the combo box
//-------------------------------------------------------------------------
function GetComboValue(objCombo)
{
	var idx = objCombo.selectedIndex;

	return objCombo[idx].value;
}

//-------------------------------------------------------------------------
// GetComboText()
// ===============
//
// Return the VALUE of the item selected in the combo box
//-------------------------------------------------------------------------
function GetComboText(objCombo)
{
	var idx = objCombo.selectedIndex;

	return objCombo[idx].text;
}

//-------------------------------------------------------------------------
// IsValidPassword()
// =================
//
// Return TRUE if the password is valid.
//
// To be valid, a password:
//		(a) must be at least 6 characters
//		(b) can not contain spaces, apostrophes or dbl quotes
//-------------------------------------------------------------------------
function IsValidPassword(sVal, bShowMsg)
{
	var sMsg = "ERROR - invalid password provided\n\n";

	// check that the pwd is long enough
	if (sVal.length < 6)
	{
		if (bShowMsg)
		{
			alert(sMsg + "Password must be at least 6 characters");
		}
		return false;
	}

	// check that there are no spaces.
	if (sVal.indexOf(" ") >= 0)
	{
		if (bShowMsg)
		{
			alert(sMsg + "Spaces are not allowed");
		}
		return false;
	}

	// check that there are no apostrophes.
	if (sVal.indexOf("'") >= 0)
	{
		if (bShowMsg)
		{
			alert(sMsg + "Apostrophes are not allowed");
		}
		return false;
	}

	// check that there are no dbl quotes.
	if (sVal.indexOf("\"") >= 0)
	{
		if (bShowMsg)
		{
			alert(sMsg + "Double quotes (\") are not allowed");
		}
		return false;
	}

	// check that there are no back ticks.
	if (sVal.indexOf("\`") >= 0)
	{
		if (bShowMsg)
		{
			alert(sMsg + "Back tick characters (\`) are not allowed");
		}
		return false;
	}

	// check there is at least one character and one number.
	if (!(/[a-zA-Z]/.test(sVal)))
	{
		if (bShowMsg)
		{
			alert(sMsg + "You must include at least one character");
		}
		return false;
	}

	if (!(/[0-9]/.test(sVal)))
	{
		if (bShowMsg)
		{
			alert(sMsg + "You must include at least one number");
		}
		return false;
	}

	return true;
}
//-->

