/*--------------------------------------------------------------------------*
 * 
 * Soft Add To Cart (Alley)
 * 
 * Version 1.0.2
 * 
 * Copyright (C) 2009 Brand Labs LLC
 * 
 *--------------------------------------------------------------------------*/

var AlleySettings={ENABLED:false,PRODUCT_ENABLED:true,CATEGORY_ENABLED:true,FORM_SELECTOR:'#content_area form[name="MainForm"]',CATEGORY_ADD_TO_CART_BUTTON_SELECTOR:'a[href^="/ShoppingCart.asp?ProductCode="] img[src$="btn_addtocart_small.gif"]',PRODUCT_ADD_TO_CART_BUTTON_SELECTOR:'input[name="btnaddtocart"]',PRODUCT_ADD_TO_WISH_LIST_BUTTON_SELECTOR:'input[name="btnaddtowishlist"]',CATEGORY_ADD_TO_CART_BUTTON_DISABLED:null,PRODUCT_ADD_TO_CART_BUTTON_DISABLED:null,HIDE_CART_SUMMARY:true,RETURN_TO_PATH:'/v/alley/added_to_cart.asp'};var Alley={LOADED:false,load:function(){try{if(!AlleySettings.ENABLED){return;}
if(Alley.LOADED){return;}
Alley.LOADED=true;if(location.pathname.toLowerCase()=='/productdetails.asp'||location.pathname.toLowerCase().indexOf('-p/')!=-1||location.pathname.toLowerCase().indexOf('_p/')!=-1){if(!AlleySettings.PRODUCT_ENABLED){return;}
Alley.loadProduct();}
else if(location.pathname.toLowerCase()=='/searchresults.asp'||location.pathname.toLowerCase().indexOf('-s/')!=-1||location.pathname.toLowerCase().indexOf('_s/')!=-1){if(!AlleySettings.CATEGORY_ENABLED){return;}
Alley.loadCategory();}}
catch(e){}},getFormElement:function(){var aa=$$(AlleySettings.FORM_SELECTOR);if(aa==null||aa.size()<1){return null;}
return aa.first();},loadProduct:function(){var ba=Alley.getFormElement();if(ba==null){return;}
if(AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_DISABLED!=null){new Image().src=AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_DISABLED;}
new AlleyProductSoftAddToCart(ba);},loadCategory:function(){var ca=Alley.getFormElement();if(ca==null){return;}
if(AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_DISABLED!=null){new Image().src=AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_DISABLED;}$$(AlleySettings.FORM_SELECTOR+' '+AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_SELECTOR).each(function(da){new AlleyCategorySoftAddToCart(da);});}};Event.observe(window,'load',Alley.load);var AlleySoftAddToCart=Class.create({CONFIRMATION_MESSAGE:'The item(s) have been added to your cart.',CART_SUMMARY_ID:'display_cart_summary',displayConfirmation:function(){this.hideCartSummary();window.alert('The item(s) have been added to your cart.');},hideCartSummary:function(){var ea=null;if(!AlleySettings.HIDE_CART_SUMMARY){return;}
ea=$(this.CART_SUMMARY_ID);if(ea==null){return;}
ea.hide();}});var AlleyProductSoftAddToCart=Class.create(AlleySoftAddToCart,{initialize:function(fa){var ga=null;this.formElement=fa;if(this.formElement==null){return;}
ga=$$(AlleySettings.FORM_SELECTOR+' '+AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_SELECTOR);if(ga==null||ga.size()<1){return;}
this.buttonElement=ga.first();Event.observe(this.buttonElement,'click',this.buttonClicked.bind(this));ga=$$(AlleySettings.FORM_SELECTOR+' '+AlleySettings.PRODUCT_ADD_TO_WISH_LIST_BUTTON_SELECTOR);if(ga!=null&&ga.size()>0){this.wishListButtonElement=ga.first();Event.observe(this.wishListButtonElement,'click',this.buttonClicked.bind(this));}
else{this.wishListButtonElement=null;}
this.buttonElementSource=null;this.enableButton();this.lastButtonClickedElement=this.buttonElement;Event.observe(this.formElement,'submit',this.formSubmitted.bind(this));},formSubmitted:function(ha){var ia=null;try{if(this.lastButtonClickedElement!=null&&this.lastButtonClickedElement!=this.buttonElement){this.lastButtonClickedElement=null;return;}
this.lastButtonClickedElement=null;Event.stop(ha);this.disableButton();ia=this;this.formElement.request({method:'post',evalJSON:true,sanitizeJSON:true,encoding:'iso-8859-1',parameters:{'ReturnTo':AlleySettings.RETURN_TO_PATH,'btnaddtocart':''},onSuccess:function(ja){if(ja.getResponseHeader('X-Added-To-Cart')==null||ja.getResponseHeader('X-Added-To-Cart')==''){ia.manualFormSubmission();return;}
ia.enableButton();ia.formElement.reset();ia.removeErrorMessage();ia.displayConfirmation();},onFailure:function(){ia.manualFormSubmission();}});}
catch(e){this.manualFormSubmission();}},enableButton:function(){this.buttonElement.writeAttribute({disabled:null});if(this.wishListButtonElement!=null){this.wishListButtonElement.writeAttribute({disabled:null});}
if(this.buttonElementSource!=null&&AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_DISABLED!=null){this.buttonElement.writeAttribute({src:this.buttonElementSource});this.buttonElementSource=null;}},disableButton:function(){this.buttonElement.writeAttribute({disabled:'disabled'});if(this.wishListButtonElement!=null){this.wishListButtonElement.writeAttribute({disabled:'disabled'});}
if(AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_DISABLED!=null){this.buttonElementSource=this.buttonElement.readAttribute('src');this.buttonElement.writeAttribute({src:AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_DISABLED});}},manualFormSubmission:function(){this.formElement.insert(new Element('input',{type:'hidden',name:'btnaddtocart',value:''}));this.formElement.submit();},removeErrorMessage:function(){var ka=null;var la=null;try{ka=$$(AlleySettings.FORM_SELECTOR+' '+'table tbody tr td table tbody tr td img[src="/a/a/i/error_alert.gif"]');la=null;if(ka==null||ka.size()<1){return;}
la=ka.first();la=la.up().up().up().up().up().up().up().up();la.previous('br').remove();la.next('br').remove();la.remove();}
catch(e){}},buttonClicked:function(ma){try{this.lastButtonClickedElement=ma.element();}
catch(e){}}});var AlleyCategorySoftAddToCart=Class.create(AlleySoftAddToCart,{initialize:function(na){this.buttonElement=na;if(this.buttonElement==null){return;}
this.anchorElement=this.buttonElement.up();if(this.anchorElement==null){return;}
this.anchorElementHref=this.anchorElement.readAttribute('href');this.productCode=this.getProductCodeFromURL(this.anchorElementHref);if(this.anchorElementHref==null||this.productCode==null){return;}
this.buttonElementSource=null;this.buttonClickedBound=this.buttonClicked.bind(this);this.enableButton();},buttonClicked:function(oa){var pa=null;try{Event.stop(oa);this.disableButton();pa=this;new Ajax.Request(this.anchorElementHref,{method:'get',evalJSON:true,evalJS:false,sanitizeJSON:true,encoding:'iso-8859-1',parameters:{'ReturnTo':AlleySettings.RETURN_TO_PATH},onSuccess:function(qa){if(qa.getResponseHeader('X-Added-To-Cart')==null||qa.getResponseHeader('X-Added-To-Cart')==''){window.location.href=pa.anchorElementHref;return;}
pa.enableButton();pa.displayConfirmation();},onFailure:function(){window.location.href=pa.anchorElementHref;}});}
catch(e){window.location.href=this.anchorElementHref;}},enableButton:function(){Event.observe(this.anchorElement,'click',this.buttonClickedBound);this.anchorElement.writeAttribute({href:this.anchorElementHref,onclick:null});if(this.buttonElementSource!=null&&AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_DISABLED!=null){this.buttonElement.writeAttribute({src:this.buttonElementSource});this.buttonElementSource=null;}},disableButton:function(){Event.stopObserving(this.anchorElement,'click',this.buttonClickedBound);this.anchorElement.writeAttribute({href:'#',onclick:'return false;'});if(AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_DISABLED!=null){this.buttonElementSource=this.buttonElement.readAttribute('src');this.buttonElement.writeAttribute({src:AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_DISABLED});}},getProductCodeFromURL:function(ra){var sa=null;if(ra==null){return null;}
sa=ra.match(/\/shoppingcart\.asp\?(?:[\&]?.*\=.*)*productcode=([^\&\#]+)/i);if(sa!=null&&sa.length>=2){return unescape(sa[1]);}
return null;}});