//RSshoppingcart customized for Greg Farrell Art Originals
//
//The code in this document is Copyright 2001 by Ricky Spears.
//It may not be used on any other site without the express written consent
//of Ricky Spears. For contact information, please visit the author's
//web site at www.rickyspears.com.
//


stateTax = .07;
shipmethod = 0; //0=purchase price formula;1=individual amounts
getShopList();

function shipping(amt) {
  if(shipmethod == 0) { //calculate based on purchase price
	if(amt < 25.01) 
	   return 5.00; 
	else if(amt < 500.01) 
	   return 10.00;  	
	else if(amt < 1000.01) 
	   return 15.00;  	
	else if(amt < 1500.01) 
	   return 20.00;  	
	else if(amt < 2000.01) 
	   return 25.00;  	
	else 
	   return 30.00;  	
  }
  else { //calculate based upon values sent with Add To Cart button
    shippingsum = 0;
	for (x=0; x<=(shopList[0]-1)*4; x=x+4){
      shippingsum += shopList[x+4] * shopList[x+1];
	}
  return shippingsum;
  }
}

function getShopList() {
	myList = WM_readCookie("shoppingcart");
	if (!myList) {
		myList = "0,0,0,0,0";
		}
	shopList = myList.split(",");
}
 
function addToCart(qty,desc,cost,ship){
	shopList[0] ++;
	alert(qty + " of " + desc + " at $" + cost + " each \n Has been added to your shopping cart.");
	shopList[(shopList[0]-1)*4+1] = qty;
	shopList[(shopList[0]-1)*4+2] = desc;
	shopList[(shopList[0]-1)*4+3] = cost;
	shopList[(shopList[0]-1)*4+4] = ship;
	saveShoppingCart();
}

function displayCart(){
	if (shopList[0] == 0) {
		document.write("<P ALIGN=CENTER><FONT SIZE='4'><B>Your shopping cart is empty. </B></FONT><BR><BR><BR>");
		document.write("<b>Visit our <a href='reproductions.html'>Reproductions</a> section, <a href='originals.html'>Original Paintings</a> or<BR> <a href='otherproducts.html'>Gift Shop</a> to add items to your cart.</b></P><BR><BR><BR><BR><BR>");
		return false;
		}
	var subTotal = 0;
	document.write("<TABLE BORDER=2 CELLPADDING=3 align=center>");
	document.write("<TR><TD>Item Number</TD><TD>Quantity</TD><TD>Description</TD><TD>Price</TD><TD>Total</TD><TD>&nbsp;</TD></TR>");
	for (x=0; x<=(shopList[0]-1)*4; x=x+4){
		document.write("<TR><TD>Item #" + (x/4+1) + "</TD>");
		document.write("<TD>" + shopList[x+1] + "</TD>");
		document.write("<TD>" + shopList[x+2] + "</TD>");
		document.write("<TD>$" + shopList[x+3] + "</TD>");
		document.write("<TD>" + currency(shopList[x+3] * shopList[x+1]) + "</TD>");
		subTotal += shopList[x+3] * shopList[x+1];
		document.write("<TD><A HREF='javascript:removeFromCart(" + (x/4+1) + ")'><IMG SRC='images/cartremoveitem.gif' ALT='Remove Item' width=83 height=18 border=0></A></TD></TR>");
	}	
	document.write("<TR><TD ALIGN=RIGHT COLSPAN=4>Total:</TD>");
	document.write("<TD>" + currency(subTotal) + "</TD><TD>&nbsp;</TD></TR>");
	document.write("</TABLE><BR><BR>");
}

function checkOutTX(){
	var subTotal = 0;
	document.write("<TABLE BORDER=2 CELLPADDING=3 align=center>");
	document.write("<TR><TD>Item Number</TD><TD>Quantity</TD><TD>Description</TD><TD>Price</TD><TD>Total</TD></TR>");
	for (x=0; x<=(shopList[0]-1)*4; x=x+4){
		document.write("<TR><TD>Line #" + (x/4+1) + "</TD>");
		document.write("<TD>" + shopList[x+1] + "</TD>");
		document.write("<TD>" + shopList[x+2] + "</TD>");
		document.write("<TD>$" + shopList[x+3] + "</TD>");
		document.write("<TD>" + currency(shopList[x+3] * shopList[x+1]) + "</TD></TR>");
		subTotal += shopList[x+3] * shopList[x+1];
	}	
	document.write("<TR><TD ALIGN=RIGHT COLSPAN=4>Sub Total:</TD><TD>" + currency(subTotal) + "</TD></TR>");
	document.write("<TR><TD ALIGN=RIGHT COLSPAN=4>Shipping:</TD><TD>" + currency(shipping(subTotal)) + "</TD></TR>");
	document.write("<TR><TD ALIGN=RIGHT COLSPAN=4>6.75% sales tax for North Carolina residents:<BR><FONT SIZE=-1><A HREF='checkoutnotx.html'>Non-North Carolina residents click here to remove sales tax</A></FONT></TD><TD>" + currency(subTotal*stateTax) + "</TD></TR>");
	document.write("<TR><TD ALIGN=RIGHT COLSPAN=4><B>Total:</B></TD><TD><B>" + currency(shipping(subTotal) + subTotal + subTotal*stateTax) + "</B></TD></TR>");
	document.write("</TABLE>");
	for (x=0; x<=(shopList[0]-1)*4; x=x+4){
		document.write("<INPUT TYPE=HIDDEN NAME='Item_" + (x/4+1) + "' VALUE='");
		document.write(shopList[x+2] + " ");
		document.write(shopList[x+1] + " @ $");
		document.write(shopList[x+3] + " each = ");
		document.write(currency(shopList[x+3] * shopList[x+1]) + "'>");
	}	
	document.write("<INPUT TYPE=HIDDEN NAME='Shipping' VALUE='" + currency(shipping(subTotal)) + "'>");
	document.write("<INPUT TYPE=HIDDEN NAME='Sales_Tax' VALUE='" + currency(subTotal*stateTax) + "'>");
	document.write("<INPUT TYPE=HIDDEN NAME='Total' VALUE='" + currency(shipping(subTotal) + subTotal + subTotal*stateTax) + "'>");

}

function checkOutNoTX(){
	var subTotal = 0;
	document.write("<TABLE BORDER=2 CELLPADDING=3 align=center>");
	document.write("<TR><TD>Item Number</TD><TD>Quantity</TD><TD>Description</TD><TD>Price</TD><TD>Total</TD></TR>");
	for (x=0; x<=(shopList[0]-1)*4; x=x+4){
		document.write("<TR><TD>Line #" + (x/4+1) + "</TD>");
		document.write("<TD>" + shopList[x+1] + "</TD>");
		document.write("<TD>" + shopList[x+2] + "</TD>");
		document.write("<TD>$" + shopList[x+3] + "</TD>");
		document.write("<TD>" + currency(shopList[x+3] * shopList[x+1]) + "</TD></TR>");
		subTotal += shopList[x+3] * shopList[x+1];
	}	
	document.write("<TR><TD ALIGN=RIGHT COLSPAN=4>Sub Total:</TD><TD>" + currency(subTotal) + "</TD></TR>");
	document.write("<TR><TD ALIGN=RIGHT COLSPAN=4>Shipping:</TD><TD>" + currency(shipping(subTotal)) + "</TD></TR>");
	document.write("<TR><TD ALIGN=RIGHT COLSPAN=4>Sales tax<BR><FONT SIZE=-1><A HREF='checkouttx.html'>North Carolina residents click here to add sales tax</A></FONT></TD><TD>$0.00</TD></TR>");
	document.write("<TR><TD ALIGN=RIGHT COLSPAN=4><B>Total:</B></TD><TD><B>" + currency(shipping(subTotal) + subTotal) + "</B></TD></TR>");
	document.write("</TABLE>");
	for (x=0; x<=(shopList[0]-1)*4; x=x+4){
		document.write("<INPUT TYPE=HIDDEN NAME='Item_" + (x/4+1) + "' VALUE='");
		document.write(shopList[x+2] + " ");
		document.write(shopList[x+1] + " @ $");
		document.write(shopList[x+3] + " each = ");
		document.write(currency(shopList[x+3] * shopList[x+1]) + "'>");
	}	
	document.write("<INPUT TYPE=HIDDEN NAME='Shipping' VALUE='" + currency(shipping(subTotal)) + "'>");
	document.write("<INPUT TYPE=HIDDEN NAME='Sales_Tax' VALUE='0.00'>");
	document.write("<INPUT TYPE=HIDDEN NAME='Total' VALUE='" + currency(shipping(subTotal) + subTotal) + "'>");
}

function saveShoppingCart(){
	tempString = shopList[0];
	for(x=1; x<=shopList[0]*4; x++) {
		tempString = tempString  + "," + shopList[x];
	}
	WM_setCookie("shoppingcart",tempString,1);
}

function removeFromCart(num){
	endNum = shopList[0];
	for (x=(num-1)*4; x<=(endNum-1)*4; x=x+4){
		shopList[x+1] = shopList[x+5];
		shopList[x+2] = shopList[x+6];
		shopList[x+3] = shopList[x+7];		
		shopList[x+4] = shopList[x+8];		
	}	
	shopList[0]--;	
	saveShoppingCart();
	document.location.reload();
}

function emptyCart(){
	shopList="0,0,0,0,0";
	WM_killCookie("shoppingcart");
	document.location.reload();
}

function parse0opt(f) {
	if (f.qty.value == 0) {
		alert("You must enter a quantity before purchasing this item!");
		return false; }
	addToCart(f.qty.value,f.desc.value,f.price.value,f.shipping.value);
}

function parse1opt(f) {
	if (f.opt1[f.opt1.selectedIndex].value == "None") {
		alert("You must select a " + f.opt1type.value + " before purchasing this item!");
		return false; }
	if (f.qty.value == 0) {
		alert("You must enter a quantity before purchasing this item!");
		return false; }
	addToCart(f.qty.value,f.desc.value + " " + f.opt1[f.opt1.selectedIndex].value,f.price.value,f.shipping.value);
}

function parse2opt(f) {
	if (f.opt1[f.opt1.selectedIndex].value == "None") {
		alert("You must select a " + f.opt1type.value + " before purchasing this item!");
		return false; }
	if (f.opt2[f.opt2.selectedIndex].value == "None") {
		alert("You must select a " + f.opt2type.value + " before purchasing this item!");
		return false; }
	if (f.qty.value == 0) {
		alert("You must enter a quantity before purchasing this item!");
		return false; }
	addToCart(f.qty.value,f.desc.value + " " + f.opt1[f.opt1.selectedIndex].value + " " + f.opt2[f.opt2.selectedIndex].value,f.price.value,f.shipping.value);
}

//
//
//Code below this section is not the property of Ricky Spears
//It is the property of the listed author and used by permission
//
//

/*
WM_setCookie(), WM_readCookie(), WM_killCookie()
A set of functions that eases the pain of using cookies.

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Nadav Savio
Author Email: nadav@wired.com
*/


// This next little bit of code tests whether the user accepts cookies.
var WM_acceptsCookies = false;
if(document.cookie == '') {
    document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
    if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {
	WM_acceptsCookies = true; 
    }// If it succeeds, set variable
} else { // there was already a cookie
  WM_acceptsCookies = true;
}


function WM_setCookie (name, value, hours, path, domain, secure) {
    if (WM_acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.
	var not_NN2 = (navigator && navigator.appName 
		       && (navigator.appName == 'Netscape') 
		       && navigator.appVersion 
		       && (parseInt(navigator.appVersion) == 2))?false:true;

	if(hours && not_NN2) { // NN2 cannot handle Dates, so skip this part
	    if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
		var numHours = hours;
	    } else if (typeof(hours) == 'number') { // calculate Date from number of hours
		var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	    }
	}
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
    }
} // WM_setCookie


function WM_readCookie(name) {
    if(document.cookie == '') { // there's no cookie, so go no further
	return false; 
    } else { // there is a cookie
	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
	var NN2Hack = firstChar + name.length;
	if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
	    firstChar += name.length + 1; // skip 'name' and '='
	    lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
	    if(lastChar == -1) lastChar = theBigCookie.length;
	    return unescape(theBigCookie.substring(firstChar, lastChar));
	} else { // If there was no cookie of that name, return false.
	    return false;
	}
    }	
} // WM_readCookie

function WM_killCookie(name, path, domain) {
  var theValue = WM_readCookie(name); // We need the value to kill the cookie
  if(theValue) {
      document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
  }
} // WM_killCookie

//-- JavaScript code written by Alan Simpson - www.coolnerds.com
function currency(anynum) {
   //-- Returns passed number as string in $xxx,xxx.xx format.
   anynum=eval(anynum)
   workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
   pStr=workStr.substr(workStr.indexOf("."))
   while (pStr.length<3){pStr+="0"}

   //--- Adds comma in thousands place.
   if (dNum>=1000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
   }

   //-- Adds comma in millions place.
   if (dNum>=1000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
   }
   retval = dStr + pStr 
   //-- Put numbers in parentheses if negative.
   if (anynum<0) {retval="("+retval+")"}
   return "$"+retval
}

