[%# 1.0@bugzilla.org %]
[%# The contents of this file are subject to the Mozilla Public
  # License Version 1.1 (the "License"); you may not use this file
  # except in compliance with the License. You may obtain a copy of
  # the License at http://www.mozilla.org/MPL/
  #
  # Software distributed under the License is distributed on an "AS
  # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  # implied. See the License for the specific language governing
  # rights and limitations under the License.
  #
  # The Original Code is the Bugzilla Testopia System.
  #
  # The Initial Developer of the Original Code is Greg Hendricks.
  # Portions created by Greg Hendricks are Copyright (C) 2001
  # Greg Hendricks. All Rights Reserved.
  #
  # Contributor(s): Greg Hendricks <ghendricks@novell.com>
  #                 Brian Kramer <bkramer@novell.com>
  #                 Scott Sudweeks <ssudweeks@novell.com>
  #%]

[%# INTERFACE:
  # ...
  #%]
  

<script src="testopia/dojo/dojo.js" type="text/javascript"></script>

<script type="text/javascript">
    dojo.require('dojo.io.*');
    function getProducts(class_ids){
    var env_products = document.getElementById("env_products");
    env_products.disabled = true;
    
      dojo.io.bind({
        url: "tr_query.cgi",
        content: { action: "get_products", class_ids: class_ids},
        method: "POST",
        load: function(tpe, data, evt){
          if(data == ""){
            env_products.add(new Option("[none available]", "none"), null);
          }
          else{
            var products = data.split("|||");
            var product;
            var product_ids = new Array();
            
            for (i = 0; i < products.length; i++){
              product = products[i].split("||");
              env_products.add(new Option(product[1], product[0]), null);
              product_ids[i] = product[0];
            }
            getCategories(product_ids);
          }
          env_products.disabled = false;
        },
        error: function(type, error){ alert("ERROR");},
          mimetype: "text/plain"
      });
    }
    
    function getCategories(prod_ids){
      var env_categories = document.getElementById("env_categories");
      env_categories.disabled = true;
      
      dojo.io.bind({
        url: "tr_query.cgi",
        method: "POST",
        content: { action: "get_categories", prod_id: prod_ids },
        load: function(type, data, evt){
          //env_categories.options.length = 0;
          
          if(data == ""){
            env_categories.add(new Option("[none available]", "none"), null);
          }
          else{
          var categories = data.split("|||");
          var category;
          var category_ids = new Array();
          
          if (categories.length == 0)
            {
              env_categories.add(new Option("[none avalible]", "none"), null);
            }
            else
            {
              for (i = 0; i < categories.length; i++)
              {
                category = categories[i].split("||");
                env_categories.add(new Option(category[1], category[0]),null);
                category_ids[i] = category[0];
              }
              getElements(category_ids);
            }
              }
              env_categories.disabled = false;
          },
        error: function(type, error){ alert("ERROR");},
          mimetype: "text/plain"
      })
    }
    
  function getElements(cat_ids){
    var env_elements = document.getElementById("env_elements");   
    env_elements.disabled = true;
    
    dojo.io.bind({
    url: "tr_query.cgi",
    method: "POST",
    content: {  action: "get_elements", cat_id: cat_ids  },
    load: function(type, data, evt){
      if(data == ""){
        env_elements.add(new Option("[none available]", "none"), null);
      }
      else{
        var elements = data.split("|||");
        var element;
        var element_ids = new Array();
        
        for (i = 0; i < elements.length; i++){
          element = elements[i].split("||");
            env_elements.add(new Option(element[1], element[0]), null);
            element_ids[i] = element[0];
              }
              getProperties(element_ids);
          }
      env_elements.disabled = false;
    },
        error:  function(type, error){ alert("ERROR");},
        mimetype: "text/plain"
      });
    }
    
    function getProperties(elmnt_ids){
      var env_properties = document.getElementById("env_properties");
      env_properties.disabled = true;
      
      dojo.io.bind({
        url: "tr_query.cgi",
        method: "POST",
        content: {  action: "get_properties", elmnt_id: elmnt_ids  },
        load: function(type, data, evt){
          if (data == ""){
            env_properties.options.add(new Option("[none avaliable]","none"), null);
          }
          else{
            var properties = data.split("|||");
            var property;
            for (i = 0; i < properties.length; i++){
              property = properties[i].split("||");
              env_properties.options.add(new Option(property[1], property[0]), null);
            }
          }
          env_properties.disabled = false;
            },
            error:  function(type, error){ alert("ERROR");},
            mimetype: "text/plain"
         });
    }
    
     function getExps(prop_ids){
      var env_expressions = document.getElementById("env_expressions");
      env_expressions.disabled = true;
      
    dojo.io.bind({
      url: "tr_query.cgi",
      method: "POST",
      content:{ action: "get_valid_exp", prop_id: prop_ids },
      load: function(type, data, evt){
            if (data == ""){
              env_expressions.add(new Option("[none available]", "none"), null);
            }
            else{
              var exps = data.split("|||");
              var exp;
              for (i = 0; i < exps.length; i++){
                exp = exps[i].split("||");
                env_expressions.add(new Option(exp[0], exp[0]), null);
              }
            }
        env_expressions.disabled = false;
            },
            error: function(type, error){ alert("ERROR");},
            mimetype: "text/plain"
        });
    }
    
    function env_class_change(class){
      class.disabled = true;
      
      document.getElementById("env_products").options.length = 0;
      document.getElementById("env_categories").options.length = 0;
      document.getElementById("env_elements").options.length = 0;
      document.getElementById("env_properties").options.length = 0;
      document.getElementById("env_expressions").options.length = 0;
      
      var class_ids = new Array();
      var count = 0;
      
      for(i = 0; i < class.length; i++){
        if (class[i].selected)
        {
          class_ids[count] = class[i].value;
          count++;
        }
      }
      
      getProducts(class_ids);
      
      class.disabled = false;
    }

    function env_products_change(prod){
      prod.disabled = true;
      
      document.getElementById("env_categories").options.length = 0;
      document.getElementById("env_elements").options.length = 0;
      document.getElementById("env_properties").options.length = 0;
      document.getElementById("env_expressions").options.length = 0;
      
      var prod_ids = new Array();
      var count = 0;
      
      for(i = 0; i < prod.length; i++){
        if (prod[i].selected)
        {
          prod_ids[count] = prod[i].value;
          count++;
        }
      }
      
      getCategories(prod_ids);
      
      prod.disabled = false;
    }
    
    function env_categories_change(cat){
      cat.disabled = true;
      
      document.getElementById("env_elements").options.length = 0;
      document.getElementById("env_properties").options.length = 0;
      document.getElementById("env_expressions").options.length = 0;
      
      var cat_ids = new Array();
      var count = 0;
      
      for(i = 0; i < cat.length; i++){
        if (cat[i].selected)
        {
          cat_ids[count] = cat[i].value;
          count++;
        }
      }
        
      getElements(cat_ids);
      
      cat.disabled = false;
    }
    
    function env_elements_change(elmnt){
      elmnt.disabled = true;
      
      document.getElementById("env_properties").options.length = 0;
      document.getElementById("env_expressions").options.length = 0;
      
      var elmnt_ids = new Array();
      var count = 0;
      
      for (i = 0; i < elmnt.length; i++){
        if (elmnt[i].selected)
        {
          elmnt_ids[count] = elmnt[i].value;
          count++;
        }
      }
      
        getProperties(elmnt_ids);
        
        elmnt.disabled = false;
    }
    
    function env_properties_change(prop){
      prop.disabled = true;
      
      document.getElementById("env_expressions").options.length = 0;
      
      var prop_ids = new Array();
      var count = 0;
      
      for (i = 0; i < prop.length; i++)
      {
        if (prop[i].selected)
        {
          prop_ids[count] = prop[i].value;
          count++;
        }
      }
      
      getExps(prop_ids);
      
      prop.disabled = false;
    }
</script> 

[%#######################################%]
[%##############  HTML  #################%]
[%#######################################%]
<h3>Search for Environments</h3>
<form action="tr_list_environments.cgi" method="GET">
<table>
  <tr>
    <td>
    [% IF Param('useclassification') %]
      <div class="bodyHead" style="padding:10px 0 5px 0;">Classification</div>
      [% PROCESS select sel = { 
                  name      => 'classification',
                  accesskey => 'c', 
                  list      => classifications,
                  elements  => 5,
                  mult      => 1,
                  events    => 'onchange="env_class_change(this);"' } %]
    [% END %]
    </td>
    <td style="width:25px;"></td>
[%###### Bugzilla Products #########%]
    <td>
      <div class="bodyHead" style="padding:10px 0 5px 0;"><u>P</u>roducts:</div>
      [% PROCESS select sel = { 
                  name      => 'env_products',
                  accesskey => 'c', 
                  list      => products,
                  elements  => 5,
                  mult      => 1,
                  events    => 'onchange="env_products_change(this);"' } %]
    </td>
    <td style="width:25px;"></td>
[%############ CATEGORY ############%]
    <td>
    <div class="bodyHead" style="padding:10px 0 5px 0;"><u>C</u>ategory:</div>
      [% PROCESS select sel = { 
                  name      => 'env_categories',
                  accesskey => 'c', 
                  list      => env.get_all_env_categories(1),
                  elements  => 5,
                  mult      => 1,
                  events    => 'onchange="env_categories_change(this);"' } %]
    </td>
    <td style="width:25px;"></td>
  </tr>
  <tr>
[%############ ELEMENTS ############%]
  <td>
    <div class="bodyHead" style="padding:10px 0 5px 0;"><u>E</u>lement:</div>
      [% PROCESS select sel = { 
                  name      => 'env_elements',
                  accesskey => 'c', 
                  list      => env.get_all_visible_elements(1),
                  elements  => 5,
                  mult      => 1,
                  events    => 'onchange="env_elements_change(this);"' } %]
    
  </td>  
  <td style="width:25px;"></td>
[%############ PROPERTIES ############%]
  <td>
      <div class="bodyHead" style="padding:10px 0 5px 0;"><u>P</u>roperties:</div>
      [% PROCESS select sel = { 
                  name      => 'env_properties',
                  accesskey => 'c', 
                  list      => env.get_all_element_properties(1),
                  elements  => 5,
                  mult      => 1,
                  events    => 'onchange="env_properties_change(this);"' } %]
    </td>
    <td style="width:25px;"></td>
    
[%######### VALID EXPRESSIONS ##########%]
    <td>
      <div class="bodyHead" style="padding:10px 0 5px 0;">Property <u>V</u>alue </div>
      [% PROCESS select sel = { 
                  name      => 'env_expressions',
                  accesskey => 'c', 
                  list      => env.get_distinct_property_values,
                  elements  => 5,
                  mult      => 1, } %]
    </td>

</tr>
</table>
<br>
<table>
  <tr>
    <th align="right">Environment Name</th>
    <td>
    [% PROCESS select sel = { name => 'name_type',
                              list => query_variants } %]</td>
    <td> <input name="name" /></td>
  </tr>
  <tr>
    <th align="right">Selected Property Value</th>
    <td>
    [% PROCESS select sel = { name => 'env_value_selected_type',
                              list => query_variants } %]</td>
    <td><input name="env_value_selected" /></td>
  </tr>
</table>

<input type="submit" value="Search" />
</form>