function add_to_cart (item_id, arr) {
    // arr: [color, size, qty]
    if (arr.length == 3) {
        for (var i=0; i < arr.length; i++) {
            if (arr[i] == '') {
                return false;
            }
        };
    }
    
    var postData = 'i=' + item_id + '&c=' + arr[0] + '&s=' + arr[1] + '&q=' + arr[2];

    var sUrl = "/shopping_bag/ajaxadd";

    var callback = {
        success:function(o) {
            if (typeof weAreInCart != "undefined" && weAreInCart == true) {
                window.location.reload();
                return;
            }
            if (o.responseText == undefined) return;
            if (typeof wait != 'undefined') wait.hide();
            responses = o.responseText.split('<SPLIT>');
            open_header_cart(responses[0]);
            document.getElementById('shopping_bag_counter').innerHTML = responses[1];
            updateRecent();
        },
        failure:function(o) {
            if (o.responseText == undefined) return;
            open_header_cart("There was an error while adding the item to your bag.");
        }
    };

    YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}

wishlist = {
    panel :  null,
    p : {
        item_id : null,
        qty : null,
        size : null,
        color : null
    },
    postData : null,
 
    add : function(sku_selection) {
        var line_id = 0;
        var customError = document.getElementById('custom-alert');
        var alertQvDiv = document.getElementById('qv_errors');
        var errCount = 0;

        if (alertQvDiv) alertQvDiv.style.display = 'none';
        if (customError) customError.innerHTML = '';
              
        var oThis = wishlist;
        
        oThis.p.item_id = sku_selection.item_id;
        oThis.p.qty     = sku_selection.qty;
        oThis.p.size    = sku_selection.size_id || sku_selection.defaults.size;
        oThis.p.color   = sku_selection.color_id;
        
        var e = document.getElementById('qty_select_' + line_id);
        if (e.value == null || e.value == '') {
            customError.innerHTML = qtyWishlistSelectError;
            errCount++;
        }
        
        var e = document.getElementById('size_select_' + line_id);
        if (e.value == null || e.value == '' && oThis.p.size != '0001') {
            customError.innerHTML = sizeWishlistSelectError;
            errCount++;
        }
                
        if (errCount) {
            alertQvDiv.style.display = 'block';
        } else {    
            oThis.makeRequest("add");
        }
        return false; /* Don't follow link */
    },
    addWithConnection : function(sku_selection){
        var oThis = wishlist;
        
        oThis.p.item_id = sku_selection.item_id;
        oThis.p.qty     = sku_selection.qty;
        oThis.p.size    = sku_selection.size_id;
        oThis.p.color   = sku_selection.color_id;
        
        return oThis.makeRequest("add");
    }, 
    
    rm : function(id){
        var oThis = wishlist;
        oThis.p.item_id = typeof id == "undefined" ? document.getElementById("item_hidden").value : id;

        oThis.makeRequest("remove");
        return false; /* Don't follow link */
    },
    
    makeRequest : function(action){
        var oThis = wishlist;

        oThis.preparePostData();
        return YAHOO.util.Connect.asyncRequest('POST', "/wishlist/"+action, oThis.callback, oThis.postData);
    },
    
    preparePostData :  function(){
        var oThis = wishlist;
        oThis.postData = '';
        for(var i in oThis.p){
            oThis.postData += "&"+i.substr(0,1)+"="+oThis.p[i];
        }
        oThis.postData += '&ajax=true';
    },

    callback : {
        success : function(o){
            var oThis = wishlist;
            if (o.responseText == undefined) return false;
            if (typeof wait != 'undefined'){ wait.hide(); }
            responses = o.responseText.split('<SPLIT>');
            oThis.prepareHeader(responses[0]);
            document.getElementById('wishlist_counter').innerHTML = responses[1];
            if(responses.length == 3)
                oThis.lnkFavorites(responses[2]);
            else
                oThis.lnkFavorites(responses[2],responses[3]);
        },
        failure : function(o){
            if (o.responseText == undefined) return false;
            wishlist.prepareHeader("There was an error with your bag.");
        }
     },

    prepareHeader : function(text){
        old_text = document.getElementById("header_wishlist_last_item").innerHTML;
        if (text == '' && old_text == '') {
            window.location = '/wishlist';
            return;
        }
        if (text != '') {
            document.getElementById("header_wishlist_last_item").innerHTML = text;
            var regex = / \d+ items in bag/;
            var match = regex.exec(text);
        }
        
        wishlist.openList();
    },
    
    openList : function(){
        if(typeof cart != 'undefined'){
            cart.hide();
        }
        wishlist.panel = 
                new YAHOO.widget.Panel(
                    'header_wishlist_last_item',  
                    {
                      width:'250px',
                      context: [ document.getElementById("top_buttons_div") , 'tr', 'br'],
                      iframe:true,
                      close:false, 
                      zindex:300,
                      modal:false,
                      constraintoviewport: false,
                      scope:this
                    }
                );
        
        wishlist.panel.render(document.body);
        wishlist.panel.show();
        setTimeout('window.scrollTo(0, 0)',1);
        setTimeout("wishlist.panel.hide();wishlist.panel.setBody('');",5000);
    },
    
    closeList : function(){
        wishlist.panel.hide();
    },
    
    lnkFavorites : function(){
        var rowAddFavorites = document.getElementById('rowFavoritesLnk');
        if(!rowAddFavorites)
            return;
        var cells = rowAddFavorites.cells;
        cells[0].innerHTML = (arguments.length > 1) ? arguments[0] : '';
        cells[1].innerHTML = (arguments.length > 1) ? arguments[1] : arguments[0];
    }
};
