oldOnloadFunc = null;
if ( typeof ( window.onload ) == 'function' )
	oldOnloadFunc = window.onload;

window.onload = function ( ) {
	if ( oldOnloadFunc != null )
		oldOnloadFunc ( );

	if ( document.forms [ 'cartForm' ] != null )
	{
		aTags = document.getElementsByTagName ( 'a' );
		for ( a = 0; a < aTags.length; a ++ )
		{
			if(aTags [ a ].target=='_blank') continue;

			aTags [ a ].onclick = function ( ) {
				return nextPage ( this.href );
			}
		}
		recalculateSubtotal ( );

		inpTags = document.forms['cartForm'].getElementsByTagName('input');
		for( a = 0 ; a < aTags.length ; a++ ) {
			if( inpTags[a] && inpTags[a].name.indexOf('num[') == 0) {
				inpTags[a].onkeypress = function(event) {
					var k = 0;
					try {
						k = event.keyCode || event.which;
					} catch (e) {
						k = window.event.keyCode;
					}
					return k != 13;
				}
			}

		}

	}
}

function nextPage ( url )
{
	document.forms [ 'cartForm' ].elements [ 'nextURL' ].value = url;
	document.forms [ 'cartForm' ].submit ( );
	return false;
}

function recalculateSubtotal ( )
{
	totalPrice = 0;

	if ( ( frm = document.forms [ 'cartForm' ] ) != null )
	{
		for ( a = 0; a < frm.elements.length; a ++ )
		{
			if ( frm.elements [ a ].name.indexOf ( 'num[' ) == 0 )
			{
				if ( isNaN ( frm.elements [ a ].value ) || frm.elements [ a ].value.length == 0 || frm.elements [ a ].value <= 0 || Math.floor ( frm.elements [ a ].value ) != frm.elements [ a ].value )
				{
					frm.elements [ a ].value = 0;
					frm.elements [ a ].select ();
					frm.elements [ a ].focus ();
				}

				articleID = frm.elements [ a ].name.substring ( 4, frm.elements [ a ].name.length - 1 );

				price = frm.elements [ a ].value * document.getElementById ( 'price_' + articleID ).value;

				totalPrice += price;
				document.getElementById ( 'priceRow_' + articleID ).innerHTML = number_format ( price, 2, ',', '.' );

				if( document.getElementById ( 'vwb_' + articleID ) != null) {
					document.getElementById ( 'vwb_' + articleID ).innerHTML = frm.elements [ a ].value;

					priceVwb = parseFloat(document.getElementById ( 'priceVwb_' + articleID ).value);

					priceVwb = priceVwb * frm.elements [ a ].value;

					document.getElementById ( 'priceRowVwb_' + articleID ).innerHTML = number_format ( priceVwb, 2, ',', '.' );

					totalPrice += priceVwb;
				}

				if ( document.getElementById ( 'insuranceCheck_' + articleID ) != null )
				{
					if ( document.getElementById ( 'insuranceCheck_' + articleID ).checked )
						num = frm.elements [ a ].value;
					else
						num = 0;

					document.getElementById ( 'insuranceNum_' + articleID ).innerHTML = num;

					price = num * document.getElementById ( 'insurancePrice_' + articleID ).value;

					totalPrice += price;

					document.getElementById ( 'insuranceRow_' + articleID ).innerHTML = number_format ( price, 2, ',', '.' );
				}

				if ( document.getElementById ( 'insuranceTransCheck_' + articleID ) != null )
				{
					if ( document.getElementById ( 'insuranceTransCheck_' + articleID ).checked )
						num = frm.elements [ a ].value;
					else
						num = 0;

					document.getElementById ( 'insuranceTransNum_' + articleID ).innerHTML = num;

					price = num * document.getElementById ( 'insuranceTransPrice_' + articleID ).value;

					totalPrice += price;

					document.getElementById ( 'insuranceTransRow_' + articleID ).innerHTML = number_format ( price, 2, ',', '.' );
				}

			}
		}
		if ( $('voucherValue').innerHTML && $('subtotalPriceWithVoucher') ) {
            voucherValue = parseFloat($('voucherValue').innerHTML.replace('-',''));
            if ( totalPrice - voucherValue >= 0 ) {
                $('subtotalPriceWithVoucher').innerHTML = number_format ( totalPrice - voucherValue, 2, ',', '.' );
            } else {
                $('subtotalPriceWithVoucher').innerHTML = number_format ( 0, 2, ',', '.' );
            }
		}

		document.getElementById ( 'subtotalPrice' ).innerHTML = number_format ( totalPrice, 2, ',', '.' );
	}
}

function number_format ( price, i, sep, thousand )
{
	if ( isNaN ( price ) || price < 0 )
		price = 0;

	if ( parseInt ( price ) != price )
	{
		eur = parseInt ( price );
		cent = Math.round ( ( price - eur ) * 100 );

		if ( cent < 10 )
			cent = '0' + cent;

	}
	else
	{
		eur = price;
		cent = '00';
	}

	if ( eur > 0 )
	{
		t = eur + '';

		eur = '';
		while ( t.length > 3 )
		{
			eur = thousand + t.substring ( t.length - 3, t.length ) + eur;
			t = t.substring ( 0, t.length - 3 );
		}
		if ( t > 0 )
			eur = t + eur;
	}
	else
		eur = '0';

	return eur + sep + cent;
}

function changeInsurance (srcObj, artID )
{
	if(srcObj.id == 'insuranceCheck_'+artID) {

		if($('insuranceTransCheck_'+artID)) {
			if(srcObj.checked) {
				$('insuranceTransCheck_'+artID).checked = 'checked';
			} else {
				$('insuranceTransCheck_'+artID).checked = '';
			}
		}
	} else if( srcObj.checked ) {
		$('insuranceCheck_'+artID).checked = 'checked';
	}

	recalculateSubtotal ();
}
