Behaviors = {
  Form: {
    AccessibleSelectFactory:  {
      'create' : function() {
        if (Prototype.Browser.IE) {
          return Behaviors.Form.IEAccessibleSelect;
        }
        return Behaviors.Form.AccessibleSelect;
      }
    },

    AccessibleSelect:  Behavior.create({
      onchange: function() {
        this.element.fire('select:changed');
      }
    }),

    IEAccessibleSelect: Behavior.create({
      initialize: function(changeEventName){
        this.changeEventName = changeEventName || 'select:changed';
        this.changed = false;
      },
      onchange: function(){
        if(!this.changed){
          return false;
        } else {
          this.changed = false;
          $(this.element).fire(this.changeEventName);
        }
        return true;
      },
      onfocus: function(){
        this.initValue = this.element.value;
        return true;
      },
      onkeydown: function(e){
        var theEvent;
        var keyCodeTab = "9";
        var keyCodeEnter = "13";
        var keyCodeEsc = "27";

        if (e)
        {
          theEvent = e;
        } else {
          theEvent = event;
        }
        if ((theEvent.keyCode == keyCodeEnter || theEvent.keyCode == keyCodeTab) && this.value != this.initValue)
        {
          this.changed = true;
          this.onchange();
        }
        else if (theEvent.keyCode == keyCodeEsc)
        {
          this.value = this.initValue;
        }
        else
        {
          this.changed = false;
        }
        return true;
      },
      onclick: function(){
        this.changed = true;
      }

    }), 
	Blocks: {
  	  closeSection: function(source, selector, alternate) {
        var sectionToHide = source.element.up('dd').next(selector);
        sectionToHide.hide();
        var toClose = sectionToHide.select(selector);
        toClose = toClose.concat(sectionToHide.select(alternate));
        toClose.unshift(sectionToHide);
        toClose = toClose.flatten();
                
        toClose.each(function(e) {        
          $A(['radio','checkbox','text']).each(function (type){
            e.select('input[type='+type+']').each(function (inp){
              var label = inp.up('label');
              if(label){
                label.removeClassName('on');
              }
              if(type == 'text'){
                inp.value = '';
              } else{
                inp.checked = false;
              }
            });
          });
          e.select('select').each(function (sel){
            sel.selectedIndex = 0;
          });
          e.hide();
          e.select('.feedback').invoke('hide');
        });
      }
    }, 
    ConditionalBlocks: Behavior.create({
      initialize: function(){
        if(this.element.getValue()){
          this.toggle();
        }
      },
      onclick: function(){
        this.toggle();
      },
      toggle: function(){        
        if(this.element.hasClassName('turn-on')){        
          this.element.up('dd').next('dd.show-if').blindDown();
        }else if(this.element.hasClassName('turn-off')){        
          Behaviors.Form.Blocks.closeSection(this, 'dd.show-if');
        }
      }
    }),
    YesNoBlocks: Behavior.create({
      initialize: function() {
        if (this.element.getValue()) {
          this.toggle();
        }
      },
      onclick: function() {
        this.toggle();
      },
      toggle: function() {
        if (this.isYes()) {
          if (this.noSection())
            Behaviors.Form.Blocks.closeSection(this, 'dd.show-if-no', 'dd.show-if-yes');
          this._showSection(this.yesSection());
        }
        else if (this.isNo()) {
          if (this.yesSection())
            Behaviors.Form.Blocks.closeSection(this, 'dd.show-if-yes', 'dd.show-if-no');
          this._showSection(this.noSection());
        }
      }, 
      isYes: function() {
        return this.element.hasClassName('yes');
      }, 
      isNo: function() {
        return this.element.hasClassName('no');
      }, 
      yesSection: function() {
      	return this._section('yes');
      }, 
      noSection: function() {
        return this._section('no');
      }, 
      _section: function(yesOrNo) {
      	return this.element.up('dd').next('dd.show-if-' + yesOrNo);
      },
      _showSection: function(section) {
        if (section && !section.visible())
          section.blindDown({ duration: 0.2 });
      }
    }), 
    NoEnterKey: Behavior.create({
      onkeypress: function(e){
        return (e.keyCode != 13);
      }
    }),
    HintedTextBox: Behavior.create({
      initialize: function(){
        this.hintText = $$("label.acc-label[for='"+this.element.id+"']").first().innerHTML.strip();
        this.onblur();
      },
      onblur: function() {        
        if('' == this.element.getValue()){
          this.element.value = this.hintText;
          this.element.addClassName('hinted');
        }
      },
      onfocus: function() {
        if(this.hintText == this.element.getValue()){
          this.element.value = '';
        }
        this.element.removeClassName('hinted');
      }
    })      
  },
  UI: {
    ShowAll: Behavior.create({
      initialize: function(toShow){
        this.toShow = toShow;
        if(toShow){
          $$(toShow).invoke('hide');
        }
      },
      onclick: function(event){      
        Event.stop(event);
        var cont = event.element().up('.show-toggle');        
        if(event.element().up('.toggle').hasClassName('show')){
          cont.down('.hide').show();
          cont.down('.show').hide();
          $$(this.toShow).invoke('show');
        }
        else{
          cont.down('.hide').hide();
          cont.down('.show').show();
          $$(this.toShow).invoke('hide');
        }
      }
    }),
    NoDoubleClick: Behavior.create({
      initialize: function(){
        this.clicked = false;
      },
      onclick: function(event){
        if(this.clicked){
          Event.stop(event);
        } else {
          this.clicked = true;
        }
      }
    })
  }
}
