isDefined = function(variable){
	return this[variable] === undefined ? false : true;
};

function updateQuantity(id, modus) {
    ele = $('addQuantity_' + id);

    if(modus == 'add')
        ele.value++;
    else if(modus == 'delete')
        ele.value--;

    if(ele.value<0)
        ele.value = 0;

    if(isDefined('vPerunit')) {
       updateBasket();
    }
}

function euroFormat(val) {

     return parseFloat(val).toFixed(2).toString().replace('.', ',');
}

function updateBasket() {

    inputs = $('orderbasket').getElements('input').filter(function(e) { return e.get('class') == 'quantity'; });
    if(inputs.length) {
        var bruttoFull = 0;
        var weightFull = 0;
        var taxesFull = {};
        inputs.each(function(e) {

            pId = e.get('id').replace('addQuantity_', '');
            quantity = e.get('value');
            newBrutto = pBruttos[pId] * quantity;

            bruttoFull += newBrutto;
            weightFull += pWeights[pId] * quantity;
            $('bProduct_' + pId).set('html', euroFormat(newBrutto));

            taxClass = pTaxes[pId];

            if(taxesFull[taxClass] == undefined)
                taxesFull[taxClass] = 0;

            taxesFull[taxClass] += pSingleTax[pId] * quantity;
        });

        weightTimes = Math.ceil(weightFull / vPerunit);
        weightFull = weightTimes * vCosts;

        $('bruttoFull').set('html', euroFormat(bruttoFull));
        $('weightFull').set('html', euroFormat(weightFull));

        if(taxesFull[19] == undefined) {
            taxesFull[19] = 0;
        }

        shippingTax = parseFloat(weightFull / (119)) * 19;
        taxesFull[19] += shippingTax;


        $H(taxesFull).each(function(el, k) {
            $('fullTax_' + k).set('html', euroFormat(el));
        });

        if($chk($('discountRow'))) {

        }else{
            tr = new Element('TR', {'id' : 'discountRow'}).injectBefore($('sumprice'));
            newtd = new Element('TD').set('html', 'Rabatt:').injectInside(tr);
            newtd = new Element('TD').set('html', '').injectAfter(newtd);
        }
        tds = $('discountRow').getElements('td');
        td = tds[1];
        td.set('html', '-' + euroFormat(pDiscount) + ' &euro;');

        $('discountRow').setStyle('display', (pDiscount > 0 ? 'table-row' : 'none'));


        $('orderTotal').set('html', euroFormat(bruttoFull + weightFull - pDiscount));
    }
}

function checkCoupon() {

    $('couponErg').set('class', '').set('html', '');
    $('couponload').setStyles({'margin-left' : '10px', 'float' : 'left', 'display' : 'block'});

    params = new Array();
    params[0] = $('couponcode').value;

    mparams = new Array();
    mparams[0] = $('bruttoFull').get('html').replace(',','.');
    fetchAjax('coupon', 'is_valid', 'coupon', params, 'isInvalid', mparams, 'p', 'HTML', couponIsValid, '', new Array('couponload', loaderImg[3]));
}

function couponIsValid(html) {
    split = html.split('#');
    $('couponload').setStyles({'display' : 'none'});
    if(split.length==3) {
        pDiscount = split[2];
        $('couponErg').addClass(split[0] + 'Box').set('html', split[1]);
    }else{
        pDiscount = 0;
        $('couponErg').set('html', 'Die Abfrage ist fehlgeschlagen').addClass('infoBox');
    }
    updateBasket();
}

window.addEvent('domready', function() {
    if($chk($('linkCheckCoupon'))) {
        $('linkCheckCoupon').setStyle('display', '');
        a = $('linkCheckCoupon').getElements('a');
        a = a[0];
        a.addEvent('click', function() {
            checkCoupon();
        });
    }
});
