Shopping Cart - Check Out

<SCRIPT>

// Catalog Array
var Item = new Array("Panda", "Koala", "Dolphin", "Otter");

// Price Array associated with the respective items
var Price = new Array(32, 16, 8, 4);

var name = price = quant = subtotal = "";  // declare variables

var total = 0;

/*************** Cookie Functions ***************/

var thisCookie = document.cookie;

function getCookie(name) {
     var index = thisCookie.
indexOf(name + "=");

     if (index == -1) return null;

     index = thisCookie.indexOf("=", index) + 1;

     var endstr = thisCookie.indexOf(";", index);

     if (endstr == -1) endstr = thisCookie.length;

     return unescape(thisCookie.substring(index, endstr));
}

function setCookie(name, quantity) {
     if (value != null && value != "") document.
cookie = name + "=" + quantity;

     thisCookie = document.cookie;
}

function deleteCookie(name, path, domain) {
     if (getCookie(name)) {
          document.cookie = name + "=" +
          ((path) ? "; path=" + path : "") +
          ((domain) ? "; domain=" + domain : "") +
          "; expires=Thu, 01-Jan-70 00:00:01 GMT";
     }
}

/*************** End of Cookie Functions ***************/

function getTotal() { // produces the Total for the cart
     for (i = 0; i < Item.
length; i++) {
          var quantity =
getCookie(Item[i]);
          total += Price[i] * quantity;
     }
     return total;
}

function updateCookie(item) {
     var quantity = document.cart[item].value;

     // if the quantity is not a number give an alert &
    
// then set the value back to original value
     if (
isNaN(quantity)) {
         
alert("Please enter a Number");
          document.cart[item].value = getCookie(item);
          return;
     }

     // delete the item if the number is not greater than 0 or empty
     if (quantity == "0" || quantity == "") deleteCookie(item)
     else setCookie(item, quantity);

     // this causes the page to reload & rewrite the entire page with the new information
     location.
reload();
}

</SCRIPT>

<FORM NAME="cart">

<TABLE BORDER="1" CELLPADDING="5" CELLSPACING="0">

<TR>
     <TH BGCOLOR="#336699"><FONT COLOR="#FFFFFF">Product</FONT></TH>
     <TH BGCOLOR="#336699"><FONT COLOR="#FFFFFF">No.</FONT></TH>
     <TH BGCOLOR="#336699"><FONT COLOR="#FFFFFF">Unit Price</FONT></TH>
     <TH BGCOLOR="#336699"><FONT COLOR="#FFFFFF">Subtotal</FONT></TH>
     <TH BGCOLOR="#336699">&nbsp;</TH>
</TR>

<SCRIPT>

var start = '<TR ALIGN="center"><TD ALIGN="left">';
var column = '</TD><TD>';
var columnR = '</TD><TD ALIGN="right">';
var end = '</TD></TR>';

for (i = 0; i < Item.length; i++) {
     if (
getCookie(Item[i])) {
          name = Item[i]; 
// item name
          price = Price[i];
// item price
          quant =
getCookie(name); // how many of the particular item

          // total for that particular item & subtotal for the table
          subtotal = "$" + quant * price + ".00";

          document.write(start + name + column)
          document.
write('<INPUT TYPE="text" NAME="' + name +
                                     '" VALUE="' + quant + '" SIZE="3">');
          document.
write(column + price + columnR + subtotal + column);
         
// what item to we want to update
          document.
write('<INPUT TYPE="button" VALUE="Update"
                                      
onClick="updateCookie('\' + name + '\')">' + end);
     }
}

document.write('<TR><TD COLSPAN="3" ALIGN="right">');
document.write('Total:' + columnR + '$' + 
getTotal() + '.00');
document.
write('</TD><TD>&nbsp</TD></TR>');

</SCRIPT>

</TABLE>

</FORM>