function setTotal(which, remove){
   // variables
  var lQuantity
	var fPrice
	var fPrevProductTotal
	var fProductTotal
	var fTotal
	// objects
	var oProductTotal
	var oQuantity
	var oPrice
	var oTotal
	var oForm
	var oGrandTotal
	var o_elt
	
	oForm = document.forms.shoppingcart

	if (!oForm) return
  
	o_elt = get_element("loadingmsg")
	if (o_elt) o_elt.style.display = ""
	enable_form("shoppingcart", false)

  if (remove) {
	  location.href = "add_to_cart.php?action=refresh&productid=" + which + "&productuniq=" + oForm.productuniq.value + "&quantity=remove&catid=" + oForm.catid.value + "&cat51=" + oForm.catid.value
    return
  }
	
	oQuantity = oForm["q" + which]
	
	if (!oQuantity) return
	lQuantity = oQuantity.value

	location.href = "add_to_cart.php?action=refresh&productid=" + which + "&productuniq=" + oForm.productuniq.value + "&quantity=" + lQuantity + "&catid=" + oForm.catid.value + "&cat51=" + oForm.catid.value

   /*
	oPrice = oForm["p" + which]
	if (!oPrice) return
	fPrice = oPrice.value
	
	oProductTotal = get_element("subtotal" + which)
	if (!oProductTotal) return
	
   fPrevProductTotal = parseFloatSafe(getDollarAmt(striptags(oProductTotal.innerHTML)))

   fPrice = parseFloatSafe(fPrice)
	lQuantity = parseIntSafe(lQuantity)	
	fProductTotal = fPrice * lQuantity

   // show new sub total for the product that was changed  
	oProductTotal.innerHTML = "$" + formatString(new String(fProductTotal),"currency")
	
	oTotal = get_element("total")
	if (!oTotal) return

	fTotal = parseFloatSafe(getDollarAmt(striptags(oTotal.innerHTML)))
	fTotal = fTotal - fPrevProductTotal + fProductTotal

	// show new total
	oTotal.innerHTML = "<strong>$" + formatString(new String(fTotal),"currency") + "</strong>"
	// also set the hidden form element
	oTotal = oForm.total
	if (oTotal) oTotal.value = fTotal
   */

	// oQuantity.focus()
	
	return
}

function validate(){
  // variables
	var fTotal
	var i
	var name
	var productId
	var productName
	var lQ
	var lAQ 
	var result
   var total_quantity
	// objects
	var oTotal
	var oForm
	var oElt
	var oElt2
	
	oForm = document.forms.shoppingcart

	if (!oForm) return false
		
	oTotal = oForm.total
	if (!oTotal) return false
	//alert(striptags(oTotal.value))
	fTotal = parseFloatSafe(getDollarAmt(striptags(oTotal.value)))
  //alert(fTotal)
	
	if (!(fTotal > 0)) {
	  alert("You have not selected any products to purchase.")
	  return false
	}
	
   total_quantity = 0

	result = true
	for (i = 0; i < oForm.elements.length; i++) {
	  oElt = oForm.elements[i]
		if (oElt) {
		  name = oElt.name
			if (name.substr(0,1) == "q") {
			   lQ = parseIntSafe(oElt.value)
			   productId = name.substr(1)
				oElt2  = oForm["n" + productId]
				if (oElt2) {
				  productName = oElt2.value
					oElt2 = oForm["av" + productId]
					lAQ = parseIntSafe(oElt2.value)
					if (lQ > lAQ) {
						alert(availQuantityMessage(lAQ,productName) + " Please update the selected quantity.")
						oElt.focus()
						result = false
						break
					} else {
                 total_quantity = total_quantity + lQ
               }
				}
			}
		}
	}

   if (result) oForm.total_quantity.value = total_quantity

	return result
} 

function availQuantityMessage(q,n) {
  if (q == 1) return "There is only one " + n + " left in the store."
  if (q > 1) return "There are only " + q + " " + n + " items left in the store."
	return ""  
}


function clearCart() {
   var oForm
	var oElt
	
	oForm = document.forms.clearcart
	if (!oForm) return
	
	oElt = oForm.clearcart
	if (!oElt) return

	oElt.value = 1
	
	oForm.submit()
	return true
}
