diff -r cca12ccb0f7a -r f21f2ebeee75 webform_simplenews.inc
--- a/webform_simplenews.inc	Tue Apr 20 16:31:38 2010 -0400
+++ b/webform_simplenews.inc	Tue Apr 20 16:32:31 2010 -0400
@@ -28,6 +28,9 @@
  */
 function _webform_theme_newsletter_email() {
   return array(
+    'webform_newsletter_email' => array(
+      'arguments' => array('element' => NULL),
+    ),  
     'webform_display_newsletter_email' => array(
       'arguments' => array('component' => NULL, 'value' => NULL, 'format' => 'plain'),
     ),
@@ -66,10 +69,17 @@
     '#default_value' => $component['extra']['news_vid'],
     '#description'   => t('Select the newsletter this form will handle.'),
     '#required'      => TRUE,
-    '#multiple'      => FALSE,
+    '#multiple'      => TRUE,
     '#size'          => sizeof($options),
     '#options'       => $options,
   );
+  $form['extra']['individual_newsletter_selection'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Allow individual selection of newsletters'),
+    '#default_value' => $component['extra']['individual_newsletter_selection'],
+    '#description' => t('If this box is checked and multiple newsletters are selected above, the user will be presented with a list of invdividual newsletters that they can subscribe to.'),
+  
+  );
   $form['extra']['action'] = array(
     '#type'          => 'radios',
     '#title'         => t('Action'),
@@ -95,6 +105,8 @@
 /**
  * Validation function for the email edit form.
  */
+
+
 function _webform_edit_newsletter_email_validate($element, &$form_state) {
   if ($form_state['values']['user_email']) {
     $form_state['values']['value'] = '%useremail';
@@ -120,21 +132,40 @@
   }
 
   $element = array(
+    '#title'             => htmlspecialchars($component['name'], ENT_QUOTES),
+    '#required'          => $component['mandatory'],
+    '#weight'            => $component['weight'],    
+    '#prefix'            => '<div class="webform-component-'. $component['type'] .'" id="webform-component-'. $component['form_key'] .'">',
+    '#suffix'            => '</div>',
+    '#webform_component' => $component,
+    '#theme'             => 'webform_newsletter_email',
+    '#element_validate'  => array('_webform_validate_newsletter_email'),    
+  );
+  
+  $element['newsletter_email_address'] = array(
     '#type'              => 'textfield',
-    '#title'             => htmlspecialchars($component['name'], ENT_QUOTES),
     '#default_value'     => $default_value,
-    '#required'          => $component['mandatory'],
-    '#weight'            => $component['weight'],
     '#attributes'        => $component['extra']['attributes'],
     '#field_prefix'      => empty($component['extra']['field_prefix']) ? NULL : check_plain($component['extra']['field_prefix']),
-    '#field_suffix'      => empty($component['extra']['field_suffix']) ? NULL : check_plain($component['extra']['field_suffix']),     
-    '#prefix'            => '<div class="webform-component-'. $component['type'] .'" id="webform-component-'. $component['form_key'] .'">',
-    '#suffix'            => '</div>',
-    '#element_validate'  => array('_webform_validate_newsletter_email'),
-    '#webform_component' => $component,
-    '#size'              => $size,
+    '#field_suffix'      => empty($component['extra']['field_suffix']) ? NULL : check_plain($component['extra']['field_suffix']),
+    '#size'              => $size,    
   );
   
+  $news_vid = $component['extra']['news_vid'];
+
+  if(count($news_vid) > 1 && $component['extra']['individual_newsletter_selection']){
+    $options = array();
+    foreach (taxonomy_get_tree(variable_get('simplenews_vid', '')) as $newsletter) {
+      if($news_vid[$newsletter->tid] != 0)
+        $options[$newsletter->tid] = $newsletter->name;
+    }    
+    $element['newsletter_selection'] = array(
+      '#prefix' => '',
+      '#type' => 'checkboxes',
+      '#default_value' => array(),
+      '#options' => $options,
+    );     
+  }
   return $element;
 }
 
@@ -159,8 +190,12 @@
   }
 
   $component = $form_element['#webform_component'];
-  if (!empty($form_element['#value']) && !valid_email_address($form_element['#value'])) {
-    form_error($form_element, t('%value is not a valid email address.', array('%value' => $form_element['#value'])));
+
+  if (!empty($form_element['newsletter_email_address']['#value']) && !valid_email_address($form_element['newsletter_email_address']['#value'])) {
+    form_error($form_element, t('%value is not a valid email address.', array('%value' => $form_element['newsletter_email_address']['#value'])));
+  }
+  elseif(empty($form_element['newsletter_email_address']['#value']) && $form_element['#required']){
+    form_error($form_element, t('E-mail address for newsletter "%name" is required.', array('%name' => $component['name'])));
   }
 }
 
@@ -169,20 +204,38 @@
  */
 function _webform_submit_newsletter_email($component, $value) {
   global $user;
-  $mail = $value;
+  $mail = $value['newsletter_email_address'];
+  $return_val = array(0 => $value['newsletter_email_address']);
+  $selected_subscriptions = array();
+  if(is_array($value['newsletter_selection'])){
+    foreach($value['newsletter_selection'] as $k => $v){
+      if($v > 0)
+        $selected_subscriptions[(int)$k] = (int)$k;
+    }
+  }
   $news_vid = $component['extra']['news_vid'];
+  if(count($selected_subscriptions) > 0)
+    $news_vid = $selected_subscriptions;
   $action = $component['extra']['action'];
-
   $account = _simplenews_user_load($mail);
   $confirm = $account->uid && $account->uid == $user->uid ? FALSE : TRUE;
-  if ($mail && $news_vid) {
+  if ($mail && isset($news_vid)) {
     if ($action == 'subscribe') {
-      simplenews_subscribe_user($mail, $news_vid, $confirm);
+      if(count($news_vid) > 0){
+        foreach ($news_vid as $k => $v){
+          simplenews_subscribe_user($mail, $v, $confirm);
+        }
+      }
     }
     else {
-      simplenews_unsubscribe_user($mail, $news_vid, $confirm);
+      if(count($news_vid) > 0){
+        foreach ($news_vid as $k => $v){      
+          simplenews_unsubscribe_user($mail, $v, $confirm);
+        }
+      }
     }
   }
+  return $return_val;
 }
 
 /**
@@ -201,6 +254,20 @@
   ); 
 }
 
+
+/**
+ * Theme a webform time element.
+ */
+function theme_webform_newsletter_email($element) {
+  if (form_get_error($element)) {
+    $element['newsletter_email_address']['#attributes']['class'] = 'error';
+  }  
+  $output = '<div class="form-item">' . drupal_render($element) . '</div>';
+  $element['#type'] = 'element';
+  return theme('form_element', $element, $output);
+}
+
+
 /**
  * Format the text output for this component.
  */
