$(document).ready(function() {
  setup_form_validation();
});

function setup_form_validation() {
  $('form').submit(function() {
    var error_count = 0;
    
    $(this).find(':input.required').each(function() {
      if($(this).val() == '') {
        error_count++;
        $(this).addClass('error');
      }
      else {
        $(this).removeClass('error');
      }
    });
    
    if(error_count > 0) {
      alert('One or more required fields were not completed.  Please make sure all required fields are filled in.');
      return (false);
    }
    else {
      return (true);
    }
    
  });
  
  $('form').each(function() {
    var form = $(this);
    
    $(this).find('.required').each(function() {
      var name = $(this).attr('name');
      form.find('label[for="'+ name + '"]').each(function() {
        $(this).append('<span class="required">*</span>');
      });
    });
  });
}