﻿Type.registerNamespace("PhotoSite.Framework");PhotoSite.Framework.ShoppingCart=function(){PhotoSite.Framework.ShoppingCart.initializeBase(this);};PhotoSite.Framework.ShoppingCart.prototype={initialize:function(){this._cartItems=new Array();this._syncCartItemDelegate=Function.createDelegate(this,this._syncCartItem);this._syncCartItemCompleteDelegate=Function.createDelegate(this,this._syncCartItemComplete);this._syncSubItemCompleteDelegate=Function.createDelegate(this,this._syncSubItemComplete);this._gotSuccessDelegate=Function.createDelegate(this,this._gotSuccess);this._gotQuickSetSuccessDelegate=Function.createDelegate(this,this._gotQuickSetSuccess);this._gotEmptyCartSuccessDelegate=Function.createDelegate(this,this._gotEmptyCartSuccess);this._gotErrorDelegate=Function.createDelegate(this,this._gotError);this._updateQuickSetCartItemsDelegate=Function.createDelegate(this,this._updateQuickSetCartItems);this._syncManager=new PhotoSite.Framework.ShoppingCartSyncManager(this._encUserID);this._syncManager.initialize();this._syncManager.add_syncCartItem(this._syncCartItemDelegate);this._syncManager.add_syncSubItemSuccess(this._syncSubItemCompleteDelegate);this._syncManager.add_syncCartItemSuccess(this._syncCartItemCompleteDelegate);this._syncManager.add_syncError(this._gotErrorDelegate);PhotoSite.Framework.ShoppingCart.callBaseMethod(this,"initialize",[this]);},dispose:function(){PhotoSite.Framework.ShoppingCart.callBaseMethod(this,"dispose");},_cartItems:null,_itemCounts:null,_gotQuickSetSuccessDelegate:null,_gotEmptyCartSuccessDelegate:null,_gotSuccessDelegate:null,_gotErrorDelegate:null,_syncCartItemDelegate:null,_syncCartItemCompleteDelegate:null,_syncSubItemCompleteDelegate:null,_updateQuickSetCartItemsDelegate:null,_syncErrorDelegate:null,_syncManager:null,_renderBatchHandler:null,_quickSetServerDone:false,_quickSetClientDone:false,_resetSelectedOption:function(B,C){var A=C.SelectedOptionValueID;for(var D=0;D<C.Values.length;D++){if(C.Values[D].EncOptionValueID==A){B.EncOptionValueID=C.Values[D].EncOptionValueID;B.Name=C.Values[D].Name;break;}}},_syncCartItem:function(A,C){if(C.ProductType==="Prints"){var B=this.getPrintSizesInfo();for(var D=0;D<C.SubItems.length;D++){var F=B[D];var E=C.SubItems[D];if(E.Quantity==0){if(E.PrintBorder){this._resetSelectedOption(E.PrintBorder,F.BorderOption);}if(E.PrintSurface){this._resetSelectedOption(E.PrintSurface,F.SurfaceOption);}}}}this.raiseEvent("syncCartItem",C);},_syncCartItemComplete:function(A,B){if(B.ProductType==="Prints"){for(var C=0;C<B.SubItems.length;C++){var D=B.SubItems[C].EncID;if(D&&D!=""){B.EncID=D;break;}}}this.raiseEvent("syncCartItemComplete",B);},_syncSubItemComplete:function(A,D){var B=D.CartItem;var C=D.SubItem;this.raiseEvent("syncSubItemComplete",D);},_gotQuickSetSuccess:function(A){this._quickSetServerDone=true;this._evalQuickSetDone();},_evalQuickSetDone:function(){if(this._quickSetServerDone==true&&this._quickSetClientDone==true){this._syncManager.set_allowSyncToServer(true);this.raiseEvent("updateQuickSetComplete",null);}},_gotEmptyCartSuccess:function(A){this.raiseEvent("emptyCartComplete",null);},_gotSuccess:function(A){},_gotError:function(C,B,A){this.raiseEvent("serviceError",{Error:C,Context:B,Method:A});},_processCartItems:function(A){for(var B=0;B<A.length;B++){var C=A[B];this._cartItems.push(C);}if(!this._renderBatchHandler){this._renderBatchHandler=this.get_events().getHandler("renderCartItemBatch");}if(this._renderBatchHandler){this._renderBatchHandler(this,A);}else{for(var B=0;B<A.length;B++){this.raiseEvent("renderCartItem",A[B]);}}if(this._cartItems.length==this._itemCounts.TotalCount){PhotoSite.Framework.ICartService.CartLoadComplete(this._encUserID,this._gotSuccessDelegate,this._gotErrorDelegate);this.raiseEvent("renderComplete",null);}},_getRemainingCartItems:function(){if(this._batchSize<=0){throw"invalid batch size ("+this._batchSize+")";}var A=this._cartItems.length;var B=Function.createDelegate(this,this._gotCartItems);while(A<this._itemCounts.TotalCount){PhotoSite.Framework.ICartService.GetCartItems(this._encUserID,A,(A+this._batchSize),B,this._gotErrorDelegate);A+=this._batchSize;}},_gotCartItems:function(A){this._processCartItems(A);},_gotCartItemCounts:function(A){this._itemCounts=A;this.raiseEvent("gotCartItemCounts",null);if(A.TotalCount>0){this._processCartItems(A.InitialCartItems);if(A.TotalCount>A.InitialCartItems.length){this._getRemainingCartItems();}}else{this.raiseEvent("renderComplete",null);}},_subProductTypeCount:function(A){for(var B=0;B<this._itemCounts.ProductTypeCounts.length;B++){if(this._itemCounts.ProductTypeCounts[B].Key==A){this._itemCounts.ProductTypeCounts[B].Value=parseInt(this._itemCounts.ProductTypeCounts[B].Value)-1;break;}}},_getPrintSizeIndexByRetailerProductID:function(B){var A=this._itemCounts.PrintSizes;for(var C=0;C<A.length;C++){if(A[C].EncRetailerProductID==B){return C;}}return null;},_getSelectedOptionValueByOption:function(A){for(var B=0;B<A.Values.length;B++){if(A.Values[B].EncOptionValueID==A.SelectedOptionValueID){return A.Values[B];}}return null;},_getPrintUpdateBatchForQuickset:function(D){var I=new Array();var G=this._itemCounts.PrintSizes;for(var A=0;A<D.length;A++){var F=new Object();F.Index=this._getPrintSizeIndexByRetailerProductID(D[A].EncRetailerProductID);F.Quantity=D[A].Quantity;I.push(F);if(!D[A].Options){continue;}var H=G[F.Index];if(D[A].Quantity==0){}for(var B=0;B<D[A].Options.length;B++){var C=D[A].Options[B];if(H.BorderOption&&H.BorderOption.EncOptionID==C.EncOptionID){var E=this._getSelectedOptionValueByOption(H.BorderOption);F.BorderOption=new Object();F.BorderOption.EncOptionValueID=E.EncOptionValueID;F.BorderOption.Name=E.Name;}else{if(H.SurfaceOption.EncOptionID==C.EncOptionID){var E=this._getSelectedOptionValueByOption(H.SurfaceOption);F.SurfaceOption=new Object();F.SurfaceOption.EncOptionValueID=E.EncOptionValueID;F.SurfaceOption.Name=E.Name;}}}}return I;},_updateQuickSetCartItems:function(H,D,K){var G=this._cartItems.length;var J=H+30;J=Math.min(J,G);while(H<J){var M=this._cartItems[H];var L=M.GroupID;var N=false;var C=false;if(M.ProductType!="Prints"){H++;continue;}K.onBeginEditCartItem(this,M);for(var A=0;A<D.length;A++){var I=D[A];var F=M.SubItems[I.Index];var O=F.EncRetailerProductID;if((I.Quantity||I.Quantity==0)&&I.Quantity!=F.Quantity){if(F.Quantity==0){C=true;}if(I.Quantity==0){F.EncID="";}F.Quantity=I.Quantity;N=true;K.onSubItemQuantityChanged(this,M,F,I.Index,C||I.Quantity==0);}if(I.BorderOption&&I.BorderOption.EncOptionValueID!=F.PrintBorder.EncOptionValueID){F.PrintBorder.EncOptionValueID=I.BorderOption.EncOptionValueID;F.PrintBorder.Name=I.BorderOption.Name;K.onSubItemBorderChanged(this,M,F);}if(I.SurfaceOption){F.PrintSurface.EncOptionValueID=I.SurfaceOption.EncOptionValueID;F.PrintSurface.Name=I.SurfaceOption.Name;K.onSubItemSurfaceChanged(this,M,F);}}if(C){this._syncManager.add_groupIDToSyncAllSubItems(L);}var E=false;for(var A=0;A<M.SubItems.length;A++){if(M.SubItems[A].Quantity>0){E=true;break;}}if(E==false){this._removeCartItem(M);J--;}else{H++;}K.onEndEditCartItem(this,M,!E,N);}if(H<this._cartItems.length){var B=this._updateQuickSetCartItemsDelegate;setTimeout(function(P,Q){return function(){B(P,Q,K);};}(H,D),1);}else{this._quickSetClientDone=true;this._evalQuickSetDone();}},_removeCartItem:function(A){this._subProductTypeCount(A.ProductType);return Array.remove(this._cartItems,A);},_groupIDFromCartItemOrGroupObject:function(A){if(typeof(A)==="object"){return A.GroupID;}else{return A;}},_isCartItemOrSubItemPropertySet:function(D){for(var C=0;C<this._cartItems.length;C++){var A=this._cartItems[C];if(A.Quantity&&A[D]){return true;}if(A.SubItems){for(var B=0;B<A.SubItems.length;B++){var E=A.SubItems[B];if(E.Quantity&&E[D]){return true;}}}}return false;},load:function(){PhotoSite.Framework.ICartService.GetCartItemCounts(this._encUserID,Function.createDelegate(this,this._gotCartItemCounts),this._gotErrorDelegate);this._renderBatchHandler=null;},deleteCartItem:function(A){this.deleteCartItemByGroupID(A.GroupID);},deleteCartItemByEncID:function(B){for(var A=0;A<this._cartItems.length;A++){if(this._cartItems[A].EncID==B){this.deleteCartItemByGroupID(this._cartItems[A].GroupID);return;}}},deleteCartItemByGroupID:function(A){var B=this.getCartItemByGroupID(A);this._syncManager.removeCartItem(B);PhotoSite.Framework.ICartService.DeleteOrderItemGroup(this._encUserID,A,this._gotSuccessDelegate,this._gotErrorDelegate);this._subProductTypeCount(B.ProductType);Array.remove(this._cartItems,B);},getPrintSizesInfo:function(){return this._itemCounts.PrintSizes;},getMaxMBForCD:function(){return this._itemCounts.MaxMBForCD;},getEditableProductOptionInfoByGroupID:function(B){var C=this.getCartItemByGroupID(B);var A=this._itemCounts.EditableProductOptionInfo;if(!A||!C.EncRetailerProductID){return null;}for(var D=0;D<A.length;D++){if(A[D].EncRetailerProductID==C.EncRetailerProductID){return A[D];}}return null;},getPrintSizeInfoByRetailerProductID:function(B){var A=this.getPrintSizesInfo();for(var C=0;C<A.length;C++){if(A[C].EncRetailerProductID==B){return A[C];}}return null;},emptyCart:function(){PhotoSite.Framework.ICartService.EmptyCart(this._encUserID,this._gotEmptyCartSuccessDelegate,this._gotErrorDelegate);},getNumProductsInCartByType:function(productType){if(this._itemCounts.ProductTypeCounts){for(var i=0;i<this._itemCounts.ProductTypeCounts.length;i++){if(this._itemCounts.ProductTypeCounts[i].Key==productType){return eval(this._itemCounts.ProductTypeCounts[i].Value);}}}return 0;},lockCartItemForEdit:function(A){this._syncManager.prepareCartItemForSync(A);},unlockCartItem:function(A){this._syncManager.scheduleCartItemForSync(A);},getCartItemByEncID:function(B){for(var A=0;A<this._cartItems.length;A++){if(this._cartItems[A].EncID==B){return this._cartItems[A];}}},getCartItemByGroupID:function(A){for(var B=0;B<this._cartItems.length;B++){if(this._cartItems[B].GroupID==A){return this._cartItems[B];}}},getCartItemByIndex:function(A){return this._cartItems[A];},getCartItemsCount:function(){return this._cartItems.length;},calculateCartTotalPrice:function(){var B=this._cartItems;var A=0;for(var C=0;C<B.length;C++){A+=this.calculateCartItemPrice(B[C]);}return A;},calculateCartItemPrice:function(A){if(A.ProductType==="Prints"){return this.calculatePrintPrice(A);}else{return A.Quantity*A.Price;}},calculatePrintPrice:function(C){var A=C.SubItems;var D=0;for(var E=0;E<A.length;E++){var B=A[E];if(B.Quantity>0){D+=B.Quantity*B.PricePer;}}return D;},calculatePrintFileSizes:function(){var B=0;for(var C=0;C<this._cartItems.length;C++){var A=this._cartItems[C];if(A.ProductType==="Prints"){B+=A.FileSize;}}return B;},calculateCDsNeededForPrints:function(){var B=this.calculatePrintFileSizes();var A=this.getMaxMBForCD()*1024;if(A>0){return parseInt(Math.ceil(B/A));}return 0;},getQuickSetInitialQuantities:function(){var K=new Array();var H=new Array();var I=new Array();var C=null;var E=0;var G=this.getPrintSizesInfo();for(var A=0;A<this._cartItems.length;A++){var J=this._cartItems[A];if(J.ProductType==="Prints"){C=this._cartItems[A];E=A;break;}}if(C===null){for(var A=0;A<G.length;A++){K.push(null);}return K;}for(var A=0;A<G.length;A++){H.push(C.SubItems[A].Quantity);I.push(true);}for(var A=E+1;A<this._cartItems.length;A++){var J=this._cartItems[A];if(J.ProductType==="Prints"){for(var B=0;B<J.SubItems.length;B++){var F=J.SubItems[B];var D=H[B];if(F.Quantity!=D){I[B]=false;}}}}for(var A=0;A<I.length;A++){if(I[A]){K.push(H[A]);}else{K.push(null);}}return K;},isWaitingForSync:function(){var A=this._syncManager.hasPendingRequests();var B=this._syncManager.hasItemsWaitingForSync();return A||B;},isCartItemWaitingForSync:function(B){var A=this._groupIDFromCartItemOrGroupObject(B);return this._syncManager.isCartItemSyncInProgress(A)||this._syncManager.isCartItemWaitingForSync(A);},isCartItemDataOutOfDate:function(B){var A=this._groupIDFromCartItemOrGroupObject(B);return this._syncManager.isFullSyncRequiredForCartItem(A);},syncCartItem:function(C){var A=this._groupIDFromCartItemOrGroupObject(C);var B=this.getCartItemByGroupID(A);this._syncManager.syncCartItem(B);},syncAllNow:function(){var A=this._syncManager.hasItemsWaitingForSync();if(A){this._syncManager.syncAll();}},hasCropWarningSet:function(){return this._isCartItemOrSubItemPropertySet("WarnCrop");},hasResolutionWarningSet:function(){return this._isCartItemOrSubItemPropertySet("WarnRes");},set_syncToServerEnabled:function(A){this._syncManager.set_allowSyncToServer(A);},get_syncToServerEnabled:function(){return this._syncManager.get_allowSyncToServer();},set_zeroQuantitySyncEnabled:function(A){this._syncManager.set_allowZeroQuantityItems(A);if(A){this._syncManager.syncAll();}},get_zeroQuantitySyncEnabled:function(){return this._syncManager.get_allowZeroQuantityItems();},updateQuickSet:function(D,A){var C=this._syncManager.hasPendingRequests();var E=this._syncManager.hasItemsWaitingForSync();if(C||E){if(E){this._syncManager.syncAll();}setTimeout(function(G,F,H){return function(){G.updateQuickSet(F,H);};}(this,D,A),100);return;}this._syncManager.set_allowSyncToServer(false);this._quickSetServerDone=false;this._quickSetClientDone=false;PhotoSite.Framework.ICartService.UpdateQuickSet(this._encUserID,D,this._gotQuickSetSuccessDelegate,this._gotErrorDelegate);var B=this._getPrintUpdateBatchForQuickset(D);this._updateQuickSetCartItems(0,B,A);}};PhotoSite.Framework.ShoppingCart.descriptor={properties:[{name:"batchSize",type:Number},{name:"syncDelay",type:Number},{name:"encUserID",type:String}],events:[{name:"serviceError"},{name:"gotCartItemCounts"},{name:"renderCartItem"},{name:"renderCartItemBatch"},{name:"renderComplete"},{name:"syncCartItem"},{name:"syncCartItemComplete"},{name:"syncSubItemComplete"},{name:"updateQuickSetComplete"},{name:"emptyCartComplete"}]};PhotoSite.Framework.ShoppingCart.registerClass("PhotoSite.Framework.ShoppingCart",PhotoSite.Framework.ComponentBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();