// JavaScript Document

function listcheckBreakPoint(value, theId)
{
	$counter = 0
	itemCost = 0;
	previouseItem = 0
	//console.debug(breakPointArray)
	quantity = value;
	if(parseInt(breakPointArray[theId]['maxstock']) == -1 || parseInt(breakPointArray[theId]['maxstock']) > value)
	{
		$.each(breakPointArray[theId]['breakpoints'],function(index, item)
		{	
			if(parseInt(value) < parseInt(index))
			{
				itemCost = previouseItem;
				return false;
			}
			
			previouseItem = item
			$counter = $counter + 1;
		});
	
		if(itemCost == 0)
		{
			itemCost = previouseItem
		}
	}
	else
	{
		$.each(breakPointArray[theId]['breakpoints'],function(index, item)
		{	
			
			
			if(parseInt(index) > parseInt(breakPointArray[theId]['maxstock']))
			{
				return false;	
			}
			itemCost = item;
			quantity = index
		});
		if(quantity < breakPointArray[theId]['maxstock'])
		{
			quantity = breakPointArray[theId]['maxstock']
		}
		
	}
	return itemCost + "|"+ quantity;
}

function listupdateprice(row)
{
	
	parentProduct = $($($(row).parent()).parent()).parent();
	value = $('.quantityChanger',$(parentProduct)).val()
	theId = $('.theId',$(parentProduct)).val()
	sliderStep = blockQty[theId];
	value = (Math.ceil(value / sliderStep) * sliderStep)
	
	itemCostArr = listcheckBreakPoint(value, theId)
	theArray = itemCostArr.split('|');
	itemCost = theArray[0];
	newQty = theArray[1];
	
	if(parseInt(value) != parseInt(newQty))
	{
		value = parseInt(newQty);
		$('.quantityChanger',$(parentProduct)).val(value)
		alert('Due to stock limitations you are only able to order ' + value + ' of this item');
	}
	
	$('.singleCostChanged',$(parentProduct)).html(parseFloat(itemCost).toFixed(2))
	$('.totalCostChanged',$(parentProduct)).html(parseFloat(itemCost * parseInt(value)).toFixed(2))
	$('.quantityChanger',$(parentProduct)).val(value)  
	
}


