var 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(){ /* PUBLIC: */ /* member variables */ this.Version = '1.9.9'; this.Shelf = new Shelf(); this.items = new Array(); this.isLoaded = false; this.pageIsReady = false; this.quantity = 0; this.total = 0; this.taxRate = 0; this.taxCost = 0; this.shippingRate = 0; this.shippingCost = 0; this.currency = EUR; this.checkoutTo = Custom; this.email = "yadtampons@free.fr"; this.merchantId = ""; this.cartHeaders = ['Name','Price','Quantity','Total']; /****************************************************** add/remove items to cart ******************************************************/ this.add = function () { /* load cart values if not already loaded */ if( !this.pageIsReady ) { this.initializeView(); this.update(); } if( !this.isLoaded ) { this.load(); this.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; } if( arguments[0] && typeof( arguments[0] ) != 'string' && typeof( arguments[0] ) != 'number' ){ arguments = arguments[0]; } newItem.parseValuesFromArray( arguments ); newItem.checkQuantityAndPrice(); /* if the item already exists, update the quantity */ if( this.hasItem(newItem) ) { var id=this.hasItem(newItem); this.items[id].quantity= parseInt(this.items[id].quantity) + parseInt(newItem.quantity); } else { this.items[newItem.id] = newItem; } this.update(); } this.remove = function( id ){ var tempArray = new Array(); for( var item in this.items ){ if( item != id ){ tempArray[item] = this.items[item]; } } this.items = tempArray; } this.empty = function () { simpleCart.items = new Array(); simpleCart.update(); } /****************************************************** checkout management ******************************************************/ this.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; } } this.paypalCheckout = function() { var winpar = "scrollbars,location,resizable,status", strn = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" + "&upload=1" + "&business=" + this.email + "¤cy_code=" + this.currency, counter = 1, itemsString = ""; for( var current in this.items ){ var item = this.items[current]; var optionsString = ""; for( var field in item ){ if( typeof(item[field]) != "function" && field != "id" && field != "price" && field != "quantity" && field != "name" ) { optionsString = optionsString + "&" + field + "=" + item[field] ; } } optionsString = optionsString.substring(1); itemsString = itemsString + "&item_name_" + counter + "=" + item["name"] + "&item_number_" + counter + "=" + counter + "&quantity_" + counter + "=" + item.quantity + "&amount_" + counter + "=" + this.currencyStringForPaypalCheckout( item.price ) + "&on0_" + counter + "=" + "Options" + "&os0_" + counter + "=" + optionsString; counter++; } strn = strn + itemsString ; window.open (strn, "paypal", winpar); } this.googleCheckout = function() { if( this.currency != USD && this.currency != GBP ){ error( "Google Checkout only allows the USD and GBP for currency."); return; } else if( this.merchantId == "" || this.merchantId == null ){ error( "Google Checkout only allows the USD and GBP for currency"); return; } var form = document.createElement("form"), counter = 1; form.style.display = "none"; form.method = "POST"; form.action = "https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/" + this.merchantId; form.acceptCharset = "utf-8"; for( var current in this.items ){ var item = this.items[current]; form.appendChild( this.createHiddenElement( "item_name_" + counter, item["name"] ) ); form.appendChild( this.createHiddenElement( "item_quantity_" + counter, item["quantity"] ) ); form.appendChild( this.createHiddenElement( "item_price_" + counter, item["price"] ) ); form.appendChild( this.createHiddenElement( "item_currency_" + counter, this.currency ) ); form.appendChild( this.createHiddenElement( "item_tax_rate_" + counter, this.taxRate ) ); form.appendChild( this.createHiddenElement( "_charset_" , "" ) ); var descriptionString = ""; for( var field in item){ if( typeof( item[field] ) != "function" && field != "id" && field != "quantity" && field != "price" ) { descriptionString = descriptionString + ", " + field + ": " + item[field]; } } descriptionString = descriptionString.substring( 1 ); form.appendChild( this.createHiddenElement( "item_description_" + counter, descriptionString) ); } document.body.appendChild( form ); form.submit(); document.body.removeChild( form ); } this.emailCheckout = function() { itemsString = ""; for( var current in me.items ){ var item = me.items[current]; itemsString += item.name + " " + item.quantity + " " + item.price + "\n"; } var form = document.createElement("form"); form.style.display = "none"; form.method = "POST"; form.action = "envoyer.php"; form.acceptCharset = "utf-8"; form.appendChild(me.createHiddenElement("jcitems", itemsString)); form.appendChild(me.createHiddenElement("jctotal", me.total)); document.body.appendChild(form); form.submit(); document.body.removeChild(form); } this.customCheckout = function() { return; } /* PRIVATE : */ /****************************************************** data storage and retrival ******************************************************/ /* load cart from cookie */ this.load = function () { /* initialize variables and items array */ this.items = new Array(); this.total = 0.00; this.quantity = 0; /* retrieve item data from cookie */ if( readCookie('simpleCart') ){ var data = unescape(readCookie('simpleCart')).split('++'); for(var x=0, xlen=data.length;x 0 ){ this.updateCartView(); } } this.updateViewTotals = function() { var outlets = [ ["quantity" , "none" ] , ["total" , "currency" ] , ["shippingRate" , "percentage" ] , ["shippingCost" , "currency" ] , ["taxCost" , "currency" ] , ["taxRate" , "percentage" ] , ["finalTotal" , "currency" ] ]; for( var x=0,xlen=outlets.length; x 6 ){ var newCurrencyString = ""; for( var x=currencyString.length-1, placeValueCounter=1; x>=0;x--,placeValueCounter++){ newCurrencyString = newCurrencyString + currencyString.substr(x,1); if( placeValueCounter == 3 ){ if( x 0 ) { newCurrencyString = newCurrencyString + ","; } placeValueCounter = 0; } } currencyString = newCurrencyString.reverse(); } return this.currencySymbol() + currencyString; } this.valueToPercentageString = function( value ){ return parseFloat( 100*value ).toFixed(0) + "%" } this.valueToImageString = function( value ){ if( value.match(/<\s*img.*src\=/) ){ return value; } else { return ""; } } this.valueToTextInput = function( value , html ){ return ""; } this.valueToLink = function( value, link, html){ return "" + value + ""; } /****************************************************** Duplicate management ******************************************************/ this.hasItem = function ( item ) { for( var current in this.items ) { var testItem = this.items[current]; var matches = true; for( var field in item ){ if( typeof( item[field] ) != "function" && field != "quantity" && field != "id" ){ if( item[field] != testItem[field] ){ matches = false; } } } if( matches ){ return current; } } return false; } /****************************************************** Cart Update managment ******************************************************/ this.update = function() { if( !simpleCart.isLoaded ){ simpleCart.load(); } if( !simpleCart.pageIsReady ){ simpleCart.initializeView(); } this.updateTotals(); this.updateView(); this.save(); } this.updateTotals = function() { this.total = 0 ; this.quantity = 0; for( var current in this.items ){ var item = this.items[current]; if( item["quantity"] < 1 ){ item.remove(); } else if( item["quantity"] != null && item["quantity"] != "undefined" ){ this.quantity = parseInt(this.quantity) + parseInt(item["quantity"]); } if( item["price"] ){ this.total = parseFloat(this.total) + parseInt(item["quantity"])*parseFloat(item["price"]); } } this.shippingCost = parseFloat(this.total)*this.shippingRate; this.taxCost = parseFloat(this.total)*this.taxRate; this.finalTotal = this.shippingCost + this.taxCost + this.total; } this.initialize = function() { simpleCart.initializeView(); simpleCart.load(); simpleCart.update(); } } /******************************************************************************************************** * Cart Item Object ********************************************************************************************************/ function CartItem() { this.id = "c" + NextId++; this.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 { this[field] = value; this.checkQuantityAndPrice(); } } else { error( "Cannot change " + field + ", this is a reserved field."); } simpleCart.update(); } this.increment = function(){ this.quantity = parseInt(this.quantity) + 1; simpleCart.update(); } this.decrement = function(){ if( parseInt(this.quanity) < 2 ){ this.remove(); } else { this.quantity = parseInt(this.quantity) - 1; simpleCart.update(); } } this.print = function () { var returnString = ''; for( var field in this ) { if( typeof( this[field] ) != "function" ) { returnString+= escape(field) + "=" + escape(this[field]) + "||"; } } return returnString.substring(0,returnString.length-2); } this.checkQuantityAndPrice = function() { if( 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, "") ); if( isNaN(this.quantity) ){ error('Quantity is not a number.'); this.quantity = 1; } } if( this.price == null || this.price == 'undefined'){ this.price=0.00; error('No price for item or price not properly formatted.'); } else { this.price = ("" + this.price).replace(/,*/gi, "" ); this.price = +( ("" + this.price).replace( /[^(\d|\.)]*/gi, "") ); if( isNaN(this.price) ){ error('Price is not a number.'); this.price = 0.00; } } } this.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