/*
 * query.js
 *
 * Provides common javascript functions to all CBC Historical Results
 * query pages, including setting query form options, and saving
 * and restoring query state.
 *
 * Authors: 	Gary Helmling
 * Modified: 	12/13/2001
 *
 */

// identifies last selected region/country/circle field
lastAreaField='';

// convenience object to use in setting empty menus
var emptyOption = new Option( "                      ", 
                              "", 
                              false, false );

/*
 * Updates the display and hidden inputs with the value and text of
 * the select element's selected option.  If no selection has been 
 * made, i.e., index = 0, then "" is used as the value; 
 */
function updateDateChoice(element, hiddenTarget, displayTarget) {
    var text;
    var value;
    if (element.selectedIndex === 0) {
        value = "";
        text = "";
    }
    else { 
        value = element.options[element.selectedIndex].value;
        text = element.options[element.selectedIndex].text;
    }
    hiddenTarget.value = value;
    displayTarget.value = text;
    
    return false;
}

function setLastField(last)
{
   lastAreaField = last;

   return true;
}

function saveQuery()
{
//   top.query = new Array();
   top.query_species = new Array();
   for (var i=0; i<document.data_query.species.options.length; i++)
   {
      if (document.data_query.species.options[i].value != "")
      {
         top.query_species[i] = copyOption(document.data_query.species.options[i]);
      }
   }
   
   var startYearValue = document.data_query.startYear.value;
   var startYearDisplay = document.data_query.startYearDisplay.value;
   
   top.queryStartYear = (startYearValue === "" ) ? "" : startYearValue;
   top.queryStartYearDisplay = (startYearDisplay === "" ) ? "" : startYearDisplay;
   
//   if (document.data_query.startYear.options[0] && document.data_query.startYear.options[0].value != "") {
//      top.query_startYear[0] = copyOption(document.data_query.startYear.options[0]);
//   }

    var endYearValue = document.data_query.endYear.value;
    var endYearDisplay = document.data_query.endYearDisplay.value;
    
    top.queryEndYear = (endYearValue === "" ) ? "" : endYearValue;
    top.queryEndYearDisplay = (endYearDisplay === "" ) ? "" : endYearDisplay;

//   if (document.data_query.endYear.options[0] && document.data_query.endYear.options[0].value != "") {
//      top.query_endYear[0] = copyOption(document.data_query.endYear.options[0]);
//   }


   if (document.data_query.region)
   {
      top.query_region = new Array();
      for (var i=0; i<document.data_query.region.options.length; i++)
      {
         if (document.data_query.region.options[i].value != "")
         {
            top.query_region[i] = copyOption(document.data_query.region.options[i]);
         }
      }
   }

   if (document.data_query.country)
   {
      top.query_country = new Array();
      for (var i=0; i<document.data_query.country.options.length; i++)
      {
         if (document.data_query.country.options[i].value != "")
         {
            top.query_country[i] = copyOption(document.data_query.country.options[i]);
         }
      }
   }

   if (document.data_query.circle_id)
   {
      top.query_circle_id = new Array();
      for (var i=0; i<document.data_query.circle_id.options.length; i++)
      {
         if (document.data_query.circle_id.options[i].value != "")
         {
            top.query_circle_id[i] = copyOption(document.data_query.circle_id.options[i]);
         }
      }
   }
}

function copyOption(opt)
{
   var newOpt = new Option( opt.text,
                            opt.value,
                            opt.defaultSelected,
                            opt.selected );
   newOpt.title = opt.title;
   return newOpt;
}

function createOption()
{
   var opt = new Option("", "", false, false);
   return opt;
}

function loadQuery()
{
      if (top.query_species)
      {
         for (var i=0; i<top.query_species.length; i++)
         {
            document.data_query.species.options[i] = copyOption( top.query_species[i] );
         }
      }

     document.data_query.startYear.value = 
         (typeof(top.queryStartYear) == "undefined") ? "" : top.queryStartYear;
     document.data_query.startYearDisplay.value = 
         (typeof(top.queryStartYearDisplay) == "undefined") ? "" : top.queryStartYearDisplay;;

     document.data_query.endYear.value = 
         (typeof(top.queryEndYear) == "undefined") ? "" : top.queryEndYear;
     document.data_query.endYearDisplay.value = 
         (typeof(top.queryEndYearDisplay) == "undefined") ? "" : top.queryEndYearDisplay;

      if (document.data_query.region)
      {
         if (top.query_region)
         {
            for (var i=0; i<top.query_region.length; i++)
            {
               document.data_query.region.options[i] = copyOption(top.query_region[i]);
            }
         }
         else
         {
            document.data_query.region.options[0] = copyOption( emptyOption );
         }
      }

      if (document.data_query.country)
      {
         if (top.query_country && top.query_country.length > 0)
         {
            for (var i=0; i<top.query_country.length; i++)
            {
               document.data_query.country.options[i] = copyOption(top.query_country[i]);
            }
         }
         else
         {
            document.data_query.country.options[0] = copyOption( emptyOption );
         }
      }

      if (document.data_query.circle_id)
      {
         if (top.query_circle_id && top.query_circle_id.length > 0)
         {
            for (var i=0; i<top.query_circle_id.length; i++)
            {
               document.data_query.circle_id.options[i] = copyOption(top.query_circle_id[i]);
            }
         }
         else
         {
            document.data_query.circle_id.options[0] = copyOption( emptyOption );
         }
      }

}


function addSelections(items, menu)
{
   var currentItems = 0;
   if (menu.options[0] && menu.options[0].value)
   {
      currentItems = menu.options.length;
   }

   var length = items.length;
   for(i=0; i<length; i++)
   {
      if (items[i].selected && items[i].value)
      {
         menu.options[ currentItems ] = copyOption(items[i]);

         // make new option selected
         menu.options[ currentItems ].defaultSelected = true;
         menu.options[ currentItems ].selected = true;

         currentItems++;
         // reset selected flag
         items[i].selected = false;
      }
   }

}

function setSelections(items, menu)
{
   var currentItems = 0;

   var length = items.length;
   for(i=0; i<length; i++)
   {
      if (items[i].selected && items[i].value)
      {
         menu.options[ currentItems ] = copyOption(items[i]);

         // make new option selected
         menu.options[ currentItems ].defaultSelected = true;
         menu.options[ currentItems ].selected = true;

         currentItems++;
         // reset selected flag
         items[i].selected = false;
      }
   }

}


/*
 *  Removes all selected entries from a given menu.
 */

function removeSelections(menu)
{
   if (menu)
   {
      var length = menu.options.length;
      for(i=0; i<length; i++)
      {
         if (menu.options[i] && menu.options[i].selected)
         {
            menu.options[i] = null;
         }
      }
   }
}


function clearSelections(menu)
{
   var cnt = 0;
   
   if (menu)
   {
      var length = menu.options.length;
      for(i=0; i<length; i++)
      {
//         if (menu.options[i])
//         {
            menu.remove(0);
//            cnt++;
//         }
      }
   }
   
//   alert("Cleared "+cnt+" menu items");
}


function subData()
{
   var retValue = false;
   var length = document.data_query.species.length;
   var selectedCount = 0;
   for (i=0; i<length; i++)
   {
      document.data_query.species.options[i].selected = true;
      selectedCount++;
   }

   if (document.data_query.circle_id.selectedIndex >= 0)
   {
      selectedCount++;
   }

   if (selectedCount > 0)
   {
      retValue = true;
   }

   return retValue;
}


function setSpecies(items)
{
   clearSelections(document.species_query.species);

   for (i=0; i<items.length; i++)
   {
         document.species_query.species.options[i] = items[i];
         document.species_query.species.options[i].defaultSelected = false;
         document.species_query.species.options[i].selected = false;
   }


//   history.go(0);
}


function setCircles(items)
{
   // remove the existing items
   clearSelections(window.parent.content.document.circle_query.circle_id);

   for (i=0; i<items.length; i++)
   {
         document.circle_query.circle_id.options[i] = items[i];
         document.circle_query.circle_id.options[i].defaultSelected = false;
         document.circle_query.circle_id.options[i].selected = false;
   }


//   history.go(0);
}


function setCurrentCircles(items)
{
   // remove the existing items
   clearSelections(document.data_query.circle_id);

   for (i=0; i<items.length; i++)
   {
         document.data_query.circle_id.options[i] = items[i];
         document.data_query.circle_id.options[i].defaultSelected = false;
         document.data_query.circle_id.options[i].selected = false;
   }


//   history.go(0);
}

function setArea(field)
{
   if (field == null || field == '')
   {
      field = lastAreaField;
   }

   if (field == 'region')
   {
      addSelections(document.locationForm.region.options, document.data_query.region);
      // clear others
      removeSelections(document.data_query.country);
      removeSelections(document.data_query.circle_id);
   }
   else if (field == 'country')
   {
      addSelections(document.locationForm.country.options, document.data_query.country);
      // clear others
      removeSelections(document.data_query.region);
      removeSelections(document.data_query.circle_id);
   }
   else if (field == 'circle_id')
   {
      addSelections(document.circle_query.circle_id.options, document.data_query.circle_id);
      // clear others
      removeSelections(document.data_query.region);
      removeSelections(document.data_query.country);
   }
}

function query_submit()
{
   var formValid = verify();
   if (formValid)
   {
      document.data_query.submit();
   }
   // otherwise do nothing
}


/*
 *  Returns a boolean value indicating whether or not a menu 
 *  has any selected values.
 *
 */
  
function isSelected(elem)
{
    if (typeof(elem) === 'undefined') {
        return false;
    }
   var sel = true;
   if (elem.type.indexOf('select') > -1)
   {
      sel = false;
      for (i=0; i<elem.options.length; i++)
      {
         if (elem.options[i].selected && elem.options[i].value)
         {
            sel = true;
            break;
         }
      }
   }
   else {
       if (elem.type == "text" || elem.type == 'hidden') { 
           sel = (elem.value().length() > 0);
       }
   }

   return sel;
}


/*
 *  Clears any input from the given form element.  This includes
 *  clearing any selections from select menus, clearing the values
 *  of text boxes and textarea fields.
 */
function clearField(elt)
{
   // alert("in clear...");
   if (elt)
   {
      // alert("type is "+elt.type);
      if (elt.type == "select-multiple" || elt.type == "select-one")
      {
         // alert(" select menu: "+elt.name);
         for (i=0; i<elt.options.length; i++)
         {
            elt.options[i].selected = false;
         }
      
         elt.selectedIndex = 0;
      }
      else if (elt.type == "text")
      {
         // text input
         elt.value = "";
      }
      else if (elt.type == "textarea")
      {
         // textarea field
         elt.value = "";
      }
      else if (elt.type == "checkbox")
      {
         // checkbox
         elt.checked = false;
      }
      else if (elt.type == "radio")
      {
         // radio button
         elt.checked = false;
      }
   }

   return true;
}

