commit f9f7b42be6af5eda49a2834ce057820eb9d71b90
Author: Harold Aling <harold@sait.nl>
Date:   Tue Jan 19 15:21:17 2016 +0100

    Temp commit

diff --git a/dvg_appointments.client.inc b/dvg_appointments.client.inc
index 7bc714b..fee169a 100644
--- a/dvg_appointments.client.inc
+++ b/dvg_appointments.client.inc
@@ -4,6 +4,7 @@ abstract class AppointmentsClientApi {
 
   protected $soap_client;
   protected $timezone;
+  protected $max_persons = 10;
 
   abstract function __construct();
 
@@ -94,6 +95,10 @@ abstract class AppointmentsClientApi {
     return $value;
   }
 
+  public function get_max_persons() {
+    return $this->max_persons;
+  }
+
   abstract public function get_available_products();
 
   abstract public function get_customer_appointments($vars);
diff --git a/dvg_appointments.js b/dvg_appointments.js
new file mode 100644
index 0000000..a9bc296
--- /dev/null
+++ b/dvg_appointments.js
@@ -0,0 +1,13 @@
+(function ($) {
+  // Resets the value of the count-field when a product with max_persons
+  // being 1 is selected. The count-field will be hidden by 'states' and
+  // 'state:visible'-event will be triggered.
+  $(document).bind('state:visible', function(event) {
+    if (!event.value && Drupal.settings.dvg_appointments) {
+      var name = event.target.name;
+      if (name && Drupal.settings.dvg_appointments[name]) {
+        $(':input[name="' + name + '"]').val(1);
+      }
+    }
+  });
+}(jQuery));
diff --git a/dvg_appointments.module b/dvg_appointments.module
index d9aed1f..c725b2e 100644
--- a/dvg_appointments.module
+++ b/dvg_appointments.module
@@ -790,8 +790,14 @@ function dvg_appointments_element_info() {
 function dvg_appointments_product_element_process($element, &$form_state, $form) {
   $client = dvg_appointments_get_client_api();
   $product_options = array('' => t('Select a product'));
+  $invisible_values = array(array('value' => ''));
+
   foreach ($client->get_available_products() as $product) {
     $product_options[$product['id']] = $product['name'];
+
+    if (isset($product['max_persons']) && $product['max_persons'] == 1) {
+      $invisible_values[] = array('value' => $product['id']);
+    }
   }
 
   $element['product'] = array(
@@ -815,9 +821,28 @@ function dvg_appointments_product_element_process($element, &$form_state, $form)
     '#size' => 4,
     '#maxlength' => 4,
     '#default_value' => !empty($element['#default_value']['count']) ? $element['#default_value']['count'] : 1,
-    '#element_validate' => array('element_validate_integer_positive', '_dvg_appointments_element_validate_person_count'),
+    '#element_validate' => array('element_validate_integer_positive'),
   );
 
+  if (count($invisible_values) > 1) {
+    $element['count']['#states']['invisible'] = array(
+      ':input[name="' . $element['#name'] . '[product]"]' => $invisible_values,
+    );
+
+    $element['#attached']['js'][] = array(
+      'type' => 'setting',
+      'data' => array(
+        'dvg_appointments' => array(
+          $element['#name'] . '[count]' => TRUE,
+        ),
+      ),
+    );
+
+    $element['#attached']['js'][] = drupal_get_path('module', 'dvg_appointments') . '/dvg_appointments.js';
+  }
+
+  $element['#element_validate'][] = '_dvg_appointments_element_validate_person_count';
+
   return $element;
 }
 
@@ -825,10 +850,18 @@ function dvg_appointments_product_element_process($element, &$form_state, $form)
  * Form element validation handler for a maximum person count.
  */
 function _dvg_appointments_element_validate_person_count($element, &$form_state) {
-  $max_persons = 10;
-  $value = $element['#value'];
-  if ($value !== '' && is_numeric($value) && (int)$value > $max_persons) {
-    form_error($element, t('%name has a maximum of %max_persons.', array('%name' => $element['#title'], '%max_persons' => $max_persons)));
+  $client = dvg_appointments_get_client_api();
+  $max_persons = $client->get_max_persons();
+  $count = $element['count']['#value'];
+  $product_id = $element['product']['#value'];
+  if ($count !== '' && is_numeric($count)) {
+    $products = $client->get_available_products();
+    if (isset($products[$product_id]) && isset($products[$product_id]['max_persons'])) {
+      $max_persons = (int)$products[$product_id]['max_persons'];
+    }
+    if ((int)$count > $max_persons) {
+      form_error($element['count'], t('%name has a maximum of %max_persons.', array('%name' => $element['count']['#title'], '%max_persons' => $max_persons)));
+    }
   }
 }
 
@@ -941,7 +974,7 @@ function dvg_appointments_date_time_element_process($element, &$form_state, $for
   $timezone = new DateTimeZone($client_api->get_timezone() ? $client_api->get_timezone() : drupal_get_user_timezone());
 
   $afternoon_start_hour = variable_get('dvg_appointments_afternoon_start_hour', '12');
-  $evening_start_hour = variable_get('dvg_appointments_evening_start_hour', '18');  
+  $evening_start_hour = variable_get('dvg_appointments_evening_start_hour', '18');
 
   foreach ($dates as $date => $time_array) {
     $unix_date = strtotime($date);
