function Bag_ListPageAddToBagServiceCall(bagItem,conditionalProducts){var data='{"bagItem":'+JSON.stringify(bagItem)+',"conditionalProducts":'+JSON.stringify(conditionalProducts)+"}";AddToBagServiceCall(data,"Services/BagService.svc/ListPageAddToBag")}function Bag_SingleProdAddToBagServiceCall(bagItems,callBack){AddToBagServiceCall(bagItems,"Services/BagService.svc/ProductPageAddToBag",callBack)}function Bag_AddToBagServiceCall(bagItems){if(typeof bagItems=="undefined"||bagItems.length===0)return;AddToBagServiceCall(JSON.stringify({bagItems:bagItems}),"Services/BagService.svc/AddToBag",null)}function AddToBagServiceCall(data,url,callBack){ShowLoading("adding");$.ajax({type:"POST",url:url,data:data,contentType:"application/json; charset=utf-8",dataType:"json",success:function(result){AddToBag_Succeeded(result,callBack)},error:function(request,status,error){AjaxErrorHandler(request,status,error,"loadingDiv")}})}function AddToBag_Succeeded(result,callBack){if(ServiceFailed(result.d))return;UpdateBagHtml(result.d.BagHtml,result.d.OrderType);if(!callBack)switch(result.d.OrderType){case "LI":AddToBagForLineItemEntry_Succeeded(result);break;case "LP":case "VM":AddToBagForVmoAndLanding_Succeeded(result);break;default:AddToBagForListingPage_Succeeded(result);break}else callBack(result);Omniture_AddToBag(result.d.ItemsAdded);hideModal("loadingDiv")}function UpdateBagHtml(bagHtml,orderType){if(bagHtml.length>0){$("#bag").html(bagHtml);orderType!="VM"&&orderType!="LP"&&ShowBasketAfterAddingToBag()}}function AddToBagForLineItemEntry_Succeeded(result){result.d.ItemsFailed.length>0&&$.each(result.d.ItemsFailed,function(n,bagItem){if(bagItem.FailedValidation||bagItem.ProductNotFound||bagItem.NoLongerAvailable)ShowErrors(bagItem.Id,bagItem.ErrorMessages);else if(bagItem.AvailableSoon)ShowAvailableSoonMessage(bagItem);else bagItem.RepDeliveryOnly&&ShowRepDeliveryOnlyMessage(bagItem)});result.d.ItemsAdded.length>0&&$.each(result.d.ItemsAdded,function(n,bagItem){$("#"+bagItem.Id).remove();var rowsOnForm=$(".row").size();rowsOnForm<10&&AddRows(10-rowsOnForm)})}function AddToBagForVmoAndLanding_Succeeded(result){if(result.d.ItemsFailed.length>0){showModal("failedToAddDiv",300,70);var nItemsFailed=result.d.ItemsFailed.length;$("#failedToAddBagMsg").html($("#numberOfItemsFailedMessage").html().replace("#",nItemsFailed));$("#showBasketAfterOkClick").text(result.d.ItemsAdded.length>0?"1":"0")}else result.d.ItemsAdded.length>0&&ShowBasketAfterAddingToBag()}function AddToBagForListingPage_Succeeded(result){var itemResult,productCtrl,msgContainer;if(result.d.ItemsFailed.length>0){itemResult=result.d.ItemsFailed[0];productCtrl=$("#product"+itemResult.Id);msgContainer=productCtrl.find("#msg");msgContainer.html(itemResult.ErrorMessages[0])}else if(result.d.ItemsAdded.length>0){itemResult=result.d.ItemsAdded[0];productCtrl=$("#product"+itemResult.Id);productCtrl.find("#msg").html("<div>"+result.d.ListPageProperties.ProductAddedMsg+"</div>"+$("#checkOutLinkDiv").html());productCtrl.find("input:text#qty").val($("#qtyString").html());UpdateConditionalProducts(result.d.ListPageProperties.ConditionalProducts)}}function ItemsNotAddedModal_OkClick(){$("#showBasketAfterOkClick").text()=="1"&&ShowBasketAfterAddingToBag();hideModal("failedToAddDiv")}function Bag_AddFromLandingPage(qtyClassName){var bagItems=[];$(":text[class="+qtyClassName+"][id!='']").each(function(){var txtBox=$(this),sku=txtBox.attr("id"),qtyString=txtBox.val(),qty=qtyString.length===0?0:isNaN(qtyString)?0:parseInt(qtyString);if(sku.length>0&&qty>0){var bagItem={Quantity:qty,Sku:sku,OrderType:"LP"};bagItems.push(bagItem)}});bagItems.length>0&&Bag_AddToBagServiceCall(bagItems)}function UpdateConditionalProducts(conditionalProducts){if(conditionalProducts.length>0)for(var i=0;i<conditionalProducts.length;i++){var conditionalPrdct=conditionalProducts[i],conditionalPrdctCtrl=$("#product"+conditionalPrdct.ProductControlIndex),cannotAddMsg=conditionalPrdctCtrl.find("#cannotAddMsg");if(conditionalPrdct.ShowAddButton){conditionalPrdctCtrl.find("#addToBagPanel").show();cannotAddMsg.html(conditionalPrdct.AddButtonMessage)}else{conditionalPrdctCtrl.find("#addToBagPanel").hide();cannotAddMsg.html(conditionalPrdctCtrl.find("#conditionalProductMsg").html())}}}function Bag_RemoveItemFromBag(sku,campaignNumber,callBack){ShowLoading("removing");var data='{"sku":"'+sku+'","campaignNumber":"'+campaignNumber+'","conditionalProducts":'+JSON.stringify(GetConditionalProducts())+"}";$.ajax({type:"POST",url:"Services/BagService.svc/RemoveItem",data:data,contentType:"application/json; charset=utf-8",dataType:"json",success:function(result){RemoveItemFromBag_Succeeded(result,callBack)},error:function(request,status,error){AjaxErrorHandler(request,status,error,"loadingDiv")}})}function RemoveItemFromBag_Succeeded(result,callBack){if(ServiceFailed(result.d))return;if(!callBack)UpdateConditionalProducts(result.d.ConditionalProducts);else callBack(result);var bagHtml=result.d.BagHtml;if(bagHtml.length>0){$("#bag").html(bagHtml);OpenBasket()}hideModal("loadingDiv")}function AddCouponToBasket(){var coupon=$("#couponCodeTxt").val();ShowLoading("coupon");var data='{"coupon":"'+escape(coupon)+'","redirectUrl":"'+escape(document.location.href)+'"}';$.ajax({type:"POST",url:"Services/BagService.svc/AddCouponCode",data:data,contentType:"application/json; charset=utf-8",dataType:"json",success:function(result){AddCouponCode_Succeeded(result)},error:function(request,status,error){AjaxErrorHandler(request,status,error,"loadingDiv")}})}function AddCouponCode_Succeeded(result){if(ServiceFailed(result.d))return;if(result.d.DoRedirect&&result.d.RedirectUrl.length>0)document.location.href=result.d.RedirectUrl;else{if(!result.d.DoRedirect&&result.d.BagHtml.length>0){$("#bag").html(result.d.BagHtml);OpenBasket()}hideModal("loadingDiv");return}}
