//Shopping cart library functions
//Suzanne Stagel
//Foothill College-Summer 1999
//COIN 70-JavaScript
//-----------------
//Global Variables
//-----------------
var today = new Date();
var exp = new Date();
//Define cookie expiration
exp.setTime(today.getTime() + 1000*60*60*24*365);
//---------
//getCookie
//---------
//
//Parameters: name of the cookie to retrieve
//Functionality: returns the value of the cookie with the specified name
//
function getCookie(Name) {
var search = Name + "=";
if (document.cookie.length > 0) { //if there are any cookies
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length;
//set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(offset, end));
}
}
}
//------------------
//initializeCookies
//------------------
//
//Parameters: None
//Functionality: initializes cookie value for each product to 0 and sets the cookies
// on the user's machine
//
//Note: A maximum of 20 distinct products can be accomodated in the shopping cart
// because this implementation is limited by the number of cookies that a
// browser will allow a domain to set (this includes items added and later deleted
// from the cart)
//
// Alert statements that are commented out are for debugging purposes only.
//
function initializeCookies() {
//Variables
var cookieValue = -1;
var defaultQty = 0;
//Define cookie expiration
var today = new Date();
var exp = new Date();
exp.setTime(today.getTime() + 1000*60*60*24*365);
//
//Set initial cookies for Tomatoes
//
cookieValue = getCookie("brandywine");
if ( cookieValue != null ) {
// alert("Cookie brandywine exists, original value: " + cookieValue);
} else {
setCookie("brandywine",defaultQty,exp);
// alert("New cookie brandywine added with value of value: " + getCookie("brandywine"));
}
cookieValue = getCookie("earlyGirl");
if ( cookieValue != null ) {
// alert("Cookie earlyGirl exists, original value: " + cookieValue);
} else {
setCookie("earlyGirl",defaultQty,exp);
// alert("New cookie earlyGirl added with value of value: " + getCookie("earlyGirl"));
}
cookieValue = getCookie("cherokeePurple");
if ( cookieValue != null ) {
// alert("Cookie cherokeePurple exists, original value: " + cookieValue);
} else {
setCookie("cherokeePurple",defaultQty,exp);
// alert("New cookie cherokeePurple added with value of value: " + getCookie("cherokeePurple"));
}
cookieValue = getCookie("chadwickCherry");
if ( cookieValue != null ) {
// alert("Cookie chadwickCherry exists, original value: " + cookieValue);
} else {
setCookie("chadwickCherry",defaultQty,exp);
// alert("New cookie chadwickCherry added with value of value: " + getCookie("chadwickCherry"));
}
//
//Set initial cookies for Watermelons
//
cookieValue = getCookie("yellowMoon");
if ( cookieValue != null ) {
// alert("Cookie yellowMoon exists, original value: " + cookieValue);
} else {
setCookie("yellowMoon",defaultQty,exp);
// alert("New cookie yellowMoon added with value of value: " + getCookie("yellowMoon"));
}
cookieValue = getCookie("moonbeam");
if ( cookieValue != null ) {
// alert("Cookie moonbeam exists, original value: " + cookieValue);
} else {
setCookie("moonbeam",defaultQty,exp);
// alert("New cookie moonbeam added with value of value: " + getCookie("moonbeam"));
}
//
//Set initial cookies for Veggies
//
cookieValue = getCookie("carrot");
if ( cookieValue != null ) {
// alert("Cookie carrot exists, original value: " + cookieValue);
} else {
setCookie("carrot",defaultQty,exp);
// alert("New cookie carrot added with value of value: " + getCookie("carrot"));
}
cookieValue = getCookie("fourLettuce");
if ( cookieValue != null ) {
// alert("Cookie fourLettuce exists, original value: " + cookieValue);
} else {
setCookie("fourLettuce",defaultQty,exp);
// alert("New cookie fourLettuce added with value of value: " + getCookie("fourLettuce"));
}
cookieValue = getCookie("redLettuce");
if ( cookieValue != null ) {
// alert("Cookie redLettuce exists, original value: " + cookieValue);
} else {
setCookie("redLettuce",defaultQty,exp);
// alert("New cookie redLettuce added with value of value: " + getCookie("redLettuce"));
}
cookieValue = getCookie("peas");
if ( cookieValue != null ) {
// alert("Cookie peas exists, original value: " + cookieValue);
} else {
setCookie("peas",defaultQty,exp);
// alert("New cookie peas added with value of value: " + getCookie("peas"));
}
cookieValue = getCookie("peas");
if ( cookieValue != null ) {
// alert("Cookie peas exists, original value: " + cookieValue);
} else {
setCookie("peas",defaultQty,exp);
// alert("New cookie peas added with value of value: " + getCookie("peas"));
}
cookieValue = getCookie("corn");
if ( cookieValue != null ) {
// alert("Cookie corn exists, original value: " + cookieValue);
} else {
setCookie("corn",defaultQty,exp);
// alert("New cookie corn added with value of value: " + getCookie("corn"));
}
} //end initializeCookies
//-----------
//listCookies
//-----------
//
//Parameters: None
//Functionality: lists the values of all the cookies set by the site (in the form of dialog alert boxes)
//
//Note: This function is used for debugging purposes only.
//
function listCookies() {
//Tomatoes
alert("earlyGirl: " + getCookie("earlyGirl"));
alert("brandywine: " + getCookie("brandywine"));
alert("cherokeePurple: " + getCookie("cherokeePurple"));
alert("chadwickCherry: " + getCookie("chadwickCherry"));
//Watermelons
alert("yellowMoon: " + getCookie("yellowMoon"));
alert("moonbeam: " + getCookie("moonbeam"));
//Veggies
alert("carrot: " + getCookie("carrot"));
alert("fourLettuce: " + getCookie("fourLettuce"));
alert("redLettuce: " + getCookie("redLettuce"));
alert("peas: " + getCookie("peas"));
alert("corn: " + getCookie("corn"));
alert("listCookies completed");
} //end listCookies
//---------
//setCookie
//---------
//
//Parameters: name (of the cookie), value (of the cookie), expiration date of the cookie (optional)
//Functionality: Sets a cookie on the user's machine.
//
function setCookie(name, value, expire) {
document.cookie = name + "=" + escape(value)
+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
} //setCookie
//---------
//addToCart
//---------
//
//Parameters: item (name of the cookie)
//Functionality: "adds" the item to the shopping cart by incrementing the value of the cookie
// by 1 and calling the updateCart() function to dynamically update the cartFrame's HTML
//
//Note: Alert statements that are commented out are for debugging purposes only.
//
function addToCart(item) {
var cookieName = item;
var cookieValue = -1;
var newQuantity = 0;
//check for existing cookie for this item
//if exists, overwrite (adding 1), otherwise create cookie with value of 1
cookieValue = getCookie(item);
if ( cookieValue != null ) {
// alert(item + "cookie exists, original value: " + cookieValue);
newQuantity = Number(cookieValue) + 1;
} else {
newQuantity = 1;
}
setCookie(item,newQuantity,exp);
//For debugging only
// cookieValue = getCookie(item);
// alert("New cookie value for " + item + " is: " + cookieValue);
//update shopping cart display
updateCart();
} //addToCart
//----------
//deleteItem
//----------
//
//Parameters: item (name of the cookie)
//Functionality: "deletes" the item from the shopping cart by setting the value of the cookie
// to 0 and calling the updateCart() function to dynamically update the cartFrame's HTML
//
//Note: Alert statements that are commented out are for debugging purposes only.
//
function deleteItem(item) {
var cookieName = item;
var cookieValue = -1;
var newQuantity = 0;
cookieValue = getCookie(item);
if ( cookieValue != null ) {
//For debugging only
// alert("Deleting existing item " + item + " with a quantity of " + cookieValue + " from cart");
} else {
alert("Error in deleteItem(): Trying to delete an item not in the shopping cart");
}
setCookie(item,newQuantity,exp);
//update shopping cart display
updateCart();
//For debugging only
// alert(item + " value is now " + getCookie(item));
} //deleteItem
//-----------
//subtractOne
//-----------
//
//Parameters: item (name of the cookie)
//Functionality: subtracts one "item" from the shopping cart by decrementing the value of the cookie
// by 1 and calling the updateCart() function to dynamically update the cartFrame's HTML
//
//Note: Alert statements that are commented out are for debugging purposes only.
//
function subtractOne(item) {
var cookieName = item;
var cookieValue = 0;
var newQuantity = 0;
//check for existing cookie for this item
//if exists, overwrite (subtracting 1), otherwise display error alert
cookieValue = getCookie(item);
if ( cookieValue != null ) {
if (cookieValue > 0) {
// alert(item + "cookie exists, original value: " + cookieValue); //For debugging only
newQuantity = Number(cookieValue) - 1;
setCookie(item,newQuantity,exp);
} else {
alert("Error in subtractOne(): Trying to subtract from an item with a quantity <= 0");
}
} else {
alert("Error in subtractOne(): Trying to subtract from an item not in the shopping cart");
}
//For debugging only
// cookieValue = getCookie(item);
// alert("New cookie value for " + item + " is: " + cookieValue);
//update shopping cart display
updateCart();
} //subtractOne
//---------------
//updateQuantity
//---------------
//
//Parameters: item (name of the cookie), quantity (new quantity in shopping cart)
//Functionality: updates the quantity of "item" in the shopping cart by setting
// its value to "quantity" and calling the updateCart() function to dynamically update the cartFrame's HTML
//
//Note: Alert statements that are commented out are for debugging purposes only.
//
function updateQuantity(item,quantity) {
var cookieName = item;
var cookieValue = 0;
//check for existing cookie for this item
//if exists, overwrite (with quantity passed in), otherwise display error alert
cookieValue = getCookie(item);
if ( cookieValue != null ) {
// alert(item + "cookie exists, original value: " + cookieValue); //For debugging only
newQuantity = quantity;
setCookie(item,quantity,exp);
} else {
alert("Error in updateQuantity(): Trying to update from an item not in the shopping cart");
}
//For debugging only
// cookieValue = getCookie(item);
// alert("New cookie value for " + item + " is: " + cookieValue);
//update shopping cart display
updateCart();
} //updateQuantity
//----------
//updateCart
//----------
//
//Parameters: None
//Functionality: dynamically creates new HTML for cartFrame based on the cookie values set
// for each product.
//
//Note: Alert statements that are commented out are for debugging purposes only.
//
function updateCart() {
var cartHTML="
Error in updateCart()
";
var beginCartHTML; //contains the initial section of HTML for the cart frame
var endCartHTML; //contains the initial section of HTML for the cart frame
var midCartHTML = ""; //contains the HTML with the individual products and quantites
var s1 = ""; //problem with the SCRIPT tags being stripped out
var rowHTML = "";
var cookieValue = 0;
var totalHTML = "$0.00";
var orderTotal = 0.00;
beginCartHTML = "" + s1 + s2
+ "
\
Shopping Cart \
"
+ s1 + s2 +
"\
\
\