function setAsterisk() {
	if ( $('#delivery_billing_differ').prop('checked') == true) {
		$("input[id^='billing_']").each(function () {
			if ($(this).attr('id') != 'billing_company_code') {
				$(this).parent().prev('td').text('*');
			}
		});
		$('#delivery_email').parent().prev('td').text('');
		$('#delivery_phone').parent().prev('td').text('');
	} else {
		$('#delivery_email').parent().prev('td').text('*');
		$('#delivery_phone').parent().prev('td').text('*');
	}
}

$(document).ready(function() {
	setAsterisk();
	
	$('#delivery_billing_differ').click(function() {
		setAsterisk();
	});

	$('.forward.continue').click(function() {
		var missing = '';
		if ( $('#delivery_billing_differ').prop('checked') == true) {
			$("input[id^='billing_']").each(function () {
				if ($(this).attr('id') != 'billing_company_code') {
					if ($(this).val() == '') {
						missing += '- ' + $(this).parent().prev('td').prev('td').text() + '\n';
					}
				}
			});
			if (missing) {
				missing = "\nMaksutiedot-osiosta puuttuu kentät:\n" + missing;
			}
		} else { /* not checked: delivery_email and delivery_phone are mandatory */
			if ($('#delivery_email').val() == '') {
				missing += '- ' + $('#delivery_email').parent().prev('td').prev('td').text() +'\n';
			}
			if ($('#delivery_phone').val() == '') {
				missing += '- ' + $('#delivery_phone').parent().prev('td').prev('td').text() +'\n';
			}
			if (missing) {
				missing = "\nSaajatiedot-osiosta puuttuu kentät:\n" + missing;
			}
		}
		if (missing) {
			alert("Lomakkeen tietoja ei lähetetty.\n" + missing);
			return false;
		}
	});
})

