﻿var szChildProductID="";
var obMem={};

//In light box, switch checkboxes for a group of media for a device
function SwitchMediaCheckBox(nAttachToID)
{
    if(obMem[nAttachToID]==null) obMem[nAttachToID]=1;
    else obMem[nAttachToID]=1-obMem[nAttachToID];
    
    var checkboxes = document.getElementsByName("chkLBFor_" + nAttachToID);
    if(checkboxes.length)
    {
        for(var i=0;i<checkboxes.length;i++) checkboxes[i].checked=obMem[nAttachToID];
    }
    else
    {
        checkboxes.checked=obMem[nAttachToID];
    }
    return false;
}

//In shopping cart, switch all checkboxes for all cart items
function SwitchAllCheckBox()
{
    if(obMem["all"]==null) obMem["all"]=1;
    else obMem["all"]=1-obMem["all"];
    
    $("INPUT[type='checkbox'][Name='chkProductID']").each(function(){
        this.checked=obMem["all"];
    });
}

//Mainly for delete function in light box cart, select a list, and then delete them by the ID list
function getSelectedLightBoxCartItemID(popup, nAttachToID) {
    var selected="";
    var i;
    var chkbox;
    var nArrayCount;
    var checkboxes ;
    szChildProductID="0,";
        
    //if(popup) {
        checkboxes = document.getElementsByName("chkLBFor_" + nAttachToID);
    //}
    //else {
    //    checkboxes = document.getElementsByName("chkLBFor_" + nAttachToID + "_B");
    //}
    
    nArrayCount = checkboxes.length;
    
    for(i=0;i<nArrayCount;i++) {        
        chkbox = checkboxes[i];
        if(chkbox.checked) {
            var arIDs=chkbox.value.split(",");
            selected += arIDs[0] + ",";
            szChildProductID += arIDs[1] + ",";
        }            
    }
    if(i==0){
        chkbox = checkboxes;
        if(chkbox.checked) {
            var arIDs=chkbox.value.split(",");
            selected += arIDs[0] + ",";
            szChildProductID += arIDs[1] + ",";
        }            
    }
    selected=selected.substring(0,selected.length-1);    
    return selected;
}

//Refresh the light box cart after delete some items
function RefreshLightBoxCart(nID, nCartItemIDs) {
   
    lightBox.load("/Cart/LightBoxRemove.aspx?pid=" + nID + "&remove=" + nCartItemIDs, function(data){
        $("#lightBoxCartContainer").html(data);
        if(document.getElementById("_LightBoxCartStatus")!=null) {
            
                sStatusMessage = document.getElementById("_LightBoxCartStatus").innerHTML;
            
                if (sStatusMessage == "EMPTY") {                
                    lightBox.hide();
                } else if (sStatusMessage != "OK"){
                    alert(sStatusMessage);
                } else {
                    J3Button.bind("#lightBoxCartContainer"); 
                }
        }
    });
}

//The lightbox cart in shopping cart mode, redirect to the remove page
function RemoveFromLightBoxCart(bPopup, nID)
{    
    nCartItemIDs = getSelectedLightBoxCartItemID(bPopup, nID);
    if(nCartItemIDs!="")
    {
        if(!confirm("Do you really want to remove these items?")) return;
        
        if(bPopup)
        {            
            RefreshLightBoxCart(nID, nCartItemIDs);
            RefreshSideCart();
        }
        else
        {        
            document.location.href="/Cart/RemoveItems.aspx?id="+nCartItemIDs+"&return=/Cart/Cart.aspx"; 
        }
    }
    else
    {
        alert('No item selected.');
    }
}

//After buy or delete some products, refresh the side cart by ajax
function RefreshSideCart()
{
    // refresh side cart
    try
    {
        addProduct(0,0,0,CONFIG.SideBarStyle);
    }
    catch(e)
    {
        addProduct(0,0,0,"WizardMode");
    }
    
    UpdateBoughtStatus();
    RefreshContentCovers();
}

//The items already been bought are grayed in landing page, so after buy or delete some products
//Need to refresh the gray cover status
//The function "InitCovers" is in ContentBody.aspx
function RefreshContentCovers()
{
	$("div").remove(".ContentItemCover");
	if(typeof(InitCovers)=="function") InitCovers();
}

//Update the attribute "AlreadyBought", to be called in InitCovers()
//The "Products" object is in ContentBody.aspx
function UpdateBoughtStatus()
{
    if(szChildProductID=="0,") return;
    
    for(var o in Products)
    {
        var szChild=","+Products[o].DefaultChildID+",";
        if(szChildProductID.indexOf(szChild)>=0) Products[o].AlreadyBought=false;
    }
}