function trim(str) 
{
	return str.replace(/^\s+|\s+$/g,"");
}

function validate_contact(theForm)
{
	theForm.name.value = trim(theForm.name.value);
	theForm.email.value = trim(theForm.email.value);
	theForm.message.value = trim(theForm.message.value);
	
	if (theForm.name.value == '') {
		alert('Please provide your name.');
		theForm.name.focus();
		return false;
	}
	if (theForm.name.value == '') {
		alert('Please provide your email address.');
		theForm.email.focus();
		return false;
	}
	if (!validate_email(theForm.email.value)) {
		alert('You have entered an invalid email address. Please provide your email address.');
		theForm.email.focus();
		theForm.email.select();
		return false;
	}
	if (theForm.message.value == '') {
		alert('Please enter message text.');
		theForm.message.focus();
		return false;
	}
	theForm.submit();
	
	return true;
}

function validate_email(str)
{
	var at = '@'
	var dot = '.'
	var pos_at = str.indexOf(at);
	var str_len = str.length;
	var pos_dot = str.indexOf(dot);
	if (str.indexOf(at) == -1) {
		return false;
	}

	if (str.indexOf(at) == 0 || str.indexOf(at) == str_len) {
	   return false;
	}

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == str_len) {
	    return false;
	}

	if (str.indexOf(at, pos_at + 1) != -1) {
	    return false;
	}

	if (str.substring(pos_at - 1, pos_at) == dot || str.substring(pos_at + 1, pos_at + 2) == dot) {
		return false;
	}

	if (str.indexOf(dot, pos_at + 2) == -1) {
	    return false;
	}
		
	if (str.indexOf(' ') != -1) {
		return false;
	}

	return true;
}
