// basket javacript
window.onunload = unloadCheck;
var newQtyValue = "";
var oldQtyValue = "";

function confirmDelete(itemType)
{
	var confirmMessage = removeFromCart;
	confirmMessage = confirmMessage.replace("\{item_type\}", itemType);
	return confirm(confirmMessage);
}

function confirmAllDelete(confirmMessage)
{
	return confirm(confirmMessage);
}

function changeListbox(cart_id, control, product_name, old_quantity)
{
	newQtyValue = control.value;
	oldQtyValue = old_quantity;
	if(confirmChanges(cart_id, control.options[control.selectedIndex].text, product_name, old_quantity)) {
		document.basket.submit();
	} else {
		newQtyValue = old_quantity;
		control.options[old_quantity].selected = true;
	}
}

function changeTextbox(cart_id, control, product_name, old_quantity)
{
	newQtyValue = control.value;
	oldQtyValue = old_quantity;
	if(confirmChanges(cart_id, control.value, product_name, old_quantity)) {
		document.basket.submit();
	}	else	{
		newQtyValue = old_quantity;
		control.value = old_quantity;
	}
}

function unloadCheck()
{
	if(newQtyValue != oldQtyValue) {
	  window.location.href = window.location.href;
	}
}


function confirmChanges(cart_id, new_quantity, product_name, old_quantity)
{
	var isConfirm = false;
	var confirmMessage = "";
  if (new_quantity < 1)
  {
		confirmMessage = cartQtyZero;
		confirmMessage = confirmMessage.replace("\{old_quantity\}", old_quantity);
		confirmMessage = confirmMessage.replace("\{product_name\}", product_name);
		if (confirm(confirmMessage))
		{
			isConfirm = true;
			document.basket.cart.value = "RM";
			document.basket.cart_id.value = cart_id;
			document.basket.new_quantity.value = 0;
		}
  }
  else
  {
		confirmMessage = alterCartQty;
		confirmMessage = confirmMessage.replace("\{old_quantity\}", old_quantity);
		confirmMessage = confirmMessage.replace("\{new_quantity\}", new_quantity);
		confirmMessage = confirmMessage.replace("\{product_name\}", product_name);
    if (confirm(confirmMessage))
		{
			isConfirm = true;
			document.basket.cart.value = "QTY";
			document.basket.cart_id.value = cart_id;
			document.basket.new_quantity.value = new_quantity;
		}
  }
	return isConfirm;
}

