
/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }

    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}

var RainforestGift = Class.create();

RainforestGift.prototype = {
	initialize: function(checkbox) {
		this.checkbox = checkbox;
		this.findWrapper();
		this.addEvents();
		this.update();
	},
	
	update: function() {
		if(this.checkbox.checked) {
			this.wrapper.setStyle({'display': ''});
		} else {
			this.wrapper.setStyle({'display': 'none'});
		}
	},
	
	addEvents: function() {
		this.checkbox.observe('click', this.update.bind(this));
	},
	
	findWrapper: function() {
		var wrapperId = this.checkbox.id.replace(/is_gift$/, 'wrapper');
		this.wrapper = $(wrapperId);
	}
	
}


var CertificateAddress = Class.create();

CertificateAddress.prototype = {
	initialize: function(select) {
		this.select = $(select);
		this.findWrapper();
		this.addEvents();
		this.update();
	},
	
	findWrapper: function() {
		var wrapperId = this.select.id.replace(/address_id$/, 'new_address_wrapper');
		this.wrapper = $(wrapperId);
	},
	
	update: function() {
		if(this.select.getValue() == '') {
			this.wrapper.setStyle({'display': ''});
		} else {
			this.wrapper.setStyle({'display': 'none'});
		}
	},
	
	addEvents: function() {
		this.select.observe('change', this.update.bind(this));
	}
};

var CertificatePost = Class.create();

CertificatePost.prototype = {
	initialize: function(emailRadio, postRadio) {
		this.emailRadio = emailRadio;
		this.postRadio = postRadio;
		this.findWrapper();
		this.addEvents();
		this.update();
	},
	
	findWrapper: function() {
		var wrapperId = this.emailRadio.id.replace(/method-email$/, 'address_wrapper');
		this.wrapper = $(wrapperId);
		var recipientEmailId = this.emailRadio.id.replace(/method-email$/, 'recipient_email');
		this.recipientEmail = $(recipientEmailId);
		this.recipientEmailLabel = this.recipientEmail.previous();
		this.recipientEmailLabelText = this.recipientEmailLabel.innerHTML;
	},
	
	update: function() {
		if($RF(this.postRadio) == 'post') {
			this.wrapper.setStyle({'display': ''});
			this.recipientEmail.removeClassName('required-entry');
			this.recipientEmailLabel.innerHTML = this.recipientEmailLabelText;
			var message = this.recipientEmail.next(0);
			if(message) {
				message.remove();
			}
		} else {
			this.wrapper.setStyle({'display': 'none'});
			this.recipientEmail.addClassName('required-entry');
			this.recipientEmailLabel.insert(' <span class="required">*</span>');
		}
	},
	
	addEvents: function() {
		this.postRadio.observe('click', this.update.bind(this));
		this.emailRadio.observe('click', this.update.bind(this));
	}
	
};

document.observe('dom:loaded', function() {
	$$('input.gift-toggle').each(function(checkbox){
		new RainforestGift(checkbox);
	});
	
	$$('select.certificate-address-select').each(function(select){
		new CertificateAddress(select);
	});
	
	$$('div.certificate-select').each(function(div){
		new CertificatePost(
			div.select('input.certificate-email')[0], 
			div.select('input.certificate-post')[0]
		);
	});
});
