// JavaScript Document
// calculate price based on quantity
function changeQty(change){
    var currentQty = parseInt($F('quant')) // Where quant is the id of your quantity input field. Gets value of currentQty field
    
    switch (change) {
        case 'add':
            currentQty += 1
            $('quant').value = currentQty
            calculate()
            break
        case 'subtract':
            if (currentQty > 1) { // only subtract if qty is greater than zero
                currentQty -= 1
                $('quant').value = currentQty
                calculate()
            }
            break
        case 'field':
            if (currentQty >= 0) {
                window.setTimeout('calculate()', 500)
            }
            break
    }
}
function calculate(){
	var products_stock = +($('products_stock').innerHTML);
    var startPrice = $F('base_price') // Where base_price is the id of your hidden base price field. Gets value of base_price field
    var startPrice2 = $F('base_price_old') // Where base_price is the id of your hidden base price field. Gets value of base_price field
	$('quant').value = Math.min(+($('quant').value), products_stock);
	
	var currentQty = parseInt($F('quant')) // Where quant is the id of your quantity input field. Gets value of currentQty field
    
    if (currentQty > 0) { // Don't want price to display if zero if customer zeros out quantity
        var qtyPrice2 = startPrice2 * currentQty // Calculate the price.
        var qtyPrice = startPrice * currentQty // Calculate the price.
        var qtyPrice2 = qtyPrice2.toFixed(2) // Only allow 2 decimals. I'll let you add rounding features up or down.
        var qtyPrice = qtyPrice.toFixed(2) // Only allow 2 decimals. I'll let you add rounding features up or down.
    } else { // set price back to original price
        qtyPrice2 = startPrice2
        qtyPrice = startPrice
    }
    var qtyPrice = qtyPrice // Add a dollar sign
    var qtyPrice2 = '$' + qtyPrice2 // Add a dollar sign
    
	if($('priceHeading'))
	{
		$('priceHeading').update(qtyPrice2) // Where priceHeading2 is the id of your span for the echoed product price
	}
	else
	{
		qtyPrice = qtyPrice2;
	}
    new Effect.Base($('priceHeading'))
if($('priceHeading2')){
    
	$('priceHeading2').update(qtyPrice) // Where priceHeading2 is the id of your span for the echoed product price
    new Effect.Base($('priceHeading2'))
}}