--- job_posting.module	2009-09-30 15:48:18.000000000 -0400
+++ job_posting_new.module	2009-09-30 17:23:26.000000000 -0400
@@ -46,6 +46,7 @@ function job_posting_node_info() {
 function job_posting_perm() {
 
   return array(
+    'apply to job posting',
     'create job posting',
     'edit job posting',
     'edit own job posting',
@@ -90,7 +91,8 @@ function job_posting_menu() {
     'title' => t('Submit an application'),
     'page callback' => 'job_posting_application_form_page',
     'page arguments' => array(3),
-    'access callback' => 'user_is_logged_in',
+    'access callback' => 'user_access',
+    'access arguments' => array('apply to job posting'),
     'type' => MENU_CALLBACK,
   );
   $items['admin/settings/job-posting'] = array(
@@ -529,12 +531,26 @@ function job_posting_application_form($f
     '#value' => t('@title (@reference)', array('@title' => $title,
       '@reference' => $reference)),
   );
-  $form['job_posting_applicant'] = array(
-    '#type' => 'item',
-    '#title' => t('Applicant'),
-    '#value' => $user->name .' &lt;'. $user->mail .'&gt;',
-    '#maxlength' => 64,
-  );
+  // present name and email fields if applicant is not a user
+  if($user->uid == 0) {
+    $form['job_posting_applicant_name'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Applicant name'),
+      '#maxlength' => 225,
+    );
+    $form['job_posting_applicant_email'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Applicant email'),
+      '#maxlength' => 225,
+    );
+  } else {
+    $form['job_posting_applicant'] = array(
+      '#type' => 'item',
+      '#title' => t('Applicant'),
+      '#value' => $user->name .' &lt;'. $user->mail .'&gt;',
+      '#maxlength' => 64,
+    );
+  }
   $form['job_posting_coverletter'] = array(
     '#type' => 'textarea',
     '#title' => t('Cover letter'),
@@ -552,6 +568,13 @@ function job_posting_application_form($f
       covering letter. Files must not exceed 1M in size and can use only 
       the following extensions: txt doc pdf.'),
   );
+
+  if(module_exists('captcha')) {
+    $form['captcha'] = array(
+      '#type' => 'captcha',
+      '#captcha_type' => 'captcha/Math',
+    );
+  }
                 
   $form['job_posting_submit'] = array('#type' => 'submit', '#value' => t('Send'));
         
@@ -595,6 +618,17 @@ function job_posting_application_form_pa
  * @param unknown_type $form_state
  */
 function job_posting_application_form_validate($form, &$form_state) {
+
+  global $user;
+  if($user->uid == 0) {
+    if($form_state['values']['job_posting_applicant_name'] == '') {
+      form_set_error('job_posting_applicant_name', t('A name is required'));
+    }
+    if($form_state['values']['job_posting_applicant_email'] == '') {
+      form_set_error('job_posting_applicant_email', t('An email address is required'));
+    }
+  }
+
   $file = file_save_upload('job_posting_resume');
   if ($file) {
     // check for allowed extensions
@@ -623,10 +657,10 @@ function job_posting_application_form_su
   global $user;
   $separator = md5(uniqid(time()));
   $module = 'job_posting';
-  $apply_from = $user->mail;
+  $apply_from = ($user->uid != 0) ? $user->mail : $form_state['values']['job_posting_applicant_email'];
   $apply_to = $form_state['values']['job_posting_email'];
   $confirm_from = $form_state['values']['job_posting_email'];
-  $confirm_to = $user->mail;
+  $confirm_to = ($user->uid != 0) ? $user->mail : $form_state['values']['job_posting_applicant_email'];
   $body = $form_state['values']['job_posting_coverletter'];
   // use applicant's language preference
   $language = user_preferred_language($user);
@@ -660,12 +694,10 @@ function job_posting_application_form_su
   if (drupal_mail($module, 'application', $apply_to, $language, $params, $apply_from)) {
     drupal_set_message(t('Your application has been submitted. Thank you.'));
     // send confirmation to applicant's profile address provided they're registered
-    if ($user->uid != 0) {
-      if (drupal_mail($module, 'confirmation', $confirm_to, $language, $params, $confirm_from)) {
-        drupal_set_message(t('A confirmation message has been sent to ') . $user->mail);
-        // on success, send user back to job listings page
-        $form_state['redirect'] = 'jobs';
-      }
+    if (drupal_mail($module, 'confirmation', $confirm_to, $language, $params, $confirm_from)) {
+      drupal_set_message(t('A confirmation message has been sent to ') . $confirm_to);
+      // on success, send user back to job listings page
+      $form_state['redirect'] = 'jobs';
     }
   }
   
@@ -947,28 +979,15 @@ function template_preprocess_job_posting
   if ($variables['node']->job_posting_applink) {
     // check if the posting is still alive or whether a deadline is even being used
     if (is_null($variables['deadline']) || ($variables['node']->job_posting_deadline > _job_posting_gmt())) {
-      // already authenticated user
-      if ($user->uid != 0) {
+      // check if user is permitted to apply
+      if (user_access('apply to job posting')) {
         $variables['joblink'] = l(t('Submit an application'), 'job/application/node/' 
           . $variables['node']->nid); 
+      } else {
+        $variables['joblink'] = t('Applications are not being accepted');
       }
-      // anonymous user
-      else {
-        // can users register themselves?
-        if (variable_get('user_register', 1)) {
-          $variables['joblink'] = 
-            t('<a href="@login">Login</a> or <a href="@register">register</a> to submit an application', 
-            array('@login' => url('user/login', array('query' => 'destination=job/application/node/'. $variables['node']->nid)), 
-            '@register' => url('user/register', array('query' => 'destination=node/'. $variables['node']->nid)))
-          );
-        }
-        else {
-          $variables['joblink'] = t('<a href="@login">Login</a> to submit an application', 
-            array('@login' => url('user/login', array('query' => 'destination=job/application/node/'. $variables['node']->nid))));
-        }
-      }  
     }
-  }    
+  }
   $variables['contact'] = check_plain($variables['node']->job_posting_contact);
   $variables['url'] = check_url($variables['node']->job_posting_url);
 }
