var orig_price;
var current_price;
var option_discount;

function check_radio_form() { 
    if ($(':radio.option_control').length==0) {
        $('form#free').submit();
    } else {
        var choice_made=false;
        $('.option_control').each(function() {if (this.checked) choice_made=true;});
        if (choice_made) {
            $('form#free').submit();
        } else {
            alert("Please make a selection before proceeding.");
        }
    }
}

function select_option() {
    var option_price=parseFloat($(this).val().split('==')[1]);
    var option_frobbled_price=option_price*option_discount*globalvat;

     if (this.checked) {
        if ($(this).attr('type')==='checkbox') {
    	    current_price += option_frobbled_price;
        } else {
            // radio button
            current_price = orig_price + option_frobbled_price;
        }
    } else {
        if ($(this).attr('type')==='checkbox') {
            current_price -= option_frobbled_price;
        } else {
            // shouldn't ever happen with radio button
        }
    }
    // sketchy currency formatting:
    var x = new String(current_price.toFixed(2)).replace(/^(\d*)(\d\d\d)(\.*\d*)$/, '£$1,$2$3');

    $('#Final_Price').val(x);
    $('#V2').val(x);
}

$(document).ready(function() {
    $('.option_control').click(select_option);
    $('.continue').click(check_radio_form);
    option_discount=parseFloat($('#option_discount').val());
    orig_price = parseFloat($('#Final_Price').val().replace(/[,£]/g, ''));
    current_price=orig_price;
});

