function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateType(theForm.requiredtype);
  reason += validateColour(theForm.requiredworktop);
  reason += validateDepth(theForm.worktop_deep);
  reason += validateLength(theForm.requiredworktop_length1);
  reason += validateWidth(theForm.requiredworktop_depth1);
  reason += validateName(theForm.name);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
  reason += validatePostcode(theForm.postcode);

      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}


function validateType(fld) {
    var error = "";

 
    if (fld.value == "") {
        fld.style.background = 'Red'; 
        error = "You didn't choose a Worktop.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validateColour(fld) {
    var error = "";

 
    if (fld.value == "") {
        fld.style.background = 'Red'; 
        error = "You didn't choose a Worktop Colour.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateDepth(fld) {
    var error = "";

 
    if (fld.value == "") {
        fld.style.background = 'Red'; 
        error = "You didn't enter a worktop depth.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 3)) {
        fld.style.background = 'Red'; 
        error = "The worktop depth is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Red'; 
        error = "The depth contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validateLength(fld) {
    var error = "";

 
    if (fld.value == "") {
        fld.style.background = 'Red'; 
        error = "Please enter at least one worktop length.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 5)) {
        fld.style.background = 'Red'; 
        error = "The worktop length should be between 2 and 5 numbers.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Red'; 
        error = "The worktop length contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateWidth(fld) {
    var error = "";

 
    if (fld.value == "") {
        fld.style.background = 'Red'; 
        error = "Please enter at least one worktop width.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 5)) {
        fld.style.background = 'Red'; 
        error = "The worktop width should be between 2 and 5 numbers.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Red'; 
        error = "The worktop width contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateName(fld) {
    var error = "";

 
    if (fld.value == "") {
        fld.style.background = 'Red'; 
        error = "You didn't enter a name.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 20)) {
        fld.style.background = 'Red'; 
        error = "The name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Red'; 
        error = "The name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePostcode(fld) {
    var error = "";

 
    if (fld.value == "") {
        fld.style.background = 'Red'; 
        error = "You didn't enter a postcode.\n";
    } else if ((fld.value.length < 6) || (fld.value.length > 9)) {
        fld.style.background = 'Red'; 
        error = "The postcode is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Red'; 
        error = "The postcode contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


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 = 'Red';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Red';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Red';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Red';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Red';
    } else if (!(stripped.length == 11)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Red';
    }
    return error;
}

function addElement() {
  var ni = document.getElementById('inputs');
  var newdiv = document.createElement('p');
  newdiv.innerHTML = "<input class='file' type='file' name='fileatt[]' />";
  ni.appendChild(newdiv);
}	