/*
 * Copyright 2007 The Australian National University
 * 
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software distributed under 
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
 * KIND, either express or implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

var varNodeId;
var searchName;
function getVariableFilter(search) {
    searchName = search.name;
         
    var datasets = search.datasets;
    if (datasets == undefined) {
        return;
    }
        
    var html = "<div class='hd'>Filter by Dataset</div>";
    html += "<div class='bd'>";
    html += "<form method='POST'>"
    html += "<p id='filterPanelText'>To filter the variables in your search results so that only variables in a particular dataset are displayed, select a dataset below.</p>";
    html += "<select name='dataset' id='filterPanelList' width=10>";
    
    for (var i = 0; i < datasets.length; i++) {
        var dataset = datasets[i];
        html += "<option value=\"" + dataset.id + "\">" + dataset.name + "</option>";
    }
    
    html += "</select>";
    html += "</form>";
    html += "</div>";
        
    var fdDiv;
    if (document.all) {
        fdDiv = document.all("filterDialog");
    } else if (document.getElementById) {
        fdDiv = document.getElementById("filterDialog");
    }
    
    fdDiv.innerHTML = html;

    var filterDialog = new YAHOO.widget.Dialog("filterDialog",  
                        { width : "400px", 
                        x : 100,
                        y : 80, 
                        visible : false,
                        postmethod:"none",
                        buttons : [ { text:"Select", handler:handleSelect, isDefault:true }, 
                            { text:"Cancel", handler:handleCancel } ] 
                        }
                    ); 

    filterDialog.render();
    filterDialog.show();
}

var handleCancel = function() { 
    this.cancel(); 
}

var handleSelect = function() {
    var datasetID = this.getData().dataset[0];
    this.cancel();
    
    parent.frames.tree.DDIINDEX.user.filterVariables(searchName, "ddiiDatasetID", datasetID);
}