function ValidateString(element, field_name, required, allowed_chars, min_size, max_size)
{
	// element.value = element.value.toUpperCase();
	if ((required == 1) && ((element.value == null) || (element.value.length < 1)))
	{
		alert("Field (" + field_name + ") needs to be filled in.");
		element.focus();
		return false;
	}

	if (element.value.length > max_size)
	{
		alert("Field (" + field_name + ") cannot be longer than " + max_size + " characters.");
		element.select();
		return false;
	}

	if (element.value.length < min_size)
	{
		alert("Field (" + field_name + ") must be at least " + min_size + " characters.");
		element.select();
		return false;
	}

	for (i = 0; i < element.value.length; i++)
	{
		c = element.value.charAt(i);

		if (allowed_chars.indexOf(c) == -1)
		{
			alert("Field (" + field_name + ") contains illegal characters.");
			element.select();
			element.focus();
			return false;
		}
	}

	return true;
}

function ValidateMultiLine(element, field_name, required, allowed_chars, max_line_size, max_lines)
{
	carriageReturnCount = 0;
	lineFeedCount = 0;
	lineSize = 0;

	if ((required == 1) && (element.value.length < 1))
	{
		alert("Field (" + field_name + ") needs to be filled in.");
		element.focus();
		return false;
	}

	for (i = 0; i < element.value.length; i++)
	{
		c = element.value.charAt(i);

		if (allowed_chars.indexOf(c) == -1)
		{
			alert("Field (" + field_name + ") contains illegal character: " + c);
			element.select();
			element.focus();
			return false;
		}

		if (c == '\r')
		{
			carriageReturnCount++;
			lineSize = 0;
		}
		else if (c == '\n')
		{
			lineFeedCount++;
			lineSize = 0;
		}
		else if (lineSize >= max_line_size)
		{
			alert("Field (" + field_name + ") contains more than " + max_line_size + " characters per line.");
			element.select();
			element.focus();
			return false;
		}
		else
		{
			lineSize++;
		}
	}

	if (carriageReturnCount == lineFeedCount)
	{
		// windows or single line
		if (((carriageReturnCount + lineFeedCount) / 2 + 1) > max_lines)
		{
			alert("Field (" + field_name + ") contains more than " + max_lines + " lines.");
			element.select();
			element.focus();
			return false;
		}
	}
	else if (carriageReturnCount > lineFeedCount)
	{
		// mac
		if ((carriageReturnCount + 1) > max_lines)
		{
			alert("Field (" + field_name + ") contains more than " + max_lines + " lines.");
			element.select();
			element.focus();
			return false;
		}
	}
	else
	{
		// unix
		if ((lineFeedCount + 1) > max_lines)
		{
			alert("Field (" + field_name + ") contains more than " + max_lines + " lines.");
			element.select();
			element.focus();
			return false;
		}
	}

	return true;
}

function ValidateAmount(element, field_name, required, min_value, max_value)
{
	var allowed_chars = "1234567890.";
	var decimal_found = 0;
	var temp = 0;

	if ((required == 1) && ((element.value == null) || (element.value.length < 1)))
	{
		alert("Field (" + field_name + ") needs to be filled in.");
		element.focus();
		return false;
	}

	if ((required == 0) && ((element.value == null) || (element.value.length < 1))) return true;

	for (i = 0; i < element.value.length; i++)
	{
		c = element.value.charAt(i);

		if (c == '.')
		{
			if (decimal_found == 0)
			{
				decimal_found = 1;
			}
			else
			{
				alert("Field (" + field_name + ") contains multiple decimal points.");
				element.select();
				element.focus();
				return false;
			}
		}

		if (allowed_chars.indexOf(c) == -1)
		{
			alert("Field (" + field_name + ") contains illegal characters.");
			element.select();
			element.focus();
			return false;
		}
	}

	if (isNaN(element.value)) return false;

	temp += element.value;

	if ((temp < min_value) || (temp > max_value))
	{
		alert("Field (" + field_name + ") must contain value between " + min_value + ".00 and " + max_value + ".00");
		element.select();
		element.focus();
		return false;
	}

	whole = Math.floor(temp);
	decimal = Math.round(temp*100) - whole*100;

	if (decimal == 100)
	{
		decimal = 0;
		whole++;
	}

	if (decimal >= 10)
	{
		element.value = whole + "." + decimal;
	}
	else if (decimal != 0)
	{
		element.value = whole + ".0" + decimal;
	}
	else
	{
		element.value = whole + ".00";
	}

	return true;
}

function ValidateDateDDMMYYYY(element, field_name, required)
{
	var allowed_chars = "0123456789";
	var success = 1;

	if ((required == 1) && ((element.value == null) || (element.value.length < 1)))
	{
		alert("Field (" + field_name + ") needs to be filled in.");
		element.focus();
		return false;
	}

	if ((required == 0) && ((element.value == null) || (element.value.length < 1))) return true;

	if (element.value.length != 10) success = 0;

	if (success == 1)
	{
		if (element.value.charAt(2) != '/') success = 0;
		if (element.value.charAt(5) != '/') success = 0;

		if (allowed_chars.indexOf(element.value.charAt(0)) == -1) success = 0;
		if (allowed_chars.indexOf(element.value.charAt(1)) == -1) success = 0;
		if (allowed_chars.indexOf(element.value.charAt(3)) == -1) success = 0;
		if (allowed_chars.indexOf(element.value.charAt(4)) == -1) success = 0;
		if (allowed_chars.indexOf(element.value.charAt(6)) == -1) success = 0;
		if (allowed_chars.indexOf(element.value.charAt(7)) == -1) success = 0;
		if (allowed_chars.indexOf(element.value.charAt(8)) == -1) success = 0;
		if (allowed_chars.indexOf(element.value.charAt(9)) == -1) success = 0;

		if ((parseInt(element.value.substring(0, 2)) < 1) || (parseInt(element.value.substring(0, 2)) > 31)) success = 0;
		if ((parseInt(element.value.substring(3, 5)) < 1) || (parseInt(element.value.substring(3, 5)) > 12)) success = 0;
		if ((parseInt(element.value.substring(6, 10)) < 2000) || (parseInt(element.value.substring(6, 10)) > 2100)) success = 0;
	}

	if (success == 0)
	{
		alert("Field (" + field_name + ") must be in the format DD/MM/YYYY");
		element.select();
		element.focus();
		return false;
	}

	return true;
}
