forked from wojodesign/simplecart-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimpleCart.js
1031 lines (854 loc) · 29.8 KB
/
simpleCart.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
Copyright (c) 2009 The Wojo Group
thewojogroup.com
simplecartjs.com
http://github.com/thewojogroup/simplecart-js/tree/master
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
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(){
/* member variables */
this.Version = '2.0';
this.Shelf = new Shelf();
this.items = {};
this.isLoaded = false;
this.pageIsReady = false;
this.quantity = 0;
this.total = 0;
this.taxRate = 0;
this.taxCost = 0;
this.shippingFlatRate = 0;
this.shippingTotalRate = 0;
this.shippingQuantityRate = 0;
this.shippingRate = 0;
this.shippingCost = 0;
this.currency = USD;
this.checkoutTo = PayPal;
this.email = "";
this.merchantId = "";
this.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
******************************************************/
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;
}
var argumentArray = arguments;
if( arguments[0] && typeof( arguments[0] ) != 'string' && typeof( arguments[0] ) != 'number' ){
argumentArray = arguments[0];
}
newItem.parseValuesFromArray( argumentArray );
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,10) + parseInt(newItem.quantity,10);
} else {
this.items[newItem.id] = newItem;
}
this.update();
};
this.remove = function( id ){
var tempArray = {};
for( var item in this.items ){
if( item != id ){
tempArray[item] = this.items[item];
}
}
this.items = tempArray;
};
this.empty = function () {
simpleCart.items = {};
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 = "";
if( this.taxRate ){
strn = strn +
"&tax_cart=" + this.currencyStringForPaypalCheckout( this.taxCost );
}
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" /*&& field != "shipping"*/) {
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++;
}
if( this.shipping() != 0){
itemsString = itemsString + "&item_name_" + counter + "=Shipping" +
"&item_number_" + counter + "=" + counter +
"&quantity_" + counter + "=1" +
"&amount_" + counter + "=" + this.currencyStringForPaypalCheckout( this.shippingCost );
}
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 || !this.merchantId ){
error( "No merchant Id for google checkout supplied.");
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() {
return;
};
this.customCheckout = function() {
return;
};
/******************************************************
data storage and retrival
******************************************************/
/* load cart from cookie */
this.load = function () {
/* initialize variables and items array */
this.items = {};
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<xlen;x++){
var info = data[x].split('||');
var newItem = new CartItem();
if( newItem.parseValuesFromArray( info ) ){
newItem.checkQuantityAndPrice();
/* store the new item in the cart */
this.items[newItem.id] = newItem;
}
}
}
this.isLoaded = true;
};
/* save cart to cookie */
this.save = function () {
var dataString = "";
for( var item in this.items ){
dataString = dataString + "++" + this.items[item].print();
}
createCookie('simpleCart', dataString.substring( 2 ), 30 );
};
/******************************************************
view management
******************************************************/
this.initializeView = function() {
this.totalOutlets = getElementsByClassName('simpleCart_total');
this.quantityOutlets = getElementsByClassName('simpleCart_quantity');
this.cartDivs = getElementsByClassName('simpleCart_items');
this.taxCostOutlets = getElementsByClassName('simpleCart_taxCost');
this.taxRateOutlets = getElementsByClassName('simpleCart_taxRate');
this.shippingCostOutlets = getElementsByClassName('simpleCart_shippingCost');
this.finalTotalOutlets = getElementsByClassName('simpleCart_finalTotal');
this.addEventToArray( getElementsByClassName('simpleCart_checkout') , simpleCart.checkout , "click");
this.addEventToArray( getElementsByClassName('simpleCart_empty') , simpleCart.empty , "click" );
this.Shelf.readPage();
this.pageIsReady = true;
};
this.updateView = function() {
this.updateViewTotals();
if( this.cartDivs && this.cartDivs.length > 0 ){
this.updateCartView();
}
};
this.updateViewTotals = function() {
var outlets = [ ["quantity" , "none" ] ,
["total" , "currency" ] ,
["shippingCost" , "currency" ] ,
["taxCost" , "currency" ] ,
["taxRate" , "percentage" ] ,
["finalTotal" , "currency" ] ];
for( var x=0,xlen=outlets.length; x<xlen;x++){
var arrayName = outlets[x][0] + "Outlets",
outputString;
for( var element in this[ arrayName ] ){
switch( outlets[x][1] ){
case "none":
outputString = "" + this[outlets[x][0]];
break;
case "currency":
outputString = this.valueToCurrencyString( this[outlets[x][0]] );
break;
case "percentage":
outputString = this.valueToPercentageString( this[outlets[x][0]] );
break;
default:
outputString = "" + this[outlets[x][0]];
break;
}
this[arrayName][element].innerHTML = "" + outputString;
}
}
};
this.updateCartView = function() {
var newRows = [],
x,newRow,item,current,header,newCell,info,outputValue,option,headerInfo;
/* create headers row */
newRow = document.createElement('div');
for( header in this.cartHeaders ){
newCell = document.createElement('div');
headerInfo = this.cartHeaders[header].split("_");
newCell.innerHTML = headerInfo[0];
newCell.className = "item" + headerInfo[0];
for(x=1,xlen=headerInfo.length;x<xlen;x++){
if( headerInfo[x].toLowerCase() == "noheader" ){
newCell.style.display = "none";
}
}
newRow.appendChild( newCell );
}
newRow.className = "cartHeaders";
newRows[0] = newRow;
/* create a row for each item in the cart */
x=1;
for( current in this.items ){
newRow = document.createElement('div');
item = this.items[current];
for( header in this.cartHeaders ){
newCell = document.createElement('div');
info = this.cartHeaders[header].split("_");
switch( info[0].toLowerCase() ){
case "total":
outputValue = this.valueToCurrencyString(parseFloat(item.price)*parseInt(item.quantity,10) );
break;
case "increment":
outputValue = this.valueToLink( "+" , "javascript:;" , "onclick=\"simpleCart.items[\'" + item.id + "\'].increment();\"" );
break;
case "decrement":
outputValue = this.valueToLink( "-" , "javascript:;" , "onclick=\"simpleCart.items[\'" + item.id + "\'].decrement();\"" );
break;
case "remove":
outputValue = this.valueToLink( "Remove" , "javascript:;" , "onclick=\"simpleCart.items[\'" + item.id + "\'].remove();\"" );
break;
case "price":
outputValue = this.valueToCurrencyString( item[ info[0].toLowerCase() ] ? item[info[0].toLowerCase()] : " " );
break;
default:
outputValue = item[ info[0].toLowerCase() ] ? item[info[0].toLowerCase()] : " ";
break;
}
for( var y=1,ylen=info.length;y<ylen;y++){
option = info[y].toLowerCase();
switch( option ){
case "image":
case "img":
outputValue = this.valueToImageString( outputValue );
break;
case "input":
outputValue = this.valueToTextInput( outputValue , "onchange=\"simpleCart.items[\'" + item.id + "\'].set(\'" + outputValue + "\' , this.value);\"" );
break;
case "div":
case "span":
case "h1":
case "h2":
case "h3":
case "h4":
case "p":
outputValue = this.valueToElement( option , outputValue , "" );
break;
case "noheader":
break;
default:
error( "unkown header option: " + option );
break;
}
}
newCell.innerHTML = outputValue;
newCell.className = "item" + info[0];
newRow.appendChild( newCell );
}
newRow.className = "itemContainer";
newRows[x] = newRow;
x++;
}
for( current in this.cartDivs ){
/* delete current rows in div */
var div = this.cartDivs[current];
while( div.childNodes[0] ){
div.removeChild( div.childNodes[0] );
}
for(var j=0, jLen = newRows.length; j<jLen; j++){
div.appendChild( newRows[j] );
}
}
};
this.addEventToArray = function ( array , functionCall , theEvent ) {
for( var outlet in array ){
var element = array[outlet];
if( element.addEventListener ) {
element.addEventListener(theEvent, functionCall , false );
} else if( element.attachEvent ) {
element.attachEvent( "on" + theEvent, functionCall );
}
}
};
this.createHiddenElement = function ( name , value ){
var element = document.createElement("input");
element.type = "hidden";
element.name = name;
element.value = value;
return element;
};
/******************************************************
Currency management
******************************************************/
this.currencySymbol = function() {
switch(this.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 "";
}
};
this.currencyStringForPaypalCheckout = function( value ){
if( this.currencySymbol == "$" ){
return "$" + parseFloat( value ).toFixed(2);
} else {
return "" + parseFloat(value ).toFixed(2);
}
};
/******************************************************
Formatting
******************************************************/
this.valueToCurrencyString = function( value ) {
return parseFloat( value ).toCurrency( this.currencySymbol() );
};
this.valueToPercentageString = function( value ){
return parseFloat( 100*value ).toFixed(0) + "%";
};
this.valueToImageString = function( value ){
if( value.match(/<\s*img.*src\=/) ){
return value;
} else {
return "<img src=\"" + value + "\" />";
}
};
this.valueToTextInput = function( value , html ){
return "<input type=\"text\" value=\"" + value + "\" " + html + " />";
};
this.valueToLink = function( value, link, html){
return "<a href=\"" + link + "\" " + html + " >" + value + "</a>";
};
this.valueToElement = function( type , value , html ){
return "<" + type + " " + html + " > " + value + "</" + type + ">";
};
/******************************************************
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,10) + parseInt(item.quantity,10);
}
if( item.price ){
this.total = parseFloat(this.total) + parseInt(item.quantity,10)*parseFloat(item.price);
}
}
this.shippingCost = this.shipping();
this.taxCost = parseFloat(this.total)*this.taxRate;
this.finalTotal = this.shippingCost + this.taxCost + this.total;
};
this.shipping = function(){
if( parseInt(this.quantity,10)===0 )
return 0;
var shipping = parseFloat(this.shippingFlatRate) +
parseFloat(this.shippingTotalRate)*parseFloat(this.total) +
parseFloat(this.shippingQuantityRate)*parseInt(this.quantity,10),
nextItem,
next;
for(next in this.items){
nextItem = this.items[next];
if( nextItem.shipping ){
if( typeof nextItem.shipping == 'function' ){
shipping += parseFloat(nextItem.shipping());
} else {
shipping += parseFloat(nextItem.shipping);
}
}
}
return shipping;
}
this.initialize = function() {
simpleCart.initializeView();
simpleCart.load();
simpleCart.update();
};
}
/********************************************************************************************************
* Cart Item Object
********************************************************************************************************/
function CartItem() {
this.id = "c" + 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 {
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.quanity,10) < 2 ){
this.remove();
} else {
this.quantity = parseInt(this.quantity,10) - 1;
simpleCart.update();
}
};
CartItem.prototype.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);
};
CartItem.prototype.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.00;
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.00;
}
}
};
CartItem.prototype.parseValuesFromArray = function( array ) {
if( array && array.length && array.length > 0) {
for(var x=0, xlen=array.length; x<xlen;x++ ){
/* ensure the pair does not have key delimeters */
array[x].replace(/||/, "| |");
array[x].replace(/\+\+/, "+ +");
/* split the pair and save the unescaped values to the item */
var value = array[x].split('=');
if( value.length>1 ){
if( value.length>2 ){
for(var j=2, jlen=value.length;j<jlen;j++){
value[1] = value[1] + "=" + value[j];
}
}
this[ unescape(value[0]).toLowerCase() ] = unescape(value[1]);
}
}
return true;
} else {
return false;
}
};
CartItem.prototype.remove = function() {
simpleCart.remove(this.id);
simpleCart.update();
};
/********************************************************************************************************
* Shelf Object for managing items on shelf that can be added to cart
********************************************************************************************************/
function Shelf(){
this.items = {};
}
Shelf.prototype.readPage = function () {
this.items = {};
var newItems = getElementsByClassName( "simpleCart_shelfItem" );
for( var current in newItems ){
var newItem = new ShelfItem();
this.checkChildren( newItems[current] , newItem );
this.items[newItem.id] = newItem;
}
};
Shelf.prototype.checkChildren = function ( item , newItem) {
for(var x=0;item.childNodes[x];x++){
var node = item.childNodes[x];
if( node.className && node.className.match(/item_/) ){
var data=node.className.split('_');
if( data[1] == "add" || data[1] == "Add" ){
var tempArray = [];
tempArray.push( node );
var addFunction = simpleCart.Shelf.addToCart(newItem.id);
simpleCart.addEventToArray( tempArray , addFunction , "click");
node.id = newItem.id;
} else {
newItem[data[1]] = node;
}
}
if( node.childNodes[0] ){
this.checkChildren( node , newItem );
}
}
};
Shelf.prototype.empty = function () {
this.items = {};
};
Shelf.prototype.addToCart = function ( id ) {
return function(){
if( simpleCart.Shelf.items[id]){
simpleCart.Shelf.items[id].addToCart();
} else {
error( "Shelf item with id of " + id + " does not exist.");
}
};
};
/********************************************************************************************************
* Shelf Item Object
********************************************************************************************************/
function ShelfItem(){
this.id = "s" + NextId++;
}
ShelfItem.prototype.remove = function () {
simpleCart.Shelf.items[this.id] = null;
};
ShelfItem.prototype.addToCart = function () {
var outStrings = [],valueString;
for( var field in this ){
if( typeof( this[field] ) != "function" && field != "id" ){
valueString = "";
switch(field){
case "price":
if( this[field].value ){
valueString = this[field].value;
} else if( this[field].innerHTML ) {
valueString = this[field].innerHTML;
}
/* remove all characters from price except digits and a period */
valueString = valueString.replace( /[^(\d|\.)]*/gi , "" );
valueString = valueString.replace( /,*/ , "" );
break;
case "image":
valueString = this[field].src;
break;
default:
if( this[field].value ){
valueString = this[field].value;
} else if( this[field].innerHTML ) {
valueString = this[field].innerHTML;
} else if( this[field].src ){
valueString = this[field].src;
} else {
valueString = this[field];
}
break;
}
outStrings.push( field + "=" + valueString );
}
}
simpleCart.add( outStrings );
};
/********************************************************************************************************
* Thanks to Peter-Paul Koch for these cookie functions (http://www.quirksmode.org/js/cookies.html)
********************************************************************************************************/
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
//*************************************************************************************************
/*
Developed by Robert Nyman, http://www.robertnyman.com
Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
if (document.getElementsByClassName) {
getElementsByClassName = function (className, tag, elm) {
elm = elm || document;
var elements = elm.getElementsByClassName(className),
nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
returnElements = [],
current;
for(var i=0, il=elements.length; i<il; i+=1){
current = elements[i];
if(!nodeName || nodeName.test(current.nodeName)) {
returnElements.push(current);
}
}
return returnElements;
};
}
else if (document.evaluate) {
getElementsByClassName = function (className, tag, elm) {
tag = tag || "*";
elm = elm || document;
var classes = className.split(" "),
classesToCheck = "",
xhtmlNamespace = "http://www.w3.org/1999/xhtml",
namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
returnElements = [],
elements,
node;
for(var j=0, jl=classes.length; j<jl; j+=1){
classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
}
try {
elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
}
catch (e) {
elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
}
while ((node = elements.iterateNext())) {
returnElements.push(node);
}
return returnElements;
};
}
else {
getElementsByClassName = function (className, tag, elm) {
tag = tag || "*";
elm = elm || document;
var classes = className.split(" "),
classesToCheck = [],
elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
current,
returnElements = [],
match;
for(var k=0, kl=classes.length; k<kl; k+=1){
classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
}
for(var l=0, ll=elements.length; l<ll; l+=1){
current = elements[l];
match = false;
for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
match = classesToCheck[m].test(current.className);
if (!match) {
break;
}
}
if (match) {
returnElements.push(current);
}
}
return returnElements;
};