// JavaScript Document

startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("nav");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}
window.onload=startList;

		// alterError - fixes a rounding bug in Netscape 2
		function alterError(value) {
			if (value<=0.99) {
				newPounds = '0';
			} else {
				newPounds = parseInt(value);
			}
			newPence = parseInt((value+.0008 - newPounds)* 100);
			if (eval(newPence) <= 9) newPence='0'+newPence;
			newString = newPounds + '.' + newPence;
			return (newString);
		}
		
		// buyItem - adds an item to the shopping basket
		function buyItem(newItemID, newItem, newPrice, newQuantity) {
			if (newQuantity <= 0) {
				rc = alert('The quantity entered is incorrect');
				return false;
			}
			if (confirm('Add '+newQuantity+' x '+newItem+' to basket')) {
				index = document.cookie.indexOf("TheBasket");
				countbegin = (document.cookie.indexOf("=", index) + 1);
				countend = document.cookie.indexOf(";", index);
				if (countend == -1) {
					countend = document.cookie.length;
				}
				document.cookie="TheBasket="+document.cookie.substring(countbegin, countend)+"["+newItemID+"|"+newItem+","+newPrice+"#"+newQuantity+"]";
			}
			return true;
		}
		
		// showItems() - displays shopping basket in a table
	function showItems() {
		index = document.cookie.indexOf("TheBasket");
		countbegin = (document.cookie.indexOf("=", index) + 1);
        	countend = document.cookie.indexOf(";", index);
        	if (countend == -1) {
            		countend = document.cookie.length;
        	}
		fulllist = document.cookie.substring(countbegin, countend);
		totprice = 0;
		document.writeln('<table border="0" cellpadding="0" cellspacing="0">');

		document.writeln('<tr><td width="60">&nbsp;</td><td width="300" align="center"><strong>Item</strong></td><td width="80" align="center"><strong>Quantity</strong></td><td width="80" align="center"><strong>Price</strong></td><td width="80" align="center"><strong>Total</strong></td></tr>');
		itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++) {
			if (fulllist.substring(i,i+1) == '[') {
				itemstart = i+1;
			} else if (fulllist.substring(i,i+1) == ']') {
				itemend = i;
				thequantity = fulllist.substring(itemstart, itemend);
				itemtotal = 0;
				itemtotal = (eval(theprice*thequantity));
				temptotal = itemtotal * 100;
				totprice = totprice + itemtotal;
				itemlist=itemlist+1;
				//var thepriceformatted = theprice;
				//var thepriceformatted = thepriceformatted.toFixed(2);
				document.writeln('<tr><td align="center"><a href="javascript:removeItem('+itemlist+')"><img src="pics/remove.gif" border="0" /></a></td><td align="center">'+theitem+'</td><td align="center">'+thequantity+'</td><td align="right">&pound;'+theprice+'</td><td align="right">&pound;'+alterError(itemtotal)+'</td></tr>');
			} else if (fulllist.substring(i,i+1) == '|') {
				theitemID = fulllist.substring(itemstart, i);
				itemstart = i+1;
			} else if (fulllist.substring(i,i+1) == ',') {
				theitem = fulllist.substring(itemstart, i);
				itemstart = i+1;
			} else if (fulllist.substring(i,i+1) == '#') {
				theprice = fulllist.substring(itemstart, i);
				itemstart = i+1;
			}
		}
		
		if(itemlist==0){
			document.writeln('<tr><td colspan="5" align="center"><br /><b><em>your basket is currently empty, please<br />browse our shop to fill your basket...</em></b><br /><br /></td></tr>');
		}
		
		if(totprice<0){
			totpriceformat = totprice.toFixed(2);
			document.writeln('<tr><td align="right" colspan="4"><b>Total:</b></td><td align="right">&pound;'+totpriceformat+'</td><td></tr>');
		} else {
			document.writeln('<tr><td align="right" colspan="4"><b>Total:</b></td><td align="right">&pound;'+alterError(totprice)+'</td></tr>');
		}
		
		document.writeln('</table>');
		
	}
	
	function removeItem(itemno) {
		if (confirm('Are you sure you wish to remove this item from your basket?')) {
			newItemList = null;
			itemlist = 0;
			for (var i = 0; i <= fulllist.length; i++) {
				if (fulllist.substring(i,i+1) == '[') {
					itemstart = i+1;
				} else if (fulllist.substring(i,i+1) == ']') {
					itemend = i;
					theitem = fulllist.substring(itemstart, itemend);
					itemlist=itemlist+1;
					if (itemlist != itemno) {
						newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
					}
				}
			}
			index = document.cookie.indexOf("TheBasket");
			document.cookie="TheBasket="+newItemList;
			location = "basket.html";
		}
	}
	
	// putItems() - displays shopping basket in a form
	function putItems() {
		index = document.cookie.indexOf("TheBasket");
		countbegin = (document.cookie.indexOf("=", index) + 1);
        	countend = document.cookie.indexOf(";", index);
        	if (countend == -1) {
            		countend = document.cookie.length;
        	}
		fulllist = document.cookie.substring(countbegin, countend);
		totprice = 0;
		document.writeln('<input type="hidden" name="items" value="');
		itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++) {
			if (fulllist.substring(i,i+1) == '[') {
				itemstart = i+1;
			} else if (fulllist.substring(i,i+1) == ']') {
				itemend = i;
				thequantity = fulllist.substring(itemstart, itemend);
				itemtotal = 0;
				itemtotal = (eval(theprice*thequantity));
				temptotal = itemtotal * 100;
				totprice = totprice + itemtotal;
				itemlist=itemlist+1;
				document.writeln(''+thequantity+' x ('+theitemID+') '+theitem+' @ £'+theprice+'\n');
			} else if (fulllist.substring(i,i+1) == '|') {
				theitemID = fulllist.substring(itemstart, i);
				itemstart = i+1;
			} else if (fulllist.substring(i,i+1) == ',') {
				theitem = fulllist.substring(itemstart, i);
				itemstart = i+1;
			} else if (fulllist.substring(i,i+1) == '#') {
				theprice = fulllist.substring(itemstart, i);
				itemstart = i+1;
			}
		}
		
		document.writeln('" />');
		
		if(itemlist==0){
			document.writeln('<input type="hidden" name="items" value="'+none+'" />');
		}
		
		var priceformat = totprice
		priceformat = priceformat.toFixed(2)
		document.writeln('<input type="hidden" name="price" value="'+priceformat+'" />');
		
	}
	
	function showInfo() {
		a=0;
		if(document.orderform.name.value==""){
			alert('You have not entered a name!');
			a=1;
		}
		if(document.orderform.phone.value==""){
			if(document.orderform.email.value==""){
				alert('You have not entered a contact phone number or email address!');
				a=1;
			}
		}
		if(a==0){
			alert('Remember, you will not pay for your order online\n\nWe will pack your items ready for you to pick up\n\nthen we will contact you to confirm your order.');
			document.orderform.submit();
		}
	}


