LicenseSelector = {
    items: [],
    current: null
};

LicenseSelector.init = function(){
    $J.$A(document.forms[0].elements).j14(function(el){
        if(el.name.match(/license|credits/))
            LicenseSelector.add(new License(el));
    })
}
LicenseSelector.add = function(item){
    if(item.isActive()){
        this.setActive(item);
    }

    this.items.push(item);
}
LicenseSelector.setActive = function(elem){
    if(this.current)
        this.current.makeInactive();
    this.current = elem;
    this.current.makeActive();
}

License = function(el){
    this.elem = $mjs(el);
    this.elem.self = this;
    this.init();
}
License.prototype.makeActive = function(){
//    console.log('Selecting');
    $mjs(this.elem.parentNode).j2('selected');
}

License.prototype.makeInactive = function(){
//    console.log('Deselecting');
    $mjs(this.elem.parentNode).j3('selected');
}

License.prototype.isActive = function(){
    return this.elem.checked;
}

License.prototype.init = function(){
    $mjs(this.elem).je1('click',function(){
        LicenseSelector.setActive(this.self);
        VAT.update();
        this.blur();
    })
}

VAT = {
    getWantService: function(){
        return this.install.checked;
    },
    getProductPrice: function(){
        return $prices[LicenseSelector.current.elem.value];
    },
    getServicePrice: function(){
        return this.getWantService()?$prices['install']:0;
    },
    getTotal: function(taxed){
        var total = this.getProductPrice() + this.getServicePrice() + this.getDiscount();

        return total >= 0 ? total : 0;
    },
    formatTotal: function(){
        return '£' + this.getTotal(true).toFixed(2);
    },
    writeTotal: function(){
        this.total.innerHTML = this.formatTotal();
    },
    getDiscount: function(){
        return $discount ? $discount : 0;
    }
};
VAT.init = function(){
    this.install         = $mjs('Checkout_installationService');
    this.total           = $mjs('total-amount');
    
    this.install.je1('click',this.update);

    this.initialized     = true;

    this.update();
}
VAT.update = function(){
    if(VAT.initialized)
        VAT.writeTotal();
}

$mjs(document).je1('domready',function(){
    LicenseSelector.init();
    if($service)
        VAT.init();

});



function showhide(id,evElm) {
    var elm = $mjs(id);

    var effect = elm.getAttribute('effect');

    evElm.blur();

    var resize = {};

    if (effect=='show') {
        resize.height 	= [
        $mjs(elm).j7().height,
        0
        ];
        elm.setAttribute('effect','hide');
    } else {
        resize.height 	= [
        $mjs(elm).j7().height,
        $mjs(elm.firstChild).j7().height
        ];
        elm.setAttribute('effect','show');
    }

    if (elm.fx) {
        elm.fx.stop();
    }
    elm.fx = new $J.FX(elm, {
        'duration': 500,
        'transition':$J.FX.Transition.quadOut
    });
    elm.fx.start(resize);

    return false;
}
