function report_error(type)
{
	{document.getElementById("emailError").style.display = "block";}
}

function whitespace(str)
// cleans up extra whitespace on all input before validating
{
	str = str.replace(new RegExp(/^\s+/),""); // at start
	str = str.replace(new RegExp(/\s+$/),""); // at end
	return str;
}

function validate_name(str)
// validates user's name by presence and length
{
	// calls function to remove whitespace
	str = whitespace(str);

	// checks is name is there
	if (str==""){
	{ document.getElementById("nameError").style.display = "block";}
	return false;
	}
	
	{document.getElementById("nameError").style.display = "none";}
	return true;
}

function validate_email(str)
// validates email address by positioning of at symbol and dot
{
	// calls function to remove whitespace
	str = whitespace(str);

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var error_type="email";

	if (str.indexOf(at)==-1){
	report_error(error_type);
	return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	report_error(error_type);
	return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	report_error(error_type);
	return false;
	}

	if (str.indexOf(at,(lat+1))!=-1){
	report_error(error_type);
	return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	report_error(error_type);
	return false;
	}

	if (str.indexOf(dot,(lat+2))==-1){
	report_error(error_type);
	return false;
	}

	if (str.indexOf(" ")!=-1){
	report_error(error_type);
	return false;
	}

	{document.getElementById("emailError").style.display = "none";}
	return true;

}

function validate_subject(str)
// validates user's name by presence and length
{
	// calls function to remove whitespace
	str = whitespace(str);

	// checks is name is there
	if (str==""){
	{ document.getElementById("subjectError").style.display = "block";
	}
	return false;
	}
	
	{document.getElementById("subjectError").style.display = "none";}
	return true;
}


function validate_message(str)
// validates user's name by presence and length
{
	// calls function to remove whitespace
	str = whitespace(str);

	// checks is name is there
	if (str==""){
	{ document.getElementById("messageError").style.display = "block";
	}
	return false;
	}
	
	{document.getElementById("messageError").style.display = "none";}
	return true;
}

function validate_form()
// performs all above validations by sequence,
// then returns true to submit the form if all passes
{
	var email_value = document.forms[0].user_email.value;
	var name_value = document.forms[0].user_name.value;
	var email_field = document.forms[0].user_email;
	var name_field = document.forms[0].user_name;
	var message_field = document.forms[0].user_message;
	var message_value = document.forms[0].user_message.value;
	var subject_field = document.forms[0].user_subject;
	var subject_value = document.forms[0].user_subject.value;
	

	if(!validate_name(name_value)){
	name_field.focus();
	return false;
	}

	if(!validate_email(email_value)){
	email_field.focus();
	return false;
	}
	
	if(!validate_subject(subject_value)){
	subject_field.focus();
	return false;
	}
	
	if(!validate_message(message_value)){
	message_field.focus();
	return false;
	}

	return true; // submits form
}

