var cartLineSelectors = new Array();

function open_edit (line_id) {
    if (!itemPanel) return;
    var postData = 'line_id='+line_id;
    var sUrl = '/detail/edit';
    var quickviewCallback =
    {
        success:function(o){
            itemPanel.setContent(o, function(){
                eval_js(o);
            });

        },
        failure:function(o){}
    };
    YAHOO.util.Connect.asyncRequest('POST', sUrl, quickviewCallback, postData);
}

function open_impulse (item) {
    if (!itemPanel) return;
    var postData = 'item='+item;
    var sUrl = '/detail/impulse';
    var quickviewCallback =
    {
        success:function(o){
            itemPanel.setContent(o, function(){
                eval_js(o);
            });

        },
        failure:function(o){}
    };
    YAHOO.util.Connect.asyncRequest('POST', sUrl, quickviewCallback, postData);
}

function selectColor_cart(selected_el, item_id, color_id, color_description, medium_img, sku_price_override, sku_price_override_flag) {

    var swatches = document.getElementById("swatches").childNodes;
    for (i in swatches) {
        if (swatches[i].firstChild) {
            swatches[i].firstChild.className = "";
        }
    }
    selected_el.firstChild.className = "selected";

    //display color info
    var span_description = YAHOO.util.Dom.get("color_description");
    var span_price = YAHOO.util.Dom.get("color_price");
    var new_price = color_size[color_id];

    for(var j in new_price){
        new_price = new_price[j];
        break;
    }
    span_description.innerHTML = color_description;
    span_price.innerHTML = "$"+new_price['price'];

    if(sku_price_override_flag == 'true'){
        // + edit cart pdp: upper right corner price label:
        var sku_current_price = YAHOO.util.Dom.get('sku_current_price');
        sku_current_price.innerHTML = sku_price_override;
        // + edit cart pdp: beside color price label:
        span_price.innerHTML = "$"+sku_price_override;
    }

    color_id_value = color_id;

    var size = document.getElementById("size_select");
    var old_size = size.value;

    while (size.length > 1) size.remove(1);

    for (var size_id in color_size[color_id]) {
        if (color_size[color_id][size_id]['description']) {
            addOption(size, color_size[color_id][size_id]['description'], size_id);
        }
    }

    if (size.length == 2) {
        size.selectedIndex = 1;
    } else if (old_size) {
        size.value = old_size;
    } else {
        size.selectedIndex = 0;
    }

    selectSize_cart(size.value);

    //
    var img = document.getElementById('si_product_image');
    if(img) img.src = medium_img;

    YAHOO.util.Dom.get("color_id").value = color_id;
}

function selectSize_cart(size_id) {
    var qty =  document.getElementById("qty_select");
    var old_qty = qty.value;

    while (qty.length > 1) qty.remove(1);

    if (color_size[color_id_value][size_id])
    {
        var max_qty = color_size[color_id_value][size_id]['quantity'];
        if (max_qty > 9) max_qty = 9;
        for (var i=2; i<=max_qty; i++) {
            addOption(qty, ''+i, ''+i);
        }
        if (old_qty > 1) {
            qty.value = old_qty;
            if (qty.value < old_qty) {
                qty.selectedIndex = qty.length - 1;
            }
        } else {
            qty.selectedIndex = 0;
        }
        selectPrice(color_size[color_id_value][size_id], '');
    }
    else
    {
        for (var size_id in color_size[color_id_value]) {
            if (color_size[color_id_value][size_id]['price']) {
                selectPrice(color_size[color_id_value][size_id], '');
            }
        }
    }
}

function setDefaultValues(swatch, color_id, size_id, qty) {
    var elem = document.getElementById(swatch);
    if (!elem || !elem.onclick) return;
    elem.onclick();
    var size_select = YAHOO.util.Dom.get("size_select");
    var index;
    for(index = 0; index < size_select.length; index++) {
        if(size_select[index].value == size_id)
        size_select.selectedIndex = index;
    }
    selectSize_cart(size_id);
    var qty_select = YAHOO.util.Dom.get("qty_select");
    for(index = 0; index < qty_select.length; index++) {
        if(qty_select[index].value == qty)
        qty_select.selectedIndex = index;
    }
}

function addtobag_cart() {
    var item = document.getElementById("item_id").value;
    var qty = document.getElementById("qty_select").value;
    var size = document.getElementById("size_select").value;
    var color = document.getElementById("color_id").value;

    if (!size) {
        return alert("Please select a size");
    }

    var sUrl = "/cart/ajaxadd";
    var postData = "i=" + item + "&c=" + color + "&s=" + size + "&q=" + qty;
    var callback = {
        success: function(o) {
    		showMiniCart();
            if (o.responseText == undefined)
                return;
            window.location.reload();
        }
    };
    YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}

function open_wit() {
    witPanel = new CustomPanel();
    witPanel.init("what_is_this", "wit_overlay", 851, 20, 239);
    var sUrl = "/cart/infoWindow";
    var postData = "window=fc_jbucks";
    var callback = {
        success: function(o) {
            if (o.responseText == undefined)
                return;
            witPanel.setContent(o);
        },
        failure: function(o) {}
    };
    YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}

function open_wit_jmc() {
    witPanel = new CustomPanel();
    witPanel.init("what_is_this", "wit_overlay", 851, 20, 239);
    var sUrl = "/cart/infoWindow";
    var postData = "window=jmc_help";
    var callback = {
        success: function(o) {
            if (o.responseText == undefined)
                return;
            witPanel.setContent(o);
        },
        failure: function(o) {}
    };
    YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}

function open_status_msg(context) {
    witPanel = new CustomPanel();
    witPanel.init("what_is_this", "wit_overlay", 851, 20, 189, 0, 100);
    var sUrl = "/cart/infoWindow";
    var postData = "window=status";
    var callback = {
        success: function(o) {
            if (o.responseText == undefined)
                return;
            witPanel.setContent(o);
        },
        failure: function(o) {}
    };
    YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}

function discount_init (jbucks, funcards) {
    if (jbucks == undefined) jbucks = 0;
    if (funcards == undefined) funcards = 0;

    var e = document.getElementById('discount_window');

    var n;

    var active = "coupon";
    if (jbucks > 0){
        n = document.getElementById('jbuck_tab');
        active = "jb";
    }
    else if (funcards > 0){
        n = document.getElementById('funcard_tab');
        active = "fc";
    }
    else
        n = document.getElementById('coupon_tab');

    e.innerHTML = n.innerHTML;
    set_tab_headers(active);
}

function discount_coupon ()
{
    var e = document.getElementById('discount_window');
    var n = document.getElementById('coupon_tab');
    set_tab_headers("coupon");
    e.innerHTML = n.innerHTML;
}

function discount_jbuck ()
{
    var e = document.getElementById('discount_window');
    var n = document.getElementById('jbuck_tab');
    set_tab_headers("jb");
    e.innerHTML = n.innerHTML;
}

function discount_funcard ()
{
    var e = document.getElementById('discount_window');
    var n = document.getElementById('funcard_tab');
    set_tab_headers("fc");
    e.innerHTML = n.innerHTML;
}

function set_tab_headers (active){
    var jb_tab = YAHOO.util.Dom.get("redeem_jb");
    var fc_tab = YAHOO.util.Dom.get("redeem_fc");
    var coupon_tab = YAHOO.util.Dom.get("redeem_coupon");
    var discount_win = YAHOO.util.Dom.get("discount_window");
    var child;
    if(jb_tab){
        YAHOO.util.Dom.removeClass(jb_tab, "selected_discount_tab");
        child = YAHOO.util.Dom.getChildrenBy(jb_tab, function(el){
            return el.tagName == "A"});
        if(child[0]) child[0].innerHTML = "<span>>>click to redeem</span><br/>JBucks";
    }
    if(fc_tab){
        YAHOO.util.Dom.removeClass(fc_tab, "selected_discount_tab");
        child = YAHOO.util.Dom.getChildrenBy(fc_tab, function(el){
            return el.tagName == "A"});
        if(child[0]) child[0].innerHTML = "<span>>>click to redeem</span><br/>Fun Cards";
    }
    if(coupon_tab){
        YAHOO.util.Dom.removeClass(coupon_tab, "selected_discount_tab");
        child = YAHOO.util.Dom.getChildrenBy(coupon_tab, function(el){
            return el.tagName == "A"});
        if(child[0]) child[0].innerHTML = "<span>>>click to enter</span><br/>coupon";
    }
    if(active == "fc"){
        YAHOO.util.Dom.addClass(fc_tab, "selected_discount_tab");
        child = YAHOO.util.Dom.getChildrenBy(fc_tab, function(el){
            return el.tagName == "A"});
        if(child[0]) child[0].innerHTML = "redeem Fun Cards";
        discount_win.style.backgroundColor = YAHOO.util.Dom.getStyle(fc_tab, 'backgroundColor');
    }
    else if(active == "jb"){
        YAHOO.util.Dom.addClass(jb_tab, "selected_discount_tab");
        child = YAHOO.util.Dom.getChildrenBy(jb_tab, function(el){
            return el.tagName == "A"});
        if(child[0]) child[0].innerHTML = "redeem JBucks";
        discount_win.style.backgroundColor = YAHOO.util.Dom.getStyle(jb_tab, 'backgroundColor');
    }
    else if(active == "coupon"){
        YAHOO.util.Dom.addClass(coupon_tab, "selected_discount_tab");
        child = YAHOO.util.Dom.getChildrenBy(coupon_tab, function(el){
            return el.tagName == "A"});
        if(child[0]) child[0].innerHTML = "enter coupon";
        discount_win.style.backgroundColor = YAHOO.util.Dom.getStyle(coupon_tab, 'backgroundColor');
    }
}

function clear_discounts(){
    document.getElementById('clear_discounts').value = true;
    if(document.getElementById('coupon_code')) document.getElementById('coupon_code').value='';
    var index = 0;
    while(document.getElementById('funcard_'+index) || document.getElementById('jbuck_'+index)){
        if(document.getElementById('funcard_'+index)) document.getElementById('funcard_'+index).value='';
        if(document.getElementById('jbuck_'+index)) document.getElementById('jbuck_'+index).value='';
        index++;
    }
    if(document.getElementById('apply')) document.getElementById('apply').click();
    if(document.getElementById('apply_discount_a')) document.getElementById('apply_discount_a').click()
}

function clear_jmc()
{
    document.getElementById('clear_jmc').value = true;
    if(document.getElementById('jmc_code')) document.getElementById('jmc_code').value='';
    if(document.getElementById('apply')) document.getElementById('apply').click();
    if(document.getElementById('apply_discount_a')) document.getElementById('apply_discount_a').click()
}
