function twoDecimals(price) {
    var decimals    = '00';
    price = price.toString();

    if(price.indexOf('.') != -1)
    {
        var priceParts  = price.split('.');

        if(priceParts[1].length == 1)
        {
            decimals = priceParts[1]+'0';
        }
        if(priceParts[1].length == 2)
        {
            decimals = priceParts[1];
        }
        price = priceParts[0];
    }
    return price+'.'+decimals;
}

function bindRemoveAjax(){
    $('.remove').each(function(){
        $(this).click(function(){
            $.ajax({
                url:$(this).attr('href'),
                data:({id : $(this).parent().parent().attr('id')}),
                type:'get',
                cache:false
            });
            var priceToSubtract = $(this).parent().parent().find('.col3').text().replace(/^\s+|\s+$/g,"").split(' ')[1];
            var oldPrice        = $('.overallPrice').text().replace(/^\s+|\s+$/g,"").split(' ');
            
            $('.overallPrice').text(oldPrice[0]+' '+twoDecimals(oldPrice[1]-priceToSubtract));

            $(this).parent().parent().fadeOut('slow', function(){$(this).remove();});
            return false;
        });
    });
}

function mandatoryCheckbox(targetUrl){
    if($('input[name=IagreeWithTaC]:checked').length>0)
    {
        var products = '';
        $('.row').each(function(){
            if($(this).attr('id') != '')
            {
                products += 'products='+$(this).attr('id')+'&';
            }
        });
        
        window.location=targetUrl+'&'+products.substr(0, (products.length-1));
    } else {
        $('.checkbox div').addClass('forgottenCheckbox');
    }
    return false;
}

// this function pitifully does not work in the label shop (throws object error), therefore it can't be triggered here for every page.
function rounded_buttons()
{
    $('.button,.submit').each(function(){
        $(this).addClass('button_right');
        var buttonWrapper = $('<span class="button_left"></span>');
        $(this).wrap(buttonWrapper).mouseover(function(){
            $(this).parent().addClass('button_over');
        }).mouseout(function(){
            $(this).parent().removeClass('button_over');
        });
    });
}

$(function(){
    bindRemoveAjax();
    rounded_buttons();
    $('.checkbox input').focus(function(){
        $(this).parent().parent().find('div').removeClass('forgottenCheckbox');
    });
    $('#tac').click(function(){
        $.ajax({
            url:$(this).attr('href'),
            cache:true,
            type:'get',
            dataType:'html',
            success:function(html){
                $('.priceOverview').after($('<div class="tac"></div>').append(html).fadeIn('slow'));
            }
        });
        return false;
    });
});