function validate() {
 
  var errorBox;
  var errorText='';
  var ageChecked='';
  var contactDetailsChecked='';
  var acceptRiskChecked='';

 
  errorBox = document.getElementById('errors');

  spanElement= document.getElementById('feedback');
  if(spanElement){
     errorBox.removeChild(spanElement);
  }

  if(document.appForm.appFirstName.value==''){
      errorText += "<p>The <em>'First name'</em> field is empty.</p>";
      document.appForm.appFirstName.style.backgroundColor="yellow";
  }
  else{
      document.appForm.appFirstName.style.backgroundColor="#F5F8FB";
  }
 
  if(document.appForm.appLastName.value==''){
      errorText += "<p>The <em>'Last name'</em> field is empty.</p>";
      document.appForm.appLastName.style.backgroundColor="yellow";
  }
  else{
      document.appForm.appLastName.style.backgroundColor="#F5F8FB";
  }


  if(document.appForm.appAddress1.value==''){
      errorText += "<p>The <em>'Address'</em> field is empty.</p>";
      document.appForm.appAddress1.style.backgroundColor="yellow";
  }
  else{
      document.appForm.appAddress1.style.backgroundColor="#F5F8FB";
  }


  if(document.appForm.appTown.value==''){
      errorText += "<p>The <em>'Town'</em> field is empty.</p>";
      document.appForm.appTown.style.backgroundColor="yellow";
  }
  else{
      document.appForm.appTown.style.backgroundColor="#F5F8FB";
  }

  if(document.appForm.appPostcode.value==''){
      errorText += "<p>The <em>'Postcode'</em> field is empty.</p>";
      document.appForm.appPostcode.style.backgroundColor="yellow";
  }
  else{
      document.appForm.appPostcode.style.backgroundColor="#F5F8FB";
  }

  if(document.appForm.appTelephoneHome.value==''){
      errorText += "<p>The <em>'Telephone (home)'</em> field is empty.</p>";
      document.appForm.appTelephoneHome.style.backgroundColor="yellow";
  }
  else{
      document.appForm.appTelephoneHome.style.backgroundColor="#F5F8FB";
  }


  if(document.appForm.appTelephoneMobile.value==''){
      errorText += "<p>The <em>'Telephone (mobile)'</em> field is empty.</p>";
      document.appForm.appTelephoneMobile.style.backgroundColor="yellow";
  }
  else{
      document.appForm.appTelephoneMobile.style.backgroundColor="#F5F8FB";
  }

  if(document.appForm.appEmail.value==''){
      errorText += "<p>The <em>'Your email address'</em> field is empty.</p>";
      document.appForm.appEmail.style.backgroundColor="yellow";
  }
  else{
      document.appForm.appEmail.style.backgroundColor="#F5F8FB";
  }
 
  


  for (var i=0; i < document.appForm.age.length; i++) {
    if (document.appForm.age[i].checked) {
      ageChecked='yes';
      if(document.appForm.age[i].value=="No"){
         alert("You must be over 18 to join the club.");
         errorText = "<p>You must be over 18 to join the club.</p>";
         giveFeedback(errorBox, errorText);
         return false;
      }
      break;
    }    
  }
 
  if(ageChecked==''){
    errorText += "<p>You need to confirm that you are over 18.</p>";
    document.getElementById('appAge').style.backgroundColor="yellow"; 
  }
  else{
    document.getElementById('appAge').style.backgroundColor="#FFFFFF"; 
  }

  for (var i=0; i < document.appForm.contactDetails.length; i++) {
    if (document.appForm.contactDetails[i].checked) {
      contactDetailsChecked='yes';
      break;
    }    
  }
 
  if(contactDetailsChecked==''){
    errorText += "<p>You need to indicate which contact details can be included in the members contact list.</p>";
    document.getElementById('appContactDetails').style.backgroundColor="yellow"; 
  }
  else{
    document.getElementById('appContactDetails').style.backgroundColor="#FFFFFF"; 
  }



  if(document.appForm.experience.value=='' || document.appForm.experience.value=='Your experience'){
      errorText += "<p>The <em>'Please provide a resume of your mountaineering experience'</em> field is empty.</p>";
      document.appForm.experience.style.backgroundColor="yellow";
  }
  else{
      document.appForm.experience.style.backgroundColor="#F5F8FB";
  }

  if(document.appForm.medical.value=='' || document.appForm.medical.value=='Any medical issues?'){
      errorText += "<p>If there are no medical conditions we need to be aware of please state <em>none</em>.</p>";
      document.appForm.medical.style.backgroundColor="yellow";
  }
  else{
      document.appForm.medical.style.backgroundColor="#F5F8FB";
  }


  for (var i=0; i < document.appForm.acceptRisk.length; i++) {
    if (document.appForm.acceptRisk[i].checked) {
      acceptRiskChecked='yes';
      if(document.appForm.acceptRisk[i].value=="No"){
         alert("You need to accept that climbing and mountaineering are activities with a danger of personal injury or death and accept these risks and indicate that you will take responsibility for your own actions and involvement before you can join the club.");
         errorText = "<p>You need to accept that climbing and mountaineering are activities with a danger of personal injury or death and accept these risks and indicate that you will take responsibility for your own actions and involvement before you can join the club.</p>";
         giveFeedback(errorBox, errorText);
         return false;
      }
      break;
    }    
  }
 
  if(acceptRiskChecked==''){
    errorText += "<p>You indicate that you accept that climbing and mountaineering are activities with a danger of personal injury or death and accept these risks and indicate that you will take responsibility for your own actions and involvement.</p>";
    document.getElementById('appAcceptRisk').style.backgroundColor="yellow"; 
  }
  else{
    document.getElementById('appAcceptRisk').style.backgroundColor="#FFFFFF"; 
  }

  
  if(errorText!=''){   
    giveFeedback(errorBox, errorText)
    return false;
  }
  else{
    return true;
  }    

 return false;
}


function giveFeedback(errorBox, errorText){
    //In Firefox etc, can just add text using innerHTML but need to create child
    // for it to work in ie7    
    var newSpan = document.createElement("span");
    newSpan.id="feedback";
    newSpan.innerHTML = "<p>Some information seems to be missing:</p>"+errorText;
    errorBox.appendChild(newSpan );
    errorBox.style.display="block";
    errorBox.style.border="thick solid red";
    window.scrollTo(0,0);  
}
