function offer_form_init()
	{
	if($('formOfferBetween'))
		{
		multiSelect.init();
		}
  
  offerTypeSwitch();
  $('offer_type').observe('change',offerTypeSwitch);
  $('contract_type').observe('change',offerTypeSwitch);
	}

function offerTypeSwitch()
  {
  var type = $F('offer_type');
  var typeNode = $('offer_type').up('li');
  var contract = $F('contract_type');
  var contractNode = $('contract_type').up('li');
  var endNode = $('end_at_d').up('li');
  
  if(type == 'training_course')
    {
    contractNode.hide();
    endNode.show();
    $('contract_type').options.selectedIndex = 0;
    }
  else
    {
    contractNode.show();
    
    if(contract == 'cdi') endNode.hide();
    else endNode.show();
    }
  }

var multiSelect = {
	
	init:function()
		{
		$('formOffer').observe('submit',multiSelect.selectAllSelecteds);
		$('offerBtnRight').observe('click',multiSelect.addSelected);
		$('offerBtnRight2').observe('click',multiSelect.addAll);
		$('offerBtnLeft').observe('click',multiSelect.removeSelected);
		$('offerBtnLeft2').observe('click',multiSelect.removeAll);
		
		$$('#offerList option').each(function(option){
			option.observe('dblclick',multiSelect.invertByClick)
			});
		$$('#offerSelect option').each(function(option){
			option.observe('dblclick',multiSelect.invertByClick)
			});
		},
	
	addAll:function(e)
		{
		$$('#offerList option').each(function(option){
			$('offerSelect').insert({ bottom: option });
			});
		e.stop();
		},
	
	addSelected:function(e)
		{
		$$('#offerList option').each(function(option){
			if(option.selected==true) $('offerSelect').insert({ bottom: option });
			});
		e.stop();
		},
	
	invertByClick:function(e)
		{
		var dest = (e.target.up('select').readAttribute('id')=='offerList')? $('offerSelect') : $('offerList');
		
		dest.insert({ bottom: e.target });
		},
	
	removeAll:function(e)
		{
		$$('#offerSelect option').each(function(option){
			$('offerList').insert({ bottom: option });
			});
		e.stop();
		},
	
	removeSelected:function(e)
		{
		$$('#offerSelect option').each(function(option){
			if(option.selected==true) $('offerList').insert({ bottom: option });
			});
		e.stop();
		},
	
	selectAllSelecteds:function(e)
		{
		$$('#offerSelect option').each(function(option){
			option.selected = true;
			});
		}
	
	};

document.observe('dom:loaded',offer_form_init);
