// round to decimals
function round(x, n) {
  if (n < 1 || n > 14) return false;
  var e = Math.pow(10, n);
  var k = (Math.round(x * e) / e).toString();
  if (k.indexOf('.') == -1) k += '.';
  k += e.toString().substring(1);
  return k.substring(0, k.indexOf('.') + n+1);
}

// change order amount +/-
function changeAmount(id,mode) {
	var value = String(document.getElementById('amount_'+id).value);
	var decimals = 0;
	if(value.indexOf(',')>=0) {
		value = value.replace(',','.');
	}
	if(value.indexOf('.') >=0) {
		decimals = value.length - value.indexOf('.') - 1;
	}
	value=Number(value);
	if(mode=='+') {
		value = value+1;
	} else {
		value = value-1;
		if(value < 0) {
			value = 0;
		}
	}
	if(decimals>0) {
		value = round(value,decimals);
	}
	
	value=String(value);
	if(value.indexOf('.')>=0) {
		value = value.replace('.',',');
	}

	document.getElementById('amount_'+id).value = value;
	return false;
}

function updatePrice(id,price) {
	if(shopProductOptions[id]) {
		if(shopProductOptions[id][0]=='-') {		
			price -= parseFloat(shopProductOptions[id][1]);
		} else {
			price += parseFloat(shopProductOptions[id][1]);
		}
	}
	return price;
}

function formatPrice(price,span) {
	price = String(round(price,2));
	decimals = 0;
	if(price.indexOf('.') >=0) {
		decimals = price.length - price.indexOf('.') - 1;
	} else {
		price += '.00';
		decimals = 2;
	}
	beforeComma = price.substr(0,price.indexOf('.'));
	if(beforeComma.length >3) {
		beforeComma = beforeComma.substr(0, beforeComma.length-3)+'.'+beforeComma.substr(beforeComma.substr(0, beforeComma.length-3).length, 3);
	}
	price = beforeComma +( span ? ',<sup>' : ',' )+price.substr(price.indexOf('.')+1,decimals)+( decimals==1 ? '0' : '' )+( span ? '</sup>' : '' );
	return price;
}

function updateArtNum(id,artNum) {
	if(shopProductOptions[id]) {
		if(shopProductOptions[id][2]) {
			artNum = shopProductOptions[id][2];
		}
	}
	return artNum;
}

function updateProductData() {
	var price = parseFloat(shopProductPriceOriginal);
	var originalPrice = '';
	var artNum = shopArtNum;
	var str = '';
	var elem = document.getElementById('productDetailForm').elements;
	for(var i = 0; i < elem.length; i++) {
		if(elem[i].type=='select-one' || elem[i].type=='select') {
			price = updatePrice(elem[i].value,price);
			artNum = updateArtNum(elem[i].value,artNum);
		} else if(elem[i].type=='radio') {
			if(elem[i].checked){
				price = updatePrice(elem[i].value,price);
				artNum = updateArtNum(elem[i].value,artNum);
			}
		} else if(elem[i].type=='checkbox') {
			if(elem[i].checked){
				id=elem[i].name;
				id=id.replace(/[A-Za-z_]/g,"");
				price = updatePrice(id,price);
				artNum = updateArtNum(id,artNum);
			}
		}
    }
	if(discount) {
		originalPrice = price;
		price = price*(100-discount)/100;
	}
    document.getElementById('shopDynamicArtNum').innerHTML=artNum;
    document.getElementById('shopDynamicPrice').innerHTML=formatPrice(price,true);
    if(originalPrice && document.getElementById('shopDynamicPriceOriginal')) {
    	document.getElementById('shopDynamicPriceOriginal').innerHTML=formatPrice(originalPrice);
    }
    if(document.getElementById('shopDynamicPricePrefix')) {
    	document.getElementById('shopDynamicPricePrefix').innerHTML='';
    }
}

function toggleSearch(mode) {
	if(mode == 1) {
		document.getElementById('productSearchInput').className = 'productSearchInputFocus';
		if(document.getElementById('productSearchInput').value == 'Produktsuche...') {
			document.getElementById('productSearchInput').value = '';
		}
	} else {
		document.getElementById('productSearchInput').className = 'productSearchInput';
		if(document.getElementById('productSearchInput').value == '') {
			document.getElementById('productSearchInput').value = 'Produktsuche...';
		}
	}		
}
