var Custom="Custom",GoogleCheckout="GoogleCheckout",PayPal="PayPal",Email="Email",AustralianDollar="AUD",AUD="AUD",CanadianDollar="CAD",CAD="CAD",CzechKoruna="CZK",CZK="CZK",DanishKrone="DKK",DKK="DKK",Euro="EUR",EUR="EUR",HongKongDollar="HKD",HKD="HKD",HungarianForint="HUF",HUF="HUF",IsraeliNewSheqel="ILS",ILS="ILS",JapaneseYen="JPY",JPY="JPY",MexicanPeso="MXN",MXN="MXN",NorwegianKrone="NOK",NOK="NOK",NewZealandDollar="NZD",NZD="NZD",PolishZloty="PLN",PLN="PLN",PoundSterling="GBP",GBP="GBP",SingaporeDollar="SGD",SGD="SGD",SwedishKrona="SEK",SEK="SEK",SwissFranc="CHF",CHF="CHF",ThaiBaht="THB",THB="THB",USDollar="USD",USD="USD",IDN="IDR"; function Cart(){ var me = this; /* member variables */ me.nextId = 1; me.Version = '2.2'; me.Shelf = null; me.items = {}; me.isLoaded = false; me.pageIsReady = false; me.quantity = 0; me.total = 1; me.taxRate = 0; me.taxCost = 0; me.shippingFlatRate = 0; me.shippingTotalRate = 0; me.shippingQuantityRate = 0; me.shippingRate = 0; me.shippingCost = 0; me.currency = IDN; me.checkoutTo = PayPal; me.email = ""; me.merchantId = ""; me.successUrl = null; me.cancelUrl = null; me.cookieDuration = 30; // default duration in days me.storagePrefix = "sc_"; me.MAX_COOKIE_SIZE = 4000; me.cartHeaders = ['Name','Price','Quantity','Total']; /* cart headers: you can set these to which ever order you would like, and the cart will display the appropriate headers and item info. any field you have for the items in the cart can be used, and 'Total' will automatically be price*quantity. there are keywords that can be used: 1) "_input" - the field will be a text input with the value set to the given field. when the user changes the value, it will update the cart. this can be useful for quantity. (ie "Quantity_input") 2) "increment" - a link with "+" that will increase the item quantity by 1 3) "decrement" - a link with "-" that will decrease the item quantity by 1 4) "remove" - a link that will remove the item from the cart 5) "_image" or "Image" - the field will be an img tag with the src set to the value. You can simply use "Image" if you set a field in the items called "Image". If you have a field named something else, like "Thumb", you can add the "_image" to create the image tag (ie "Thumb_image"). 6) "_noHeader" - this will skip the header for that field (ie "increment_noHeader") */ /****************************************************** add/remove items to cart ******************************************************/ me.add = function ( values ) { var me=this; /* load cart values if not already loaded */ if( !me.pageIsReady ) { me.initializeView(); me.update(); } if( !me.isLoaded ) { me.load(); me.update(); } var newItem = new CartItem(); /* check to ensure arguments have been passed in */ if( !arguments || arguments.length === 0 ){ error( 'No values passed for item.'); return null; } var argumentArray = arguments; if( values && typeof( values ) !== 'string' && typeof( values ) !== 'number' ){ argumentArray = values; } newItem.parseValuesFromArray( argumentArray ); newItem.checkQuantityAndPrice(); /* if the item already exists, update the quantity */ if( me.hasItem(newItem) ) { var foundItem=me.hasItem(newItem); foundItem.quantity= parseInt(foundItem.quantity,10) + parseInt(newItem.quantity,10); newItem = foundItem; } else { me.items[newItem.id] = newItem; } me.update(); return newItem; }; me.remove = function( id ){ var tempArray = {}; me.each(function(item){ if( item.id !== id ){ tempArray[item.id] = item; } }); this.items = tempArray; }; me.empty = function () { simpleCart.items = {}; simpleCart.update(); }; /****************************************************** item accessor functions ******************************************************/ me.find = function (criteria) { if( !criteria ){ return null; } var results = []; me.each(function(item,x,next){ fits = true; me.each( criteria , function(value,j,name){ if( !item[name] || item[name] != value ){ fits = false; } }); if( fits ){ results.push( item ); } }); return (results.length === 0 ) ? null : results; }; me.each = function( array , callback ){ var next, x=0, result; if( typeof array === 'function' ){ var cb = array items = me.items; } else if( typeof callback === 'function' ){ var cb = callback, items = array; } else { return; } for( next in items ){ if( typeof items[next] !== "function" ){ result = cb.call( me , items[next] , x , next ); if( result === false ){ return; } x++; } } }; me.chunk = function(str, n) { if (typeof n==='undefined'){ n=2; } var result = str.match(RegExp('.{1,'+n+'}','g')); return result || []; }; /****************************************************** checkout management ******************************************************/ me.checkout = function() { if( simpleCart.quantity === 0 ){ error("Cart is empty"); return; } switch( simpleCart.checkoutTo ){ case PayPal: simpleCart.paypalCheckout(); break; case GoogleCheckout: simpleCart.googleCheckout(); break; case Email: simpleCart.emailCheckout(); break; default: simpleCart.customCheckout(); break; } }; me.paypalCheckout = function() { var me = this, winpar = "scrollbars,location,resizable,status", strn = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" + "&upload=1" + "&business=" + me.email + "¤cy_code=" + me.currency, counter = 1, itemsString = "", current, item, optionsString, field; if( me.taxRate ){ strn = strn + "&tax_cart=" + me.currencyStringForPaypalCheckout( me.taxCost ); } me.each(function(item,iter){ counter = iter+1; optionsString = ""; me.each( item , function( value, x , field ){ if( field !== "id" && field !== "price" && field !== "quantity" && field !== "name" && field !== "shipping") { optionsString = optionsString + ", " + field + "=" + value ; } }); optionsString = optionsString.substring(2); itemsString = itemsString + "&item_name_" + counter + "=" + item.name + "&item_number_" + counter + "=" + counter + "&quantity_" + counter + "=" + item.quantity + "&amount_" + counter + "=" + me.currencyStringForPaypalCheckout( item.price ) + "&on0_" + counter + "=" + "Options" + "&os0_" + counter + "=" + optionsString; }); if( me.shipping() !== 0){ itemsString = itemsString + "&shipping=" + me.currencyStringForPaypalCheckout( me.shippingCost ); } if( me.successUrl ){ itemsString = itemsString + "&return=" + me.successUrl; } if( me.cancelUrl ){ itemsString = itemsString + "&cancel_return=" + me.cancelUrl; } strn = strn + itemsString ; window.open (strn, "paypal", winpar); }; me.googleCheckout = function() { var me = this; if( me.currency !== IDN && me.currency !== USD ){ error( "Google Checkout only allows the USD and IDR for currency."); return; } else if( me.merchantId === "" || me.merchantId === null || !me.merchantId ){ error( "No merchant Id for google checkout supplied."); return; } var form = document.createElement("form"), counter=1, current, item, descriptionString; form.style.display = "none"; form.method = "POST"; form.action = "https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/" + me.merchantId; form.acceptCharset = "utf-8"; me.each(function(item,iter){ counter = iter+1; form.appendChild( me.createHiddenElement( "item_name_" + counter, item.name ) ); form.appendChild( me.createHiddenElement( "item_quantity_" + counter, item.quantity ) ); form.appendChild( me.createHiddenElement( "item_price_" + counter, item.price ) ); form.appendChild( me.createHiddenElement( "item_currency_" + counter, me.currency ) ); form.appendChild( me.createHiddenElement( "item_tax_rate_" + counter, me.taxRate ) ); form.appendChild( me.createHiddenElement( "_charset_" , "" ) ); descriptionString = ""; me.each( item , function( value , x , field ){ if( field !== "id" && field !== "quantity" && field !== "price" ) { descriptionString = descriptionString + ", " + field + ": " + value; } }); descriptionString = descriptionString.substring( 1 ); form.appendChild( me.createHiddenElement( "item_description_" + counter, descriptionString) ); }); // hack for adding shipping if( me.shipping() !== 0){ form.appendChild(me.createHiddenElement("ship_method_name_1", "Shipping")); form.appendChild(me.createHiddenElement("ship_method_price_1", parseFloat(me.shippingCost).toFixed(2))); form.appendChild(me.createHiddenElement("ship_method_currency_1", me.currency)); } document.body.appendChild( form ); form.submit(); document.body.removeChild( form ); }; me.emailCheckout = function() { return; }; me.customCheckout = function() { return; }; /****************************************************** data storage and retrival ******************************************************/ /* load cart from cookie */ me.load = function () { var me = this, id; /* initialize variables and items array */ me.items = {}; me.total = 0; me.quantity = 0; /* retrieve item data from cookie */ if( readCookie(simpleCart.storagePrefix + 'simpleCart_' + "chunks") ){ var chunkCount = 1*readCookie(simpleCart.storagePrefix + 'simpleCart_' + "chunks"), dataArray = [], dataString = "", data = "", info, newItem, y=0; if(chunkCount>0) { for( y=0;y 0 ){ me.updateCartView(); } }; me.updateViewTotals = function() { var outlets = [ ["quantity" , "none" ] , ["total" , "currency" ] , ["shippingCost" , "currency" ] , ["taxCost" , "currency" ] , ["taxRate" , "percentage" ] , ["finalTotal" , "currency" ] ]; for( var x=0,xlen=outlets.length; x"; } }; me.valueToTextInput = function( value , html ){ return ""; }; me.valueToLink = function( value, link, html){ return "" + value + ""; }; me.valueToElement = function( type , value , html ){ return "<" + type + " " + html + " > " + value + ""; }; /****************************************************** Duplicate management ******************************************************/ me.hasItem = function ( item ) { var current, matches, field, match=false; me.each(function(testItem){ matches = true; me.each( item , function( value , x , field ){ if( field !== "quantity" && field !== "id" && item[field] !== testItem[field] ){ matches = false; } }); if( matches ){ match = testItem; } }); return match; }; /****************************************************** Language managment ******************************************************/ me.ln = { "en_us": { quantity: "Quantity" , price: "Price" , total: "Total" , decrement: "Decrement" , increment: "Increment" , remove: "Remove" , tax: "Tax" , shipping: "Shipping" , image: "Image" } }; me.language = "en_us"; me.print = function( input ) { var me = this; return me.ln[me.language] && me.ln[me.language][input.toLowerCase()] ? me.ln[me.language][input.toLowerCase()] : input; }; /****************************************************** Cart Update managment ******************************************************/ me.update = function() { if( !simpleCart.isLoaded ){ simpleCart.load(); } if( !simpleCart.pageIsReady ){ simpleCart.initializeView(); } me.updateTotals(); me.updateView(); me.save(); }; me.updateTotals = function() { me.total = 0 ; me.quantity = 0; me.each(function(item){ if( item.quantity < 1 ){ item.remove(); } else if( item.quantity !== null && item.quantity !== "undefined" ){ me.quantity = parseInt(me.quantity,10) + parseInt(item.quantity,10); } if( item.price ){ me.total = parseFloat(me.total) + parseInt(item.quantity,10)*parseFloat(item.price); } }); me.shippingCost = me.shipping(); me.taxCost = parseFloat(me.total)*me.taxRate; me.finalTotal = me.shippingCost + me.taxCost + me.total; }; me.shipping = function(){ if( parseInt(me.quantity,10)===0 ) return 0; var shipping = parseFloat(me.shippingFlatRate) + parseFloat(me.shippingTotalRate)*parseFloat(me.total) + parseFloat(me.shippingQuantityRate)*parseInt(me.quantity,10), next; me.each(function(nextItem){ if( nextItem.shipping ){ if( typeof nextItem.shipping == 'function' ){ shipping += parseFloat(nextItem.shipping()); } else { shipping += parseFloat(nextItem.shipping); } } }); return shipping; } me.initialize = function() { simpleCart.initializeView(); simpleCart.load(); simpleCart.update(); }; } /******************************************************************************************************** * Cart Item Object ********************************************************************************************************/ function CartItem() { while( simpleCart.items["c" + simpleCart.nextId] ) simpleCart.nextId++; this.id = "c" + simpleCart.nextId; } CartItem.prototype.set = function ( field , value ){ field = field.toLowerCase(); if( typeof( this[field] ) !== "function" && field !== "id" ){ if( field == "quantity" ){ value = value.replace( /[^(\d|\.)]*/gi , "" ); value = value.replace(/,*/gi, ""); value = parseInt(value,10); } else if( field == "price"){ value = value.replace( /[^(\d|\.)]*/gi, ""); value = value.replace(/,*/gi , ""); value = parseFloat( value ); } if( typeof(value) == "number" && isNaN( value ) ){ error( "Improperly formatted input."); } else { if( value.match(/\~|\=/) ){ error("Special character ~ or = not allowed: " + value); } value = value.replace(/\~|\=/g, ""); this[field] = value; this.checkQuantityAndPrice(); } } else { error( "Cannot change " + field + ", this is a reserved field."); } simpleCart.update(); }; CartItem.prototype.increment = function(){ this.quantity = parseInt(this.quantity,10) + 1; simpleCart.update(); }; CartItem.prototype.decrement = function(){ if( parseInt(this.quantity,10) < 2 ){ this.remove(); } else { this.quantity = parseInt(this.quantity,10) - 1; simpleCart.update(); } }; CartItem.prototype.print = function () { var returnString = '', field; simpleCart.each(this ,function(item,x,name){ returnString+= escape(name) + "=" + escape(item) + "||"; }); return returnString.substring(0,returnString.length-2); }; CartItem.prototype.checkQuantityAndPrice = function() { if( !this.quantity || 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; } } }; CartItem.prototype.parseValuesFromArray = function( array ) { if( array && array.length && array.length > 0) { for(var x=0, xlen=array.length; x1 ){ if( value.length>2 ){ for(var j=2, jlen=value.length;j