/**
 * Set the form type
 * 
 * @param string name Name of the form-type
 * @return void
 */
function setForm(name) {
	$('input[name=formName]').val(name);
}

/**
 * Set payment types
 * 
 * @param string types
 * @return void
 */
function setPaymentTypes(types, defaultType) {
	$('input[name=allowedTypes]').val(types);
	$('input[name=subscriptionTypeId]').val(defaultType);
}

/**
 * Open a tab with a specific ID
 * 
 * @param int id
 * @return void
 */
function openTab(tab, id) {
	var tabGroup = $(tab).parent().parent();
	tabGroup.find('.tab').hide();
	tabGroup.find('.tab-box-menu .item').removeClass('active');
	$(tab).addClass('active');
	tabGroup.find('#' + id).show();
}

/**
 * Show pricing
 * 
 * @return void
 */
function showPricing(paymentOption) {
	// Insert options
	var priceList = prices[type][paymentOption].types;
	var html = '';
	var allowedTypes = '';
	var subscriptionTypeId;
	var c = 0;
	var aCurrency = currency;
	
	if(prices[type][paymentOption].currency) {
		aCurrency = prices[type][paymentOption].currency;
	}
	for(var i in priceList) {
		if(priceList[i].active) {
			if(c == 0) {
				subscriptionTypeId = i;
				html+= '<input type="radio" name="type" value="' + i + ':' + aCurrency + '" checked="checked" />' + priceList[i].description + '<br />';
			} else {
				html+= '<input type="radio" name="type" value="' + i + ':' + aCurrency + '" />' + priceList[i].description + '<br />';
			}
			allowedTypes+= c > 0 ? ',' + i + ':' + aCurrency : i + ':' + aCurrency;
			c++;
		}
	}
	
	// Render html
	$('#price-list').html(html);
	
	// Add listener to radio
	$('#ccbill-join-form input:radio[name=type]').click(function() {
		$('#subscriptionTypeId').val($(this).val());
	});
	
	// Change form type
	$('#formName').val(prices[type][paymentOption].form);
	
	// Change subscription type id to first entry of allowed types
	$('#subscriptionTypeId').val(subscriptionTypeId);
	
	// Change allowed types
	$('#allowedTypes').val(allowedTypes);
}

/**
 * Initialize
 */
$(function() {
	// Activate pricing selection
	$('#payment-options').change(function() {
		showPricing($(this).val());
	});	
	
	// Show prices for first entry
	showPricing(1);
	
	// Setup language change
	$('#select-language').change(function() {
		$('#language').val($(this).val());
	});
	
	// Setup tabs
	$('.tab').each(function() {
		if(!$(this).hasClass('active')) $(this).hide();
	});
});
