forked from wojodesign/simplecart-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleCart.min.js
35 lines (35 loc) · 15.1 KB
/
simpleCart.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var a,NextId=1,Custom="Custom",GoogleCheckout="GoogleCheckout",PayPal="PayPal",Email="Email",AustralianDollar=AUD="AUD",CanadianDollar=CAD="CAD",CzechKoruna=CZK="CZK",DanishKrone=DKK="DKK",Euro=EUR="EUR",HongKongDollar=HKD="HKD",HungarianForint=HUF="HUF",IsraeliNewSheqel=ILS="ILS",JapaneseYen=JPY="JPY",MexicanPeso=MXN="MXN",NorwegianKrone=NOK="NOK",NewZealandDollar=NZD="NZD",PolishZloty=PLN="PLN",PoundSterling=GBP="GBP",SingaporeDollar=SGD="SGD",SwedishKrona=SEK="SEK",SwissFranc=CHF="CHF",USDollar=
USD="USD";
function Cart(){var c=this;c.Version="2.0.1";c.Shelf=new Shelf;c.items={};c.isLoaded=false;c.pageIsReady=false;c.quantity=0;c.total=0;c.taxRate=0;c.taxCost=0;c.shippingFlatRate=0;c.shippingTotalRate=0;c.shippingQuantityRate=0;c.shippingRate=0;c.shippingCost=0;c.currency=USD;c.checkoutTo=PayPal;c.email="";c.merchantId="";c.cartHeaders=["Name","Price","Quantity","Total"];c.add=function(){var b=this;if(!b.pageIsReady){b.initializeView();b.update()}if(!b.isLoaded){b.load();b.update()}var d=new CartItem;
if(!arguments||arguments.length===0)error("No values passed for item.");else{var e=arguments;if(arguments[0]&&typeof arguments[0]!="string"&&typeof arguments[0]!="number")e=arguments[0];d.parseValuesFromArray(e);d.checkQuantityAndPrice();if(b.hasItem(d)){e=b.hasItem(d);b.items[e].quantity=parseInt(b.items[e].quantity,10)+parseInt(d.quantity,10)}else b.items[d.id]=d;b.update()}};c.remove=function(b){var d={};for(var e in this.items)if(e!=b)d[e]=this.items[e];this.items=d};c.empty=function(){simpleCart.items=
{};simpleCart.update()};c.find=function(b){if(!b)return null;var d=[];for(var e in c.items){var f=c.items[e],g=true;for(var h in b)if(!f[h]||f[h]!=b[h])g=false;g&&d.push(c.next)}return d.length==0?null:d};c.checkout=function(){if(simpleCart.quantity===0)error("Cart is empty");else switch(simpleCart.checkoutTo){case PayPal:simpleCart.paypalCheckout();break;case GoogleCheckout:simpleCart.googleCheckout();break;case Email:simpleCart.emailCheckout();break;default:simpleCart.customCheckout();break}};c.paypalCheckout=
function(){var b=this,d="https://www.paypal.com/cgi-bin/webscr?cmd=_cart&upload=1&business="+b.email+"¤cy_code="+b.currency,e=1,f="";if(b.taxRate)d=d+"&tax_cart="+b.currencyStringForPaypalCheckout(b.taxCost);for(var g in b.items){var h=b.items[g],i="";for(var j in h)if(typeof h[j]!="function"&&j!="id"&&j!="price"&&j!="quantity"&&j!="name"&&j!="shipping")i=i+", "+j+"="+h[j];i=i.substring(2);f=f+"&item_name_"+e+"="+h.name+"&item_number_"+e+"="+e+"&quantity_"+e+"="+h.quantity+"&amount_"+e+"="+
b.currencyStringForPaypalCheckout(h.price)+"&on0_"+e+"=Options&os0_"+e+"="+i;e++}if(b.shipping()!=0)f=f+"&item_name_"+e+"=Shipping&item_number_"+e+"="+e+"&quantity_"+e+"=1&amount_"+e+"="+b.currencyStringForPaypalCheckout(b.shippingCost);d+=f;window.open(d,"paypal","scrollbars,location,resizable,status")};c.googleCheckout=function(){var b=this;if(b.currency!=USD&&b.currency!=GBP)error("Google Checkout only allows the USD and GBP for currency.");else if(b.merchantId===""||b.merchantId===null||!b.merchantId)error("No merchant Id for google checkout supplied.");
else{var d=document.createElement("form"),e=1;d.style.display="none";d.method="POST";d.action="https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/"+b.merchantId;d.acceptCharset="utf-8";for(var f in b.items){var g=b.items[f];d.appendChild(b.createHiddenElement("item_name_"+e,g.name));d.appendChild(b.createHiddenElement("item_quantity_"+e,g.quantity));d.appendChild(b.createHiddenElement("item_price_"+e,g.price));d.appendChild(b.createHiddenElement("item_currency_"+e,b.currency));d.appendChild(b.createHiddenElement("item_tax_rate_"+
e,b.taxRate));d.appendChild(b.createHiddenElement("_charset_",""));var h="";for(var i in g)if(typeof g[i]!="function"&&i!="id"&&i!="quantity"&&i!="price")h=h+", "+i+": "+g[i];h=h.substring(1);d.appendChild(b.createHiddenElement("item_description_"+e,h));e++}document.body.appendChild(d);d.submit();document.body.removeChild(d)}};c.emailCheckout=function(){};c.customCheckout=function(){};c.load=function(){var b=this;b.items={};b.total=0;b.quantity=0;if(readCookie("simpleCart"))for(var d=unescape(readCookie("simpleCart")).split("++"),
e=0,f=d.length;e<f;e++){var g=d[e].split("||"),h=new CartItem;if(h.parseValuesFromArray(g)){h.checkQuantityAndPrice();b.items[h.id]=h}}b.isLoaded=true};c.save=function(){var b="";for(var d in this.items)b=b+"++"+this.items[d].print();createCookie("simpleCart",b.substring(2),30)};c.initializeView=function(){var b=this;b.totalOutlets=getElementsByClassName("simpleCart_total");b.quantityOutlets=getElementsByClassName("simpleCart_quantity");b.cartDivs=getElementsByClassName("simpleCart_items");b.taxCostOutlets=
getElementsByClassName("simpleCart_taxCost");b.taxRateOutlets=getElementsByClassName("simpleCart_taxRate");b.shippingCostOutlets=getElementsByClassName("simpleCart_shippingCost");b.finalTotalOutlets=getElementsByClassName("simpleCart_finalTotal");b.addEventToArray(getElementsByClassName("simpleCart_checkout"),simpleCart.checkout,"click");b.addEventToArray(getElementsByClassName("simpleCart_empty"),simpleCart.empty,"click");b.Shelf.readPage();b.pageIsReady=true};c.updateView=function(){c.updateViewTotals();
c.cartDivs&&c.cartDivs.length>0&&c.updateCartView()};c.updateViewTotals=function(){for(var b=[["quantity","none"],["total","currency"],["shippingCost","currency"],["taxCost","currency"],["taxRate","percentage"],["finalTotal","currency"]],d=0,e=b.length;d<e;d++){var f=b[d][0]+"Outlets",g;for(var h in c[f]){switch(b[d][1]){case "none":g=""+c[b[d][0]];break;case "currency":g=c.valueToCurrencyString(c[b[d][0]]);break;case "percentage":g=c.valueToPercentageString(c[b[d][0]]);break;default:g=""+c[b[d][0]];
break}c[f][h].innerHTML=""+g}}};c.updateCartView=function(){var b=[],d,e,f,g,h,i,j,k,l;e=document.createElement("div");for(h in c.cartHeaders){i=document.createElement("div");f=c.cartHeaders[h].split("_");i.innerHTML=f[0];i.className="item"+f[0];d=1;for(xlen=f.length;d<xlen;d++)if(f[d].toLowerCase()=="noheader")i.style.display="none";e.appendChild(i)}e.className="cartHeaders";b[0]=e;d=1;for(g in c.items){e=document.createElement("div");f=c.items[g];for(h in c.cartHeaders){i=document.createElement("div");
j=c.cartHeaders[h].split("_");switch(j[0].toLowerCase()){case "total":k=c.valueToCurrencyString(parseFloat(f.price)*parseInt(f.quantity,10));break;case "increment":k=c.valueToLink("+","javascript:;","onclick=\"simpleCart.items['"+f.id+"'].increment();\"");break;case "decrement":k=c.valueToLink("-","javascript:;","onclick=\"simpleCart.items['"+f.id+"'].decrement();\"");break;case "remove":k=c.valueToLink("Remove","javascript:;","onclick=\"simpleCart.items['"+f.id+"'].remove();\"");break;case "price":k=
c.valueToCurrencyString(f[j[0].toLowerCase()]?f[j[0].toLowerCase()]:" ");break;default:k=f[j[0].toLowerCase()]?f[j[0].toLowerCase()]:" ";break}for(var m=1,n=j.length;m<n;m++){l=j[m].toLowerCase();switch(l){case "image":case "img":k=c.valueToImageString(k);break;case "input":k=c.valueToTextInput(k,"onchange=\"simpleCart.items['"+f.id+"'].set('"+k+"' , this.value);\"");break;case "div":case "span":case "h1":case "h2":case "h3":case "h4":case "p":k=c.valueToElement(l,k,"");break;case "noheader":break;
default:error("unkown header option: "+l);break}}i.innerHTML=k;i.className="item"+j[0];e.appendChild(i)}e.className="itemContainer";b[d]=e;d++}for(g in c.cartDivs){for(d=c.cartDivs[g];d.childNodes[0];)d.removeChild(d.childNodes[0]);e=0;for(h=b.length;e<h;e++)d.appendChild(b[e])}};c.addEventToArray=function(b,d,e){for(var f in b){var g=b[f];if(g.addEventListener)g.addEventListener(e,d,false);else g.attachEvent&&g.attachEvent("on"+e,d)}};c.createHiddenElement=function(b,d){var e=document.createElement("input");
e.type="hidden";e.name=b;e.value=d;return e};c.currencySymbol=function(){switch(c.currency){case JPY:return"¥";case EUR:return"€";case GBP:return"£";case USD:case CAD:case AUD:case NZD:case HKD:case SGD:return"$";default:return""}};c.currencyStringForPaypalCheckout=function(b){return c.currencySymbol()=="$"?"$"+parseFloat(b).toFixed(2):""+parseFloat(b).toFixed(2)};c.valueToCurrencyString=function(b){return parseFloat(b).toCurrency(c.currencySymbol())};c.valueToPercentageString=
function(b){return parseFloat(100*b)+"%"};c.valueToImageString=function(b){return b.match(/<\s*img.*src\=/)?b:'<img src="'+b+'" />'};c.valueToTextInput=function(b,d){return'<input type="text" value="'+b+'" '+d+" />"};c.valueToLink=function(b,d,e){return'<a href="'+d+'" '+e+" >"+b+"</a>"};c.valueToElement=function(b,d,e){return"<"+b+" "+e+" > "+d+"</"+b+">"};c.hasItem=function(b){for(var d in c.items){var e=c.items[d],f=true;for(var g in b)if(typeof b[g]!="function"&&g!="quantity"&&g!="id")if(b[g]!=
e[g])f=false;if(f)return d}return false};c.update=function(){simpleCart.isLoaded||simpleCart.load();simpleCart.pageIsReady||simpleCart.initializeView();c.updateTotals();c.updateView();c.save()};c.updateTotals=function(){c.total=0;c.quantity=0;for(var b in c.items){var d=c.items[b];if(d.quantity<1)d.remove();else if(d.quantity!==null&&d.quantity!="undefined")c.quantity=parseInt(c.quantity,10)+parseInt(d.quantity,10);if(d.price)c.total=parseFloat(c.total)+parseInt(d.quantity,10)*parseFloat(d.price)}c.shippingCost=
c.shipping();c.taxCost=parseFloat(c.total)*c.taxRate;c.finalTotal=c.shippingCost+c.taxCost+c.total};c.shipping=function(){if(parseInt(c.quantity,10)===0)return 0;var b=parseFloat(c.shippingFlatRate)+parseFloat(c.shippingTotalRate)*parseFloat(c.total)+parseFloat(c.shippingQuantityRate)*parseInt(c.quantity,10),d,e;for(e in c.items){d=c.items[e];if(d.shipping)b+=typeof d.shipping=="function"?parseFloat(d.shipping()):parseFloat(d.shipping)}return b};c.initialize=function(){simpleCart.initializeView();
simpleCart.load();simpleCart.update()}}function CartItem(){this.id="c"+NextId++}a=CartItem.prototype;
a.set=function(c,b){c=c.toLowerCase();if(typeof this[c]!="function"&&c!="id"){if(c=="quantity"){b=b.replace(/[^(\d|\.)]*/gi,"");b=b.replace(/,*/gi,"");b=parseInt(b,10)}else if(c=="price"){b=b.replace(/[^(\d|\.)]*/gi,"");b=b.replace(/,*/gi,"");b=parseFloat(b)}if(typeof b=="number"&&isNaN(b))error("Improperly formatted input.");else{this[c]=b;this.checkQuantityAndPrice()}}else error("Cannot change "+c+", this is a reserved field.");simpleCart.update()};
a.increment=function(){this.quantity=parseInt(this.quantity,10)+1;simpleCart.update()};a.decrement=function(){if(parseInt(this.quantity,10)<2)this.remove();else{this.quantity=parseInt(this.quantity,10)-1;simpleCart.update()}};a.print=function(){var c="";for(var b in this)if(typeof this[b]!="function")c+=escape(b)+"="+escape(this[b])+"||";return c.substring(0,c.length-2)};
a.checkQuantityAndPrice=function(){if(!this.price||this.quantity==null||this.quantity=="undefined"){this.quantity=1;error("No quantity for item.")}else{this.quantity=(""+this.quantity).replace(/,*/gi,"");this.quantity=parseInt((""+this.quantity).replace(/[^(\d|\.)]*/gi,""),10);if(isNaN(this.quantity)){error("Quantity is not a number.");this.quantity=1}}if(!this.price||this.price==null||this.price=="undefined"){this.price=0;error("No price for item or price not properly formatted.")}else{this.price=
(""+this.price).replace(/,*/gi,"");this.price=parseFloat((""+this.price).replace(/[^(\d|\.)]*/gi,""));if(isNaN(this.price)){error("Price is not a number.");this.price=0}}};a.parseValuesFromArray=function(c){if(c&&c.length&&c.length>0){for(var b=0,d=c.length;b<d;b++){c[b].replace(/||/,"| |");c[b].replace(/\+\+/,"+ +");var e=c[b].split("=");if(e.length>1){if(e.length>2)for(var f=2,g=e.length;f<g;f++)e[1]=e[1]+"="+e[f];this[unescape(e[0]).toLowerCase()]=unescape(e[1])}}return true}else return false};
a.remove=function(){simpleCart.remove(this.id);simpleCart.update()};function Shelf(){this.items={}}Shelf.prototype.readPage=function(){this.items={};var c=getElementsByClassName("simpleCart_shelfItem");for(var b in c){var d=new ShelfItem;this.checkChildren(c[b],d);this.items[d.id]=d}};
Shelf.prototype.checkChildren=function(c,b){for(var d=0;c.childNodes[d];d++){var e=c.childNodes[d];if(e.className&&e.className.match(/item_[^ ]+/)){var f=/item_[^ ]+/.exec(e.className)[0].split("_");if(f[1]=="add"||f[1]=="Add"){f=[];f.push(e);var g=simpleCart.Shelf.addToCart(b.id);simpleCart.addEventToArray(f,g,"click");e.id=b.id}else b[f[1]]=e}e.childNodes[0]&&this.checkChildren(e,b)}};Shelf.prototype.empty=function(){this.items={}};
Shelf.prototype.addToCart=function(c){return function(){simpleCart.Shelf.items[c]?simpleCart.Shelf.items[c].addToCart():error("Shelf item with id of "+c+" does not exist.")}};function ShelfItem(){this.id="s"+NextId++}ShelfItem.prototype.remove=function(){simpleCart.Shelf.items[this.id]=null};
ShelfItem.prototype.addToCart=function(){var c=[],b;for(var d in this)if(typeof this[d]!="function"&&d!="id"){b="";switch(d){case "price":if(this[d].value)b=this[d].value;else if(this[d].innerHTML)b=this[d].innerHTML;b=b.replace(/[^(\d|\.)]*/gi,"");b=b.replace(/,*/,"");break;case "image":b=this[d].src;break;default:b=this[d].value?this[d].value:this[d].innerHTML?this[d].innerHTML:this[d].src?this[d].src:this[d];break}c.push(d+"="+b)}simpleCart.add(c)};
function createCookie(c,b,d){if(d){var e=new Date;e.setTime(e.getTime()+d*24*60*60*1E3);d="; expires="+e.toGMTString()}else d="";document.cookie=c+"="+b+d+"; path=/"}function readCookie(c){c=c+"=";for(var b=document.cookie.split(";"),d=0;d<b.length;d++){for(var e=b[d];e.charAt(0)==" ";)e=e.substring(1,e.length);if(e.indexOf(c)===0)return e.substring(c.length,e.length)}return null}function eraseCookie(c){createCookie(c,"",-1)}
var getElementsByClassName=function(c,b,d){getElementsByClassName=document.getElementsByClassName?function(e,f,g){g=g||document;e=g.getElementsByClassName(e);f=f?new RegExp("\\b"+f+"\\b","i"):null;g=[];for(var h,i=0,j=e.length;i<j;i+=1){h=e[i];if(!f||f.test(h.nodeName))g.push(h)}return g}:document.evaluate?function(e,f,g){f=f||"*";g=g||document;var h=e.split(" "),i="",j=document.documentElement.namespaceURI==="http://www.w3.org/1999/xhtml"?"http://www.w3.org/1999/xhtml":null;e=[];for(var k,l=0,m=
h.length;l<m;l+=1)i+="[contains(concat(' ', @class, ' '), ' "+h[l]+" ')]";try{k=document.evaluate(".//"+f+i,g,j,0,null)}catch(n){k=document.evaluate(".//"+f+i,g,null,0,null)}for(;f=k.iterateNext();)e.push(f);return e}:function(e,f,g){f=f||"*";g=g||document;var h=e.split(" ");e=[];f=f==="*"&&g.all?g.all:g.getElementsByTagName(f);g=[];var i;i=0;for(var j=h.length;i<j;i+=1)e.push(new RegExp("(^|\\s)"+h[i]+"(\\s|$)"));j=0;for(var k=f.length;j<k;j+=1){h=f[j];i=false;for(var l=0,m=e.length;l<m;l+=1){i=
e[l].test(h.className);if(!i)break}i&&g.push(h)}return g};return getElementsByClassName(c,b,d)};String.prototype.reverse=function(){return this.split("").reverse().join("")};Number.prototype.withCommas=function(){for(var c=6,b=parseFloat(this).toFixed(2).toString().reverse();c<b.length;){b=b.substring(0,c)+","+b.substring(c);c+=4}return b.reverse()};Number.prototype.toCurrency=function(c){return(c?c:"$")+this.withCommas()};function error(c){try{console.log(c)}catch(b){}}var simpleCart=new Cart;
if(typeof jQuery!=="undefined")$(document).ready(function(){simpleCart.initialize()});else if(typeof Prototype!=="undefined")Event.observe(window,"load",function(){simpleCart.initialize()});else window.onload=simpleCart.initialize;
©2009 Google - Terms of Service - Privacy Policy - Google Home