### Eclipse Workspace Patch 1.0
#P Drupal Modules
Index: uc_discounts/uc_discounts.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_discounts_alt/uc_discounts/uc_discounts.admin.inc,v
retrieving revision 1.10
diff -u -r1.10 uc_discounts.admin.inc
--- uc_discounts/uc_discounts.admin.inc	22 Aug 2009 21:30:39 -0000	1.10
+++ uc_discounts/uc_discounts.admin.inc	3 May 2010 15:55:53 -0000
@@ -70,7 +70,8 @@
   $form           = array();
   $form_submitted = $form_state["submitted"];
   $is_edit        = $discount_id != 0;
-  $seed           = rand(1, 1000000);
+  // To store form elements that should be toggled, format: trigger_element => toggled_element
+  $toggled_elements = array();
 
   if (!$form_submitted && $is_edit) {
 
@@ -125,19 +126,6 @@
 
 
   $has_qualifying_amount_max = isset($form_state["values"]["has_qualifying_amount_max"]) ? $form_state["values"]["has_qualifying_amount_max"] : HAS_QUALIFYING_AMOUNT_MAX_DEFAULT;
-  $display_string = ($has_qualifying_amount_max) ? "" : " style='display:none'";
-
-  $qualifying_amount_max_container_id = "qualifying-amount-max-container-". $seed;
-  $qualifying_amount_max_prefix = sprintf("<div id='%s' onclick='if ( jQuery(\"#%s input[type=\\\"checkbox\\\"]\").attr(\"checked\") {  )" . "{ jQuery(\"#%s .qualifying-amount-max-container\").slideDown(); }" . "else { jQuery(\"#%s .qualifying-amount-max-container\").slideUp();} }'>",
-    $qualifying_amount_max_container_id, $qualifying_amount_max_container_id,
-    $qualifying_amount_max_container_id, $qualifying_amount_max_container_id,
-    $qualifying_amount_max_container_id, $qualifying_amount_max_container_id
-  );
-
-  $form["qualifying_amount_max_header"] = array(
-    "#type" => "hidden",
-    "#prefix" => $qualifying_amount_max_prefix,
-  );
 
   $form["has_qualifying_amount_max"] = array(
     "#type" => "checkbox",
@@ -157,15 +145,8 @@
     "#default_value" => $form_state["values"]["qualifying_amount_max"],
     "#size" => 15,
     "#required" => FALSE,
-    "#prefix" => sprintf("<div class='qualifying-amount-max-container'%s>", $display_string),
-    "#suffix" => "</div>",
   );
-
-  $form["qualifying_amount_max_footer"] = array(
-    "#type" => "hidden",
-    "#suffix" => "</div>",
-  );
-
+  $toggled_elements["has_qualifying_amount_max"] = "qualifying_amount_max";
 
   $form["discount_type"] = array(
     "#type" => "select",
@@ -185,17 +166,6 @@
   );
 
   $requires_code = isset($form_state["values"]["requires_code"]) ? $form_state["values"]["requires_code"] : REQUIRES_CODE_DEFAULT;
-  $display_string = ($requires_code) ? "" : " style='display:none'";
-
-  $codes_container_id = "codes-container-". $seed;
-  $codes_prefix = sprintf("<div id='%s' onclick='if ( jQuery(\"#%s input[type=\\\"checkbox\\\"]\").attr(\"checked\") {  )" . "{ jQuery(\"#%s .codes-container\").slideDown(); }" . "else { jQuery(\"#%s .codes-container\").slideUp();} }'>",
-    $codes_container_id, $codes_container_id, $codes_container_id, $codes_container_id, $codes_container_id, $codes_container_id
-  );
-
-  $form["codes_header"] = array(
-    "#type" => "hidden",
-    "#prefix" => $codes_prefix,
-  );
 
   $form["requires_code"] = array(
     "#type" => "checkbox",
@@ -222,33 +192,13 @@
     "#description" => t("Enter discount codes in box above, one code per line.  Spaces are permitted but may confuse consumers. Note codes will be trimmed."),
     "#default_value" => $codes_string,
     "#rows" => 5,
-    "#prefix" => sprintf("<div class='codes-container'%s>", $display_string) ."<div class='discount-codes-wrapper'>",
-    "#suffix" => "</div></div>",
-  );
-
-  $form["codes_footer"] = array(
-    "#type" => "hidden",
-    "#suffix" => "</div>",
   );
+  $toggled_elements["requires_code"] = "codes";
 
   $filter_type = isset($form_state["values"]["filter_type"]) ? $form_state["values"]["filter_type"] : FILTER_TYPE_DEFAULT;
-  $products_display_string = ($filter_type == FILTER_TYPE_PRODUCTS) ? "" : " style='display:none'";
-  $terms_display_string = ($filter_type == FILTER_TYPE_TERMS) ? "" : " style='display:none'";
-  $skus_display_string = ($filter_type == FILTER_TYPE_SKUS) ? "" : " style='display:none'";
-
-  $filter_type_container_id = "filter-type-container-". $seed;
-  $filter_type_prefix = sprintf(
-    "<div id='%s' onclick='value = jQuery(\"#%s select\").val();" . "if (value == %d) { jQuery(\"#%s .products-container\").slideDown(); jQuery(\"#%s .terms-container\").slideUp(); jQuery(\"#%s .skus-container\").slideUp(); }" . "if (value == %d) { jQuery(\"#%s .products-container\").slideUp(); jQuery(\"#%s .terms-container\").slideDown(); jQuery(\"#%s .skus-container\").slideUp(); }" . "if (value == %d) { jQuery(\"#%s .products-container\").slideUp(); jQuery(\"#%s .terms-container\").slideUp(); jQuery(\"#%s .skus-container\").slideDown(); }'>",
-    $filter_type_container_id, $filter_type_container_id,
-    FILTER_TYPE_PRODUCTS, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id,
-    FILTER_TYPE_TERMS, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id,
-    FILTER_TYPE_SKUS, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id
-  );
-
-  $form["filter_type_header"] = array(
-    "#type" => "hidden",
-    "#prefix" => $filter_type_prefix,
-  );
+
+  // Store the filtered textareas to insert into js
+  $filter_areas = array();
 
   //Create filter_type form element
   $options = array();
@@ -284,9 +234,8 @@
     "#options" => $options,
     "#default_value" => $product_ids,
     "#multiple" => TRUE,
-    "#prefix" => sprintf("<div class='products-container'%s>", $products_display_string),
-    "#suffix" => "</div>",
   );
+  $filter_areas[] = "product_ids";
 
   //Get current discount terms
   $term_ids = null;
@@ -307,9 +256,8 @@
     "#options" => $options,
     "#default_value" => $term_ids,
     "#multiple" => TRUE,
-    "#prefix" => sprintf("<div class='terms-container'%s>", $terms_display_string),
-    "#suffix" => "</div>",
   );
+  $filter_areas[] = "term_ids";
 
   //Get current discount SKUs
   $skus = null;
@@ -330,27 +278,10 @@
     "#options" => $options,
     "#default_value" => $skus,
     "#multiple" => TRUE,
-    "#prefix" => sprintf("<div class='skus-container'%s>", $skus_display_string),
-    "#suffix" => "</div>",
-  );
-
-  $form["filter_type_footer"] = array(
-    "#type" => "hidden",
-    "#suffix" => "</div>",
   );
+  $filter_areas[] = "skus";
 
   $has_role_filter = isset($form_state["values"]["has_role_filter"]) ? $form_state["values"]["has_role_filter"] : HAS_ROLE_FILTER_DEFAULT;
-  $display_string = ($has_role_filter) ? "" : " style='display:none'";
-
-  $roles_container_id = "roles-container-". $seed;
-  $roles_prefix = sprintf("<div id='%s' onclick='if ( jQuery(\"#%s input[type=\\\"checkbox\\\"]\").attr(\"checked\") {  )" . "{ jQuery(\"#%s .roles-container\").slideDown(); }" . "else { jQuery(\"#%s .roles-container\").slideUp();} }'>",
-    $roles_container_id, $roles_container_id, $roles_container_id, $roles_container_id, $roles_container_id, $roles_container_id
-  );
-
-  $form["roles_header"] = array(
-    "#type" => "hidden",
-    "#prefix" => $roles_prefix,
-  );
 
   $form["has_role_filter"] = array(
     "#type" => "checkbox",
@@ -378,14 +309,8 @@
     "#options" => $options,
     "#default_value" => $role_ids,
     "#multiple" => TRUE,
-    "#prefix" => sprintf("<div class='roles-container'%s>", $display_string),
-    "#suffix" => "</div>",
-  );
-
-  $form["roles_footer"] = array(
-    "#type" => "hidden",
-    "#suffix" => "</div>",
   );
+  $toggled_elements["has_role_filter"] = "role_ids";
 
   $form["requires_single_product_to_qualify"] = array(
     "#type" => "checkbox",
@@ -443,17 +368,6 @@
   );
 
   $has_expiration = isset($form_state["values"]["has_expiration"]) ? $form_state["values"]["has_expiration"] : HAS_EXPIRATION_DEFAULT;
-  $display_string = ($has_expiration) ? "" : " style='display:none'";
-
-  $expiration_container_id = "expiration-container-". $seed;
-  $expiration_prefix = sprintf("<div id='%s' onclick='if ( jQuery(\"#%s input[type=\\\"checkbox\\\"]\").attr(\"checked\") {  )" . "{ jQuery(\"#%s .expiration-container\").slideDown(); }" . "else { jQuery(\"#%s .expiration-container\").slideUp();} }'>",
-    $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id
-  );
-
-  $form["expiration_header"] = array(
-    "#type" => "hidden",
-    "#prefix" => $expiration_prefix,
-  );
 
   $form["has_expiration"] = array(
     "#type" => "checkbox",
@@ -469,14 +383,8 @@
     "#title" => t("Discount expiration") . sprintf("<span title='". t("This field is required.") ."' class='form-required'>*</span>"),
     "#description" => t("Date and time when the discount expires."),
     "#default_value" => date("Y-m-d H:i:s", $expiration),
-    "#prefix" => sprintf("<div class='expiration-container'%s>", $display_string),
-    "#suffix" => "</div>",
-  );
-
-  $form["expiration_footer"] = array(
-    "#type" => "hidden",
-    "#suffix" => "</div>",
   );
+  $toggled_elements["has_expiration"] = "expiration";
 
   $is_published = isset($form_state["values"]["is_published"]) ? $form_state["values"]["is_published"] : IS_PUBLISHED_DEFAULT;
   $form["is_published"] = array(
@@ -499,6 +407,45 @@
 
   $form["submit"] = array("#type" => "submit", "#value" => t("Submit"));
 
+  $js_toggled_elements = array();
+  foreach ($toggled_elements as $key => $elem) {
+    $js_toggled_elements["edit-" . str_replace("_", "-", $key)] = "edit-" . str_replace("_", "-", $elem);
+  }
+  $js_filter_areas = array();
+  foreach ($filter_areas as $area) {
+    $js_filter_areas[] = "edit-" . str_replace("_", "-", $area);
+  }
+  drupal_add_js("var admin = " . drupal_to_js($js_toggled_elements) . ";
+Drupal.behaviors.uc_discounts_admin = function(context){
+  for (var elem in admin) {
+    $('#' + elem).click(uc_discounts_admin_slide);
+    uc_discounts_admin_slide.call($('#' + elem)[0])
+  }
+  $('#edit-filter-type').change(uc_discounts_filter_slide);
+  uc_discounts_filter_slide.call($('#edit-filter-type')[0])
+}
+function uc_discounts_admin_slide() {
+  if ($(this).is(':checked')) {
+    $('#' + admin[this.id] + '-wrapper').slideDown();
+  }
+  else
+  {
+    $('#' + admin[this.id] + '-wrapper').slideUp();
+  }
+}
+function uc_discounts_filter_slide() {
+  var filter_areas = " . drupal_to_js($js_filter_areas) . ";
+  for(var i = 0;i < filter_areas.length; i++) {
+    if(i + 1 == $(this).val()){
+      $('#' + filter_areas[i] + '-wrapper').slideDown();
+    }
+    else
+    {
+      $('#' + filter_areas[i] + '-wrapper').slideUp();
+    }
+  }
+}", "inline");
+
   return $form;
 }
 
