// JavaScript Document
function validateForm(form){
	var reason = "";
	reason += checkName(form.name.value);
    reason += checkEmail(form.email.value);
	reason += checkEmailMatch(form);
	reason += checkAddress(form);
    reason += checkPhone(form.phone.value);
	reason += checkDropdown(form.where_did_you_hear_nwtf.value, form);
	reason += checkMessage(form.content.value);
    if (reason != "") {
       alert(reason);
       return false;
    }
	return true;
}

function checkRadio(checkvalue, form) {
var error = "";
   if (!(checkvalue)) {
       error = "Please tell us where you heard about us.\n";
    }
	if(checkvalue == "directory"){
		if(form.directory_state.value == ""){
			error = "Please state which directory told you about us.\n";
		}
	}
	if(checkvalue == "other"){
		if(form.other_state.value == ""){
			error = "Please state where you heard about us.\n";
		}
	}
return error;
}

function checkDropdown(strng, form){
	var error="";
	if (strng == "#") {
		error = "Please tell us where you heard about us.\n";
	}
	if(strng == "directory"){
		if(form.state.value == ""){
			error = "Please state which directory told you about us.\n";
		}
	}
	if(strng == "other"){
		if(form.state.value == ""){
			error = "Please state where you heard about us.\n";
		}
	}
	return error;
}

function checkName(strng){
var error="";
if (strng == "") {
   error = "You didn't enter your name.\n";
}
return error;
}

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;    
}

function checkEmailMatch(form){
var error="";
if (form.email.value != form._confirm_email.value) { 
   error = "E-mail addresses do not match.\n";
}
return error; 
}

function checkAddress(form){
var error="";
if (form.address_l1.value == "") { 
   error = "The first line of the adress is not complete.\n";
}
if (form.address_city.value == "") { 
   error = "Please enter your town / city.\n";
}
if (form.address_postcode.value == "") { 
   error = "Please enter your postcode.\n";
}
return error; 
}

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
} else {

	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length >= 10)) {
	error = "Make sure your phone number included an area code.\n";
    }
}
return error;
}

function checkMessage(strng){
var error="";
if (strng == "") {
   error = "You didn't enter a message.\n";
}
return error;
}