/* Greather than or equal to search
 *
 * Step 1: Add this code to an external js file called in the head of your
 * document.
 *
 * Step 2: Build out your form as if it were using the second smc
 * form option - either or logic. Add the field number to the end
 * of the fieldoptionid in the select name: fieldoptionid_103[]. Don't
 * set your select fields to multiple, this defeats the purpose. (If you
 * want to perform an inclusive search using <select multiple> fields, you
 * don't need this js file.) For more information see smc docs:
 * http://help.intrcomm.net/sitepages/pid112.php
 *
 * Step 3: Add a call to your expandSearchOnId to your search form, passing
 * in an array of fields you want to search on:
 * onsubmit="expandSearch([39,91,103])"
 *
 * Additional notes: this script does nothing to sort - you have to do that
 * in your smc preview or search results template. Any manual ordering will
 * break during pagination. This code should not require any customization.
 */

function expandSearch(ids) {
 // Clear out hidden fields to prevent previous searches from interfering.
 clearDynamicForm();

 // Perform search expansion on each id in the array passed to this function. 
 for (var a = 0; a < ids.length; a++) {
  expandSearchOnId(ids[a]);
 }
}

function expandSearchOnId(id) {
 // Look for the form and select box targeted by the id that has been passed in. 
 // We'll perform search expansion based on those options.
 var name = 'fieldoptionid_' + id + '[]';
 
 var forms = document.forms;
 for(var a = 0; a < forms.length; a++) {
  var form = forms[a];
  var elements = form.elements;
  
  for (var b = 0; b < elements.length; b++) {
   var select = elements[b];
   if (select.name ==  name) {
    expandSearchOnSelect(select, form);
   }
  }

  // This forces IE to reprocess form.
  if (form.normalize) {
   form.normalize();
  }   
 }
}


function expandSearchOnSelect(select, form) {
 var selected = false;
 var options = select.options;
 if (options) {
 for (var c = 0; c < options.length; c++) {
  var option = options[c];
  if (option.selected && option.value != "") {
   // If an option is selected, set selected flag so that all options
   // that follow will be added as hidden fields.
   selected = true;
  } else if (selected) {
   // If a selected option was encountered, create a hidden field containing expanded
   // search parameters.
   var input = document.createElement("input");
   input.type = "hidden";
   input.name = select.name;
   input.value = option.value;

   // Set className so that next time this form is submitted we can make sure
   // the last round of hidden fields are removed.
   input.className = "dynamicInput";

   // Add the new hidden field to the form.
   form.appendChild(input);
  }
 }
 }
}

function clearDynamicForm() {
 // Clear the form by searching for input fields with a class name of 
 // dynamicInput. This clears previous search parameters from the form.
 var forms = document.forms;
 for (var a = 0; a < forms.length; a++) {
  var form = forms[a];
  var inputs = form.getElementsByTagName('input');
  for (var b = 0; b < inputs.length; b++) {  
   if (inputs[b].className == 'dynamicInput') {
    alert('Removing: ' + inputs[b].name + ":" + inputs[b].value);
    inputs[b].name = "";
    inputs[b].value = "";
    inputs[b].className = "";
   }
  }
 }
}