// This function loops through the child elements of an element and returns the
// index of the required child element.
function findBasketItem(parentElement,productId) {
// loop through the items in the cart to find the item required
	for (i=0; i<parentElement.childNodes.length; i++) {
	
		// check item id to verify if it is the item required
		if (parentElement.childNodes[i].id == productId) {
		
			// if item is the one required return its index number and exit the function
			return i;
			}
		}
	// if the element is not found then return -1
	return -1;
	}
	
//The addItem function adds a text element containing the text productName and with
//the id productId
//
//The function is called by the onClick method of the Add button
//
//<button type="button" onclick="addItem('20 inch silver frying pan','20inchpan')">Add</button>

function addBasketItem(productName, productId) {
// check for correct version of JavaScript
	if (document.getElementById) {
	
		// set cartList to the element with id itemlist - the shopping cart
		cartList = document.getElementById("basket");
		
		// check that the item to be added does not already exist
		if (findBasketItem(cartList,productId)<0) {
		
			// if the item does not exist create a new node
			newProduct = document.createElement("p");
			newProduct.id = productId;
			
			// create a text node with the required text and append it to the new node
			newText = document.createTextNode(productName);
			newProduct.appendChild(newText);
			
			// append the new node to the cart
			cartList.appendChild(newProduct);
			}
		}		
	}
	
//The deleteItem function removes a node from the shopping cart haing checked first that it exists
//The function is called by the onClick method of the Delete button
//
//<button type="button" onclick="deleteItem('20inchpan')">Delete</button>
//
function deleteBasketItem(productId) {

// check for correct version of JavaScript
	if (document.getElementById) {
		// assign the node with the id itemlist (the list of items in the cart) to a variable
		cartList = document.getElementById("basket");
		
		// assign the index of the item to a variable
		itempos = findBasketItem(cartList,productId)
		
		// if the item exists then remove it from the cart
		if (itempos>=0) {
			
			cartList.removeChild(cartList.childNodes[itempos]);
			return
			}
		}
	}
	
function thankyou() {
	window.open("br-thankyou.php", "toolbar=yes,location=yes,scrollbars=yes,status=yes,resizeable=yes,menubar=yes")
	}
