﻿Type.registerNamespace("SuB");

function Round2Decimals( amount )
{
    if( isNaN( amount ) )
        return amount;
        
    return Math.round( amount * 100 ) / 100;
}

function SetInnerText(element, text) {
    if (document.all) {
        element.innerText = text;
    } else {
        element.textContent = text;
    }
}

SuB.OrderPosition = function( order, amountElement, sumElement, price, bundle )
{
    this.bundle = bundle;   
    this.order = order;
    this.price = price;
    this.amountElement = amountElement;
    this.sumElement = sumElement;
    this.amount = 0;
    
    $addHandler( amountElement, 'change', function( e ) { order.sum(); } );
}

SuB.OrderPosition.prototype = {
    sum : function()
    {
        this.amount = parseInt(this.amountElement.value);
        if( isNaN( this.amount ) )
            this.amount = 0;

        var sum = Round2Decimals(this.amount * this.price);
        SetInnerText( this.sumElement, sum );
        
        return sum;
    }            
}

SuB.OrderPosition.registerClass('SuB.OrderPosition');

SuB.Order = function( sumElement, vatElement, shippingElement, finalElement )
{
    this.positions = new Array();
    this.sumElement = sumElement;
    this.vatElement = vatElement;
    
    this.shippingElement = shippingElement;
    this.finalElement = finalElement;
}

SuB.Order.prototype = {
    add: function(amountElement, sumElement, price, bundle) {
        /*debugger;*/
        this.positions.push(new SuB.OrderPosition(this, amountElement, sumElement, price, bundle));
        this.sum();
    },

    sum: function() {
        var total = 0; // this sometimes gets lost
        for (var i = 0; i < this.positions.length; ++i) {
            total += this.positions[i].sum();
        }

        SetInnerText(this.sumElement, Round2Decimals(total));
        SetInnerText(this.vatElement, Round2Decimals(total / 1.19 * 0.19));
        /*this.totalElement.data = Round2Decimals( total ); */

        SetInnerText(this.finalElement, Round2Decimals(this.shipping() + total));
    },

    shipping: function() {
        var amounts = new Object();
        amounts[size02.key] = [];
        amounts[size07.key] = [];
        amounts[size05.key] = [];

        // this sometimes gets lost
        for (var i = 0; i < this.positions.length; ++i) {
            var position = this.positions[i];
            var bundle = position.bundle;
            amounts[bundle.size.key].push( { amount: position.amount, size: position.bundle.amount } );
        }

        var total = size02.calcShipping(amounts[size02.key]) + size07.calcShipping(amounts[size07.key]) + size05.calcShipping(amounts[size05.key]);
        SetInnerText(this.shippingElement, Round2Decimals(total));

        return total;
    }
}

SuB.Order.registerClass('SuB.Order');
      
SuB.Size07 = function()
{
    this.key = "size07";
}

SuB.Size07.prototype = {
    calcShipping: function(amounts) {
        
        var amount = 0;
        for (var i = 0; i < amounts.length; ++i) {
            amount += amounts[i].amount * amounts[i].size;
        }

        var remaining = amount;
        var bundle6 = Math.floor(remaining / 6);
        var bundle3 = 0;
        var bundle2 = 0;
        var bundle1 = 0;

        remaining -= bundle6 * 6;
        if (remaining >= 4)
            bundle6 += 1;
        else if (remaining == 3)
            bundle3 = 1;
        else if (remaining == 2)
            bundle2 = 1;
        else if (remaining == 1)
            bundle1 = 1;
        /*    
        remaining -= bundle3 * 3;
        var bundle2 = Math.floor( remaining / 2 );
        var bundle1 = remaining - bundle2 * 2;
        */

        return Round2Decimals(bundle6 * 6.5 + bundle3 * 6.5 + bundle2 * 6.5 + bundle1 * 4.5);
    }
}      

SuB.Size07.registerClass('SuB.Size07');

SuB.Size02 = function()
{
    this.key = "size02";
}

SuB.Size02.prototype = {
    calcShipping: function(amounts) {
        
        var amount = 0;
        for (var i = 0; i < amounts.length; ++i) {
            amount += amounts[i].amount;
        }

        return amount * 4.5; /* 4.5 EUR per pack */
    }
}

SuB.Size02.registerClass('SuB.Size02');


SuB.Size05 = function() {
    this.key = "size05";
}

SuB.Size05.prototype = {
    calcShipping: function(amounts) {
        
        var amount = 0;
        for (var i = 0; i < amounts.length; ++i) {
            amount += amounts[i].amount;
        }

        return amount * 4.5; /* 4.5 EUR per pack */
    }
}

SuB.Size05.registerClass('SuB.Size05');

SuB.Bundle = function( size, amount )
{
    this.size = size;
    this.amount = amount;
}

SuB.Bundle.registerClass('SuB.Bundle');

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();



