// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
function $RF(el, radioGroup)
{
  if($(el).type && $(el).type.toLowerCase() == 'radio')
  {
    var radioGroup = $(el).name;
    var el = $(el).form;
  }
  else if ($(el).tagName.toLowerCase() != 'form')
  {
    return false;
  }

  var checked = $(el).getInputs('radio', radioGroup).find(
    function(re) {return re.checked;}
  );
  return (checked) ? $F(checked) : null;
}

function getSelectedText(elementId)
{
  var elt = document.getElementById(elementId);
  if (elt.selectedIndex == -1)
    return null;
  return elt.options[elt.selectedIndex].text;
}

function toggleBasedOnOptionText(testId, testValue, targetId)
{
  if (getSelectedText(testId) == testValue)
  {
    Element.show(targetId);    
    HighlightEffect(targetId);
  }
  else
  {
    Element.hide(targetId);
  }
}

function selectedOptionText(el)
{
  var sel = document.getElementById(el);
  return sel.options[sel.selectedIndex].text;
}

function displayIfClassMatch(selectedText, matchClass, targetId)
{
  var action = 'show';
  var humanizedClass = matchClass.replace('_', ' ');
  if (humanizedClass != selectedText)
  {
    action = 'hide'; // Selected option text doesn't match target class
  }
  if (action == 'show')
  {
    Element.show(targetId); 
    HighlightEffect(targetId);
  }
  else
  {
    Element.hide(targetId);
  }
}

function isArray(obj)
{
  if (obj.constructor.toString().indexOf("Array") == -1)
  {
    return false;
  }
  else
  {
    return true;
  }
}

function displayIfMatch(valueWhenTrue, testValues, targetId)
{
  if ( ! isArray(testValues))
  {
    testValues = [testValues];
  }
  for (i=0; i<testValues.length; i++)
  {
    if (valueWhenTrue == testValues[i])
    {
      Element.show(targetId); 
      HighlightEffect(targetId);
      return true;
    }
  }
  Element.hide(targetId);
  return false;
}

function optionValueFromText(array2d, mixed)
{
  var testArray = [];
  var x = 0;
  var returnArray = [];
  if (isArray(mixed))
  {
    testArray = mixed
  }
  else
  {
    testArray = [mixed]
  }
  for (i=0; i<array2d.length; i++)
  {
    for (j=0; j<testArray.length; j++)
    {
      if (array2d[i])
      {
        if (array2d[i][1] == testArray[j])
        {
          returnArray[x] = array2d[i][0];
          x++;
        }
      }
    }
  }
  return returnArray;
}

function displayIfValueSelected(selectedValue, matchMixed, targetId)
{
  var matchArray = new Array;
  if (isArray(matchMixed))
  {
    matchArray = matchMixed;
  }
  else
  {
    matchArray[0] = matchMixed;
  }
  // Show target element if selected value found in array
  var action = 'hide';
  for (var i=0; i<matchArray.length; i++)
  {
    if (selectedValue == matchArray[i])
    {
      action = 'show'; // Selected option text doesn't match target class
    }
  }
  if (action == 'show')
  {
    Element.show(targetId); 
    HighlightEffect(targetId);
  }
  else
  {
    Element.hide(targetId);
  }
}

function HighlightEffect(element)
{
  new Effect.Highlight(element, 
  {
    //startcolor: "#ff0000",
    //endcolor: "#0000ff",
    //restorecolor: "#00ff00",
    //duration: 1
  })
}

function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
