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

function remoteSelectUpdate(id_str,option_json)
{
  var x = document.getElementById(id_str)
  var new_opts_ary = JSON.parse(option_json)
  x.length = new_opts_ary.length
  var i
  for (i=0;i<new_opts_ary.length;i++)
  {
    x.options[i].value = new_opts_ary[i].value
    x.options[i].text = new_opts_ary[i].text
  }
  x.options[0].selected = true
  x.disabled=false
}

function normalizeSpan(span) {
  niceSpan = toDecimal(span);
  return ( niceSpan == 0 ? 1 : niceSpan );
}

function activateTab(objRef,activeTabClass,panelClass,panelID) {
  curTab = $(objRef);
  curPanel = $(panelID);
  if(curTab.hasClassName(activeTabClass)) { return false; }
  messages = $$('#flash_container .error-msg','#flash_container .info-msg','#flash_container .warning-msg');
  messages.each(function(node){ Element.remove(node);});
  allActiveTabs = document.getElementsByClassName(activeTabClass);
  allActiveTabs.each( function(tabObj) {
    tabObj.removeClassName(activeTabClass);
  });
  curTab.addClassName(activeTabClass)
  allActivePanels = document.getElementsByClassName(panelClass);
  allActivePanels.each( function(panelObj) {
    Element.hide(panelObj);
  });
  Element.show(curPanel);
}

function twoStageToggle(curObj) {
  return $(curObj).toggleClassName('depressed').hasClassName('depressed');
}

function showClass(className){
    $$("." + className).invoke('show');
}

function hideClass(className){
    $$("." + className).invoke('hide');
}

function toggleClass(className){
    $$("." + className).invoke('toggle');
}

function toggleButton(button,label1,label2) {
    buttonElement = $(button);
    if (buttonElement.value == label1) {
        buttonElement.value = label2;
        return true;
    } else {
        buttonElement.value = label1;
        return false;
    }
}

function toDecimal(orig) {
  switch (typeof(orig)) {
    case 'string': return parseInt(parseInt(orig).toFixed(0)); break;
    case 'number': return parseInt(orig.toFixed(0)); break;
    default: alert("Unknown type to convert to Decimal (" + typeof(orig) + ")"); break;
  }
}

function ie_restyle() {
  Try.these( function(){ return document.recalc();});
}

var HTMLRelatedSelectStruct = {
  select_children: undefined,
  select_parent: undefined,
  relation_hash: undefined,
  empty_prompt: undefined,
  disable_on_empty: false,
  dependent_controls: undefined,
  extended_html_select_object: true,
  forceselect: function(value) {
    for (i=0; i < this.options.length; i++) {
      if (this.options[i].value == value) {
        this.selectedIndex = i;
      }
    }
    this.refresh_children();
  },
  child_add: function(child) {
    if (this.select_children == undefined) this.select_children = [];
    this.select_children.push(child);
  },
  empty_prompt_set: function(prompt) {
    this.empty_prompt = new Option(prompt, '');
  },
  disable_on_empty_set: function(state) {
    this.disable_on_empty = state;
  },
  dependent_control_add: function(control_id) {
    if (this.dependent_controls == undefined) this.dependent_controls = [];
    this.dependent_controls.push(control_id);
  },
  refresh_controls: function() {
    if (this.dependent_controls != undefined) {
      var do_enable = this.selectedIndex > -1 && this.options[this.selectedIndex].value != '';
      this.dependent_controls.each( function(control_id) {
        var ctrl;
        if( (ctrl = $(control_id)) != null ) {
          if(do_enable) { ctrl.enable(); }
          else { ctrl.disable(); }
        }
      });
    }
  },
  refresh_children: function() {
    if (this.select_children != undefined) {
      this.select_children.each( function(child){ child.refresh(); } );
    }
  },
  refresh: function() {
    this.options.length = 0;
    if (this.select_parent != undefined && this.relation_hash != undefined && this.select_parent.selectedIndex > -1) {
      opts = this.relation_hash[this.select_parent.options[this.select_parent.selectedIndex].value];
      if (opts != undefined) {
        this.enable();
        for (i=0; i<opts.length; i++) this.options[i] = opts[i];
        if(this.selectedIndex == undefined) this.selectedIndex = 0;
        this.refresh_children();
      } else {
        if (this.empty_prompt != undefined) { this.options[0] = this.empty_prompt; }
        if (this.disable_on_empty) { this.disable(); }
        this.refresh_children();
      }
    } else {
      if (this.empty_prompt != undefined) { this.options[0] = this.empty_prompt(); }
      if (this.disable_on_empty) { this.disable; }
      this.refresh_children();
    }
    this.refresh_controls();
  },
  onchange: function() { 
    this.refresh_children();
    this.refresh_controls();
  }
};