// 2006-09-20
function recalcPrice(iQty) {
	var bMatch = false
	var totalPrice, campPrice
	if (validateBy('0123456789',iQty)) {
		var iPrice, iCamprice
		if (aPrices.length > 0) {
			for (i=0; i < aPrices.length; i++) {
				if (aPrices[i][0] == iSuffix) {
					if (iQty >= parseInt(aPrices[i][1])) {
						iPrice = aPrices[i][2]
					}
					bMatch = true
				}
			}
			if (!bMatch) {
				for (i=0; i < aPrices.length; i++) {
					if (aPrices[i][0] == '') {
						if (iQty >= parseInt(aPrices[i][1])) {
							iPrice = aPrices[i][2]
						}
					}
				}
			}
		}
		totalPrice = iBaseprice
		if (iPrice < iBaseprice) totalPrice = iPrice
		totalPrice = compareCampain(totalPrice)
	} else {
		alert('Endast heltal i antal-rutan')
	}
}
function compareCampain(iPrice) {
	var bCampain = false
	var iCampprice
	if (aCampaignPrices.length>0) bCampain = true
	if (bCampain) {
		iCampprice = getCampainPrice()
		if (iCampprice < iPrice) {
			return iCampprice
		} else {
			return iPrice
		}
	} else {
		return iPrice
	}
}
function getCampainPrice(loc_iSuffix, iOrdinaryPrice) {
	var k, price = 0, bFound = false
	for (k=0; k < aCampaignPrices.length; k++) {
		if (aCampaignPrices[k][1] == loc_iSuffix) {
			price = aCampaignPrices[k][0]
			bFound = true
		}
	}
	if (!bFound) {
		price = iOrdinaryPrice
	}
	return parseInt(price)
}

function setOptions(id) {
	var i, iOption = 0, iSelect, bMatch = false, bCampain = false, bImageMatch = false, iSuffix
	var oVS = document.getElementById('variantSelector')
	if (aCampaignPrices.length>0) bCampain = true
	if (id == 'optionSelector') oVS.sizeSelector.options.length = 0
	for (i=0; i < aArticles.length; i++) {
		if (oVS.optionSelector.value == aArticles[i][1]) {
			if (aArticles[i][5] == '') {
				document.getElementById('productImage').src = sImagePath+sImageDefault
				document.getElementById('productImage').setAttribute('large', sImagePathLarge+sImageDefault.substring(sImageDefault.indexOf("_")+1))

				} else {
				document.getElementById('productImage').src = sImagePath+aArticles[i][5]
				document.getElementById('productImage').setAttribute('large', sImagePathLarge+aArticles[i][5])
				bImageMatch = true
			}
		}

		if ((oVS.optionSelector.value == aArticles[i][1]) && (oVS.sizeSelector.value == aArticles[i][2])) {
			iSuffix = aArticles[i][0]
			if (bCampain) {
				document.getElementById('campaignprice').innerHTML = formatCurrency(getCampainPrice(iSuffix, aArticles[i][3])/100)
			}
			document.getElementById('art_price').innerHTML = formatCurrency(aArticles[i][3]/100)
			document.getElementById('deliverytime').innerHTML = aArticles[i][7]
			document.getElementById('prodweight').innerHTML = aArticles[i][6]
			document.getElementById('stockvalue').innerHTML = aArticles[i][4]
			bMatch = true
		}
	}

	if (oVS.optionSelector.options.length == 2) {
		oVS.optionSelector.options[0] = null
		oVS.optionSelector.options[0].selectedIndex
	}	

	if (!bMatch) {
		if (!bImageMatch) {
			document.getElementById('productImage').src = sImagePath+sImageDefault
		}

		oVS.sizeSelector.options.length = 0
		oVS.sizeSelector.options[0] = new Option('Välj storlek','')
		for (i=0; i < aArticles.length; i++) {
			if (aArticles[i][1] == oVS.optionSelector.value) {
				iOption++
				oVS.sizeSelector.options[iOption] = new Option(aArticles[i][2], aArticles[i][2])
			}
		}
		iSuffix = ''
	}
	if (oVS.sizeSelector.options.length<=1) {		
		if (bMatch) {
			oVS.sizeSelector.options.length = 0
		}
		oVS.sizeSelector.options[0] = new Option('- - - - - - - - - -','')
		oVS.sizeSelector.style.backgroundColor='#c7c7c7'
	} else {
		oVS.sizeSelector.style.backgroundColor=''
	}
	if(oVS.optionSelector.options.length>0) {
		if (aArticles[0][1] == '') {
			oVS.optionSelector.options.length = 0
			oVS.optionSelector.options[0] = null
			oVS.optionSelector.options[0] = new Option(sHeader,'')
		}
	}
	document.getElementById('suffix').innerHTML = iSuffix
	document.getElementById('basket_prod_suffix').value = iSuffix
	genPricelist(iSuffix)
}
