// Ensure document.getElementById('ID') works on all browsers
if (document.all && !document.getElementById)
{
	document.getElementById = function(id) {
		 return document.all[id];
	}
}

// TRIM - Compensates for Javascript's inherent stupidity
function trim(str)
{
	var str = str.replace(/^\s*|\s*$/g,'');
	return str;
}

// REQUIRED - Checks field 'id' for input
function required(id, title)
{
	var element = document.getElementById(id);
	if (trim(element.value) == '')
	{
		alert('The '+title+' field is required');
		element.select();
		return false;
	}
	else
	{
		return true;
	}
}

// ALPHA - Checks to see if field 'id' contains only alphabetical characters
function alpha(id, title)
{
	var element = document.getElementById(id);
	var regex = /^([a-z])+$/i;
	if (regex.test(trim(element.value)) || (element.value == ''))
	{
		return true;
	}
	else
	{
		alert('The '+title+' field can only contain alphabetical characters');
		element.select();
		return false;
	}
}

// ALPHA_NUMERIC - Checks to see if field 'id' contains only alphanumeric characters
function alpha_numeric(id, title)
{
	var element = document.getElementById(id);
	var regex = /^([a-z0-9])+$/i;
	if (regex.test(trim(element.value)) || (element.value == ''))
	{
		return true;
	}
	else
	{
		alert('The '+title+' field can only contain letters and numbers');
		element.select();
		return false;
	}
}

// VALID_EMAIL - Checks field 'id' for a valid email address
function valid_email(id, title)
{
	var element = document.getElementById(id);
	var regex = /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/i;
	if (regex.test(trim(element.value)) || (element.value == ''))
	{
		return true;
	}
	else
	{
		alert('The '+title+' field does not contain a valid email address.');
		element.select();
		return false;
	}
}

// NUMERIC - Checks to see if field 'id' contains only numeric characters
function numeric(id, title) {
	var element = document.getElementById(id);
	var regex = /^[0-9\.]+$/;
	if (regex.test(trim(element.value)) || (element.value == ''))
	{
		return true;
	}
	else
	{
		alert('The '+title+' field can only contain numbers');
		element.select();
		return false;
	}
}

// MATCHES - Checks to see that field 'id1' matches field 'id2'
function matches(id1, title1, id2, title2)
{
	var element1 = document.getElementById(id1);
	var element2 = document.getElementById(id2);
	if (trim(element1.value) != trim(element2.value))
	{
		alert('The '+title1+' field must match the '+title2+' field.');
		element1.select();
		return false;
	}
	else
	{
		return true;
	}
}

// MIN_LENGTH - Checks field 'id' for minimum length input
function min_length(id, title, len)
{
	var element = document.getElementById(id);
	var this_length = trim(element.value).length;
	if ((this_length < len) && (element.value != ''))
	{
		alert('The '+title+' field must be at least '+len+' characters');
		element.select();
		return false;
	}
	else
	{
		return true;
	}
}

// EXACT_LENGTH - Checks field 'id' for exact length input
function exact_length(id, title, len)
{
	var element = document.getElementById(id);
	var this_length = trim(element.value).length;
	if ((this_length != len) && (element.value != ''))
	{
		alert('The '+title+' field must be exactly '+len+' characters');
		element.select();
		return false;
	}
	else
	{
		return true;
	}
}