Shopping Cart - Check Out

<SCRIPT>

var thisCookie = document.cookie;

var name = price = quant = subtotal = "";

var total = 0;

if (!getCookie("cart")) Item = ""; // if the cart is empty then create an empty string
else { // split the cart to create an Item array
     Item = getCookie("cart").split(";"); // Otter;Panda;Koala => Item[0] = Otter
     Price = getCookie("price").split(";"); // 4;32;16 => Price[1] = 32
}

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

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() {
     for (i = 0; i < Item.
length; i++) {
          var quantity =
getCookie(Item[i]);
          total += Price[i] * quantity;
     }
     return total;
}

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

     if (isNaN(quantity)) {
         
alert("Please enter a Number");
    
     document.cart[Item[i]].value = getCookie(Item[i]);
          return;
     }

     // an item is deleted so we need to pull that item from the cart &
    
// the corresponding price from the price string
     if (quantity == 0) {
          deleteCookie(Item[i]);
          Item[i] = "";  // set the array item element to an empty string
          Price[i] = "";  // set the array item element to an empty string
          cartStr = Item.join(";").replace(/;;/g, ";");
          priceStr = Price.join(";").replace(/;;/g, ";");
         
// scenarios:
         
// 1) ;Panda;Koala
         
// 2) Otter;;Koala => Otter;Koala is taken care of above
         
// 3) Otter;Panda;

          // scenario 1) ;Panda;Koala => Panda;Koala
          if (i == 0) {
               cartStr = cartStr.substring(1);
    
          priceStr = priceStr.substring(1);
          }

          // scenario 3) Otter;Panda; => Otter;Panda
          if (cartStr.
lastIndexOf(";") == cartStr.length - 1) {
               cartStr = cartStr.substring(0, cartStr.length - 1);
               priceStr = priceStr.substring(0, priceStr.length - 1);
          }

                if (!cartStr.length ) { // if the cart is empty then delete it
               deleteCookie("cart");
               deleteCookie("price");
          }
          else { // if the cart exists then create the cookie
               setCookie("cart", cartStr);
               setCookie("price", priceStr);
          }
     }
     else setCookie(Item[i], quantity);

     location.reload(); // reload the page
}

</SCRIPT>

<SCRIPT>

if (Item.length) { // if the cart isn't empty display the cart
     document.
write('<FORM NAME="cart">')
     document.
write('<TABLE><TR>');
     document.write('<TH BGCOLOR="#336699" NOWRAP ALIGN="left">
                                <FONT COLOR="#FFFFFF">Item</FONT>
                                </TH>');
     document.write('<TH BGCOLOR="#336699">
                                <FONT COLOR="#FFFFFF">No.</FONT>
                                </TH>');
     document.write('<TH BGCOLOR="#336699">
                                <FONT COLOR="#FFFFFF">Unit Price</FONT>
                                </TH>');
     document.write('<TH BGCOLOR="#336699">
                                <FONT COLOR="#FFFFFF">Subtotal</FONT>
                                </TH>');
     document.
write('</TR>');

     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];
               price = Price[i];
               quant =
getCookie(name);

               subtotal = "$" + quant * price + ".00";

               document.write(start + name + column)
               document.
write('<INPUT TYPE="text" NAME="' + name +
                                         '" VALUE="' + quant + '" SIZE="3"

                                         onChange
="updateCookie('\' + i + '\')">');
               document.
write(column + price + columnR + subtotal + end);
          }
     }

     document.write('<TR><TD COLSPAN="3" ALIGN="right">');
     document.write('Total:' + columnR + '$' + 
getTotal() + '.00');
     document.
write('</TD></TR>');
     document.
write('</TABLE>');
     document.
write('</FORM>');
}
else {
// if the cart is empty write out the message that the cart is empty
     document.
write('<H3>The Shopping Cart is Empty</H3>');
}

</SCRIPT>