// Validates Prayer Request form

//initialize form events
function initializeEvents(){

  var theForm=document.getElementById("prayerRequest");
  
  theForm.submitButton.onclick=function(){
    validatePrayer();
  }
    
    
    
}

  
  




function validatePrayer(){

  var theForm=document.getElementById("prayerRequest");
  //If prayer is requested for self, and requested by is empty - alert the user
  if(!((theForm.requestedBy.value.trim()).length)){
    alert("Please enter your name in the \"Requested By\" Field of the form");
    theForm.requestedBy.focus();
    return;
  }
  //If the prayer is for family or friend, then requested for must be included
  if((theForm.prayFor[1].checked==true || theForm.prayFor[2].checked==true) && !((theForm.requestedFor.value.trim()).length)){
    alert("Please enter the name of the person the prayer is requested for in the \"Requested For\" Field of the form");
    theForm.requestedFor.focus();
    return;
  }
  
  //If the prayer is for family or friend, then check that a value has been entered for the person's saved status
  if((theForm.prayFor[0].checked==true || theForm.prayFor[1].checked==true || theForm.prayFor[2].checked==true) && ((theForm.saved[0].checked==false) && (theForm.saved[1].checked==false) && (theForm.saved[2].checked==false))){
    alert("Please indicate if the person being prayed for is Saved, Unsaved, or Not Sure");
    theForm.saved[0].focus();
    return;
  }
  
  //Check contact method
  //first - email
  if(theForm.contactMethod1.checked==true){
    //email checked
    //check for presence of email address
    if (!(theForm.email.value.trim()).length){
      alert("Please ensure that you have entered your email address");
      theForm.email.focus();
      return;
    }
    //now check for a valid email address
    var someEmail=new EmailCheck(theForm.email.value.trim());
    if(someEmail.validate()==false){
      alert("Please check that you have entered a valid email address");
      return;
      }
    
      
       
  }  
  
  //Next Telephone
  if(theForm.contactMethod2.checked==true){
    //telephone checked
    //check for presence of email address
    if (!(theForm.telephone.value.trim()).length){
      alert("Please ensure that you have entered your telephone number");
      theForm.telephone.focus();
      return;
    }

  }
  
  //Next The Address
  if(theForm.contactMethod3.checked==true){
    //mail checked
    //check for presence of ea street and city state zip
    if (!(theForm.mail_1.value.trim()).length){
      alert("Please ensure that you have entered your Street Address");
      theForm.mail_1.focus();
      return;
    }
    if (!(theForm.mail_3.value.trim()).length){
      alert("Please ensure that you have entered your City, State, and Zip Code");
      theForm.mail_3.focus();
      return;
    }

  }
  
  //Check that a contact method has been explicity set
  if(theForm.contactMethod1.checked==false && theForm.contactMethod2.checked==false && theForm.contactMethod3.checked==false && theForm.contactMethod4.checked==false ){
    alert("Please indicate how you would like to be contacted, or check the \"No Contact\" Box");
    return;
    }
  
  
  //Finally, check that a prayer has been entered
  if(!(theForm.requestBody.value.trim()).length){
    alert("You haven't typed a prayer request.  Please type your request in the form");
    theForm.requestBody.focus();
    return;
    }
    
  
  //To reach this point validation is passed - congratulations!
  //submit the form
  theForm.submit();
  
  
  
  
  
  
  
}






  

