function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#ffcdcd';
        error = "Please provide your email address so we can get in touch.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#ffcdcd';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#ffcdcd';
        error = "The email address contains illegal characters. Please enter a valid email address\n";
    }
    return error;
}

function validateEmpty(fld) 
{
	var error = "";
	
    if (fld.value.length == 0) {
		fld.style.background = '#ffcdcd';
        error = "The required field '" + fld.name + "' was left empty.\n";
    }
	
	return error;
}

function submit_contact_form(){

	var reason = "";

	reason += validateEmail(document.contactform.email);
	reason += validateEmpty(document.contactform.name);
      
  if (reason != "") {
		alert("Before we can send your message, a few things need fixing:\n" + reason);
	
    return;
  }

	document.contactform.submit();
	return true;
}
