function checkWholeForm(theForm) {
    var why = "";
    why += checkRequiredField(theForm.yourName.value,"full name");
    why += checkEmail(theForm.yourEmail.value);
    why += checkPhone(theForm.yourNumber.value);

    why += checkRequiredField(theForm.yourAddress1.value,"address line1");
    why += checkRequiredField(theForm.yourTownCity.value,"town/city");
    if(theForm.yourService!=undefined){
	why += checkRadio(theForm.yourService,"service");    
    }
    why += postcodeChecker(theForm.yourPostCode);
    if (why != "") {
       alert(why);
       return false;
    }
return true;
}


function checkEmail (strng) {
var error="";
if (isEmpty(strng)) {
   error = "Please enter your email address.\n";
   return error;
}

    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;    
}

// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (isEmpty(strng)) {
   error = "Please enter your contact number.\n";
   return error;
}

	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
	    if (isNaN(parseInt(stripped))) {
	       error = "The contact number contains illegal characters.";
	  
	    }
	    if (!(stripped.length == 11)) {
		error = "The contact number is of wrong length. Make sure you included an area code.\n";
	    } 

return error;
}


function checkRequiredField(fieldValue,message) {
var error = "";
if (isEmpty(fieldValue)==true) {
   error = "Please enter your "+message+".\n";
}

return error;
}    

// non-empty textbox

function isEmpty(strng) {
var error = "";
  if (strng.length == 0 || strng.replace(/[\(\)\.\-\ ]/g, '')=='') {
	     return true;
  }else{
  	return false;	  
  }	
}

// exactly one radio button is chosen

function checkRadio(formField,message) {
var error = "";
var checkvalue;
for (i=0, n=formField.length; i<n; i++) {
   if (formField[i].checked) {
       checkvalue = formField[i].value;
      break;
   }
}

   if (!(checkvalue)) {
       error = "Please select option for "+message+".\n";
    }
return error;
}





function postcodeChecker(postCodeField){ //check postcode format is valid
var error = "";
 postcode = postCodeField.value; 
 size = postcode.length;
 postcode = postcode.toUpperCase(); //Change to uppercase
 
 while (postcode.slice(0,1) == " ") //Strip leading spaces
  {
  	postcode = postcode.substr(1,size-1);size = postcode.length
  }
 
 while(postcode.slice(size-1,size)== " ") //Strip trailing spaces
  {
  	postcode = postcode.substr(0,size-1);size = postcode.length
  }
 postCodeField.value = postcode; //write back to form field
 
 if(postcode.length ==0){
 	error="Please enter your postcode. \n";
	 return error;
 }
 
 if (size < 5 || size > 8){ //Code length rule
   error =postcode + " is not a valid  -  postcode has the wrong length.\n";  
  return error;	
  }
  
 if (!(isNaN(postcode.charAt(0)))){ //leftmost character must be alpha character rule
   error =postcode + " is not a valid  - postcodes cannot start with a number.\n";
   return error;
  }
  
 if (isNaN(postcode.charAt(size-3))){ //first character of inward code must be numeric rule
   error =postcode + " is not a valid  - the second part of your postcode must start with a number.\n";
   return error;

  }
  
 if (!(isNaN(postcode.charAt(size-2)))){ //second character of inward code must be alpha rule
   error =postcode + " is not a valid  - the last two characters of your postcode must be letters.\n";
   return error;
  }
  
 if (!(isNaN(postcode.charAt(size-1)))){ //third character of inward code must be alpha rule
   error =postcode + " is not a valid  - the last two characters of your postcode must be letters.\n";
   return error;
  }
  
 count1 = postcode.indexOf(" ");count2 = postcode.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
   error =postcode + " is not a valid  - only one space is allowed between the two parts of your postcode.\n";
   return error;

  }

return error;
}
