var iOldPrice = null;
function updatetotalprice()
{
	var oTotalPrice = $('totalprice');
	fTotalPrice = parseFloat(get_only_numbers(oTotalPrice.innerHTML))/100;
	
	var oTotalPriceEx = $('totalpriceex');
	var oTotalPriceIn = $('totalpricein');
	
	var oAttributes = $('attributes');
	
	if (iOldPrice == null)
	{
		iOldPrice = fTotalPrice;
	}
	
	iTotalPrice = iOldPrice;
	
	aSelectElements = $$('select.attributeselect');
	for (selectelement in aSelectElements)
	{
		aSelect = aSelectElements[selectelement];
		if (aSelect.selectedIndex)
		{
			var aMatches = aSelect.options[aSelect.selectedIndex].text.split(/\s+/);
			var s = get_only_numbers(aMatches[aMatches.length-1]);
			if (s != '')
			{
				iAdd = parseFloat(s) / 100;
				iTotalPrice += iAdd;
			}
		}
	}
	iTotalPrice *= parseFloat($('quantity').value);
	
	oTotalPrice.innerHTML = format_number(iTotalPrice);
	
	if (oTotalPriceEx != null)
	{
		oTotalPriceEx.innerHTML = format_number(iTotalPrice/119*100);
	}
	else if (oTotalPriceIn != null)
	{
		oTotalPriceIn.innerHTML = format_number(iTotalPrice/100*119);
	}
}

function get_only_numbers(s)
{
	sReturn = '';
	for (ss = 0; ss < s.length; ss++)
	{
		if (s.charAt(ss) == '0' ||
			s.charAt(ss) == '1' ||
			s.charAt(ss) == '2' ||
			s.charAt(ss) == '3' ||
			s.charAt(ss) == '4' ||
			s.charAt(ss) == '5' ||
			s.charAt(ss) == '6' ||
			s.charAt(ss) == '7' ||
			s.charAt(ss) == '8' ||
			s.charAt(ss) == '9')
		{
			sReturn += s.charAt(ss);
		}
	}
	return sReturn;
}

function format_number(fNumber)
{
	fNumber *= 100;
	fNumber = Math.round(fNumber);
	fNumber /= 100;
	
	x = String(fNumber);
	x = x.split(/\./);
	if (x[1] == null)
	{
		x[1] = "00";
	}
	else if (x[1].length == 1)
	{
		x[1] = x[1] + "0";
	}
	return x[0] + "," + x[1];
}

var sCourierPrice = null;
var sTotalPrice = null;
var sShippingText = null;
function afhalen_check()
{
	if (sCourierPrice == null && sTotalPrice == null)
	{
		// Save original prices
		sCourierPrice = $('basket_courier_price').innerHTML;
		sTotalPrice = $('basket_total_price').innerHTML;
		sShippingText = $('basket_courier_text').innerHTML;
	}
	
	aRadios = $('ChoosePayment').payment;
	
	bChecked = false;
	for (i = 0; i < aRadios.length; i++)
	{
		if (aRadios[i].checked && aRadios[i].id == 'afhalen')
		{
			bChecked = true;
		}
	}
	
	if (bChecked)
	{
		// Make numbers of them
		iTotalPrice = get_only_numbers(sTotalPrice);
		iCourierPrice = get_only_numbers(sCourierPrice);
		
		// Substract shipping costs from total price
		iTotalPrice -= iCourierPrice;
		
		// Remove shipping div (hide it)
		$('basket_courier_text').innerHTML = 'Afhalen';
		$('basket_courier_price').innerHTML = '0,00';
		
		// Display correct total price
		$('basket_total_price').innerHTML = format_number(iTotalPrice / 100);
	}
	else
	{
		// Put old total price back
		$('basket_total_price').innerHTML = sTotalPrice;
		
		// Show the shipping again
		$('basket_courier_text').innerHTML = sShippingText;
		$('basket_courier_price').innerHTML = sCourierPrice;
	}
}
