// checks format of UK postcodes
function invalidUKPostcode(formName,controlName,countryName){ //check postcode format is valid
 var formObject = eval('document.'+formName+'.' + controlName);
 var formCountryObject = eval('document.'+formName+'.' + countryName);
 var test = formObject.value; 
 var size = test.length
 if (formCountryObject.value != "United Kingdom") {
   return false;
 }
 test = test.toUpperCase(); //Change to uppercase
 while (test.slice(0,1) == " ") //Strip leading spaces
  {test = test.substr(1,size-1);size = test.length
  }
 while(test.slice(size-1,size)== " ") //Strip trailing spaces
  {test = test.substr(0,size-1);size = test.length
  }
 document.form1.postCode.value = test; //write back to form field
 if (size < 6 || size > 8){ //Check length
  alert(test + " is not a valid postcode - please check you have entered all the digits");
  formObject.focus();
  return true;
  }
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character
   alert(test + " is not a valid postcode - postcodes cannot start with a number");
   formObject.focus();
   return true;
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric
   alert(test + " is not a valid postcode - there appears to be an alpha character in the wrong position");
   formObject.focus();
   return true;
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha
   alert(test + " is not a valid postcode - there appears to be a number in the wrong position");
   formObject.focus();
   return true;
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha
   alert(test + " is not a valid postcode - there appears to be a number in the wrong position");
   formObject.focus();
   return true
  }
 if (!(test.charAt(size-4) == " ")){//space in position length-3
   alert(test + " is not a valid postcode - there is either no space or a space in the wrong position");
   formObject.focus();
   return true;
   }
 var count1 = test.indexOf(" ");
 var count2 = test.lastIndexOf(" ");
 if (count1 != count2){//only one space
   alert(test + " is not a valid postcode - please ensure there is only one space");
   formObject.focus();
   return true
  }
return false;
}
// check if email address is invalid
function invalidEmail(formName,controlName,errorStr) {
  var formObject = eval('document.'+formName+'.' + controlName);
  var emailAlert = isValidEmailAddress(formObject.value);
  if (!emailAlert == "") {
    alert(errorStr + emailAlert);
    formObject.focus();
    return true;
  }
}
// check validity of the telephone number
function invalidPhoneNum(formName,controlName) {
  var formObject = eval('document.'+formName+'.' + controlName);
  var phoneNum = formObject.value;
  if (phoneNum.length > 0) {
    if (!isAllNumbers(phoneNum)) {
      alert("Telephone numbers should only contain numbers. Also please make sure there are no spaces.");
      formObject.focus();
      return true;
     }
     if (phoneNum.length < 8) {
       alert("Please enter the full telephone number including dialling code");
       formObject.focus();
       return true;
     }
   }
   return false;
}
// check validity of a date
function invalidDate(formName,dayObject,monthObject,yearObject,fieldNameStr) {
  var formDayObject = eval('document.'+formName+'.' + dayObject);
  var formMonthObject = eval('document.'+formName+'.' + monthObject);
  var formYearObject = eval('document.'+formName+'.' + yearObject);
  var theDay = formDayObject.value;
  var theMonthStr = formMonthObject.value.toUpperCase();
  var theMonth = getMonthNum(theMonthStr)
  var theYear = formYearObject.value;
    if (theDay != "" || theMonthStr != "" || theYear != "") {
      if (!checkDay(theDay,theMonth,theYear)) {
        alert(fieldNameStr + " is not a valid date, please check and re-enter");
        return true;
           }
         }
  return false;
}
// check that the email address is entered the dsame in both fields
function emailMismatch(formName,email,reEmail) {
  var formEmailObject = eval('document.'+formName+'.' + email);
  var formReEmailObject = eval('document.'+formName+'.' + reEmail);
  if (formEmailObject.value != formReEmailObject.value) {
    alert("Please check that both your email addresses are the same");
    return true;
  }
  return false;
}
// check that the number entered is at least the given number
function notAtLeast(formName, fieldName, atLeastNumber, alertText) {
  var theNumberObject = eval('document.'+formName+'.'+fieldName);
  var theNumber = theNumberObject.value;
  if (theNumber < atLeastNumber) {
    alert(alertText);
    theNumberObject.focus();
    return true;
  }
  return false;
}

// check that a field conforms to the given validation
function invalidMandatory(formName,controlName, minLength, maxLength, errorMsgMinFail, errorMsgMaxFail) {
  var invalid = false;
  var formObject = eval('document.'+formName+'.' + controlName);
  var controlValueLength = formObject.value.length;
  if (controlValueLength < minLength) {
    alert(errorMsgMinFail);
    invalid = true;
  }
  if (controlValueLength > maxLength) {
    alert(errorMsgMaxFail);
    invalid = true;
  }
  if (invalid) {
    formObject.focus();
  }
  return invalid;
} 
// end function invalidMandatory()