// JavaScript Document
// 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);
}

        // 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 width=100% BORDER=0 bordercolor="#0062D1" cellpadding=5 cellspacing=0>');
                document.writeln('<TR><TD bgcolor="#0062D1"><FONT FACE="Arial,Helvetica" SIZE=2 color="#ffffff"><center><b>Item Description</b></TD><TD bgcolor="#0062D1"><FONT FACE="Arial,Helvetica" SIZE=2 color="#ffffff"><center><b>Quantity</b></TD><TD bgcolor="#0062D1"><FONT FACE="Arial,Helvetica" SIZE=2 color="#ffffff"><center><b>Unit<BR>Cost</b></TD><td bgcolor="#0062D1"><FONT FACE="Arial,Helvetica" SIZE=2 color="#ffffff"><center><b>Total<BR>Cost</b></TD><TD bgcolor="#0062D1"><FONT FACE="Arial,Helvetica" SIZE=2 color="#ffffff"><center><b>Action</b></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;
                                totweight = 0;
                                totweight = (eval(theweight*thequantity));
                                itemlist=itemlist+1;
                                document.writeln('<tr><td>'+theitem+'</td><td align=center>'+thequantity+'</td><td align=center>$'+theprice+'</td><td align=center>$'+alterError(itemtotal)+'</td><td align=center><A HREF="Javascript:removeItem('+itemlist+')">Remove</a></td></tr>');
                        } 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;
                        } else if (fulllist.substring(i,i+1) == '#') {
                                theweight = fulllist.substring(itemstart, i);
                                itemstart = i+1;
                        }
                }

                document.writeln('<tr><td colspan=3 align=right><b>Subtotal - </b></td><td align=center><B>$'+alterError(totprice)+'</td><td>&nbsp;</td></tr>');
                document.writeln('</TABLE>');
                if (itemlist == 0) {
                 	alert ("Your shopping cart is empty.  If you have added items to the cart and they have not appeared then your browser may not be set to accept cookies.");
                }
}

function removeItem(itemno) {
                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 = "order_view.htm";
}

        // clearBasket() - removes all items from the basket
        
function clearBasket() {
                if (confirm('Are you sure you wish to clear the basket')) {
                        index = document.cookie.indexOf("TheBasket");
                        document.cookie="TheBasket=";
                        location = "order_view.htm";
                }
}
