/*
 * FormUtil - This JS class provides tools for working with forms in Javascript.
 *
 * This source code is owned by Ephibian, Inc. Any use of this software requires
 * the explicit permission of Ephibian, Inc.
 * 
 * Copyright 1997-2006, Ephibian Inc. All Rights Reserved.
 */
 
// Empty constructor
function FormUtil(){}

/**
 * Selects specified option value in select box.
 * @param slct is the select box element you want to work with.
 * @param optionValue is the value of the option that should be selected.
 */
FormUtil.selectOption = function(slct, optionValue){
  for(var i=0; i<slct.options.length; ++i){
    if(slct.options[i].value == optionValue){
      slct.options[i].selected = true;
      break;
    }
  }
}

/**
 * Moves an option from one select box to another.
 * @param fromSelect is the select box element you want to move from
 * @param toSelect is the select box element you want to move to
 */
FormUtil.moveSelectedOption = function(fromSelect, toSelect){
  // get selected items
  var fromOpts = new Array(0);
  for(var i=0; i<fromSelect.options.length; ++i){
    var opt = fromSelect.options[i];
    if(opt.selected){
      var newOpt = new Option(opt.text, opt.value);
      newOpt.selected = true;
      // add to
      toSelect.options[toSelect.options.length] = newOpt;
    }else{
      fromOpts[fromOpts.length] = new Option(opt.text, opt.value);
    }
  }
  // rebuild from select box
  var startl = fromSelect.options.length;
  for(var i=startl; i>=0; --i){
    if(i < fromOpts.length){
      fromSelect.options[i] = fromOpts[i];
    }else{
      fromSelect.options[i] = null;
    }
  }
}

/**
 * Moves the first selected option down one.
 * @param slct is the select box you want to work with
 */
FormUtil.moveOptionDown = function(slct){
  FormUtil.orderSelectedOption(slct, "down");
}
/**
 * Moves the first selected option up one.
 * @param slct is the select box you want to work with
 */
FormUtil.moveOptionUp = function(slct){
  FormUtil.orderSelectedOption(slct, "up");
}

/**
 * Moves the first selected option up or down one.
 * @param slct is the select box you want to work with
 * @param direction can be either 'up' or 'down'
 */
FormUtil.orderSelectedOption = function(slct, direction){
  for(var i=0; i<slct.options.length; ++i){
    var opt = slct.options[i];
    if(opt.selected){
      if(direction == "up"){
        //check for top
        if(i - 1 < 0){
          // Don't do anything
          break;
        }else{
          // otherwise trade places with the item above
          var above = slct.options[i-1];
          var tmp = slct.options[i];
          slct.options[i] = new Option(above.text, above.value);
          slct.options[i-1] = new Option(tmp.text, tmp.value);
          slct.options[i-1].selected = true;
        }
      }else if(direction == "down"){
        //check for bottom
        if(i + 1 >= slct.options.length){
          // Don't do anything
          break;
        }else{
          // otherwise trade places with the item below
          var below = slct.options[i+1];
          var tmp = slct.options[i];
          slct.options[i] = new Option(below.text, below.value);
          slct.options[i+1] = new Option(tmp.text, tmp.value);
          slct.options[i+1].selected = true;
        }
      }
      // Only allow the first selected item to be moved
      break;
    }
  }
}

/**
 * Returns the index of the checked radio
 *
 */
FormUtil.getCheckedRadioIndex = function(elements) {
  for (i=0; i<elements.length; i++) {
    if (elements[i].checked) {
      return i;
    }
  }
  return -1;
} 

/**
 * Returns the value of the checked radio
 *
 */
FormUtil.getCheckedRadioValue = function(elements) {
  var n = FormUtil.getCheckedRadioIndex(elements);
  if(n >= 0){
    return elements[n].value;
  }else{
    return;
  }
}


