var myBox = srv_box;

function buildBoxInit(){
    drawBoxRefresh();
    fixedMenu.init();
    check_store_enabled();
}

function delItemFromBox(item, qtd){
    //alert("delItemFromBox");
    if(qtd == "" || qtd == null){
        qtd = 1;   
    }
    //if(confirm("Are you sure you want to remove "+qtd+"x "+descr+" from your box?")){
        var ci = new CartItem(item, qtd);
        myBox.delItem(ci);
        //myBox.save();
        x_aciDelItem(item, qtd, drawBox);
        //drawBox(document.getElementById("buildabox"));
    //}
}

function addBoxToCart_cb(){
    location.href='../view-cart';
}

function addBoxToCart(){
    if(myBox.getNumItems() == myBox.getMaxSubs()){
        //myCart.addItem(myBox);
        x_aciAddBox(addBoxToCart_cb);        
        //myCart.save();
        //setSCookie("tc_", " ");
        myBox.clearSubs();
        //myBox.save();
    }else{
        alert("Your box is not full.  Please fill it completely before adding it to your cart.\n\nYour box has "+myBox.getNumItems()+" items in it.  When full it holds "+myBox.getMaxSubs()+" items.");
        return false;
    }
}

function setBoxItem(item){
    x_aciSetBox(item,setBoxItem_cb);
}

function setBoxItem_cb(max_subs){
    var box_sel = document.getElementById("box_id");
    if(isNaN(max_subs) && max_subs >= ""){
        alert(max_subs);
        var index = getIndexForValue(box_sel, myBox.getSku());
        box_sel.selectedIndex = index;
        return false;
    }else{
        var box_sel = document.getElementById("box_id");
        myBox.setSku(box_sel.value);
        myBox.setQty(1);
        myBox.setMaxSubs(max_subs);
        drawBoxRefresh();
        return true;    
    }
}

function getIndexForValue(sel, val){
    for(var i = 0; i < sel.options.length; i++){
        if(sel.options[i].value == val){
            return i;
        }
    }
    return false;
}

function drawBox(){
    var div_id = document.getElementById("buildabox");
    var count = 0;
    var picSize = 58;
    //if(myBox.getMaxSubs() > 24){
    if(myBox.getMaxSubs() > 12){
        picSize = 29;
    }
    myBox.initIterators()
    while(myBox.next()){
        var q = myBox.getNextQty();
        var s = myBox.getNextSku();
        //var p = myBox.getNextPic();
        var x = 0;
        for(x = 0; x < q; x++){
            var obj = document.getElementById("buildabox_p"+count);
            if(s != obj.alt){
                //var obj_d = document.getElementById("buildabox_d"+count);
                //obj_d.innerHTML = "<A id=\"buildabox_a"+count+"\" href=\"#\" onclick=\"return delItemFromBox('"+s+"')?false:false;\"><img id=\"buildabox_p"+count+"\" width=\""+picSize+"\" height=\""+picSize+"\" border=\"0\" src=\"getpic.php?sku="+s+"\" title=\""+s+"\" alt=\""+s+"\" /><a/>";
                var obj_a = document.getElementById("buildabox_a"+count);
                var obj_d = document.getElementById("pic_"+s);
                obj.alt = obj_d.alt;
                //obj.src = "getpic.php?sku="+s;
                obj.src = obj_d.src;
                obj.title = obj_d.title;
                obj_a.onclick = new Function("return delItemFromBox('"+s+"')?false:false;");
            }
            count++;
        }
    }
    
    while(count < myBox.getMaxSubs()){
        var obj = document.getElementById("buildabox_p"+count);
        if(obj.alt != ""){
            //var obj = document.getElementById("buildabox_d"+count);
            //obj.innerHTML = "<A id=\"buildabox_a"+count+"\" href=\"#\" onclick=\"return false;\"><img id=\"buildabox_p"+count+"\" src=\"inc/spacer.gif\" border=\"0\" width=\""+picSize+"\" height=\""+picSize+"\" /></a>";
            var obj_a = document.getElementById("buildabox_a"+count);
            obj.alt = "";
            obj.title = "";
            obj.src = "inc/spacer.gif";
            //obj_a.onclick = function(){return false;};
            obj_a.onclick =new Function("return false;");
        }
        count++;
    }
}

function drawBoxRefresh(){
    var div_id = document.getElementById("buildabox");
    var cols = 4;
    var rows = myBox.getMaxSubs()/cols;
    var count = 0;
    var picSize = 58;
    var rowSize = 4;
    var linSize = 1;
    //if(myBox.getMaxSubs() > 24){
    if(myBox.getMaxSubs() > 12){
        picSize = 28;
        rowSize = 8;
        linSize = 1;
    }
    
    var html = "";
    if(myBox.getNumItems() > 0){
        html = "<table style=\"border-style:"+linSize+"px solid;\">"
    }else{
        html = "<table style=\"border-style:"+linSize+"px solid;\">"
    }

    myBox.initIterators()
    //alert(myBox.getNumItems());
    while(myBox.next()){
        var q = myBox.getNextQty();
        var s = myBox.getNextSku();
        //var p = myBox.getNextPic();
        var x = 0;
        for(x = 0; x < q; x++){
            if(count%rowSize == 0){
                if(count != 0){
                    html += "</tr>";
                }
                html += "<tr>";
            }
            var obj_d = document.getElementById("pic_"+s);
            s_alt = obj_d.alt;
            s_title = obj_d.title;
            s_src = obj_d.src;
            //html += "<td><div id=\"buildabox_d"+count+"\"><A id=\"buildabox_a"+count+"\" href=\"#\" onclick=\"return delItemFromBox('"+s+"')?false:false;\"><img id=\"buildabox_p"+count+"\" width=\""+picSize+"\" height=\""+picSize+"\" border=\"0\" src=\"getpic.php?sku="+s+"\" title=\""+s_title+"\" alt=\""+s_alt+"\" /><a/></div></td>";
            html += "<td><div id=\"buildabox_d"+count+"\"><A id=\"buildabox_a"+count+"\" href=\"#\" onclick=\"return delItemFromBox('"+s+"')?false:false;\"><img id=\"buildabox_p"+count+"\" width=\""+picSize+"\" height=\""+picSize+"\" border=\"0\" src=\""+s_src+"\" title=\""+s_title+"\" alt=\""+s_alt+"\" /><a/></div></td>";
            count++;
        }
    }
    while(count < myBox.getMaxSubs()){
        if(count%rowSize == 0){
            if(count != 0){
                html += "</tr>";
            }
            html += "<tr>";
        }
        html += "<td width=\""+picSize+"\" height=\""+picSize+"\"><div id=\"buildabox_d"+count+"\"><A id=\"buildabox_a"+count+"\" href=\"#\" onclick=\"return false;\"><img id=\"buildabox_p"+count+"\" src=\"inc/spacer.gif\" border=\"0\" width=\""+picSize+"\" height=\""+picSize+"\" /></a></div></td>";
        count++;
    }
    html += "</tr></table>";
    div_id.innerHTML = html;
}

function addItemToBox(item, descr, qtd, pic){
    if(qtd == "" || qtd == null){
        qtd = 1;   
    }
    if(pic == "" || pic == null){
        pic = "default.jpg";
    }
    if(isNaN(qtd) || qtd < 1){
        alert("Invalid quantity, please re-enter the desired quantity and try again.");
        return false;
    }
    
    //alert(pic);
    
    var ci = new CartItem(item, qtd, 0, "tc_", pic);
    //alert("ci: " + ci.getQty() + "\nnum: " + myBox.getNumItems() + "\nmax: " + myBox.getMaxSubs() + "\nadd: " + (Number(ci.getQty()) + Number(myBox.getNumItems())));
    if(Number(ci.getQty()) + Number(myBox.getNumItems()) > Number(myBox.getMaxSubs())){
        //alert("Your selection exceeds the maximun capacity of your box.  The selected items were not added to the box.");
        box_sel = document.getElementById("box_id");
        //alert(box_sel.options.length);
//        var biggest_box = box_sel.options[box_sel.options.length-1].value;
//        var php = new PHP_Serializer();
//        var aBox = php.unserialize(biggest_box);
//        var max_size = aBox.getMaxSubs();
        var max_size = 48;
        if(box_sel.options.length > box_sel.selectedIndex+1 && Number(max_size) >= Number(ci.getQty()) + Number(myBox.getNumItems())){
            if(confirm("Your box is too small to hold your selections, would you like a bigger box?")){
                //box_sel.selectedIndex = box_sel.selectedIndex + 1;
                box_sel.value = box_sel.options[box_sel.selectedIndex+1].value;
                setBoxItem(box_sel.value);
                myBox.addItem(ci);
                x_aciAddItem(item, qtd, drawBox);
                //drawBox(document.getElementById("buildabox"));
                //myBox.save();
                return true;
            }else{
                return false;
            }
        }else{
            alert("Your selection exceeds the maximun capacity of any box.  The selected items were not added.");
            return false;
        }
    }
    
	//if(confirm("Are you sure you want to add " + qtd + "x " + descr + " to your box?")){
        myBox.addItem(ci);
        x_aciAddItem(item, qtd, drawBox);
        //drawBox(document.getElementById("buildabox"));
        //myBox.save();
        return true;
	//}
	return false;
}

