// eliminate the left and right spaces from the string.
function trim(string) {
	
	var i;
	var intCount;
		
	//truncate the left spaces.
	// 1. get the number of spaces at left side of the string.
	// 2. get the new string without the left spaces.
	
	intCount = 0;
	for (i = 0; i < string.length; i++) 
		if ((string.charAt(i)) != " ") 
			break;
		else 
			intCount = intCount + 1;
		
	string = string.substring(intCount, string.length);
	
		
	
	//truncate the right spaces.
	// 1. use the string that has been truncated just now and get
	//	  the number of spaces on the right side of the string.
	// 2. get the final string and return.
	
	intCount = 0;
	for (i = string.length-1; i >= 0; i--) 
		if ((string.charAt(i)) != " ") 
			break;
		else 
			intCount = intCount + 1;
		
	
	string = string.substring(0, string.length-intCount);	
	return string;		
	
}


function changeCase(frmObj) {
var index;
var tmpStr;
var tmpChar;
var preString;
var postString;
var strlen;
tmpStr = frmObj.value.toLowerCase();
strLen = tmpStr.length;
if (strLen > 0)  {
for (index = 0; index < strLen; index++)  {
if (index == 0)  {
tmpChar = tmpStr.substring(0,1).toUpperCase();
postString = tmpStr.substring(1,strLen);
tmpStr = tmpChar + postString;
}
else {
tmpChar = tmpStr.substring(index, index+1);
if (tmpChar == " " && index < (strLen-1))  {
tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
preString = tmpStr.substring(0, index+1);
postString = tmpStr.substring(index+2,strLen);
tmpStr = preString + tmpChar + postString;
         }
      }
   }
}
frmObj.value = tmpStr;
}

function IsValidTime(timeStr) 
{
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null) 
	{
		//alert("Time is not in a valid format.");
		
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];
	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }

	if (hour < 0  || hour > 23) 
	{
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}
	if (hour <= 12 && ampm == null) 
	{
		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) 
		{
			alert("You must specify AM or PM.");
			return false;
		}
	}
	if  (hour > 12 && ampm != null) 
	{
		alert("You can't specify AM or PM for military time.");
		return false;
	}
	if (minute<0 || minute > 59) 
	{
		alert ("Minute must be between 0 and 59.");
		return false;
	}
	if (second != null && (second < 0 || second > 59)) 
	{
		alert ("Second must be between 0 and 59.");
		return false;
	}
	return true;
}

//check whether number of decimal places is 2
function checkDecimals(fieldName, fieldValue) {

decallowed = 2;  // how many decimals are allowed?

if (isNaN(fieldValue) || fieldValue == "") {
alert("Oops!  That does not appear to be a valid number.  Please try again.");
fieldName.value = '';
return false;
}
else {
if (fieldValue.indexOf('.') == -1) fieldValue += ".";
dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);

if (dectext.length > decallowed)
{
alert ("Oops!  Please enter a number with up to " + decallowed + " decimal places.  Please try again.");
fieldName.value = '';
return false;
      }
else 
return true;
   }
}