// prepare the form when the DOM is ready 
$(document).ready(function() { 


$("#contact_submit").click(function(){
  submitForm('contact');
  return false; // Make sure we return false.
});

$("#feedback_submit").click(function(){
  submitForm('feedback');
  return false; // Make sure we return false.
});

$("#volunteer_submit").click(function(){
  submitForm('volunteer');
  return false; // Make sure we return false.
});


});



function submitForm(type) {

$.ajax({
type: 'POST', //We're telling jQuery we're doing a POST.
url: '/',
data: $("#"+type+"_form").serialize(), // This encapsulates all the form data and passes it as FORM_DATA.
success: function(data){
if (data.search(/error/) >= 0) // Check to see if the word 'error' occurs in the returned data.
{
  var $data = $(data);

  var err = "There were errors found in your entry:\n";

  $data.find('li').each(function() { // Check each instance of the <li> in the returned data and iterate through.
  err+= "\t" + $(this).text() + "\n";
  });

  alert(err); // Alert the Error which will detail all the values of the <li> (i.e the errors!)
}
else
{
  //success... overwrite the output!
  $("#contact_form_wrapper").html(data);

}
}
});



}



