// general input validation functions.

// can not contain blanks and isValidText
function isValidNonBlankText (field, fieldName)
{
	if (!isValidText (field, fieldName))
		return false;
	// now just check to see if it is empty.	
	if (field.value == '') 
	{
		alert (fieldName + " can not be blank");
		field.focus();
		return false;
	}
	return true;
}

// trim off white space and check for invalid chars.
function isValidText (field, fieldName)
{
	if (!field)
	{
		alert (fieldName + " is not a valid form element");
		return false;
	}
	// trim leading and trailing spaces
	var strText = field.value.replace(/^\s+/g, '');
	strText = strText.replace(/\s+$/g, '');
	// check for empty (white space) string
	// check for invalid characters
	if (strText.search(/"/g) != -1)
	{
		alert (fieldName + ' can not contain a "');
		field.focus();
		return false;
	}
	field.value = strText
	return true;
}

function isValidDBKey (field, fieldName)
{
	if (!isValidNonBlankText (field, fieldName))
		return false;
	// now check for single quotes
	var strText = field.value.replace(/^\s+/g, '');
	strText = strText.replace(/\s+$/g, '');
	if (strText.search(/'/g) != -1)
	{
		alert (fieldName + ' can not contain a single quote');
		field.focus();
		return false;
	}
	field.value = strText
	
	return true;
}

function isPositiveIntegerNoBlank(field, fieldName) 
{
	if (isPositiveInteger(field, fieldName))
		if (field.value != '')
			return true;
	return false;
}
	
// validate valid number and make positive.
function isPositiveInteger(field, fieldName) 
{
	if (!field)
	{
		alert (fieldName + " is not a valid form element");
		return false;
	}
	if (field.value == '')
		return true;
	if (parseInt(field.value) != field.value) 
	{
		alert (fieldName + " is not a number");
		field.focus();
		return false;
	}
	// set field value to positive integer
	field.value = Math.abs(parseInt(field.value));
	return true;
}

// convert money to float w/ only decimals.
function isMoney(field, fieldName)
{
	if (!field)
	{
		alert (fieldName + " is not a valid form element");
		return false;
	}
	var strText = field.value.replace(/^\$/, '');
	strText = strText.replace(/,/g, '');
	if (field.value == '')
		return true;

	if (parseFloat (strText) != strText)
	{
		alert (fieldName + " in not a valid dolar value");
		return false;
	}
	field.value = strText;
	return true;
}
	
