var checkBenefitSelected = Class.create({
	initialize: function (requiredElement, options){
		this.benefitSelects = requiredElement;
		this.options = Object.extend({
			// options go here
		}, options || {});			
		this.toggleSubmitButtons(); 
		this.attachToggleHandlerToSelects();
	},
	toggleSubmitButtons: function(){
		
		this.benefitSelects.each( function(sDropdown) { 
			
			 
			var sDropdown = $(sDropdown); 							   
			if (sDropdown.value == '') { 
				sDropdown.up('form').down('input[type=image]').hide();
				if (sDropdown.up('form').down('img[class=tempDisabled')) { 
					sDropdown.up('form').down('img[class=tempDisabled').show(); 
				} else { 
					var image = Element('img', { 'class':'tempDisabled', 'src':'/shop/library/images/general/btn_buy_now_off.gif'} );
					sDropdown.up('form').down('div.plan-actions').insert({ top:$(image) }); 		
				}
			}
			else {
				sDropdown.up('form').down('input[type=image]').show();
				if (sDropdown.up('form').down('img[class=tempDisabled')) { 
					sDropdown.up('form').down('img[class=tempDisabled').hide();
				}	
			}										  
		}); 
	}, 
	attachToggleHandlerToSelects: function() { 
		this.benefitSelects.each( function(sDropdown) { 
			var sDropdown = $(sDropdown);
			Element.observe(sDropdown, 'change', this.toggleSubmitButtons.bind(this)); 
		}.bind(this)); 
	}
});
				
var benefitAdviceToggler = Class.create({ 
	initialize: function(radioClass, benefitAdviceClass, miniBasket, confirmationUrlBenefit, confirmationUrlNoBenefit) { 
		this.radioClass = $$(radioClass); 
		this.benefitAdviceClass = benefitAdviceClass; 
		this.miniBasket = miniBasket; 
		
		this.confirmationUrlBenefit = confirmationUrlBenefit; 
		this.confirmationUrlNoBenefit = confirmationUrlNoBenefit; 
		
		this.addRadioClickHandlers(); 
		this.toggleBenefitAdvice(); 
	}, 
	
	toggleBenefitAdvice: function() { 
		this.radioClass.each( function(i) { 
			var advice = i.up('.plan').down(this.benefitAdviceClass); 
			if (i.checked && advice) { 
				var toHighlight = (advice.style.display == 'none') ? true : false;
				advice.show(); 
				this.miniBasket.options.confirmationUrl = this.confirmationUrlBenefit; 
				if (toHighlight) new Effect.Highlight(advice, {startcolor: '#ff5500'}); 
			} else if ((!i.checked) && advice) { 
				advice.hide(); 
				this.miniBasket.options.confirmationUrl = this.confirmationUrlNoBenefit;
			}
		}.bind(this)); 
	},
	
	addRadioClickHandlers: function() { 
		this.radioClass.each( function(i) { 
			i.observe('click', this.toggleBenefitAdvice.bind(this)); 
		}.bind(this)); 									  
	}
	
}); 

var packagePriceUpdater = Class.create({
	initialize: function(observeRadioClass, observeRadioName, url, updatingDiv, varsToSend, options) { 
		this.radioClass = observeRadioClass;
		this.observeRadioName = observeRadioName; 
		this.varsToSend = varsToSend.split(',');
		this.url = url;
		this.updatingDiv = updatingDiv;
		this.options = Object.extend({
			// options go here
		}, options || {});		
				
		this.addRadioClickHandlers();
		this.updatePhonePrices(); 
	},
	updatePhonePrices: function() { 
		if (getRadioValue('flexible-package-select-form', this.observeRadioName) != '') { 		
			url = this.url; 
			this.varsToSend.each(function(n) { url = url + "&" + n + "=" + getRadioValue('flexible-package-select-form', n)});
			$$('.additionalPartForm').each(function(n) { url = url + "&" + n.name + "=" + $F(n); });
			
			new Ajax.Updater(this.updatingDiv, url, { 
				method:'get', 
				onComplete: function(response) { 		
					this.addRadioClickHandlers(); 
					if (this.options.onUpdatePackage) this.notify('onUpdatePackage');
				}.bind(this)
			}); 
		}
	},
	addRadioClickHandlers: function() { 
		$$(this.radioClass).each( function(i) { 
			i.observe('click', this.updatePhonePrices.bind(this)); 
		}.bind(this));
	}, 
	notify: function(event_name){
		try{
			if(this.options[event_name])
				return [this.options[event_name].bindAsEventListener(this).apply(this.options[event_name],$A(arguments).slice(1))];
		}catch(e){
			if(e != $break)
				throw e;
			else
				return false;
		}
	}
}); 