brand.checkout = {
     abort: false,
     isCartView: false,  
     hasErrors : false,
     
     canContinueCheckout: function(state) {
	console.log("brand.checkout.canContinueCheckout: " + state);
	var style = state ? "visibility:visible" : "visibility:hidden";
	$$(".btn-checkout").invoke("setStyle", style); 
     },     
     
     makeAdditionalInfoBtns: function() {  
      //console.log("brand.checkout.makeAdditionalInfo Btns");
      // popups
      var popargs = {
        w: 480,
        h: 660,
        resizable: "no",
        scrollbar: "yes"
      }   
      var popup_shipping = new generic.popup({        
        activator: "btn_popup_shipping",
        url: "/cms/checkout/popup/shipping_popup.tmpl", 
        resizable: popargs.resizable,
        scrollbars: popargs.scrollbars,
        width: popargs.w,
        height: popargs.h       
    });    
    var popup_tax = new generic.popup({     
        activator: "btn_popup_tax",
        url: "/cms/checkout/popup/tax_popup.tmpl",  
        resizable: popargs.resizable,
        scrollbars: popargs.scrollbars,
        width: popargs.w,
        height: 200   
    });
    var popup_returns = new generic.popup({ 
        activator: "btn_popup_returns",
        url: "/cms/checkout/popup/return_popup.tmpl",  
        resizable: popargs.resizable,
        scrollbars: popargs.scrollbars,
        width: popargs.w,
        height: 500   
    });     
    var popup_security = new generic.popup({    
        activator: "btn_popup_security",
        url: "/cms/checkout/popup/security_popup.tmpl", 
        resizable: popargs.resizable,
        scrollbars: popargs.scrollbars,
        width: popargs.w,
        height: popargs.h   
    }); 
  },
  
  makeExitBtn: function() {
    //console.log("btn_exit_checkout");
    // exit checkout button: sends user to last page before entering checkout
    var btn_exit_checkout = $("btn_exit_checkout"); 
    
    if (btn_exit_checkout) {
        btn_exit_checkout.observe("click", function() {   
    	var loc = "/"; // home page default 
    	var ref = document.referrer; 
    	if (ref && (ref.indexOf(".maccosmetics")>-1) && (ref.indexOf("checkout")==-1)) {
       		loc = ref;
    	}
    	window.location = loc;
       });
    } 
  } 
};

        
brand.checkout.cartStatus = {  
    countNodeId: "global_cart_count",
    countContainerId: "shopping_bag_items", 
    
    init: function() {
        console.log("brand.checkout.cartStatus.init");
        this.countNode = $(this.countNodeId); 
        var self = this;
          
        generic.events.observe("cart:countsUpdated", function(args) { 
            self.updateCount();
        });
          
        var countContainer = $(this.countContainerId);
        if (countContainer) {
            countContainer.removeClassName("hidden");
        } 
         
    }, 
    updateCount: function() {
        console.log("brand.checkout.cartStatus.updateCount");
        if (this.countNode) { 
            this.countNode.innerHTML = brand.checkout.cartHandler.getTotalItems();
        }
    }
    
}

brand.checkout.cartItemsHandler = {  
    init: function(contents) { 
        // summary:
        //      cart item handlers for cart & review pages
        //      qty, remove functions
        console.log("brand.checkout.cartItemsHandler.init");         
        if (typeof(contents) == "undefined" && contents.length == 0) {   
            // if cart is empty: hide checkout button in cart panel
            this.hideCartSubmit(); 
            return;
        } 
            var storeHandlers = {};
            var self = this;
            
            contents.each(function(item) {  
                // check for giftcard item
                var skuPath, itemType;
                if (item.giftcard_id) {
                    skuPath = item.giftcard_id;
                    itemType = "giftcarditem";
                } else {
                    skuPath = item.path;
                }
                                
                storeHandlers[skuPath] = new brand.checkout.checkoutItemHandler({
                    buttonRemove:"btn_remove-"+skuPath,
                    selectQty: "cart_item_qty_"+skuPath,
                    skuPath: skuPath,
                    itemContainer: "co_prod-"+skuPath,
                    progressIndicator: "co_prod_progress-"+skuPath,
                    progressOffset: {w: 0, h: -2},
                    itemType: itemType
                }); 
                
            });
            
            this.itemHandlers = storeHandlers; 
    },
    
    hideCartSubmit: function() { 
        // hide checkout now button in cart panel
        var cartbtn = $("btn_checkout_now");
        if (cartbtn) {
            cartbtn.style.visibility = "hidden";
        }   
    } 
}
 
/** Offer Code
brand.checkout.offerCodeHandler = {  
    offercode_submit: "btn_offercode",
    offercode_field: "input_offercode", 
    callback: null, 
    init: function() {  
        //console.log("brand.checkout.offerCodeHandler.init "+brand.checkout.isCartView ); 
        var self = this;  
        this.callback = brand.checkout.isCartView ? brand.checkout.reloadCartView : self.onResponse;
        this.offercode_submit = $(this.offercode_submit);
        this.offercode_field = $(this.offercode_field);
        if (this.offercode_submit) {
          this.offercode_submit.observe('click', self.sendOffercode.bind(self));
          this.offercode_field.observe('keypress', self.sendOffercode.bind(self));
        } 
    },
    sendOffercode: function(event) {  
       //console.log("brand.checkout.offerCodeHandler.send_offercode"); 
       var self = this;  
       if ( event.type === "keypress" && (event.keyCode != Event.KEY_RETURN) ) {  
            return false;           
        }  
       brand.checkout.cartHandler.updateCart({ 
            params: [{"OFFER_CODE":this.offercode_field.value}],
            onSuccess: self.callback,
            onFailure: self.callback
         });  
       Event.stop(event);
    }, 
    onResponse: function(args) {
        console.log("brand.checkout.offerCodeHandler.onResponse "+args);
        
    }
}
**/
 
brand.checkout.giftWrapOptions = {
	checkboxNode: null,
	optionsContainerNode:null,
	formNode:null,
	giftMessageHiddenNode:null,
	giftMessageArray:null,
	
	init: function(args) {
	   if (!args) return;
	   generic.updateProperties.apply(this, [args]);
	   if (!this.checkboxNode || !this.optionsContainerNode) return;
	  
	   var self = this; 
	  
	   this.checkboxNode.observe("click", self.showOptions.bind(self));  
	   this.formNode.onsubmit = function() { 
	   	self.setMessage(); 
	   	return true;
	   };
	   
             self.showOptions(); //in case page is loaded with checkbox checked 
	},
	showOptions: function() {
	    if (this.checkboxNode.checked) {
	    	this.optionsContainerNode.removeClassName("hidden"); 
	    }
	    else {
	    	this.optionsContainerNode.addClassName("hidden"); 
	    }
	},
	setMessage: function() { 
	    if (this.checkboxNode.checked && this.giftMessageHiddenNode) { 
	       var message = $F("gift_message_1") + "[br]" + $F("gift_message_2") + "[br]"  + $F("gift_message_3") + "[br]"  + $F("gift_message_4"); 
	       //console.log("brand.checkout.giftWrapOptions.setMessage: "+message);
	       this.giftMessageHiddenNode.value = message;
	    }
	}
} 
 
