//function validateForm() {
function validateGiftCardForm(numOfProds) {
	var i;
	var flag = 0;
	var catEntryIDs = "";
	var itemQuantity = "";
	var numOfProd = 0;
	var giftCardProdField = null;
	var giftCardQtyField = null;
	
	//alert("Beginning");
//<%	
//	for (int prod_idx = 0; prod_idx < products.length; prod_idx++)
//	{
//%>	
	for (prod_idx = 0; prod_idx < numOfProds; prod_idx++) {
		//alert(products.length);
		giftCardProdField = eval("document.giftcard.catEntryId_" + (prod_idx+1));
		giftCardQtyField = eval("document.giftcard.quantity_" + (prod_idx+1));					
		//if (document.giftcard.catEntryId_<%=prod_idx+1%>.checked) {
		if (giftCardProdField.checked) {
			//if ((document.giftcard.quantity_<%=prod_idx+1%>.value != "")||(document.giftcard.quantity_<%=prod_idx+1%>.value == null)) {
			if ((giftCardQtyField.value != "") || (giftCardQtyField.value == null)) {
				//if (<%=products.length%> == 1) {
				if (numOfProds == 1) {
					i = document.giftcard.attribute.selectedIndex;
					//document.giftcard.catEntryId_<%=prod_idx+1%>.value = document.giftcard.attribute.options[i].value;
					giftCardProdField.value = document.giftcard.attribute.options[i].value;
				}
				else {
					//i = document.giftcard.attribute[<%=prod_idx%>].selectedIndex;
					i = document.giftcard.attribute[prod_idx].selectedIndex;
					//document.giftcard.catEntryId_<%=prod_idx+1%>.value = document.giftcard.attribute[<%=prod_idx%>].options[i].value;
					giftCardProdField.value = document.giftcard.attribute[prod_idx].options[i].value;
				}
			
				var isQuantityNumeric = true
			
				//if (document.giftcard.quantity_<%=prod_idx+1%>.value == '')
				if (giftCardQtyField.value == '')
					flag = -1
			
				//for (y = 0; y < document.giftcard.quantity_<%=prod_idx+1%>.value.length; y++) {
				for (y = 0; y < giftCardQtyField.value.length; y++) {
					//if ((!parseInt(document.giftcard.quantity_<%=prod_idx+1%>.value.charAt(y))) && !(document.giftcard.quantity_<%=prod_idx+1%>.value.charAt(y) == '0'))
					if ((!parseInt(giftCardQtyField.value.charAt(y))) && !(giftCardQtyField.value.charAt(y) == '0'))
						isQuantityNumeric = false
				}
				
				//if ((!isQuantityNumeric) || (document.giftcard.quantity_<%=prod_idx+1%>.value < 1))
				if ((!isQuantityNumeric) || (giftCardQtyField.value < 1))
					flag = -1
				else
					flag = 1;
			}
			else
			{
				flag = -1;
			}
			//catEntryIDs = catEntryIDs + document.giftcard.catEntryId_<%=prod_idx+1%>.value + ",";
			catEntryIDs = catEntryIDs + giftCardProdField.value + ",";
			//itemQuantity = itemQuantity + document.giftcard.quantity_<%=prod_idx+1%>.value + ",";
			itemQuantity = itemQuantity + giftCardQtyField.value + ",";
			numOfProd++;
			//Due to the static form in the jsp, catEntryId is passed as a querystring, which causes a duplicate order item.
			//document.giftcard.catEntryId_<%=prod_idx+1%>.value = "";
			giftCardProdField.value = "";
			//document.giftcard.quantity_<%=prod_idx+1%>.value = "";

		}

		document.giftcard.catEntryId.value = catEntryIDs;
		document.giftcard.quantity.value = itemQuantity; 
		document.giftcard.numOfProducts.value = numOfProd;
		//alert("catEntryIDs: " + document.giftcard.catEntryId.value);
		//alert("itemQuantity: " + document.giftcard.quantity.value);
	//<%
	//}
	//%>
	}
	
	if (flag == 0)
	{
		alert("Please select a gift card.");
		return false;
	}
	else if (flag == -1)
	{
		alert("Please enter a quantity.");
		return false;
	}
	else {
		return true;
	}

}

function updateSelectedGiftItems(quantity, selectedGiftcardIndex, numOfProds)
{
	var giftCardProdField = null;
	//<%	
	//for (int prod_idx = 0; prod_idx < products.length; prod_idx++)
	//{
	//%>	
	for (prod_idx = 0; prod_idx < numOfProds; prod_idx++) {
	
		giftCardProdField = eval("document.giftcard.catEntryId_" + (prod_idx+1));
		//if (quantity != "" && quantity !="0" && selectedGiftcardIndex =='<%=prod_idx+1%>') {
		if (quantity != "" && quantity !="0" && selectedGiftcardIndex == (prod_idx+1)) {
			//document.giftcard.catEntryId_<%=prod_idx+1%>.checked = true;
			giftCardProdField.checked = true;
		}
		
		//if((quantity =="" ||quantity =="0") && selectedGiftcardIndex =='<%=prod_idx+1%>') {
		if((quantity =="" ||quantity =="0") && selectedGiftcardIndex == (prod_idx+1)) {
			//document.giftcard.catEntryId_<%=prod_idx+1%>.checked = false;
			giftCardProdField.checked = false;
		}
	}
	//<%
	//}
	//%>
}

function setDefaultQuantity(selectedGiftcardIndex, numOfProds)
{
	var giftCardProdField = null;
	var giftCardQtyField = null;
	//<%	
	//for (int prod_idx = 0; prod_idx < products.length; prod_idx++)
	//{
	//%>	
	for (prod_idx = 0; prod_idx < numOfProds; prod_idx++) {

		giftCardProdField = eval("document.giftcard.catEntryId_" + (prod_idx+1));
		giftCardQtyField = eval("document.giftcard.quantity_" + (prod_idx+1));			
		//if (selectedGiftcardIndex =='<%=prod_idx+1%>' && document.giftcard.catEntryId_<%=prod_idx+1%>.checked) {
		if (selectedGiftcardIndex == (prod_idx+1) && giftCardProdField.checked) {
			//if ((document.giftcard.quantity_<%=prod_idx+1%>.value == "")||(document.giftcard.quantity_<%=prod_idx+1%>.value == null)) {
			if ((giftCardQtyField.value == "")||(giftCardQtyField.value == null)) {
				//document.giftcard.quantity_<%=prod_idx+1%>.value = "1";
				giftCardQtyField.value = "1";
			}
		}
	}
	//<%
	//}
	//%>
}