/*
*
*	Check a feedback form for correct formatting.
*
*	Name input's ID must be "name"
*	Email input's ID must be "email"
*	Phone input's ID must be "phone"
*	Message input's ID must be "message"
*
*/


//	Check to see that at least 2 characters have
//	been added to the name field.

function checkName (theForm)
{
	if (theForm.name.value.length > 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//	Make sure a valid email address has been entered

function checkEmail (theForm)
{
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	
	if (re.test(theForm.email.value))
	{
		return true;
	}
	else
	{
		return false;
	}
}

//	Make sure a valid phone number has been entered
//	Re-format if so

function checkPhone (theForm)
{
	var re = /^\(?(\d{3})\)?[\.\-\/]?(\d{3})[\.\-\/]?(\d{4})$/;
	
	var validPhone = re.exec(theForm.phone.value);
	
	if (validPhone)
	{
		theForm.phone.value = "(" + validPhone[1] + ") " + validPhone[2] + "-" + validPhone[3];
		return true;
	}
	else
	{
		return false;
	}
}

//	Make sure some text has been added to the message

function checkMessage (theForm)
{
	if (theForm.message.value != '')
	{
		return true;
	}
	else
	{
		return false;
	}
}

//	Actual function called
//	Calls each of the above functions one at a time
	
function checkForm (theForm)
{
	if (!(checkName(theForm)))
	{
		alert('Please enter your full name');
		theForm.name.focus();
		theForm.name.select();
		return false;
	}
	else if (!(checkEmail(theForm)))
	{
		alert('Please enter a valid email address');
		theForm.email.focus();
		theForm.email.select();
		return false;
	}
	else if(theForm.phone.value != '' &&  !(checkPhone(theForm)))
	{
		alert('Please enter a valid phone number');
		theForm.phone.focus();
		theForm.phone.select();
		return false;
	}
	else if (!(checkMessage(theForm)))
	{
		alert('Please enter a message');
		theForm.message.focus();
		theForm.message.select();
		return false;
	}
	else
	{
		return true;
	}
}