// Trim all whitespace from form text elements
function TrimFormElements(frm)
{
	for(var i = 0; i < frm.length; i++)
	{
		if(frm.elements[i].type == "text")
		{
			frm.elements[i].value = Trim(frm.elements[i].value);
		}
	}	
}

function LTrim(str)
{
	return str.replace(/^\s+/g, "");
}

function RTrim(str)
{
	return str.replace(/\s+$/g, "");

}

function Trim(str)
{
	return str.replace(/^\s+/g, "").replace(/\s+$/g, "");
}

function RequireField(fld, msg)
{
	//--This function is called to ensure entries are made for required fields.
	if(fld.value.length == 0)
	{
		fld.focus();
		fld.select();
		alert(msg);
		return false;
	}
	return true; 
}
function RequireRadioField(fld, msg) 
{
	var intCount, blnSelected = false;
	
	for( intCount = 0; intCount < fld.length; intCount++ ) 
	{
		if( fld[intCount].checked == true )	
		{
			blnSelected = true;
			break;
		}
	}
		
	if( blnSelected == false) 
	{
		alert(msg);
		return false;
	}
	else
		return true;
	
}

function RequireIntegerField(fld, msg, blnAllowNull)
{
	if(blnAllowNull && fld.value.length == 0)
	{
		return true;
	}
	
	if(fld.value.length == 0 || !IsInteger(fld))
	{
		fld.focus();
		fld.select();
		alert(msg);
		return false;
	}
	return true; 
}

function RequireNumericField(fld, msg, blnAllowNull)
{
	if(blnAllowNull && fld.value.length == 0)
	{
		return true;
	}
	
	if(fld.value.length == 0 || !IsNumber(fld))
	{
		fld.focus();
		fld.select();
		alert(msg);
		return false;
	}
	return true; 
}

function RequireDateField(fld, msg, blnAllowNull)
{
	if(blnAllowNull && fld.value.length == 0)
	{
		return true;
	}

	if(fld.value.length == 0 || !IsDate(fld))
	{
		fld.focus();
		fld.select();
		alert(msg);
		return false;
	}
	return true; 
}

function RequireEmailField(fld, msg, blnAllowNull)
{
	if(blnAllowNull && fld.value.length == 0)
	{
		return true;
	}
	
	if(fld.value.length == 0 || !IsEmail(fld))
	{
		fld.focus();
		fld.select();
		alert(msg);
		return false;
	}
	return true; 
}

function IsInteger(theField) {

	var valid = "0123456789"
	var ok = "yes";
	var temp;

	for (var i=0; i<theField.value.length; i++) {
		temp = "" + theField.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}

	if (ok == "no") {
		return(false);		
	}
	else
	{	return(true);

	}
}

function IsNumber(theField) {

	var valid = "0123456789"
	var ok = "yes";
	var temp;
	var nDecimals = 0;

	for (var i=0; i<theField.value.length; i++)
	{
		temp = "" + theField.value.substring(i, i+1);
		if(temp == ".")
		{
			nDecimals++;
		}
		else if(valid.indexOf(temp) == "-1")
		{
			ok = "no";
		}
	}
	
	if(nDecimals > 1)
	{
		ok = "no";
	}

	if (ok == "no") {
		return(false);		
	}
	else
	{	return(true);

	}
}

function IsDate(theField) {
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables
	
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	
	var matchArray = theField.value.match(datePat); // is the format ok?
	if (matchArray == null) {
		//alert("Date is not in a valid format.")
		return false;
	}
	
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];

	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
	return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+month+" doesn't have 31 days!")
		return false
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	
		if (day>29 || (day==29 && !isleap)) {
			//alert("February " + year + " doesn't have " + day + " days!");
			return false;
	   }
	}
	return true;  // date is valid
}
function IsDateFourYear(theField) {
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables
	
	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	// To require a 4 digit year entry, use this line instead:
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	
	var matchArray = theField.value.match(datePat); // is the format ok?
	if (matchArray == null) {
		//alert("Date is not in a valid format.")
		return false;
	}
	
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];

	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
	return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+month+" doesn't have 31 days!")
		return false
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	
		if (day>29 || (day==29 && !isleap)) {
			//alert("February " + year + " doesn't have " + day + " days!");
			return false;
	   }
	}
	return true;  // date is valid
}

function IsEmail(theField)
{
	// constructs Regular Expression Object and sets pattern
	// to the value between the "/" characters.	
	var regexp = new RegExp("^([a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-])*)+@{1}([a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-])*)+\.{1}[a-zA-Z]{2,3}$","ig");
	
	return regexp.test(theField.value);
}