var couponCart=Class.create();couponCart.prototype=Object.extend(new freezeClass,{addSelector:".cart_add",removeSelector:".cart_remove",activitySelector:".cart_activity",inCartSelector:".cart_in",outCartSelector:".cart_out",removeURL:"/ajax.php?func=CartRemCoupon",addURL:"/ajax.php?func=CartAddCoupon",cartButton:"cartButton",updateElem:"couponCount",updateTemplate:"( #{response} )",couponID:null,container:null,inCart:false,started:false,initialize:function(){this.register.root=this;},register:{root:null,couponID:function(_1){this.root.frozen(true);if(typeof (_1)!="number"){throw "Argument must be a number";}this.root.couponID=_1;return this;},container:function(_2){this.root.frozen(true);if(!(_2=$(_2))){throw "Argument must be a valid DOM element";}this.root.container=_2;return this;},updateElem:function(_3){this.root.frozen(true);if(!(_3=$(_3))){throw "Argument must be a valid DOM element";}this.root.updateElem=_3;return this;}},start:function(){this.container=$(this.container);if(!this.container){throw "A container must be set before this coupon is started. Use register.container()";}this.container.getElementsBySelector(this.activitySelector).invoke("hide");this.container.getElementsBySelector(this.inCartSelector).invoke(this.inCart?"show":"hide");this.container.getElementsBySelector(this.outCartSelector).invoke(this.inCart?"hide":"show");this.container.getElementsBySelector(this.addSelector).invoke("observe","click",function(){try{this.add.bind(this)();}catch(e){}}.bind(this));this.container.getElementsBySelector(this.removeSelector).invoke("observe","click",function(){try{this.remove.bind(this)();}catch(e){}}.bind(this));this.started=true;},add:function(){this.frozen(true);if(this.started&&!this.inCart){this.freeze();this.container.getElementsBySelector(this.activitySelector).invoke("show");this.container.getElementsBySelector(this.inCartSelector,this.outCartSelector).invoke("hide");var _4=new Ajax.Request(this.addURL,{method:"post",parameters:"CouponID="+this.couponID,onSuccess:function(_5){if(this.updateElem=$(this.updateElem)){var _6=new Template(this.updateTemplate);this.updateElem.update(_6.evaluate({response:_5.responseText}));}if(this.cartButton=$(this.cartButton)){if(Number(_5.responseText)<=0){this.cartButton.hide();}else{this.cartButton.show();}}this.container.getElementsBySelector(this.inCartSelector).invoke("show");}.bind(this),onFailure:function(){this.container.getElementsBySelector(this.outCartSelector).invoke("show");Alert("An error was encountered while adding this coupon to your cart. Please try again.");}.bind(this),onComplete:function(){this.unfreeze();this.container.getElementsBySelector(this.activitySelector).invoke("hide");}.bind(this)});}this.inCart=true;return this;},remove:function(){this.frozen(true);if(this.started&&this.inCart){this.freeze();this.container.getElementsBySelector(this.activitySelector).invoke("show");this.container.getElementsBySelector(this.inCartSelector,this.outCartSelector).invoke("hide");var _7=new Ajax.Request(this.removeURL,{method:"post",parameters:"CouponID="+this.couponID,onSuccess:function(_8){if(this.updateElem=$(this.updateElem)){var _9=new Template(this.updateTemplate);this.updateElem.update(_9.evaluate({response:_8.responseText}));}if(this.cartButton=$(this.cartButton)){if(Number(_8.responseText)<=0){this.cartButton.hide();}else{this.cartButton.show();}}this.container.getElementsBySelector(this.outCartSelector).invoke("show");}.bind(this),onFailure:function(){this.container.getElementsBySelector(this.inCartSelector).invoke("show");Alert("An error was encountered while removing this coupon from your cart. Please try again.");}.bind(this),onComplete:function(){this.unfreeze();this.container.getElementsBySelector(this.activitySelector).invoke("hide");}.bind(this)});}this.inCart=false;return this;}});
